Commits

David Lin  committed 75f21b9

set_matrix_4x4f

  • Participants
  • Parent commits 0a3ab74

Comments (0)

Files changed (2)

File lib/program.rb

 
 module My
   class Program
+    include OpenGL::Constants
     
     def initialize(gl)
       @gl = gl
       @id = nil
     end
     
+    def set_matrix_4x4f(name, mat)
+      if loc = glGetUniformLocation(@id, name)
+        ::FFI::Buffer.new_in(:GLfloat, 16) do |ptr|
+          ptr.write_array_of_GLfloat(mat.to_a.flatten)
+          glUniformMatrix4fv(loc, 1, GL_FALSE, ptr)
+        end
+      end
+    end
+    
     class LinkerError < Exception
     end
   end
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     
     glUseProgram(@program.id)
-    if (loc = glGetUniformLocation(@program.id, "u_Projection"))
-      FFI::Buffer.new_in(:GLfloat, 16) do |ptr|
-        ptr.write_array_of_GLfloat(self[:projection].to_a.flatten)
-        glUniformMatrix4fv(loc, 1, GL_FALSE, ptr)
-      end
-    end
-    if (loc = glGetUniformLocation(@program.id, "u_ModelView"))
-      FFI::Buffer.new_in(:GLfloat, 16) do |ptr|
-        modelview = self[:view] * self[:world]
-        ptr.write_array_of_GLfloat(modelview.to_a.flatten)
-        glUniformMatrix4fv(loc, 1, GL_FALSE, ptr)
-      end
-    end
+    @program.set_matrix_4x4f 'u_Projection', self[:projection]
+    @program.set_matrix_4x4f 'u_ModelView', self[:view] * self[:world]
     
     glEnable GL_DEPTH_TEST
     @geom.bind_vertex_attrib