Skip to content

Commit

Permalink
Simplifying things with a common utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
markmandel committed Jan 19, 2013
1 parent 973812e commit 5ca28c7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 52 deletions.
88 changes: 88 additions & 0 deletions lib/opengl/gl_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
java_import org.lwjgl.opengl.GL20
java_import org.lwjgl.opengl.GL11

module OpenGL::GLUtils

# initialised the program with the shaders that have been passed through
# @param [String] vertex_shader
# @param [String] frag_shader
def compile_program(vertex_shader, frag_shader)
vertex_shader = compile_shader(GL20::GL_VERTEX_SHADER, vertex_shader)
frag_shader = compile_shader(GL20::GL_FRAGMENT_SHADER, frag_shader)

program_id = GL20.gl_create_program

GL20.gl_attach_shader(program_id, vertex_shader)
GL20.gl_attach_shader(program_id, frag_shader)
GL20.gl_link_program(program_id)
GL20.gl_validate_program(program_id)

puts "Validate Program", GL20.gl_get_program_info_log(program_id, 200)

GL20.gl_delete_shader(vertex_shader)
GL20.gl_delete_shader(frag_shader)

program_id
end

# create a shader for you, and return the id
# @param [Integer] shader_type the GL20 shader type int
# @param [String] file_name the name of the glsl file
# @return [Integer] the shader id
def compile_shader(shader_type, file_name)
shader_id = GL20.gl_create_shader(shader_type)
shader_file = File.new File.expand_path("../../glsl/#{file_name}", __FILE__)

GL20.gl_shader_source(shader_id, shader_file.read)
GL20.gl_compile_shader(shader_id)

puts file_name, GL20.gl_get_shader_info_log(shader_id, 200)

raise "Error compiling shader #{file_name}" if (GL20.gl_get_shader(shader_id, GL20::GL_COMPILE_STATUS) == GL11::GL_FALSE)

shader_id
end

# run the render loop
# @param [Proc] display
def render_loop(&display_block)

while(!Display.is_close_requested)
display_block.call

# we'll force it down to 60 FPS
Display.sync(60)
Display.update()
end

end

def destory_display
Display.destroy
end

def create_display(title, width=800, height=600)
Display.display_mode = DisplayMode.new(width, height)
Display.title = title
Display.create
end

# hook for class methods
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

# creates a start script for this item
def add_start
class << self
define_method :start do
self.new
end
end
end

end

end
60 changes: 8 additions & 52 deletions lib/opengl/show_triangle_colours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
java_import org.lwjgl.opengl.GL30
java_import org.lwjgl.BufferUtils

require "opengl/gl_utils"

#
# Let's display a triangle, with a gradient
class OpenGL::ShowTriangleColours
include OpenGL::GLUtils
add_start

# initialise
def initialize
Expand All @@ -25,26 +29,17 @@ def initialize
#pre calculate the byte size of a float.
@float_size = (java.lang.Float::SIZE/8)

Display.display_mode = DisplayMode.new(800, 600)
Display.title = "I am a triangle! (with different colours!)"
Display.create
create_display("I am a triangle! (with different colours!)")

#initialise the viewport
GL11.gl_viewport(0, 0, Display.width, Display.height)

init_program
@program_id = compile_program 'colour_gradient_vertex.glsl', 'colour_grandient_fragment.glsl'
init_vertex_buffers

while(!Display.is_close_requested)

display
render_loop { display }

# we'll force it down to 60 FPS
Display.sync(60)
Display.update()
end

Display.destroy
close_display


end
Expand Down Expand Up @@ -78,24 +73,6 @@ def display

end

# initialise the program
def init_program
vertex_shader = create_shader(GL20::GL_VERTEX_SHADER, 'colour_gradient_vertex.glsl')
frag_shader = create_shader(GL20::GL_FRAGMENT_SHADER, 'colour_grandient_fragment.glsl')

@program_id = GL20.gl_create_program
GL20.gl_attach_shader(@program_id, vertex_shader)
GL20.gl_attach_shader(@program_id, frag_shader)
GL20.gl_link_program(@program_id)
GL20.gl_validate_program(@program_id)

puts "Validate Program", GL20.gl_get_program_info_log(@program_id, 200)

GL20.gl_delete_shader(vertex_shader)
GL20.gl_delete_shader(frag_shader)

end

# initialise the vertex buffers
def init_vertex_buffers
#vao_id = GL30.gl_gen_vertex_arrays
Expand All @@ -117,25 +94,4 @@ def init_vertex_buffers
#GL30.gl_bind_vertex_array(0)
end

# create a shader for you, and return the id
# @param [Integer] shader_type the GL20 shader type int
# @param [String] file_name the name of the glsl file
# @return [Integer] the shader id
def create_shader(shader_type, file_name)
shader_id = GL20.gl_create_shader(shader_type)
shader_file = File.new File.expand_path("../../glsl/#{file_name}", __FILE__)

GL20.gl_shader_source(shader_id, shader_file.read)
GL20.gl_compile_shader(shader_id)

puts file_name, GL20.gl_get_shader_info_log(shader_id, 200)

shader_id
end

# off we go
def self.start
OpenGL::ShowTriangleColours.new
end

end

0 comments on commit 5ca28c7

Please sign in to comment.