#include "GLProgram.h" #include "ruby.h" #include "gl3w.h" typedef struct { GLuint id; } GLProgram; static VALUE ruby_class; static void GLProgram_free(void * ptr) { GLProgram * program = (GLProgram *)ptr; if (program->id > 0u) { glDeleteProgram(program->id); } delete program; } static VALUE GLProgram_new(VALUE klass) { GLProgram * glprogram = new GLProgram(); glprogram->id = glCreateProgram(); if (glprogram->id == 0u) { rb_raise(rb_eRuntimeError, "Error allocating GL program object"); } return Data_Wrap_Struct(klass, NULL, GLProgram_free, glprogram); } void GLProgram_Init() { ruby_class = rb_define_class("GLProgram", rb_cObject); rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))GLProgram_new, 0); rb_define_attr(ruby_class, "id", 0, 1); } #if 0 GLProgram & GLProgram::attach_shader(GLShaderRef shader) { if (m_id > 0u) { glAttachShader(m_id, shader->get_id()); } return *this; } GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index) { if (m_id > 0u) { glBindAttribLocation(m_id, index, attribute); } return *this; } bool GLProgram::link() { if (m_id == 0u) return false; glLinkProgram(m_id); GLint link_status; glGetProgramiv(m_id, GL_LINK_STATUS, &link_status); if (link_status != GL_TRUE) { GLint log_length; glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &log_length); if (log_length > 0) { char *log = new char[log_length]; glGetProgramInfoLog(m_id, log_length, &log_length, log); cerr << "Program log:" << endl << log << endl; delete[] log; } return false; } return true; } 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