#==============================================================================
# ** GL
#------------------------------------------------------------------------------
# Graphics API interface.
# All functions here should be used to deal with 3D rendering.
# By Felix "Xilefian" Jones [Version a.1]
#==============================================================================
module GL
DEBUG_CONSOLE = 0 # Set to 1 to show console when debugging
#==============================================================================
# ** Texture
#------------------------------------------------------------------------------
# GPU texture class.
#==============================================================================
class Texture
attr_reader :name, :width, :height
#--------------------------------------------------------------------------
# * Colour components
#--------------------------------------------------------------------------
R = 1
RG = 2
RGB = 3
RGBA = 4
#--------------------------------------------------------------------------
# * Allocates a texture with a given component count
#--------------------------------------------------------------------------
def self.Alloc( components, width, height )
Console::Print( "Allocating texture [#{width}, #{height}]" )
tex = new( OpenGL::GenTexture( components, width, height, 0 ) )
tex.send( :SetSize, width, height )
return tex
end
#--------------------------------------------------------------------------
# * Allocates a texture with a given bitmap
#--------------------------------------------------------------------------
def self.AllocWithBitmap( bitmap )
width = bitmap.width
height = bitmap.height
handle = bitmap.__id__
Console::Print( "Allocating texture from Bitmap [#{width}, #{height}]" )
tex = new( OpenGL::GenTexture( 0, 0, 0, handle ) )
tex.send( :SetSize, width, height )
return tex
end
#--------------------------------------------------------------------------
# * Destroys the texture
#--------------------------------------------------------------------------
def Dealloc
if @name != 0
Console::Print( "Deallocating texture" )
OpenGL::DestroyTex( @name )
@name = 0
@width = 0
@height = 0
end
end
#--------------------------------------------------------------------------
# * Copies a bitmap into glTexture
#--------------------------------------------------------------------------
def LoadBitmap( bitmap )
OpenGL::TexSubBitmap( @name, bitmap.__id__ )
end
#--------------------------------------------------------------------------
# * Sets texture to GPU unit
#--------------------------------------------------------------------------
def BindToUnit( unit )
OpenGL::BindTextureToUnit( @name, unit )
end
#--------------------------------------------------------------------------
# * Sets the texture size (!PRIVATE!)
#--------------------------------------------------------------------------
def SetSize( width, height )
@width = width
@height = height
end
private :SetSize
#--------------------------------------------------------------------------
# * Initialize (!PRIVATE!)
#--------------------------------------------------------------------------
private_class_method :new
def initialize( name )
@name = name
@width = 0
@height = 0
end
end
#==============================================================================
# ** Shader
#------------------------------------------------------------------------------
# Class for compiling shaders.
#==============================================================================
class Shader
attr_reader :name, :type
#--------------------------------------------------------------------------
# * Shader types
#--------------------------------------------------------------------------
VERTEX = 1
GEOMETRY = 2
FRAGMENT = 3
#--------------------------------------------------------------------------
# * Creates vertex shader
#--------------------------------------------------------------------------
def self.CreateVertex( src )
Console::Print( "Creating Vertex shader" )
shader = new( OpenGL::CreateShader( VERTEX ), VERTEX )
status = shader.send( :Compile, src )
if status != 0
shader.Dealloc
return nil
else
return shader
end
end
#--------------------------------------------------------------------------
# * Creates geometry shader
#--------------------------------------------------------------------------
def self.CreateGeometry( src )
Console::Print( "Creating Geometry shader" )
shader = new( OpenGL::CreateShader( GEOMETRY ), GEOMETRY )
status = shader.send( :Compile, src )
if status != 0
shader.Dealloc
return nil
else
return shader
end
end
#--------------------------------------------------------------------------
# * Creates fragment shader
#--------------------------------------------------------------------------
def self.CreateFragment( src )
Console::Print( "Creating Fragment shader" )
shader = new( OpenGL::CreateShader( FRAGMENT ), FRAGMENT )
status = shader.send( :Compile, src )
if status != 0
shader.Dealloc
return nil
else
return shader
end
end
#--------------------------------------------------------------------------
# * Deletes shader
#--------------------------------------------------------------------------
def Dealloc
Console::Print( "Deallocating shader" )
OpenGL::DestroyShader( @name )
@name = 0
@type = 0
end
#--------------------------------------------------------------------------
# * Compiles the shader source (!PRIVATE!)
#--------------------------------------------------------------------------
def Compile( src )
return OpenGL::ShaderCompile( @name, src )
end
private :Compile
#--------------------------------------------------------------------------
# * Initialize (!PRIVATE!)
#--------------------------------------------------------------------------
private_class_method :new
def initialize( name, type )
@name = name
@type = type
end
end
#==============================================================================
# ** ShaderMaterial
#------------------------------------------------------------------------------
# Class for managing shader programs.
#==============================================================================
class ShaderMaterial
@@current_shader = nil
#--------------------------------------------------------------------------
# * Current bound shader handle
#--------------------------------------------------------------------------
def self.GetCurrentShader
return @@current_shader
end
#--------------------------------------------------------------------------
# * Current bound shader handle
#--------------------------------------------------------------------------
def self.SetCurrentShader( shader )
@@current_shader = shader
end
#--------------------------------------------------------------------------
# * Creates a shader program
#--------------------------------------------------------------------------
def self.Create( vertex, fragment )
Console::Print( "Creating vertex/fragment program" )
material = new( OpenGL::CreateProgam() )
material.send( :AttachShaders, vertex, fragment, nil )
return material
end
#--------------------------------------------------------------------------
# * Creates a shader program with a geometry shader
#--------------------------------------------------------------------------
def self.CreateWithGeometry( vertex, geometry, fragment )
Console::Print( "Creating vertex/geometry/fragment program" )
material = new( OpenGL::CreateProgam() )
material.send( :AttachShaders, vertex, fragment, geometry )
return material
end
#--------------------------------------------------------------------------
# * Attach Shaders
#--------------------------------------------------------------------------
def AttachShaders( vsh, fsh, gsh )
@vsh = vsh
@fsh = fsh
@gsh = gsh
end
#--------------------------------------------------------------------------
# * Destroys this program
#--------------------------------------------------------------------------
def Dealloc
Console::Print( "Deallocating program" )
OpenGL::DestroyProg( @name )
@name = 0
@vsh = nil
@gsh = nil
@fsh = nil
@uniformMap = nil
@uniLocationMap = nil
end
#--------------------------------------------------------------------------
# * Activates this shader for rendering
#--------------------------------------------------------------------------
def UseProgram
ShaderMaterial.SetCurrentShader( self )
if @isLinked == false
status = self.send( :Link )
if status != 0
self.Dealloc
return
end
@isLinked = true
end
# Use program
OpenGL::UseProgram( @name )
# Update uniforms
self.UploadUniforms
end
#--------------------------------------------------------------------------
# * Uploads Uniforms to current shader
#--------------------------------------------------------------------------
def UploadUniforms
@uniformMap.each do |uniformName, value|
case value
when Float
OpenGL::SetUniform1f( uniformName, [value] )
when Integer
OpenGL::SetUniform1i( uniformName, value )
when Array
# Check first value of array, assume that is what we are uploading
case value.count
when 1
case value[0]
when Float
OpenGL::SetUniform1f( uniformName, value )
when Integer
OpenGL::SetUniform1i( uniformName, value )
end
when 2
case value[0]
when Float
OpenGL::SetUniform2f( uniformName, value )
when Integer
OpenGL::SetUniform2i( uniformName, value )
when Matrix
case value[1]
when 2
value[0].UploadMatrix2( uniformName )
when 3
value[0].UploadMatrix3( uniformName )
when 4
value[0].UploadMatrix4( uniformName )
end
end
when 3
case value[0]
when Float
OpenGL::SetUniform3f( uniformName, value )
when Integer
OpenGL::SetUniform3i( uniformName, value )
end
when 4
case value[0]
when Float
OpenGL::SetUniform4f( uniformName, value )
when Integer
OpenGL::SetUniform4i( uniformName, value )
end
end
end
end
@uniformMap.clear
end
#--------------------------------------------------------------------------
# * Sets a program uniform to a value
#--------------------------------------------------------------------------
def SetUniform( uniformName, value )
if @isLinked == false
status = self.send( :Link )
if status != 0
self.Dealloc
return
end
@isLinked = true
end
if @uniLocationMap[uniformName] == nil
intVal = OpenGL::GetUniformLocation( @name, uniformName )
Console::Print( "Found uniform #{uniformName} == #{intVal}" )
@uniLocationMap[uniformName] = intVal
else
intVal = @uniLocationMap[uniformName]
end
@uniformMap[intVal] = value
if ShaderMaterial.GetCurrentShader() == self
self.UploadUniforms
end
end
#--------------------------------------------------------------------------
# * Sets a program uniform to a mat2
#--------------------------------------------------------------------------
def SetUniformMatrix2( uniformName, matrix )
if @uniLocationMap[uniformName] == nil
intVal = OpenGL::GetUniformLocation( @name, uniformName )
@uniLocationMap[uniformName] = intVal
else
intVal = @uniLocationMap[uniformName]
end
@uniformMap[intVal] = [matrix, 2]
if ShaderMaterial.GetCurrentShader() == self
self.UploadUniforms
end
end
#--------------------------------------------------------------------------
# * Sets a program uniform to a mat3
#--------------------------------------------------------------------------
def SetUniformMatrix3( uniformName, matrix )
if @uniLocationMap[uniformName] == nil
intVal = OpenGL::GetUniformLocation( @name, uniformName )
@uniLocationMap[uniformName] = intVal
else
intVal = @uniLocationMap[uniformName]
end
@uniformMap[intVal] = [matrix, 3]
if ShaderMaterial.GetCurrentShader() == self
self.UploadUniforms
end
end
#--------------------------------------------------------------------------
# * Sets a program uniform to a mat4
#--------------------------------------------------------------------------
def SetUniformMatrix4( uniformName, matrix )
if @uniLocationMap[uniformName] == nil
intVal = OpenGL::GetUniformLocation( @name, uniformName )
@uniLocationMap[uniformName] = intVal
else
intVal = @uniLocationMap[uniformName]
end
@uniformMap[intVal] = [matrix, 4]
if ShaderMaterial.GetCurrentShader() == self
self.UploadUniforms
end
end
#--------------------------------------------------------------------------
# * Sets a program attribute to an index
#--------------------------------------------------------------------------
def SetAttributeIndex( attribStr, index )
if @isLinked == true
Console::Print( "ERROR - Tried to set attribute location of linked shader!" )
Console::Print( "- Call .SetAttributeIndex BEFORE linking program!" )
return
end
Console::Print( "Setting attribute #{attribStr} to #{index}" )
OpenGL::BindAttribLocation( @name, index, attribStr )
end
#--------------------------------------------------------------------------
# * Links shaders to this program (!PRIVATE!)
#--------------------------------------------------------------------------
def Link
if @gsh
return OpenGL::ProgLink( @name, @vsh.name, @gsh.name, @fsh.name )
else
return OpenGL::ProgLink( @name, @vsh.name, 0, @fsh.name )
end
end
private :Link
private :AttachShaders
#--------------------------------------------------------------------------
# * Initialize (!PRIVATE!)
#--------------------------------------------------------------------------
private_class_method :new
def initialize( name )
@name = name
@vsh = nil
@gsh = nil
@fsh = nil
@isLinked = false
@uniformMap = Hash.new
@uniLocationMap = Hash.new
end
end
#==============================================================================
# ** VertexBuffer
#------------------------------------------------------------------------------
# Class for uploading vertex data to the GPU.
#==============================================================================
class VertexBuffer
#--------------------------------------------------------------------------
# * Draw Modes
#--------------------------------------------------------------------------
DRAW_POINTS = 1 # Point cloud
DRAW_LINES = 2 # Pairs of verts = line
DRAW_LINE_STRIP = 3 # Verts = line path
DRAW_LINE_LOOP = 4 # Verts = line path, ending back at first
DRAW_TRIANGLES = 5 # 3 verts = triangle
DRAW_TRIANGLE_STRIP = 6 # Each new vert makes a triangle from the last two
DRAW_TRIANGLE_FAN = 7 # Each new vert makes a traingle from last + first
#--------------------------------------------------------------------------
# * Creates the GPU VBO
#--------------------------------------------------------------------------
def self.Alloc
Console::Print( "Allocating VertexBuffer" )
return new
end
#--------------------------------------------------------------------------
# * Deletes the GPU handles for this VBO
#--------------------------------------------------------------------------
def Dealloc
Console::Print( "Deallocating VertexBuffer" )
OpenGL::DestroyVBO( @name )
end
#--------------------------------------------------------------------------
# * Updates this VBO and then renders it
#--------------------------------------------------------------------------
def DrawArrays( drawMode )
OpenGL::BindVBO( @name )
if @vertexArray
OpenGL::VBOData( @vertexArray );
@vertexArray = nil
end
index = 0
offset = 0
@description.each do |arg|
OpenGL::VBOAttrib( index, arg, @stride, offset )
offset += arg
index += 1
end
OpenGL::DrawVBO( drawMode, @vertexCount )
end
#--------------------------------------------------------------------------
# * Sets how the buffer should be read
# * eg: vbo.DescribeBuffer( 3, [3, 2, 4] ) # 3 verts = vec3, vec2, vec4
#--------------------------------------------------------------------------
def DescribeBuffer( vertexCount, args )
@stride = 0
args.each { |a| @stride += a }
@vertexCount = vertexCount
@description = args
end
#--------------------------------------------------------------------------
# * Uploads a given vertex array to this buffer object
#--------------------------------------------------------------------------
def UploadVertexArray( vertexArray )
@vertexArray = vertexArray.flatten
end
#--------------------------------------------------------------------------
# * Initialize (!PRIVATE!)
#--------------------------------------------------------------------------
private_class_method :new
def initialize
@name = OpenGL::GenVBO()
@stride = 0
@vertexCount = 0
@vertexArray = nil
@description = nil
end
end
#==============================================================================
# ** FrameBuffer
#------------------------------------------------------------------------------
# Class for creating frame buffers to render to.
#==============================================================================
class FrameBuffer
attr_reader :name, :width, :height
#--------------------------------------------------------------------------
# * Clear Modes
#--------------------------------------------------------------------------
Clear = 1
#--------------------------------------------------------------------------
# * Creates the GPU FB with no attachments
#--------------------------------------------------------------------------
def self.Alloc( width, height )
Console::Print( "Allocating FrameBuffer [#{width}, #{height}]" )
return new( width, height )
end
#--------------------------------------------------------------------------
# * Deletes the GPU handles for this FBO
#--------------------------------------------------------------------------
def Dealloc
Console::Print( "Deallocating FrameBuffer" )
OpenGL::DestroyFBO( @name )
@width = 0
@height = 0
@colourAttachment0 = nil
@name = 0
end
#--------------------------------------------------------------------------
# * Copies a frame buffer contents into bitmap
#--------------------------------------------------------------------------
def ToBitmap( bitmap )
OpenGL::GetTexImage( @name, bitmap.__id__ )
end
#--------------------------------------------------------------------------
# * Attaches a texture to this FBO
#--------------------------------------------------------------------------
def AttachTexture( texture )
if texture.width != @width || texture.height != @height
message = "Expected size [#{@width}, #{@height}] " <<
"given size [#{texture.width}, #{texture.height}]"
Console::Print( "FrameBuffer attach failure!" )
Console::Print( message )
else
oldAttachment = @colourAttachment0
@colourAttachment0 = texture
Console::Print( "Attaching Texture to FrameBuffer" )
OpenGL::FBOTexure( @name, texture.name )
if oldAttachment != nil
return oldAttachment
end
end
end
#--------------------------------------------------------------------------
# * Test method for rendering to FBO
#--------------------------------------------------------------------------
def DrawShader( shader )
shader.UseProgram
OpenGL::DrawToFBO( @name )
end
#--------------------------------------------------------------------------
# * Makes this FBO the render target
#--------------------------------------------------------------------------
def Bind( willClear = 0 )
OpenGL::BindFBO( @name )
if willClear == 1
OpenGL::Clear()
end
end
#--------------------------------------------------------------------------
# * Initialize (!PRIVATE!)
#--------------------------------------------------------------------------
private_class_method :new
def initialize( width, height )
@width = width
@height = height
@colourAttachment0 = nil
@name = OpenGL::GenFBO()
end
end
#==============================================================================
# ** OpenGL
#------------------------------------------------------------------------------
# OpenGL management.
# Object life-time for 3D elements managed here.
#==============================================================================
class OpenGL
#--------------------------------------------------------------------------
# * Starts up the Graphics interface
#--------------------------------------------------------------------------
def initialize
initStatus = Lib.XGL_Init
case initStatus
when 1
SceneManager.exit_error( "Unsupported GL version\nOpenGL 2.1 Required" )
when 2
SceneManager.exit_error( "Missing extension FrameBufferObject" )
end
end
#--------------------------------------------------------------------------
# * Clears current buffer
#--------------------------------------------------------------------------
def self.Clear
Lib.Graphics_Clear()
end
#--------------------------------------------------------------------------
# * Create a glTexImage
#--------------------------------------------------------------------------
def self.DestroyTex( texName )
Lib.Graphics_DestroyTex( texName )
end
#--------------------------------------------------------------------------
# * Create a glTexImage
#--------------------------------------------------------------------------
def self.GenTexture( components, width, height, bmpHandle )
return Lib.Graphics_GenTexture( components, width, height, bmpHandle )
end
#--------------------------------------------------------------------------
# * Loads a bitmap into a glTexImage
#--------------------------------------------------------------------------
def self.TexSubBitmap( texName, bmpHandle )
return Lib.Graphics_TexSubBitmap( texName, bmpHandle )
end
#--------------------------------------------------------------------------
# * Dumps a glTexImage into a bitmap
#--------------------------------------------------------------------------
def self.GetTexImage( texName, bmpHandle )
return Lib.Graphics_GetTexImage( texName, bmpHandle )
end
#--------------------------------------------------------------------------
# * Creates a new FBO
#--------------------------------------------------------------------------
def self.GenFBO
return Lib.Graphics_GenFBO
end
#--------------------------------------------------------------------------
# * Creates a new FBO
#--------------------------------------------------------------------------
def self.DestroyFBO( fboName )
Lib.Graphics_DestroyFBO( fboName )
end
#--------------------------------------------------------------------------
# * Attaches texture to FBO
#--------------------------------------------------------------------------
def self.FBOTexure( fboName, texName )
Lib.Graphics_FBOTexure( fboName, texName )
end
#--------------------------------------------------------------------------
# * Creates a new shader of given type
#--------------------------------------------------------------------------
def self.CreateShader( shaderType )
return Lib.Graphics_CreateShader( shaderType )
end
#--------------------------------------------------------------------------
# * Deletes glShader
#--------------------------------------------------------------------------
def self.DestroyShader( shaderName )
Lib.Graphics_DestroyShader( shaderName )
end
#--------------------------------------------------------------------------
# * Compiles shader source and returns the result
#--------------------------------------------------------------------------
def self.ShaderCompile( shaderName, src )
return Lib.Graphics_ShaderCompile( shaderName, src )
end
#--------------------------------------------------------------------------
# * Creates a shader program
#--------------------------------------------------------------------------
def self.CreateProgam
return Lib.Graphics_CreateProgam
end
#--------------------------------------------------------------------------
# * Links shaders to this program
#--------------------------------------------------------------------------
def self.ProgLink( progName, vsh, gsh, fsh )
return Lib.Graphics_ProgLink( progName, vsh, gsh, fsh )
end
#--------------------------------------------------------------------------
# * Deletes a shader program
#--------------------------------------------------------------------------
def self.DestroyProg( progName )
return Lib.Graphics_DestroyProg( progName )
end
#--------------------------------------------------------------------------
# * Deletes a shader program
#--------------------------------------------------------------------------
def self.GetUniformLocation( progName, uniString )
return Lib.Graphics_UniformLocation( progName, uniString )
end
#--------------------------------------------------------------------------
# * Draws texName to fboName using progName
#--------------------------------------------------------------------------
def self.DrawToFBO( fboName )
Lib.Graphics_DrawToFBO( fboName )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform1f( name, args )
larr = args.pack("f").unpack("l")
Lib.Graphics_Uniform1f( name, larr[0] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform2f( name, args )
larr = args.pack( "ff" ).unpack( "ll" )
Lib.Graphics_Uniform2f( name, larr[0], larr[1] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform3f( name, args )
larr = args.pack( "fff" ).unpack( "lll" )
Lib.Graphics_Uniform3f( name, larr[0], larr[1], larr[2] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform4f( name, args )
larr = args.pack( "ffff" ).unpack( "llll" )
Lib.Graphics_Uniform4f( name, larr [0], larr [1], larr[2], larr[3] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform1i( name, args )
Lib.Graphics_Uniform1i( name, args )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform2i( name, args )
Lib.Graphics_Uniform2i( name, args[0], args[1] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform3i( name, args )
Lib.Graphics_Uniform3i( name, args[0], args[1], args[2] )
end
#--------------------------------------------------------------------------
# * Sets program uniform value at location
#--------------------------------------------------------------------------
def self.SetUniform4i( name, args )
Lib.Graphics_Uniform4i( name, args[0], args[1], args[2], args[3])
end
#--------------------------------------------------------------------------
# * Activates a shader program for usage (And modifying)
#--------------------------------------------------------------------------
def self.UseProgram( shaderName )
Lib.Graphics_UseProgram( shaderName )
end
#--------------------------------------------------------------------------
# * Binds (and activates) a texture to a given unit
#--------------------------------------------------------------------------
def self.BindTextureToUnit( textureName, unitIndex )
Lib.Graphics_BindTexUnit( textureName, unitIndex )
end
#--------------------------------------------------------------------------
# * Creats a new VBO (Must be destroyed!)
#--------------------------------------------------------------------------
def self.GenVBO
return Lib.Graphics_GenVBO
end
#--------------------------------------------------------------------------
# * Destroys a VBO
#--------------------------------------------------------------------------
def self.DestroyVBO( vboName )
Lib.Graphics_DestroyVBO( vboName )
end
#--------------------------------------------------------------------------
# * Binds a VBO (Making it current)
#--------------------------------------------------------------------------
def self.BindVBO( vboName )
Lib.Graphics_BindVBO( vboName )
end
#--------------------------------------------------------------------------
# * Uploads the vertex data for current VBO
#--------------------------------------------------------------------------
def self.VBOData( floatArray )
stringPacker = "f" * floatArray.length
floatString = floatArray.pack( stringPacker )
Lib.Graphics_VBOData( floatString, floatArray.length )
end
#--------------------------------------------------------------------------
# * Applies the attribute data for current VBO
#--------------------------------------------------------------------------
def self.VBOAttrib( index, components, stride, offset )
Lib.Graphics_VBOAttrib( index, components, stride, offset )
end
#--------------------------------------------------------------------------
# * Draws the current VBO
#--------------------------------------------------------------------------
def self.DrawVBO( mode, count )
Lib.Graphics_DrawVBO( mode, count )
end
#--------------------------------------------------------------------------
# * Draws the current VBO
#--------------------------------------------------------------------------
def self.BindAttribLocation( shaderName, index, attribStr )
Lib.Graphics_BindAttribLocation( shaderName, index, attribStr )
end
#--------------------------------------------------------------------------
# * Binds a FBO (Target render buffer)
#--------------------------------------------------------------------------
def self.BindFBO( fboName )
Lib.Graphics_BindFBO( fboName )
end
end
#==============================================================================
# ** Matrix
#------------------------------------------------------------------------------
# Manipulates and uploads matrices
#==============================================================================
class Matrix
#--------------------------------------------------------------------------
# * Wrapper for Matrix.new
#--------------------------------------------------------------------------
def self.Alloc
matrix = new()
matrix.Push
return matrix
end
#--------------------------------------------------------------------------
# * Pushes an identity matrix to the top
#--------------------------------------------------------------------------
def Push
Lib.Matrix_Push( @name )
end
#--------------------------------------------------------------------------
# * Removes the top matrix
#--------------------------------------------------------------------------
def Pop
Lib.Matrix_Pop( @name )
end
#--------------------------------------------------------------------------
# * Uploads the top matrix to the current shader's mat2 at location
#--------------------------------------------------------------------------
def UploadMatrix2( location )
Lib.Matrix_UploadMatrix2( @name, location )
end
#--------------------------------------------------------------------------
# * Uploads the top matrix to the current shader's mat3 at location
#--------------------------------------------------------------------------
def UploadMatrix3( location )
Lib.Matrix_UploadMatrix3( @name, location )
end
#--------------------------------------------------------------------------
# * Uploads the top matrix to the current shader's mat4 at location
#--------------------------------------------------------------------------
def UploadMatrix4( location )
Lib.Matrix_UploadMatrix4( @name, location )
end
#--------------------------------------------------------------------------
# * Translates the matrix
#--------------------------------------------------------------------------
def Translate( x, y, z )
larr = [x, y, z].pack("fff").unpack("lll")
Lib.Matrix_Translate( @name, larr[0], larr[1], larr[2] )
end
#--------------------------------------------------------------------------
# * Scales the matrix
#--------------------------------------------------------------------------
def Scale( x, y, z )
larr = [x, y, z].pack("fff").unpack("lll")
Lib.Matrix_Scale( @name, larr[0], larr[1], larr[2] )
end
#--------------------------------------------------------------------------
# * Rotates the matrix in the X axis by radian
#--------------------------------------------------------------------------
def RotateX( radian )
larr = [radian].pack("f").unpack("l")
Lib.Matrix_RotateX( @name, larr[0] )
end
#--------------------------------------------------------------------------
# * Rotates the matrix in the Y axis by radian
#--------------------------------------------------------------------------
def RotateY( radian )
larr = [radian].pack("f").unpack("l")
Lib.Matrix_RotateY( @name, larr[0] )
end
#--------------------------------------------------------------------------
# * Rotates the matrix in the Z axis by radian
#--------------------------------------------------------------------------
def RotateZ( radian )
larr = [radian].pack("f").unpack("l")
Lib.Matrix_RotateZ( @name, larr[0] )
end
#--------------------------------------------------------------------------
# * Destroys the matrix
#--------------------------------------------------------------------------
def Dealloc
Lib.Matrix_Destroy( @name )
end
#--------------------------------------------------------------------------
# * Creates the matrix handle
#--------------------------------------------------------------------------
private_class_method :new
def initialize
@name = Lib.Matrix_Create()
end
end
#==============================================================================
# ** Console
#------------------------------------------------------------------------------
# Debug console, feel free to ignore.
#==============================================================================
class Console
#--------------------------------------------------------------------------
# * When in test mode this will show the console
#--------------------------------------------------------------------------
def initialize
if $TEST && DEBUG_CONSOLE == 1
Lib.Console_Show
end
end
#--------------------------------------------------------------------------
# * Prints debug text
#--------------------------------------------------------------------------
def self.Print( str )
return Lib.Console_Print( "[RMVXA-GL] #{str}" )
end
end
#==============================================================================
# ** Lib
#------------------------------------------------------------------------------
# DLL library interface here.
# This should generally be considered private.
#==============================================================================
module Lib
#--------------------------------------------------------------------------
# * Library API methods
#--------------------------------------------------------------------------
def self.XGL_Init( *args );return @@XGL_Init.call( *args );end
def self.XGL_Terminate( *args );return @@XGL_Terminate.call( *args );end
def self.Console_Show( *args );return @@Console_Show.call( *args );end
def self.Console_Hide( *args );return @@Console_Hide.call( *args );end
def self.Console_Print( *args );return @@Console_Print.call( *args );end
def self.Graphics_Clear( *args );return @@Graphics_Clear.call( *args );end
def self.Graphics_GenTexture( *args );return @@Graphics_GenTexture.call( *args );end
def self.Graphics_TexSubBitmap( *args );return @@Graphics_TexSubBitmap.call( *args );end
def self.Graphics_DestroyTex( *args );return @@Graphics_DestroyTex.call( *args );end
def self.Graphics_BindTexUnit( *args );return @@Graphics_BindTexUnit.call( *args );end
def self.Graphics_GenFBO( *args );return @@Graphics_GenFBO.call( *args );end
def self.Graphics_FBOTexure( *args );return @@Graphics_FBOTexure.call( *args );end
def self.Graphics_GetTexImage( *args );return @@Graphics_GetTexImage.call( *args );end
def self.Graphics_DestroyFBO( *args );return @@Graphics_DestroyFBO.call( *args );end
def self.Graphics_BindFBO( *args );return @@Graphics_BindFBO.call( *args );end
def self.Graphics_CreateShader( *args );return @@Graphics_CreateShader.call( *args );end
def self.Graphics_DestroyShader( *args );return @@Graphics_DestroyShader.call( *args );end
def self.Graphics_ShaderCompile( *args );return @@Graphics_ShaderCompile.call( *args );end
def self.Graphics_CreateProgam( *args );return @@Graphics_CreateProgam.call( *args );end
def self.Graphics_ProgLink( *args );return @@Graphics_ProgLink.call( *args );end
def self.Graphics_DestroyProg( *args );return @@Graphics_DestroyProg.call( *args );end
def self.Graphics_UniformLocation( *args );return @@Graphics_UniformLocation.call( *args );end
def self.Graphics_Uniform1f( *args );return @@Graphics_Uniform1f.call( *args );end
def self.Graphics_Uniform2f( *args );return @@Graphics_Uniform2f.call( *args );end
def self.Graphics_Uniform3f( *args );return @@Graphics_Uniform3f.call( *args );end
def self.Graphics_Uniform4f( *args );return @@Graphics_Uniform4f.call( *args );end
def self.Graphics_Uniform1i( *args );return @@Graphics_Uniform1i.call( *args );end
def self.Graphics_Uniform2i( *args );return @@Graphics_Uniform2i.call( *args );end
def self.Graphics_Uniform3i( *args );return @@Graphics_Uniform3i.call( *args );end
def self.Graphics_Uniform4i( *args );return @@Graphics_Uniform4i.call( *args );end
def self.Graphics_Uniform1ui( *args );return @@Graphics_Uniform1ui.call( *args );end
def self.Graphics_Uniform2ui( *args );return @@Graphics_Uniform2ui.call( *args );end
def self.Graphics_Uniform3ui( *args );return @@Graphics_Uniform3ui.call( *args );end
def self.Graphics_Uniform4ui( *args );return @@Graphics_Uniform4ui.call( *args );end
def self.Graphics_UseProgram( *args );return @@Graphics_UseProgram.call( *args );end
def self.Graphics_BindAttribLocation( *args );return @@Graphics_BindAttribLocation.call( *args );end
def self.Graphics_GenVBO( *args );return @@Graphics_GenVBO.call( *args );end
def self.Graphics_DestroyVBO( *args );return @@Graphics_DestroyVBO.call( *args );end
def self.Graphics_BindVBO( *args );return @@Graphics_BindVBO.call( *args );end
def self.Graphics_VBOData( *args );return @@Graphics_VBOData.call( *args );end
def self.Graphics_VBOAttrib( *args );return @@Graphics_VBOAttrib.call( *args );end
def self.Graphics_DrawVBO( *args );return @@Graphics_DrawVBO.call( *args );end
def self.Matrix_Create( *args );return @@Matrix_Create.call( *args );end
def self.Matrix_Destroy( *args );return @@Matrix_Destroy.call( *args );end
def self.Matrix_Pop( *args );return @@Matrix_Pop.call( *args );end
def self.Matrix_Push( *args );return @@Matrix_Push.call( *args );end
def self.Matrix_Size( *args );return @@Matrix_Size.call( *args );end
def self.Matrix_UploadMatrix2( *args );return @@Matrix_UploadMatrix2.call( *args );end
def self.Matrix_UploadMatrix3( *args );return @@Matrix_UploadMatrix3.call( *args );end
def self.Matrix_UploadMatrix4( *args );return @@Matrix_UploadMatrix4.call( *args );end
def self.Matrix_LoadMatrix4( *args );return @@Matrix_LoadMatrix4.call( *args );end
def self.Matrix_MultiplyMatrix4( *args );return @@Matrix_MultiplyMatrix4.call( *args );end
def self.Matrix_MultiplyMatrixStack( *args );return @@Matrix_MultiplyMatrixStack.call( *args );end
def self.Matrix_Rotate( *args );return @@Matrix_Rotate.call( *args );end
def self.Matrix_RotateX( *args );return @@Matrix_RotateX.call( *args );end
def self.Matrix_RotateY( *args );return @@Matrix_RotateY.call( *args );end
def self.Matrix_RotateZ( *args );return @@Matrix_RotateZ.call( *args );end
def self.Matrix_Scale( *args );return @@Matrix_Scale.call( *args );end
def self.Matrix_Translate( *args );return @@Matrix_Translate.call( *args );end
#--------------------------------------------------------------------------
# * Library name, read as LIB_NAME.dll
#--------------------------------------------------------------------------
LIB_NAME = "RMVXA-GL"
#--------------------------------------------------------------------------
# * Win32API function pointer bindings
#--------------------------------------------------------------------------
@@XGL_Init = Win32API.new( LIB_NAME, "XGL_Init", "v", "i" )
@@XGL_Terminate = Win32API.new( LIB_NAME, "XGL_Terminate", "v", "v" )
@@Console_Show = Win32API.new( LIB_NAME, "Console_Show", "v", "v" )
@@Console_Hide = Win32API.new( LIB_NAME, "Console_Hide", "v", "v" )
@@Console_Print = Win32API.new( LIB_NAME, "Console_Print", "p", "v" )
@@Graphics_Clear = Win32API.new( LIB_NAME, "Graphics_Clear", "v", "v" )
@@Graphics_GenTexture = Win32API.new( LIB_NAME, "Graphics_GenTexture", "iiip", "i" )
@@Graphics_TexSubBitmap = Win32API.new( LIB_NAME, "Graphics_TexSubBitmap", "ip", "v" )
@@Graphics_DestroyTex = Win32API.new( LIB_NAME, "Graphics_DestroyTex", "i", "v" )
@@Graphics_BindTexUnit = Win32API.new( LIB_NAME, "Graphics_BindTexUnit", "ii", "v" )
@@Graphics_GenFBO = Win32API.new( LIB_NAME, "Graphics_GenFBO", "v", "i" )
@@Graphics_FBOTexure = Win32API.new( LIB_NAME, "Graphics_FBOTexure", "ii", "v" )
@@Graphics_GetTexImage = Win32API.new( LIB_NAME, "Graphics_GetTexImage", "ip", "v" )
@@Graphics_DestroyFBO = Win32API.new( LIB_NAME, "Graphics_DestroyFBO", "i", "v" )
@@Graphics_BindFBO = Win32API.new( LIB_NAME, "Graphics_BindFBO", "i", "v" )
@@Graphics_CreateShader = Win32API.new( LIB_NAME, "Graphics_CreateShader", "i", "i" )
@@Graphics_DestroyShader = Win32API.new( LIB_NAME, "Graphics_DestroyShader", "i", "v" )
@@Graphics_ShaderCompile = Win32API.new( LIB_NAME, "Graphics_ShaderCompile", "ip", "i" )
@@Graphics_CreateProgam = Win32API.new( LIB_NAME, "Graphics_CreateProgam", "v", "i" )
@@Graphics_ProgLink = Win32API.new( LIB_NAME, "Graphics_ProgLink", "iiii", "i" )
@@Graphics_DestroyProg = Win32API.new( LIB_NAME, "Graphics_DestroyProg", "i", "v" )
@@Graphics_UniformLocation = Win32API.new( LIB_NAME, "Graphics_UniformLocation", "ip", "i" )
@@Graphics_Uniform1f = Win32API.new( LIB_NAME, "Graphics_Uniform1f", "ii", "v" )
@@Graphics_Uniform2f = Win32API.new( LIB_NAME, "Graphics_Uniform2f", "iii", "v" )
@@Graphics_Uniform3f = Win32API.new( LIB_NAME, "Graphics_Uniform3f", "iiii", "v" )
@@Graphics_Uniform4f = Win32API.new( LIB_NAME, "Graphics_Uniform4f", "iiiii", "v" )
@@Graphics_Uniform1i = Win32API.new( LIB_NAME, "Graphics_Uniform1i", "ii", "v" )
@@Graphics_Uniform2i = Win32API.new( LIB_NAME, "Graphics_Uniform2i", "iii", "v" )
@@Graphics_Uniform3i = Win32API.new( LIB_NAME, "Graphics_Uniform3i", "iiii", "v" )
@@Graphics_Uniform4i = Win32API.new( LIB_NAME, "Graphics_Uniform4i", "iiiii", "v" )
@@Graphics_UseProgram = Win32API.new( LIB_NAME, "Graphics_UseProgram", "i", "v" )
@@Graphics_BindAttribLocation = Win32API.new( LIB_NAME, "Graphics_BindAttribLocation", "iip", "v" )
@@Graphics_GenVBO = Win32API.new( LIB_NAME, "Graphics_GenVBO", "v", "i" )
@@Graphics_DestroyVBO = Win32API.new( LIB_NAME, "Graphics_DestroyVBO", "i", "v" )
@@Graphics_BindVBO = Win32API.new( LIB_NAME, "Graphics_BindVBO", "i", "v" )
@@Graphics_VBOData = Win32API.new( LIB_NAME, "Graphics_VBOData", "pi", "v" )
@@Graphics_VBOAttrib = Win32API.new( LIB_NAME, "Graphics_VBOAttrib", "iiii", "v" )
@@Graphics_DrawVBO = Win32API.new( LIB_NAME, "Graphics_DrawVBO", "ii", "v" )
@@Matrix_Create = Win32API.new( LIB_NAME, "Matrix_Create", "v", "i" )
@@Matrix_Destroy = Win32API.new( LIB_NAME, "Matrix_Destroy", "i", "v" )
@@Matrix_Pop = Win32API.new( LIB_NAME, "Matrix_Pop", "i", "v" )
@@Matrix_Push = Win32API.new( LIB_NAME, "Matrix_Push", "i", "v" )
@@Matrix_Size = Win32API.new( LIB_NAME, "Matrix_Size", "i", "i" )
@@Matrix_Pop = Win32API.new( LIB_NAME, "Matrix_Pop", "i", "v" )
@@Matrix_Push = Win32API.new( LIB_NAME, "Matrix_Push", "i", "v" )
@@Matrix_UploadMatrix2 = Win32API.new( LIB_NAME, "Matrix_UploadMatrix2", "ii", "v" )
@@Matrix_UploadMatrix3 = Win32API.new( LIB_NAME, "Matrix_UploadMatrix3", "ii", "v" )
@@Matrix_UploadMatrix4 = Win32API.new( LIB_NAME, "Matrix_UploadMatrix4", "ii", "v" )
@@Matrix_LoadMatrix4 = Win32API.new( LIB_NAME, "Matrix_LoadMatrix4", "ip", "v" )
@@Matrix_MultiplyMatrix4 = Win32API.new( LIB_NAME, "Matrix_MultiplyMatrix4", "ip", "v" )
@@Matrix_MultiplyMatrixStack = Win32API.new( LIB_NAME, "Matrix_MultiplyMatrixStack", "ii", "v" )
@@Matrix_Rotate = Win32API.new( LIB_NAME, "Matrix_Rotate", "iiiii", "v" )
@@Matrix_RotateX = Win32API.new( LIB_NAME, "Matrix_RotateX", "ii", "v" )
@@Matrix_RotateY = Win32API.new( LIB_NAME, "Matrix_RotateY", "ii", "v" )
@@Matrix_RotateZ = Win32API.new( LIB_NAME, "Matrix_RotateZ", "ii", "v" )
@@Matrix_Scale = Win32API.new( LIB_NAME, "Matrix_Scale", "iiii", "v" )
@@Matrix_Translate = Win32API.new( LIB_NAME, "Matrix_Translate", "iiii", "v" )
@@Matrix_DownloadMatrix2 = Win32API.new( LIB_NAME, "Matrix_DownloadMatrix2", "i", "p" )
@@Matrix_DownloadMatrix3 = Win32API.new( LIB_NAME, "Matrix_DownloadMatrix3", "i", "p" )
@@Matrix_DownloadMatrix4 = Win32API.new( LIB_NAME, "Matrix_DownloadMatrix4", "i", "p" )
end
end
#==============================================================================
# ** SceneManager
#------------------------------------------------------------------------------
# Adds a finalisation stage to SceneManager.
# Used by XGL to cleanup the OpenGL context.
#==============================================================================
module SceneManager
@@error = nil
#--------------------------------------------------------------------------
# * Override SceneManager
#--------------------------------------------------------------------------
class << self
alias super_exit exit
alias super_run run
def run
GL::Console.new
GL::OpenGL.new
if @@error != nil
messageBox = Win32API.new( "User32", "MessageBox", "lppl", "l" )
messageBox.call( 0, @@error, "SceneManager Error", 0 )
else
super_run
end
end
#--------------------------------------------------------------------------
# * Pre-Termination Processing
#--------------------------------------------------------------------------
def exit
GL::Lib.XGL_Terminate
super_exit
end
end
#--------------------------------------------------------------------------
# * Sets the scene manager to exit with an error message
#--------------------------------------------------------------------------
def self.exit_error( str )
@@error = str
end
end