summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjussi2023-06-03 18:46:20 +0300
committerjussi2023-06-03 18:46:20 +0300
commit8ade9eeb436f1bc8090df698ccf24595cd21b5e3 (patch)
tree97e9528dc066fa619e0679116cb3583d5483b973
parented6d37294a6b72382cca09f6808565b61bd0c36b (diff)
downloadreilua-enhanced-8ade9eeb436f1bc8090df698ccf24595cd21b5e3.tar.gz
reilua-enhanced-8ade9eeb436f1bc8090df698ccf24595cd21b5e3.tar.bz2
reilua-enhanced-8ade9eeb436f1bc8090df698ccf24595cd21b5e3.zip
rlgl Vertex level operations and Draw Textured Polygon Example.
-rw-r--r--API.md82
-rw-r--r--ReiLua_API.lua62
-rw-r--r--changelog2
-rw-r--r--devnotes1
-rw-r--r--examples/draw_textured_polygon/main.lua93
-rw-r--r--include/lrlgl.h10
-rw-r--r--src/lua_core.c16
-rw-r--r--src/rlgl.c189
8 files changed, 451 insertions, 4 deletions
diff --git a/API.md b/API.md
index 0f2b15b..45fa871 100644
--- a/API.md
+++ b/API.md
@@ -6928,6 +6928,88 @@ NOTE: We store current viewport dimensions
---
+## RLGL - Vertex level operations
+
+---
+
+> success = RL.rlBegin( int mode )
+
+Initialize drawing mode ( how to organize vertex )
+
+- Failure return false
+- Success return true
+
+---
+
+> RL.rlEnd()
+
+Finish vertex providing
+
+---
+
+> success = RL.rlVertex2f( Vector2 position )
+
+Define one vertex ( position )
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlVertex3f( Vector3 position )
+
+Define one vertex ( position )
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlTexCoord2f( Vector2 texCoord )
+
+Define one vertex ( texture coordinate ) - 2 float
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlNormal3f( Vector3 normal )
+
+Define one vertex ( normal ) - 3 float
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlColor4ub( Color color )
+
+Define one vertex ( color ) - 4 byte
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlColor3f( Vector3 color )
+
+Define one vertex ( color ) - 3 float
+
+- Failure return false
+- Success return true
+
+---
+
+> success = RL.rlColor4f( Vector4 color )
+
+Define one vertex ( color ) - 4 float
+
+- Failure return false
+- Success return true
+
+---
+
## RLGL - Textures state
---
diff --git a/ReiLua_API.lua b/ReiLua_API.lua
index f7e1bb8..13751ff 100644
--- a/ReiLua_API.lua
+++ b/ReiLua_API.lua
@@ -5631,6 +5631,68 @@ function RL.rlOrtho( left, right, bottom, top, znear, zfar ) end
---@return any success
function RL.rlViewport( viewport ) end
+-- RLGL - Vertex level operations
+
+---Initialize drawing mode ( how to organize vertex )
+---- Failure return false
+---- Success return true
+---@param mode integer
+---@return any success
+function RL.rlBegin( mode ) end
+
+---Finish vertex providing
+---@return any RL.rlEnd
+function RL.rlEnd() end
+
+---Define one vertex ( position )
+---- Failure return false
+---- Success return true
+---@param position table
+---@return any success
+function RL.rlVertex2f( position ) end
+
+---Define one vertex ( position )
+---- Failure return false
+---- Success return true
+---@param position table
+---@return any success
+function RL.rlVertex3f( position ) end
+
+---Define one vertex ( texture coordinate ) - 2 float
+---- Failure return false
+---- Success return true
+---@param texCoord table
+---@return any success
+function RL.rlTexCoord2f( texCoord ) end
+
+---Define one vertex ( normal ) - 3 float
+---- Failure return false
+---- Success return true
+---@param normal table
+---@return any success
+function RL.rlNormal3f( normal ) end
+
+---Define one vertex ( color ) - 4 byte
+---- Failure return false
+---- Success return true
+---@param color table
+---@return any success
+function RL.rlColor4ub( color ) end
+
+---Define one vertex ( color ) - 3 float
+---- Failure return false
+---- Success return true
+---@param color table
+---@return any success
+function RL.rlColor3f( color ) end
+
+---Define one vertex ( color ) - 4 float
+---- Failure return false
+---- Success return true
+---@param color table
+---@return any success
+function RL.rlColor4f( color ) end
+
-- RLGL - Textures state
---Select and active a texture slot
diff --git a/changelog b/changelog
index 0b9f415..e84ed83 100644
--- a/changelog
+++ b/changelog
@@ -24,6 +24,8 @@ KEY CHANGES:
- ADDED: rlgl Textures state functions
- ADDED: rlgl Some Render batch management functions
- ADDED: rlgl Matrix operations functions
+ - ADDED: rlgl Vertex level operations
+ - ADDED: Draw Textured Polygon Example
Detailed changes:
- FIXED: uluaGetRay was looking for integers instead of tables
diff --git a/devnotes b/devnotes
index d4ae534..84f5b73 100644
--- a/devnotes
+++ b/devnotes
@@ -1,6 +1,5 @@
Current {
* rlgl
- * Vertex level operations.
* Vertex buffers state.
* Vertex buffers management
* Shaders management
diff --git a/examples/draw_textured_polygon/main.lua b/examples/draw_textured_polygon/main.lua
new file mode 100644
index 0000000..dc90c69
--- /dev/null
+++ b/examples/draw_textured_polygon/main.lua
@@ -0,0 +1,93 @@
+-- Based on raylib example - Draw Textured Polygon by Chris Camacho (@codifies)
+
+package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
+
+Vec2 = require( "vector2" )
+
+local monitor = 0
+local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
+local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
+local winSize = Vec2:new( RL.GetScreenSize() )
+
+local polygon = {
+ texture = -1,
+ texcoords = {
+ Vec2:new( 0.75, 0.0 ),
+ Vec2:new( 0.25, 0.0 ),
+ Vec2:new( 0.0, 0.5 ),
+ Vec2:new( 0.0, 0.75 ),
+ Vec2:new( 0.25, 1.0 ),
+ Vec2:new( 0.375, 0.875 ),
+ Vec2:new( 0.625, 0.875 ),
+ Vec2:new( 0.75, 1.0 ),
+ Vec2:new( 1.0, 0.75 ),
+ Vec2:new( 1.0, 0.5 ),
+ Vec2:new( 0.75, 0.0 ),
+ },
+ points = {},
+ positions = {},
+ angle = 0.0,
+}
+
+function RL.init()
+ RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
+ RL.SetWindowState( RL.FLAG_VSYNC_HINT )
+ RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
+ RL.SetWindowTitle( "Textured Polygon" )
+
+ polygon.texture = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" )
+
+ -- Define the base poly vertices from the UV's
+ -- NOTE: They can be specified in any other way
+ for _, texcoord in ipairs( polygon.texcoords ) do
+ table.insert( polygon.points, Vec2:new( ( texcoord.x - 0.5 ) * 256, ( texcoord.y - 0.5 ) * 256 ) )
+ end
+
+ -- Define the vertices drawing position
+ -- NOTE: Initially same as points but updated every frame
+ for _, point in ipairs( polygon.points ) do
+ table.insert( polygon.positions, point:clone() )
+ end
+end
+
+function RL.process( delta )
+ polygon.angle = polygon.angle + delta
+
+ -- Update points rotation with an angle transform
+ -- NOTE: Base points position are not modified
+ for i = 1, #polygon.points do
+ polygon.positions[i] = Vec2:new( RL.Vector2Rotate( polygon.points[i], polygon.angle ) )
+ end
+end
+
+-- Draw textured polygon, defined by vertex and texture coordinates
+-- NOTE: Polygon center must have straight line path to all points
+-- without crossing perimeter, points must be in anticlockwise order
+local function drawTexturePoly( texture, center, points, texcoords, tint )
+ RL.rlSetTexture( RL.GetTextureId( texture ) )
+
+ -- Texturing is only supported on RL_QUADS
+ RL.rlBegin( RL.RL_QUADS )
+ RL.rlColor4ub( tint )
+
+ for i = 1, #points - 1 do
+ RL.rlTexCoord2f( { 0.5, 0.5 } )
+ RL.rlVertex2f( center )
+
+ RL.rlTexCoord2f( texcoords[i] )
+ RL.rlVertex2f( center + points[i] )
+
+ RL.rlTexCoord2f( texcoords[i + 1] )
+ RL.rlVertex2f( center + points[i + 1] )
+ -- Dublicate of last vertex to complete quad.
+ RL.rlTexCoord2f( texcoords[i + 1] )
+ RL.rlVertex2f( center + points[i + 1] )
+ end
+ RL.rlEnd()
+end
+
+function RL.draw()
+ RL.ClearBackground( RL.RAYWHITE )
+
+ drawTexturePoly( polygon.texture, winSize:scale( 0.5 ), polygon.positions, polygon.texcoords, RL.WHITE )
+end
diff --git a/include/lrlgl.h b/include/lrlgl.h
index 5f2adae..c56e664 100644
--- a/include/lrlgl.h
+++ b/include/lrlgl.h
@@ -12,6 +12,16 @@ int lrlglMultMatrixf( lua_State *L );
int lrlglFrustum( lua_State *L );
int lrlglOrtho( lua_State *L );
int lrlglViewport( lua_State *L );
+/* Vertex level operations */
+int lrlglBegin( lua_State *L );
+int lrlglEnd( lua_State *L );
+int lrlglVertex2f( lua_State *L );
+int lrlglVertex3f( lua_State *L );
+int lrlglTexCoord2f( lua_State *L );
+int lrlglNormal3f( lua_State *L );
+int lrlglColor4ub( lua_State *L );
+int lrlglColor3f( lua_State *L );
+int lrlglColor4f( lua_State *L );
/* Textures state */
int lrlglActiveTextureSlot( lua_State *L );
int lrlglEnableTexture( lua_State *L );
diff --git a/src/lua_core.c b/src/lua_core.c
index 1ff92cf..74c1d9e 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -1475,7 +1475,7 @@ void luaRegister() {
assingGlobalFunction( "IsLightEnabled", llightsIsLightEnabled );
/* RLGL */
- /* Matrix operations */
+ /* Matrix operations. */
assingGlobalFunction( "rlMatrixMode", lrlglMatrixMode );
assingGlobalFunction( "rlPushMatrix", lrlglPushMatrix );
assingGlobalFunction( "rlPopMatrix", lrlglPopMatrix );
@@ -1487,7 +1487,17 @@ void luaRegister() {
assingGlobalFunction( "rlFrustum", lrlglFrustum );
assingGlobalFunction( "rlOrtho", lrlglOrtho );
assingGlobalFunction( "rlViewport", lrlglViewport );
- /* Textures state */
+ /* Vertex level operations. */
+ assingGlobalFunction( "rlBegin", lrlglBegin );
+ assingGlobalFunction( "rlEnd", lrlglEnd );
+ assingGlobalFunction( "rlVertex2f", lrlglVertex2f );
+ assingGlobalFunction( "rlVertex3f", lrlglVertex3f );
+ assingGlobalFunction( "rlTexCoord2f", lrlglTexCoord2f );
+ assingGlobalFunction( "rlNormal3f", lrlglNormal3f );
+ assingGlobalFunction( "rlColor4ub", lrlglColor4ub );
+ assingGlobalFunction( "rlColor3f", lrlglColor3f );
+ assingGlobalFunction( "rlColor4f", lrlglColor4f );
+ /* Textures state. */
assingGlobalFunction( "rlActiveTextureSlot", lrlglActiveTextureSlot );
assingGlobalFunction( "rlEnableTexture", lrlglEnableTexture );
assingGlobalFunction( "rlDisableTexture", lrlglDisableTexture );
@@ -1519,7 +1529,7 @@ void luaRegister() {
assingGlobalFunction( "rlDrawRenderBatchActive", lrlglDrawRenderBatchActive );
assingGlobalFunction( "rlCheckRenderBatchLimit", lrlglCheckRenderBatchLimit );
assingGlobalFunction( "rlSetTexture", lrlglSetTexture );
- /* Textures management */
+ /* Textures management. */
assingGlobalFunction( "rlLoadTexture", lrlglLoadTexture );
assingGlobalFunction( "rlLoadTextureDepth", lrlglLoadTextureDepth );
assingGlobalFunction( "rlUnloadTexture", lrlglUnloadTexture );
diff --git a/src/rlgl.c b/src/rlgl.c
index eb4033c..a4ddd36 100644
--- a/src/rlgl.c
+++ b/src/rlgl.c
@@ -234,6 +234,195 @@ int lrlglViewport( lua_State *L ) {
}
/*
+## RLGL - Vertex level operations
+*/
+
+/*
+> success = RL.rlBegin( int mode )
+
+Initialize drawing mode ( how to organize vertex )
+
+- Failure return false
+- Success return true
+*/
+int lrlglBegin( lua_State *L ) {
+ if ( !lua_isnumber( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlBegin( int mode )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ rlBegin( lua_tointeger( L, 1 ) );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> RL.rlEnd()
+
+Finish vertex providing
+*/
+int lrlglEnd( lua_State *L ) {
+ rlEnd();
+
+ return 0;
+}
+
+/*
+> success = RL.rlVertex2f( Vector2 position )
+
+Define one vertex ( position )
+
+- Failure return false
+- Success return true
+*/
+int lrlglVertex2f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlVertex2f( Vector2 position )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector2 position = uluaGetVector2Index( L, 1 );
+
+ rlVertex2f( position.x, position.y );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlVertex3f( Vector3 position )
+
+Define one vertex ( position )
+
+- Failure return false
+- Success return true
+*/
+int lrlglVertex3f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlVertex3f( Vector3 position )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector3 position = uluaGetVector3Index( L, 1 );
+
+ rlVertex3f( position.x, position.y, position.z );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlTexCoord2f( Vector2 texCoord )
+
+Define one vertex ( texture coordinate ) - 2 float
+
+- Failure return false
+- Success return true
+*/
+int lrlglTexCoord2f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlTexCoord2f( Vector2 texCoord )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector2 texCoord = uluaGetVector2Index( L, 1 );
+
+ rlTexCoord2f( texCoord.x, texCoord.y );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlNormal3f( Vector3 normal )
+
+Define one vertex ( normal ) - 3 float
+
+- Failure return false
+- Success return true
+*/
+int lrlglNormal3f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlNormal3f( Vector3 normal )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector3 normal = uluaGetVector3Index( L, 1 );
+
+ rlNormal3f( normal.x, normal.y, normal.z );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlColor4ub( Color color )
+
+Define one vertex ( color ) - 4 byte
+
+- Failure return false
+- Success return true
+*/
+int lrlglColor4ub( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlColor4ub( Color color )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Color color = uluaGetColorIndex( L, 1 );
+
+ rlColor4ub( color.r, color.g, color.b, color.a );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlColor3f( Vector3 color )
+
+Define one vertex ( color ) - 3 float
+
+- Failure return false
+- Success return true
+*/
+int lrlglColor3f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlColor3f( Vector3 color )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector3 color = uluaGetVector3Index( L, 1 );
+
+ rlColor3f( color.x, color.y, color.z );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> success = RL.rlColor4f( Vector4 color )
+
+Define one vertex ( color ) - 4 float
+
+- Failure return false
+- Success return true
+*/
+int lrlglColor4f( lua_State *L ) {
+ if ( !lua_istable( L, 1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.rlColor4f( Vector4 color )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Vector4 color = uluaGetVector4Index( L, 1 );
+
+ rlColor4f( color.x, color.y, color.z, color.w );
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
## RLGL - Textures state
*/