Last active
August 29, 2015 14:27
-
-
Save xranby/631dc8d025ea1b466d5b to your computer and use it in GitHub Desktop.
Initialized fine now
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dir.glob('*.jar').each {|jar| require jar } | |
java_import 'com.jogamp.opengl.GLCapabilities', | |
'com.jogamp.opengl.GLDrawableFactory', | |
'com.jogamp.opengl.GLProfile', | |
'com.jogamp.opengl.GL4', | |
'com.jogamp.nativewindow.util.Rectangle', | |
'com.jogamp.newt.opengl.GLWindow', | |
'com.jogamp.opengl.util.FPSAnimator', | |
'com.jogamp.opengl.GLEventListener', | |
'java.awt.GraphicsEnvironment', | |
'java.nio.FloatBuffer' | |
# Vertex data | |
VERTS = [ | |
0.0, 0.5, 0.0, | |
0.5, -0.5, 0.0, | |
-0.5, -0.5, 0.0 | |
] | |
# vertex shader | |
VERTEX_SHADER = " | |
#version 400 | |
in vec3 vertex; | |
void main(){ | |
gl_Position = vec4( vertex, 1.0 ); | |
} | |
" | |
# fragment shader | |
FRAGMENT_SHADER = " | |
#version 400 | |
out vec4 color; | |
void main(){ | |
color = vec4( 0.5, 0.0, 0.5, 1.0 ); | |
} | |
" | |
# Renderer | |
class Renderer | |
include GL4, GLEventListener | |
# Called when the renderer's GL state is initialized. | |
def init( surface ) | |
gl = surface.gl | |
puts "Initializing with: #{gl}" | |
# Setup default states | |
gl.gl_enable( GL_DEPTH_TEST ) | |
gl.gl_depth_func( GL_LESS ) | |
gl.gl_clear_color( 0.0, 0.0, 0.0, 1.0 ) | |
print "Creating VBO ... " | |
b = [0].to_java( :int ) | |
gl.gl_gen_buffers( 1, b, 0 ) | |
@vbo = b[0] | |
gl.gl_bind_buffer( GL_ARRAY_BUFFER, @vbo ) | |
v = FloatBuffer.wrap( VERTS.to_java( :float ) ) | |
gl.gl_buffer_data( GL_ARRAY_BUFFER, v.capacity, v, GL_STATIC_DRAW ) | |
puts "done." | |
print "Creating Vertex Shader ... " | |
vsh = gl.gl_create_shader( GL_VERTEX_SHADER ) | |
vss = VERTEX_SHADER.each_line.to_a | |
gl.gl_shader_source( vsh, vss.length, vss.to_java( :string ), vss.map {|l| l.length }.to_java( :int ), 0 ) | |
gl.gl_compile_shader( vsh ) | |
puts "done." | |
print "Creating Fragment Shader ... " | |
fsh = gl.gl_create_shader( GL_FRAGMENT_SHADER ) | |
fss = FRAGMENT_SHADER.each_line.to_a | |
gl.gl_shader_source( fsh, fss.length, fss.to_java( :string ), fss.map {|l| l.length }.to_java( :int ), 0 ) | |
gl.gl_compile_shader( fsh ) | |
puts "done." | |
print "Creating Program ... " | |
@prgm = gl.gl_create_program() | |
gl.gl_attach_shader( @prgm, vsh ) | |
gl.gl_attach_shader( @prgm, fsh ) | |
gl.gl_link_program( @prgm ) | |
puts "done." | |
print "Fetching VAO position from linked program... " | |
@attribPosition = gl.glGetAttribLocation(@prgm, "vertex") | |
print "Creating VAO ... " | |
a = [0].to_java( :int ) | |
gl.gl_gen_vertex_arrays( 1, a, 0 ) | |
@vao = a[0] | |
gl.gl_bind_vertex_array( @vao ) | |
gl.gl_bind_buffer( GL_ARRAY_BUFFER, @vbo ) | |
# fixme buffer data here | |
gl.gl_vertex_attrib_pointer( @attribPosition , 3, GL_FLOAT, false, 0, 0 ) | |
gl.gl_enable_vertex_attrib_array( 0 ) | |
puts "done." | |
puts "VBO: #{@vbo}", "VAO: #{@vao}", "VERTEX SHADER: #{vsh}", "FRAGMENT SHADER: #{fsh}", "PROGRAM: #{@prgm}" | |
end | |
# Called when the viewport is reshaped. | |
def reshape( surface, x, y, w, h ) | |
surface.gl.gl_viewport( 0, 0, w.to_i, w.to_i ) | |
end | |
# Called when the renderer and its underlying surface are being disposed of. | |
def dispose( surface ) | |
# TODO | |
end | |
# Called for each frame to be rendered. | |
def display( surface ) | |
gl = surface.gl | |
gl.gl_clear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) | |
# RENDERING CODE GOES HERE | |
gl.gl_use_program( @prgm ) | |
gl.gl_bind_vertex_array( @vao ) | |
gl.gl_draw_arrays( GL_TRIANGLES, 0, 3 ) | |
end | |
end | |
# Get references | |
@gl_profile = GLProfile.get( GLProfile::GL4 ) | |
@gl_capabilities = GLCapabilities.new( @gl_profile ) | |
# Setup window | |
@window = GLWindow.create( @gl_capabilities ) | |
@window.add_gl_event_listener( Renderer.new() ) | |
@window.set_size( 500, 500 ) | |
# Get animator | |
@animator = FPSAnimator.new( | |
@window, | |
GraphicsEnvironment.local_graphics_environment.default_screen_device.display_mode.refresh_rate | |
) | |
# Go! | |
@window.set_visible( true ) | |
@animator.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment