You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
=begin gtkglarea2.rb draw a triangle using shaders on a grey background this script need the opengl-bindings gem from : https://github.com/vaiorabbit/ruby-opengl gem install opengl-bindings sources: http://antongerdelan.net/opengl/hellotriangle.html opengl-bindings/sample/RedBook/varray opengl-bindings/sample/GLES/gles.rb http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ https://developer.gnome.org/gtk3/stable/GtkGLArea.html https://www.bassi.io/articles/2015/02/17/using-opengl-with-gtk/ http://stackoverflow.com/questions/30337845/gldrawarrays-not-working-using-gtkglarea-in-gtk3=endrequire"gtk3"require"opengl"unlessGtk::Version.or_later?(3,16,0)puts"This sample requires GTK+ 3.16.0 or later: #{Gtk::Version::STRING}"exitendcaseOpenGL.get_platformwhen:OPENGL_PLATFORM_WINDOWSOpenGL.load_lib('opengl32.dll','C:/Windows/System32')when:OPENGL_PLATFORM_MACOSXOpenGL.load_lib('libGL.dylib','/System/Library/Frameworks/OpenGL.framework/Libraries')when:OPENGL_PLATFORM_LINUXOpenGL.load_lib('libGL.so','/usr/lib')elseraiseRuntimeError,"Unsupported platform."endincludeOpenGLwindow=Gtk::Window.new("OpenGL widget shaders test")window.set_size_request(400,400)glarea=Gtk::GLArea.newwindow.add(glarea)vertex_shader=<<EOF#version 400in vec3 vp;void main () { gl_Position = vec4 (vp, 1.0);};EOFfragment_shader=<<EOF#version 400out vec4 frag_colour;void main () { frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);};EOFdefsetup_shaders(vertex,fragment)# Load the shaders sourcesvs_handle=glCreateShader(GL_VERTEX_SHADER)fs_handle=glCreateShader(GL_FRAGMENT_SHADER)glShaderSource(vs_handle,1,[vertex].pack("p"),[vertex.bytesize].pack("I"))glShaderSource(fs_handle,1,[fragment].pack("p"),[fragment.bytesize].pack("I"))# Compile the vertex shaderglCompileShader(vs_handle)# Check the vertex shader compilationvertCompiled_buf=" "glGetShaderiv(vs_handle,GL_COMPILE_STATUS,vertCompiled_buf)vertCompiled=vertCompiled_buf.unpack("L")[0]ifvertCompiled == 0infoLog=" " * 1024glGetShaderInfoLog(vs_handle,1023,nil,infoLog)puts"Shader InfoLog:\n#{infoLog}\n"end# Compile the fragment shaderglCompileShader(fs_handle)# Check the fragment shader compilationfragCompiled_buf=" "glGetShaderiv(fs_handle,GL_COMPILE_STATUS,fragCompiled_buf)fragCompiled=fragCompiled_buf.unpack('L')[0]iffragCompiled == 0infoLog=" " * 1024glGetShaderInfoLog(fs_handle,1023,nil,infoLog)puts"Shader InfoLog:\n#{infoLog}\n"endreturnfalseif(vertCompiled == 0 || fragCompiled == 0)# Load those 2 shaders (vertex and fragment) into a GPU shader programprog_handle=glCreateProgram()glAttachShader(prog_handle,vs_handle)glAttachShader(prog_handle,fs_handle)glLinkProgram(prog_handle)# Check if the program is validlinked_buf=" "glGetProgramiv(prog_handle,GL_LINK_STATUS,linked_buf)linked=linked_buf.unpack("L")[0]iflinked == 0infoLog=" " * 1024glGetProgramInfoLog(prog_handle,1023,nil,infoLog)puts"Program InfoLog:\n#{infoLog}\n"endreturnfalseiflinked==0returntrue,prog_handleendshader_program=nilgl_vao=nilglarea.signal_connect("realize")do |widget|
widget.make_currentputs"realize"# Define a triangle in a vertex buffer ( Vertex Buffer Object)points=[0.0,0.5,0.0,#x1, y1, z10.5, -0.5,0.0,#x2, y2, z2
-0.5, -0.5,0.0#x3, y3, z3]vao_buf=" "glGenVertexArrays(1,vao_buf)gl_vao=vao_buf.unpack("L")[0]glBindVertexArray(gl_vao)# We copy those points onto the graphics card in a unit called vertex buffer object (vbo).# Create an empty buffervbo_buf=" "glGenBuffers(1,vbo_buf)g_vbo=vbo_buf.unpack("L")[0]# Set the empty buffer as the current OpenGL's state machine by "binding"glBindBuffer(GL_ARRAY_BUFFER,g_vbo)# Copy the points in the currently bound bufferglBufferData(GL_ARRAY_BUFFER,3 * 4 * Fiddle::SIZEOF_FLOAT,points.pack("F*"),GL_STATIC_DRAW)# First attribute buffer : verticesglEnableVertexAttribArray(0)glVertexAttribPointer(0,# No particular reason for 03,# sizeGL_FLOAT,# typeGL_FALSE,# normalized?0,# stride0# array buffer offset)glBindVertexArray(0)# Load the shaders:ok,shader_program=setup_shaders(vertex_shader,fragment_shader)endglarea.signal_connect("render")do |widget,context|
puts"render"glClearColor(0.3,0.3,0.3,1)glClear(GL_COLOR_BUFFER_BIT)# Set the created shader program as currentglUseProgram(shader_program)glBindVertexArray(gl_vao)glDrawArrays(GL_TRIANGLES,0,3)trueendglarea.signal_connect("unrealize")do |widget|
puts"unrealize"widget.make_currentglDeleteVertexArrays(1,gl_vao);glDeleteProgram(shader_program);endwindow.signal_connect("destroy"){Gtk.main_quit}window.show_allGtk.main
For the signal "unrealize" of the widget I try to clean all the allocated data (vao and shader program).
The script works well unitl I close the window which trigger the "unrealize" event and the glVertexArrays result in a segfault:
Good. I've just released opengl-bindings 1.4.0. https://rubygems.org/gems/opengl-bindings
Now you can safely commit your new sample scripts that use `load_lib'. Thank you.
here is the example:
For the signal "unrealize" of the widget I try to clean all the allocated data (vao and shader program).
The script works well unitl I close the window which trigger the "unrealize" event and the glVertexArrays result in a segfault:
I don't really know how to use
glDeleteVertexArrays
and I haven't found any example in your sample scripts.Regards
The text was updated successfully, but these errors were encountered: