GLProgram: load and store uniform locations

This commit is contained in:
josh committed 2014-07-29 17:27:17 -04:00
1 parent cad9bc056f
commit 914f08952b
4 files changed
+86 -28

No files matched your search

+39 -23
View File
@@ -1,10 +1,6 @@
#include "GLProgram.h"
#include "GLShader.h"
typedef struct
{
GLuint id;
} GLProgram;
#include <string.h>
static VALUE ruby_class;
@@ -92,6 +88,43 @@ static VALUE GLProgram_link(VALUE self)
return Qnil;
}
static VALUE GLProgram_load_uniform_location(VALUE self, VALUE uniform)
{
static const char * uniform_names[] = {
"viewport_size",
"position",
"size",
"texture",
"color",
};
GLProgram * glprogram;
Data_Get_Struct(self, GLProgram, glprogram);
uniform = rb_funcall(uniform, rb_intern("to_s"), 0);
const char * uniform_cstr = StringValueCStr(uniform);
int uniform_index = -1;
for (int i = 0;
i < (int)(sizeof(uniform_names) / sizeof(uniform_names[0]));
i++)
{
if (strcmp(uniform_cstr, uniform_names[i]) == 0)
{
uniform_index = i;
break;
}
}
if (uniform_index >= 0)
{
glprogram->uniforms[uniform_index] = glGetUniformLocation(glprogram->id, uniform_cstr);
}
else
{
rb_raise(rb_eRuntimeError,
"Unknown uniform name '%s'",
uniform_cstr);
}
return Qnil;
}
void GLProgram_Init()
{
ruby_class = rb_define_class("GLProgram", rb_cObject);
@@ -99,6 +132,7 @@ void GLProgram_Init()
rb_define_method(ruby_class, "attach_shader", (VALUE(*)(...))GLProgram_attach_shader, 1);
rb_define_method(ruby_class, "bind_attribute", (VALUE(*)(...))GLProgram_bind_attribute, 2);
rb_define_method(ruby_class, "link", (VALUE(*)(...))GLProgram_link, 0);
rb_define_method(ruby_class, "load_uniform_location", (VALUE(*)(...))GLProgram_load_uniform_location, 1);
}
GLuint GLProgram_GetID(VALUE program)
@@ -107,21 +141,3 @@ GLuint GLProgram_GetID(VALUE program)
Data_Get_Struct(program, GLProgram, glprogram);
return glprogram->id;
}
#if 0
GLint GLProgram::get_uniform(const std::string & uniform_name)
{
if (m_id > 0u)
{
auto it = m_uniforms.find(uniform_name);
if (it != m_uniforms.end())
{
return it->second;
}
GLint loc = glGetUniformLocation(m_id, uniform_name.c_str());
m_uniforms[uniform_name] = loc;
return loc;
}
return -1;
}
#endif
+16
View File
@@ -3,6 +3,22 @@
#include "ruby.h"
#include "gl3w.h"
#include <unordered_map>
enum
{
UNIFORM_VIEWPORT_SIZE,
UNIFORM_POSITION,
UNIFORM_SIZE,
UNIFORM_TEXTURE,
UNIFORM_COLOR,
};
struct GLProgram
{
GLuint id;
std::unordered_map<int, GLuint> uniforms;
};
void GLProgram_Init();
GLuint GLProgram_GetID(VALUE program);