Skip to content

Instantly share code, notes, and snippets.

@terryjsmith
Created October 5, 2014 23:24
Show Gist options
  • Save terryjsmith/3b03aefa9501bd3ca156 to your computer and use it in GitHub Desktop.
Save terryjsmith/3b03aefa9501bd3ca156 to your computer and use it in GitHub Desktop.
Evolution ShaderProgram implementation
#include <shaderprogram.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
#include <stdlib.h>
ShaderProgram::ShaderProgram() {
program = 0;
m_list = 0;
}
ShaderProgram::~ShaderProgram() {
if(m_list) {
ShaderVariable* current = m_list;
while(current) {
ShaderVariable* next = current->next;
free(current->name);
delete current;
current = next;
}
}
if(program) {
glDeleteProgram(program);
}
}
int ShaderProgram::_var(char* name, bool uniform) {
// First, try finding it in our list
ShaderVariable* current = m_list;
ShaderVariable* last = m_list;
while(current) {
if((strcmp(current->name, name) == 0) && (current->uniform == uniform))
return(current->location);
last = current;
current = current->next;
}
// Otherwise, we need to get it from OGL
int location = -1;
if(uniform) {
location = glGetUniformLocation(program, name);
}
else {
location = glGetAttribLocation(program, name);
}
if(location >= 0) {
ShaderVariable* var = new ShaderVariable;
var->location = location;
var->next = 0;
var->uniform = uniform;
var->name = (char*)malloc(strlen(name + 1));
strcpy(var->name, name);
if(m_list == 0) {
m_list = var;
}
else {
last->next = var;
}
}
return(location);
}
int ShaderProgram::uniform(char *name) {
return(_var(name, true));
}
int ShaderProgram::attribute(char* name) {
return(_var(name, false));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment