Skip to content

Commit

Permalink
A triangle that moves around in a circle
Browse files Browse the repository at this point in the history
  • Loading branch information
markmandel committed Jan 9, 2013
1 parent 83035b6 commit 1e7aa1c
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
9 changes: 9 additions & 0 deletions bin/triangle_vert_offset
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env jruby

# run
raise("Only runs on JRuby.") unless (RUBY_PLATFORM =~ /java/)

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'opengl'

OpenGL::ShowTriangleVertOffset.start
10 changes: 10 additions & 0 deletions lib/glsl/offset_vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 330

layout(location = 0) in vec4 position;
uniform vec2 offset;

void main()
{
vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);
gl_Position = position + totalOffset;
}
1 change: 1 addition & 0 deletions lib/opengl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ module OpenGL
require 'opengl/show_triangle'
require 'opengl/show_triangle_gradient'
require 'opengl/show_triangle_colours'
require 'opengl/show_triangle_vert_offset'
5 changes: 4 additions & 1 deletion lib/opengl/show_triangle_colours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def initialize
0.0, 0.0, 1.0, 1.0,
]

#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
Expand Down Expand Up @@ -65,7 +68,7 @@ def display
GL20.gl_vertex_attrib_pointer(0, 4, GL11::GL_FLOAT, false, 0, 0)

#Floats are 4 bytes, and there are 12 of them = 48 offset
GL20.gl_vertex_attrib_pointer(1, 4, GL11::GL_FLOAT, false, 0, 48)
GL20.gl_vertex_attrib_pointer(1, 4, GL11::GL_FLOAT, false, 0, @float_size * 12)
GL11.gl_draw_arrays(GL11::GL_TRIANGLES, 0, 3)

GL15.gl_bind_buffer(GL15::GL_ARRAY_BUFFER, 0)
Expand Down
151 changes: 151 additions & 0 deletions lib/opengl/show_triangle_vert_offset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
java_import org.lwjgl.opengl.Display
java_import org.lwjgl.opengl.DisplayMode
java_import org.lwjgl.opengl.GL11
java_import org.lwjgl.opengl.GL15
java_import org.lwjgl.opengl.GL20
java_import org.lwjgl.opengl.GL30
java_import org.lwjgl.BufferUtils
java_import org.lwjgl.Sys

#
# Let's display a triangle!
class OpenGL::ShowTriangleVertOffset

# initialise
def initialize

@vertex_positions = [
0.75, 0.75, 0.0, 1.0,
0.75, -0.75, 0.0, 1.0,
-0.75, -0.75, 0.0, 1.0,
]

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

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

init_program
init_vertex_buffers

while(!Display.is_close_requested)

display

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

Display.destroy


end

# display loop
def display

x_offset, y_offset = compute_position_offsets

#set the colour to clear.
GL11.gl_clear_color(0.0, 0.0, 0.0, 0.0)

#clear the buffer. Remember that Java static types come back as Ruby Constants.
GL11.gl_clear(GL11::GL_COLOR_BUFFER_BIT)

GL20.gl_use_program(@program_id)

GL20.gl_uniform2f(@offset_location, x_offset, y_offset)

GL15.gl_bind_buffer(GL15::GL_ARRAY_BUFFER, @buffer_id)
GL20.gl_enable_vertex_attrib_array(0)
GL20.gl_vertex_attrib_pointer(0, 4, GL11::GL_FLOAT, false, 0, 0)
GL11.gl_draw_arrays(GL11::GL_TRIANGLES, 0, 3)

GL20.gl_disable_vertex_attrib_array(0)
GL20.gl_use_program(0)

end

# compute the offsets based on the time
# @return [Float] [Float] the x_offset, y_offset
def compute_position_offsets
loop_duration = 5
scale = (Math::PI * 2.0) / loop_duration
elapsed_time = Sys.get_time / 1000.0
current_time_through_loop = elapsed_time % loop_duration

x_offset = Math.cos(current_time_through_loop * scale) * 0.5
y_offset = Math.sin(current_time_through_loop * scale) * 0.5

return x_offset, y_offset

end

# initialise the program
def init_program
vertex_shader = create_shader(GL20::GL_VERTEX_SHADER, 'offset_vertex.glsl')
frag_shader = create_shader(GL20::GL_FRAGMENT_SHADER, 'basic_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)

@offset_location = GL20.gl_get_uniform_location(@program_id, "offset")

puts "Offset Location: #{@offset_location}"

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
#GL30.gl_bind_vertex_array(vao_id)

@buffer_id = GL15.gl_gen_buffers
GL15.gl_bind_buffer(GL15::GL_ARRAY_BUFFER, @buffer_id)

float_buffer = BufferUtils.create_float_buffer(@vertex_positions.size)
float_buffer.put(@vertex_positions.to_java(:float))

#MUST FLIP THE BUFFER! THIS PUTS IT BACK TO THE BEGINNING!
float_buffer.flip

GL15.gl_buffer_data(GL15::GL_ARRAY_BUFFER, float_buffer, GL15::GL_STATIC_DRAW)

# cleanup
GL15.gl_bind_buffer(GL15::GL_ARRAY_BUFFER, 0)
#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::ShowTriangleVertOffset.new
end

end

0 comments on commit 1e7aa1c

Please sign in to comment.