diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rlgl/main.lua | 83 | ||||
| -rw-r--r-- | examples/rlgl_hello_triangle/main.lua | 177 |
2 files changed, 177 insertions, 83 deletions
diff --git a/examples/rlgl/main.lua b/examples/rlgl/main.lua deleted file mode 100644 index 4b59419..0000000 --- a/examples/rlgl/main.lua +++ /dev/null @@ -1,83 +0,0 @@ --- Work In Progress! - -local monitor = 0 -local texture = -1 -local triSize = 32.0 -local vertices = { - 0.0, 0.0, 0.0, - 0.0, 0.0, triSize, - triSize, 0.0, triSize -} -local colors = { - 255, 0, 0, - 0, 255, 0, - 0, 0, 255 -} - -local VBO_VERTEX_POS = 0 -local VBO_COLOR_POS = 1 - -local mesh = { - vaoId = -1, - vboIds = { - vertices = -1, - colors = -1, - } -} - -local function uploadMesh() - mesh.vaoId = RL.rlLoadVertexArray() - - RL.rlEnableVertexArray( mesh.vaoId ) - - -- Vertices. - local vertexBuffer = RL.LoadBuffer( vertices, RL.BUFFER_FLOAT ) - mesh.vboIds.vertices = RL.rlLoadVertexBuffer( vertexBuffer, false ) - RL.rlSetVertexAttribute( VBO_VERTEX_POS, 3, RL.RL_FLOAT, false, 0, 0 ) - RL.rlEnableVertexAttribute( VBO_VERTEX_POS ) - - -- Colors. - local colorBuffer = RL.LoadBuffer( colors, RL.BUFFER_UNSIGNED_CHAR ) - mesh.vboIds.colors = RL.rlLoadVertexBuffer( colorBuffer, false ) - RL.rlSetVertexAttribute( VBO_COLOR_POS, 4, RL.RL_UNSIGNED_BYTE, false, 0, 0 ) - RL.rlEnableVertexAttribute( VBO_COLOR_POS ) - - RL.rlDisableVertexArray() - - print( "\nMesh:" ) - print( "\tvaoId: "..mesh.vaoId ) - print( "\tvboIds.vertices: "..mesh.vboIds.vertices ) - print( "\tvboIds.colors: "..mesh.vboIds.colors ) -end - -function RL.init() - local mPos = RL.GetMonitorPosition( monitor ) - local mSize = RL.GetMonitorSize( monitor ) - local winSize = RL.GetScreenSize() - - RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) - RL.SetWindowState( RL.FLAG_VSYNC_HINT ) - RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) - - uploadMesh() -end - -local function drawMesh() -end - -function RL.draw() - RL.ClearBackground( { 100, 150, 100 } ) -end - --- You need to manually free resources. -function RL.exit() - if 0 <= mesh.vaoId then - RL.rlUnloadVertexArray( mesh.vaoId ) - end - - for _, vboId in pairs( mesh.vboIds ) do - if 0 <= vboId then - RL.rlUnloadVertexBuffer( vboId ) - end - end -end
\ No newline at end of file diff --git a/examples/rlgl_hello_triangle/main.lua b/examples/rlgl_hello_triangle/main.lua new file mode 100644 index 0000000..571acb0 --- /dev/null +++ b/examples/rlgl_hello_triangle/main.lua @@ -0,0 +1,177 @@ +package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" + +Util = require( "utillib" ) +Vec2 = require( "vector2" ) + +local res = Vec2:new( 1024, 720 ) +local winScale = 1 +local winSize = res:scale( winScale ) +local monitor = 0 + +local triangle = { + texture = { + id = -1, + data = nil, + size = Vec2:new(), + mipmaps = 0, + format = 0, + }, + vao = -1, + vbos = { + positions = -1, + texcoords = -1, + colors = -1, + }, +} + +local vertexShader = [[ + #version 330 core + layout (location = 0) in vec3 aPos; + layout (location = 1) in vec4 aColor; + layout (location = 2) in vec2 aTexCoord; + + out vec4 flagColor; + out vec2 fragTexCoord; + + void main() { + flagColor = aColor; + fragTexCoord = aTexCoord; + gl_Position = vec4( aPos, 1.0 ); + } +]] +local fragmentShader = [[ + #version 330 core + out vec4 FragColor; + + in vec4 flagColor; + in vec2 fragTexCoord; + + uniform sampler2D ourTexture; + + void main() { + FragColor = texture( ourTexture, fragTexCoord ) * flagColor; + } +]] + +local shaderProgram = -1 + +-- Let's make our own custom texture. +local function loadTexture( path ) + local image = RL.LoadImage( path ) + + triangle.texture.data = RL.GetImageData( image ) + triangle.texture.size = Vec2:new( RL.GetImageSize( image ) ) + triangle.texture.format = RL.GetImageFormat( image ) + triangle.texture.mipmaps = RL.GetImageMipmaps( image ) + + triangle.texture.id = RL.rlLoadTexture( + triangle.texture.data, + triangle.texture.size, + triangle.texture.format, + triangle.texture.mipmaps + ) +end + +local function createTriangle() + loadTexture( RL.GetBasePath().."../resources/images/monkey_tex.png" ) + -- Set texture to shader uniform. + local shaderLoc = RL.rlGetLocationUniform( shaderProgram, "ourTexture" ) + + RL.rlEnableShader( shaderProgram ) + -- NOTE: Default texture is always activated as GL_TEXTURE0 so our slot will be 1. + RL.rlSetUniformSampler( shaderLoc, triangle.texture.id ) + RL.rlDisableShader() + + -- Setup triangle buffers. + triangle.vao = RL.rlLoadVertexArray() + RL.rlEnableVertexArray( triangle.vao ) + + -- Positions. + local vertexBuffer = RL.LoadBuffer( + { + -0.5, -0.5, 0.0, + 0.5, -0.5, 0.0, + 0.0, 0.5, 0.0 + }, + RL.BUFFER_FLOAT + ) + triangle.vbos.positions = RL.rlLoadVertexBuffer( vertexBuffer, false ) + RL.rlSetVertexAttribute( 0, 3, RL.RL_FLOAT, false, 0, 0 ) + RL.rlEnableVertexAttribute( 0 ) + + -- Colors. + local colors = RL.LoadBuffer( + { + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 0, 1, 1 + }, + RL.BUFFER_FLOAT + ) + triangle.vbos.colors = RL.rlLoadVertexBuffer( colors, false ) + RL.rlSetVertexAttribute( 1, 4, RL.RL_FLOAT, false, 0, 0 ) + RL.rlEnableVertexAttribute( 1 ) + + -- Texcoords. + local texcoors = RL.LoadBuffer( + { + 0, 0, + 1, 0, + 0.5, 1, + }, + RL.BUFFER_FLOAT + ) + triangle.vbos.texcoors = RL.rlLoadVertexBuffer( texcoors, false ) + RL.rlSetVertexAttribute( 2, 2, RL.RL_FLOAT, false, 0, 0 ) + RL.rlEnableVertexAttribute( 2 ) + + -- Disable. + RL.rlDisableVertexBuffer() + RL.rlDisableVertexArray() +end + +function RL.init() + local monitorPos = Vec2:new( RL.GetMonitorPosition( monitor ) ) + local monitorSize = Vec2:new( RL.GetMonitorSize( monitor ) ) + + RL.SetWindowTitle( "RLGL Hello Triangle" ) + RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE ) + RL.SetWindowState( RL.FLAG_VSYNC_HINT ) + RL.SetWindowSize( winSize ) + RL.SetWindowPosition( { monitorPos.x + monitorSize.x / 2 - winSize.x / 2, monitorPos.y + monitorSize.y / 2 - winSize.y / 2 } ) + + RL.rlViewport( { 0, 0, res.x ,res.y } ) + shaderProgram = RL.rlLoadShaderCode( vertexShader, fragmentShader ) + + createTriangle() +end + +local function drawTriangle() + -- Texture slot 0 is the default texture. + RL.rlActiveTextureSlot( 1 ) + RL.rlEnableTexture( triangle.texture.id ) + + RL.rlEnableShader( shaderProgram ) + + RL.rlEnableVertexArray( triangle.vao ) + RL.rlDrawVertexArray( 0, 3 ) + + -- Disable. + RL.rlDisableVertexArray() + RL.rlDisableVertexBuffer() + RL.rlDisableTexture() + RL.rlDisableShader() +end + +function RL.draw() + RL.ClearBackground( RL.BLACK ) + drawTriangle() +end + +function RL.exit() + RL.rlUnloadVertexArray( triangle.vao ) + RL.rlUnloadVertexBuffer( triangle.vbos.positions ) + RL.rlUnloadVertexBuffer( triangle.vbos.colors ) + RL.rlUnloadVertexBuffer( triangle.vbos.texcoords ) + RL.rlUnloadTexture( triangle.texture.id ) +end |
