Skip to content

Commit

Permalink
migrate to opengl-bindings2 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
danini-the-panini authored Nov 11, 2024
1 parent 30d0199 commit 37040b8
Show file tree
Hide file tree
Showing 31 changed files with 757 additions and 758 deletions.
4 changes: 2 additions & 2 deletions lib/mittsu/renderers/generic_lib.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Mittsu
module GenericLib
def discover
case OpenGL.get_platform
case GL.get_platform
when :OPENGL_PLATFORM_WINDOWS
self::Windows.new
when :OPENGL_PLATFORM_MACOSX
self::MacOS.new
when :OPENGL_PLATFORM_LINUX
self::Linux.new
else
warn "WARNING: Unsupported platform: #{OpenGL.get_platform}"
warn "WARNING: Unsupported platform: #{GL.get_platform}"
Base.new
end
end
Expand Down
88 changes: 43 additions & 45 deletions lib/mittsu/renderers/glfw_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,98 +4,96 @@
require 'mittsu/utils'
require 'mittsu/renderers/glfw_lib'
glfw_lib = Mittsu::GLFWLib.discover
GLFW.load_lib(ENV["MITTSU_LIBGLFW_FILE"] || glfw_lib.file, ENV["MITTSU_LIBGLFW_PATH"] || glfw_lib.path) unless Mittsu.test?

include GLFW
::GLFW.load_lib(ENV["MITTSU_LIBGLFW_FILE"] || glfw_lib.file, ENV["MITTSU_LIBGLFW_PATH"] || glfw_lib.path) unless Mittsu.test?

module Mittsu
module GLFW
class Window
attr_accessor :key_press_handler, :key_release_handler, :key_repeat_handler, :char_input_handler, :cursor_pos_handler, :mouse_button_press_handler, :mouse_button_release_handler, :scroll_handler, :framebuffer_size_handler

def initialize(width, height, title, antialias: 0)
glfwInit
::GLFW.Init

glfwWindowHint GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE
glfwWindowHint GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE
glfwWindowHint GLFW_CONTEXT_VERSION_MAJOR, 3
glfwWindowHint GLFW_CONTEXT_VERSION_MINOR, 3
glfwWindowHint GLFW_CONTEXT_REVISION, 0
::GLFW.WindowHint ::GLFW::OPENGL_PROFILE, ::GLFW::OPENGL_CORE_PROFILE
::GLFW.WindowHint ::GLFW::OPENGL_FORWARD_COMPAT, GL::TRUE
::GLFW.WindowHint ::GLFW::CONTEXT_VERSION_MAJOR, 3
::GLFW.WindowHint ::GLFW::CONTEXT_VERSION_MINOR, 3
::GLFW.WindowHint ::GLFW::CONTEXT_REVISION, 0

if antialias > 0
glfwWindowHint GLFW_SAMPLES, antialias
::GLFW.WindowHint ::GLFW::SAMPLES, antialias
end

@width, @height, @title = width, height, title
@handle = glfwCreateWindow(@width, @height, @title, nil, nil)
@handle = ::GLFW.CreateWindow(@width, @height, @title, nil, nil)
if @handle.null?
raise "Unable to create window."
end
glfwMakeContextCurrent @handle
glfwSwapInterval 1
::GLFW.MakeContextCurrent @handle
::GLFW.SwapInterval 1

this = self
@key_callback = ::GLFW::create_callback(:GLFWkeyfun) do |window_handle, key, scancode, action, mods|
if action == GLFW_PRESS
if action == ::GLFW::PRESS
this.key_press_handler.call(key) unless this.key_press_handler.nil?
this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
elsif action == GLFW_RELEASE
elsif action == ::GLFW::RELEASE
this.key_release_handler.call(key) unless this.key_release_handler.nil?
elsif action == GLFW_REPEAT
elsif action == ::GLFW::REPEAT
this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
end
end
glfwSetKeyCallback(@handle, @key_callback)
::GLFW.SetKeyCallback(@handle, @key_callback)

@char_callback = ::GLFW::create_callback(:GLFWcharfun) do |window_handle, codepoint|
char = [codepoint].pack('U')
this.char_input_handler.call(char) unless this.char_input_handler.nil?
end
glfwSetCharCallback(@handle, @char_callback)
::GLFW.SetCharCallback(@handle, @char_callback)

@cursor_pos_callback = ::GLFW::create_callback(:GLFWcursorposfun) do |window_handle, xpos, ypos|
this.cursor_pos_handler.call(Vector2.new(xpos, ypos)) unless this.cursor_pos_handler.nil?
end
glfwSetCursorPosCallback(@handle, @cursor_pos_callback)
::GLFW.SetCursorPosCallback(@handle, @cursor_pos_callback)

@mouse_button_callback = ::GLFW::create_callback(:GLFWmousebuttonfun) do |window_handle, button, action, mods|
mpos = this.mouse_position
if action == GLFW_PRESS
if action == ::GLFW::PRESS
this.mouse_button_press_handler.call(button, mpos) unless this.mouse_button_press_handler.nil?
elsif action == GLFW_RELEASE
elsif action == ::GLFW::RELEASE
this.mouse_button_release_handler.call(button, mpos) unless this.mouse_button_release_handler.nil?
end
end
glfwSetMouseButtonCallback(@handle, @mouse_button_callback)
::GLFW.SetMouseButtonCallback(@handle, @mouse_button_callback)

@scroll_callback = ::GLFW::create_callback(:GLFWscrollfun) do |window_handle, xoffset, yoffset|
this.scroll_handler.call(Vector2.new(xoffset, yoffset)) unless this.scroll_handler.nil?
end
glfwSetScrollCallback(@handle, @scroll_callback)
::GLFW.SetScrollCallback(@handle, @scroll_callback)

@frabuffer_size_callback = ::GLFW::create_callback(:GLFWframebuffersizefun) do |window_handle, new_width, new_height|
this.framebuffer_size_handler.call(new_width, new_height) unless this.framebuffer_size_handler.nil?
end
glfwSetFramebufferSizeCallback(@handle, @frabuffer_size_callback)
::GLFW.SetFramebufferSizeCallback(@handle, @frabuffer_size_callback)

@joystick_buttons = poll_all_joysticks_buttons
end

def run
while glfwWindowShouldClose(@handle) == 0
while ::GLFW.WindowShouldClose(@handle) == 0
yield

glfwSwapBuffers @handle
glfwPollEvents
::GLFW.SwapBuffers @handle
::GLFW.PollEvents
poll_joystick_events
end
glfwDestroyWindow @handle
glfwTerminate
::GLFW.DestroyWindow @handle
::GLFW.Terminate
end

def framebuffer_size
width, height = ' '*8, ' '*8
glfwGetFramebufferSize(@handle, width, height)
::GLFW.GetFramebufferSize(@handle, width, height)
[width.unpack('L')[0], height.unpack('L')[0]]
end

Expand All @@ -112,7 +110,7 @@ def on_key_typed &block
end

def key_down?(key)
glfwGetKey(@handle, key) == GLFW_PRESS
::GLFW.GetKey(@handle, key) == ::GLFW::PRESS
end

def on_character_input &block
Expand All @@ -133,12 +131,12 @@ def on_mouse_button_released &block

def mouse_position
xpos, ypos = ' '*8, ' '*8
glfwGetCursorPos(@handle, xpos, ypos);
::GLFW.GetCursorPos(@handle, xpos, ypos);
Vector2.new(xpos.unpack('D')[0], ypos.unpack('D')[0])
end

def mouse_button_down?(button)
glfwGetMouseButton(@handle, button) == GLFW_PRESS
::GLFW.GetMouseButton(@handle, button) == ::GLFW::PRESS
end

def on_scroll &block
Expand All @@ -149,15 +147,15 @@ def on_resize &block
@framebuffer_size_handler = block
end

def joystick_buttons(joystick = GLFW_JOYSTICK_1)
def joystick_buttons(joystick = ::GLFW::JOYSTICK_1)
@joystick_buttons = poll_all_joysticks_buttons
@joystick_buttons[joystick]
end

def joystick_axes(joystick = GLFW_JOYSTICK_1)
def joystick_axes(joystick = ::GLFW::JOYSTICK_1)
return [] unless joystick_present?(joystick)
count = ' ' * 4
array = glfwGetJoystickAxes(joystick, count)
array = ::GLFW.GetJoystickAxes(joystick, count)
count = count.unpack('l')[0]
array[0, count * 4].unpack('f' * count)
end
Expand All @@ -170,38 +168,38 @@ def on_joystick_button_released &block
@joystick_button_release_handler = block
end

def joystick_present?(joystick = GLFW_JOYSTICK_1)
glfwJoystickPresent(joystick).nonzero?
def joystick_present?(joystick = ::GLFW::JOYSTICK_1)
::GLFW.JoystickPresent(joystick).nonzero?
end

def joystick_button_down?(button, joystick = GLFW_JOYSTICK_1)
def joystick_button_down?(button, joystick = ::GLFW::JOYSTICK_1)
@joystick_buttons[joystick][button]
end

def joystick_name(joystick = GLFW_JOYSTICK_1)
glfwGetJoystickName(joystick)
def joystick_name(joystick = ::GLFW::JOYSTICK_1)
::GLFW.GetJoystickName(joystick)
end

def set_mouselock(value)
if value
glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
::GLFW.SetInputMode(@handle, ::GLFW::CURSOR, ::GLFW::CURSOR_DISABLED)
else
glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL)
::GLFW.SetInputMode(@handle, ::GLFW::CURSOR, ::GLFW::CURSOR_NORMAL)
end
end

private

def poll_all_joysticks_buttons
(GLFW_JOYSTICK_1..GLFW_JOYSTICK_LAST).map do |joystick|
(::GLFW::JOYSTICK_1..::GLFW::JOYSTICK_LAST).map do |joystick|
poll_joystick_buttons(joystick)
end
end

def poll_joystick_buttons(joystick)
return nil unless joystick_present?(joystick)
count = ' ' * 4
array = glfwGetJoystickButtons(joystick, count)
array = ::GLFW.GetJoystickButtons(joystick, count)
count = count.unpack('l')[0]
array[0, count].unpack('c' * count).map(&:nonzero?)
end
Expand Down
44 changes: 22 additions & 22 deletions lib/mittsu/renderers/opengl/core/geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ def init_particle_buffers(object)
end

def create_line_buffers
@vertex_array_object = glCreateVertexArray
@vertex_array_object = GL.CreateVertexArray

@vertex_buffer = glCreateBuffer
@color_buffer = glCreateBuffer
@line_distance_buffer = glCreateBuffer
@vertex_buffer = GL.CreateBuffer
@color_buffer = GL.CreateBuffer
@line_distance_buffer = GL.CreateBuffer

@renderer.info[:memory][:geometries] += 1
end

def create_particle_buffers
@vertex_array_object = glCreateVertexArray
@vertex_array_object = GL.CreateVertexArray

@vertex_buffer = glCreateBuffer
@color_buffer = glCreateBuffer
@vertex_buffer = GL.CreateBuffer
@color_buffer = GL.CreateBuffer

@renderer.info[:memory][:geometries] += 1
end
Expand All @@ -99,8 +99,8 @@ def set_line_buffers(hint)
@vertex_array[offset + 2] = vertex.z
end

glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, @vertex_buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, @vertex_array, hint)
end

if @colors_need_update
Expand All @@ -112,17 +112,17 @@ def set_line_buffers(hint)
@color_array[offset + 2] = color.b
end

glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, @color_buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, @color_array, hint)
end

if @line_distances_need_update
@line_distances.each_with_index do |l, d|
@line_distance_array[d] = l
end

glBindBuffer(GL_ARRAY_BUFFER, @line_distance_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @line_distance_array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, @line_distance_buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, @line_distance_array, hint)
end

if @custom_attributes
Expand Down Expand Up @@ -172,8 +172,8 @@ def set_line_buffers(hint)
end
end

glBindBuffer(GL_ARRAY_BUFFER, custom_attribute.buffer)
glBufferData_easy(GL_ARRAY_BUFFER, custom_attribute.array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, custom_attribute.buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, custom_attribute.array, hint)

custom_attribute.needs_update = false
end
Expand All @@ -191,8 +191,8 @@ def set_particle_buffers(hint)
end


glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, @vertex_buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, @vertex_array, hint)
end

if @colors_need_update
Expand All @@ -204,8 +204,8 @@ def set_particle_buffers(hint)
@color_array[offset + 2] = color.b
end

glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, @color_buffer)
GL.BufferData_easy(GL::ARRAY_BUFFER, @color_array, hint)
end

if @custom_attribute
Expand Down Expand Up @@ -254,8 +254,8 @@ def set_particle_buffers(hint)
end
end

glBindBuffer(GL_ARRAY_BUFFER, customAttribute.buffer)
glBufferData(GL_ARRAY_BUFFER, customAttribute.array, hint)
GL.BindBuffer(GL::ARRAY_BUFFER, customAttribute.buffer)
GL.BufferData(GL::ARRAY_BUFFER, customAttribute.array, hint)

custom_attribute.needs_update = false
end
Expand Down Expand Up @@ -332,7 +332,7 @@ def init_custom_attributes(object)

attribute.array = Array.new(nvertices * size) # Float32Array

attribute.buffer = glCreateBuffer
attribute.buffer = GL.CreateBuffer
attribute.buffer.belongs_to_attribute = name

attribute.needs_update = true
Expand Down
8 changes: 4 additions & 4 deletions lib/mittsu/renderers/opengl/core/object_3d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def setup_matrices(camera)
end

def load_uniforms_matrices(uniforms)
glUniformMatrix4fv(uniforms['modelViewMatrix'],
1, GL_FALSE,
GL.UniformMatrix4fv(uniforms['modelViewMatrix'],
1, GL::FALSE,
array_to_ptr_easy(@model_view_matrix.elements))

if uniforms['normalMatrix']
glUniformMatrix3fv(uniforms['normalMatrix'],
1, GL_FALSE,
GL.UniformMatrix3fv(uniforms['normalMatrix'],
1, GL::FALSE,
array_to_ptr_easy(@normal_matrix.elements))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mittsu/renderers/opengl/materials/material.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def allocate_bones(object = nil)
# - limit here is ANGLE's 254 max uniform vectors
# (up to 54 should be safe)

n_vertex_uniforms = (glGetParameter(GL_MAX_VERTEX_UNIFORM_COMPONENTS) / 4.0).floor
n_vertex_uniforms = (GL.GetParameter(GL::MAX_VERTEX_UNIFORM_COMPONENTS) / 4.0).floor
n_vertex_matrices = ((n_vertex_uniforms - 20) / 4.0).floor

max_bones = n_vertex_matrices
Expand Down
Loading

0 comments on commit 37040b8

Please sign in to comment.