RLGL Hello triangle example.

This commit is contained in:
jussi
2023-12-14 23:08:19 +02:00
parent 5ebdba6af0
commit ba4595305f
11 changed files with 254 additions and 87 deletions

18
API.md
View File

@@ -3984,6 +3984,14 @@ Check if a shader is ready
---
> shaderId = RL.GetShaderId( Shader shader )
Get shader program id
- Success return int
---
> location = RL.GetShaderLocation( Shader shader, string uniformName )
Get shader uniform location
@@ -5842,6 +5850,14 @@ Get image pixel color at (x, y) position
---
> imageData = RL.GetImageData( Image image )
Get image data as Buffer
- Success return Buffer
---
> size = RL.GetImageSize( Image image )
Get image size
@@ -9573,7 +9589,7 @@ Unload vertex buffer (VBO)
> RL.rlSetVertexAttribute( int index, int compSize, int type, bool normalized, int stride, int pointer )
Set vertex attribute
Set vertex attribute. Note! Pointer should be given in size of bytes
---

View File

@@ -1564,6 +1564,12 @@ function RL.LoadShaderFromMemory( vsCode, fsCode ) end
---@return any isReady
function RL.IsShaderReady( shader ) end
---Get shader program id
---- Success return int
---@param shader any
---@return any shaderId
function RL.GetShaderId( shader ) end
---Get shader uniform location
---- Success return int
---@param shader any
@@ -3248,6 +3254,12 @@ function RL.GetImageColor( image, pixelPos ) end
-- Textures - Image configuration functions
---Get image data as Buffer
---- Success return Buffer
---@param image any
---@return any imageData
function RL.GetImageData( image ) end
---Get image size
---- Success return Vector2
---@param image any
@@ -6615,7 +6627,7 @@ function RL.rlUnloadVertexArray( vaoId ) end
---@return any RL.rlUnloadVertexBuffer
function RL.rlUnloadVertexBuffer( vboId ) end
---Set vertex attribute
---Set vertex attribute. Note! Pointer should be given in size of bytes
---@param index integer
---@param compSize integer
---@param type integer

View File

@@ -23,6 +23,7 @@ KEY CHANGES:
- ADDED: More Model management functions.
- ADDED: More Model animations management functions.
- ADDED: 2D lightmap example.
- ADDED: RLGL Hello triangle example.
DETAILED CHANGES:
- REMOVED: DrawLineBezierQuad, DrawLineBezierCubic.
@@ -44,6 +45,9 @@ DETAILED CHANGES:
- CHANGE: Renamed ExportBufferAsCode to ExportDataAsCode.
- ADDED: BoneInfo.
- ADDED: Transform.
- ADDED: GetShaderId.
- FIXED: rlSetVertexAttribute pointer offset.
- ADDED: GetImageData.
------------------------------------------------------------------------
Release: ReiLua version 0.6.0 Using Raylib 4.5

View File

@@ -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

View File

@@ -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

View File

@@ -71,6 +71,7 @@ int lcoreEndScissorMode( lua_State *L );
int lcoreLoadShader( lua_State *L );
int lcoreLoadShaderFromMemory( lua_State *L );
int lcoreIsShaderReady( lua_State *L );
int lcoreGetShaderId( lua_State *L );
int lcoreGetShaderLocation( lua_State *L );
int lcoreGetShaderLocationAttrib( lua_State *L );
int lcoreSetShaderLocationIndex( lua_State *L );

View File

@@ -56,6 +56,7 @@ int ltexturesLoadImagePalette( lua_State *L );
int ltexturesGetImageAlphaBorder( lua_State *L );
int ltexturesGetImageColor( lua_State *L );
/* Image configuration functions. */
int ltexturesGetImageData( lua_State *L );
int ltexturesGetImageSize( lua_State *L );
int ltexturesGetImageMipmaps( lua_State *L );
int ltexturesGetImageFormat( lua_State *L );

View File

@@ -884,6 +884,21 @@ int lcoreIsShaderReady( lua_State *L ) {
return 1;
}
/*
> shaderId = RL.GetShaderId( Shader shader )
Get shader program id
- Success return int
*/
int lcoreGetShaderId( lua_State *L ) {
Shader *shader = uluaGetShader( L, 1 );
lua_pushinteger( L, shader->id );
return 1;
}
/*
> location = RL.GetShaderLocation( Shader shader, string uniformName )

View File

@@ -1225,6 +1225,7 @@ void luaRegister() {
assingGlobalFunction( "LoadShader", lcoreLoadShader );
assingGlobalFunction( "LoadShaderFromMemory", lcoreLoadShaderFromMemory );
assingGlobalFunction( "IsShaderReady", lcoreIsShaderReady );
assingGlobalFunction( "GetShaderId", lcoreGetShaderId );
assingGlobalFunction( "GetShaderLocation", lcoreGetShaderLocation );
assingGlobalFunction( "GetShaderLocationAttrib", lcoreGetShaderLocationAttrib );
assingGlobalFunction( "SetShaderLocationIndex", lcoreSetShaderLocationIndex );
@@ -1497,6 +1498,7 @@ void luaRegister() {
assingGlobalFunction( "GetImageAlphaBorder", ltexturesGetImageAlphaBorder );
assingGlobalFunction( "GetImageColor", ltexturesGetImageColor );
/* Image configuration functions. */
assingGlobalFunction( "GetImageData", ltexturesGetImageData );
assingGlobalFunction( "GetImageSize", ltexturesGetImageSize );
assingGlobalFunction( "GetImageMipmaps", ltexturesGetImageMipmaps );
assingGlobalFunction( "GetImageFormat", ltexturesGetImageFormat );

View File

@@ -1105,7 +1105,7 @@ int lrlglUnloadVertexBuffer( lua_State *L ) {
/*
> RL.rlSetVertexAttribute( int index, int compSize, int type, bool normalized, int stride, int pointer )
Set vertex attribute
Set vertex attribute. Note! Pointer should be given in size of bytes
*/
int lrlglSetVertexAttribute( lua_State *L ) {
int index = luaL_checkinteger( L, 1 );
@@ -1115,7 +1115,7 @@ int lrlglSetVertexAttribute( lua_State *L ) {
int stride = luaL_checkinteger( L, 5 );
int pointer = luaL_checkinteger( L, 6 );
rlSetVertexAttribute( index, compSize, type, normalized, stride, &pointer );
rlSetVertexAttribute( index, compSize, type, normalized, stride, (void*)( pointer * sizeof( char ) ) );
return 0;
}

View File

@@ -839,6 +839,28 @@ int ltexturesGetImageColor( lua_State *L ) {
## Textures - Image configuration functions
*/
/*
> imageData = RL.GetImageData( Image image )
Get image data as Buffer
- Success return Buffer
*/
int ltexturesGetImageData( lua_State *L ) {
Image *image = uluaGetImage( L, 1 );
Buffer buffer = (Buffer){
.type = BUFFER_UNSIGNED_CHAR,
.size = GetPixelDataSize( image->width, image->height, image->format ),
};
buffer.data = malloc( buffer.size * sizeof( unsigned char ) );
memcpy( buffer.data, image->data, buffer.size );
uluaPushBuffer( L, buffer );
return 1;
}
/*
> size = RL.GetImageSize( Image image )