diff options
| author | jussi | 2022-04-05 01:38:03 +0300 |
|---|---|---|
| committer | jussi | 2022-04-05 01:38:03 +0300 |
| commit | fbfe1e0cb6207b6c8afd42457cb1304c7264734f (patch) | |
| tree | beb8e4725b6e9842fee5f929ad3f3b24663a26d2 /src | |
| parent | 2a46afbf91c17463b1360faf7773a9aeb69b4e46 (diff) | |
| download | reilua-enhanced-fbfe1e0cb6207b6c8afd42457cb1304c7264734f.tar.gz reilua-enhanced-fbfe1e0cb6207b6c8afd42457cb1304c7264734f.tar.bz2 reilua-enhanced-fbfe1e0cb6207b6c8afd42457cb1304c7264734f.zip | |
Lights.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 86 | ||||
| -rw-r--r-- | src/lights.c | 108 | ||||
| -rw-r--r-- | src/lua_core.c | 11 | ||||
| -rw-r--r-- | src/models.c | 2 | ||||
| -rw-r--r-- | src/state.c | 14 |
5 files changed, 210 insertions, 11 deletions
@@ -44,7 +44,7 @@ bool validCamera3D( size_t id ) { } } -static inline bool validShader( size_t id ) { +bool validShader( size_t id ) { if ( id < 0 || state->shaderCount < id || state->shaders[ id ] == NULL ) { TraceLog( LOG_WARNING, "%s %d", "Invalid shader", id ); return false; @@ -859,8 +859,8 @@ int lcoreLoadShader( lua_State *L ) { } state->shaders[i] = malloc( sizeof( Shader ) ); // *state->shaders[i] = LoadShader( lua_tostring( L, -2 ), lua_tostring( L, -1 ) ); - // *state->shaders[i] = LoadShader( vsFileName, fsFileName ); - *state->shaders[i] = LoadShader( 0, fsFileName ); + *state->shaders[i] = LoadShader( vsFileName, fsFileName ); + // *state->shaders[i] = LoadShader( 0, fsFileName ); lua_pushinteger( L, i ); checkShaderRealloc( i ); @@ -876,13 +876,23 @@ Load shader from code strings and bind default locations - Success return int */ -//TODO Should also allow only one shader. int lcoreLoadShaderFromMemory( lua_State *L ) { - if ( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) ) { - TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadShaderFromMemory( string vsCode, string fsCode )" ); - lua_pushinteger( L, -1 ); - return 1; + // if ( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) ) { + // TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadShaderFromMemory( string vsCode, string fsCode )" ); + // lua_pushinteger( L, -1 ); + // return 1; + // } + + char fs[ STRING_LEN ] = { '\0' }; + char vs[ STRING_LEN ] = { '\0' }; + + if ( lua_isstring( L, -1 ) ) { + strcpy( fs, lua_tostring( L, -1 ) ); + } + if ( lua_isstring( L, -2 ) ) { + strcpy( vs, lua_tostring( L, -2 ) ); } + int i = 0; for ( i = 0; i < state->shaderCount; i++ ) { @@ -891,7 +901,8 @@ int lcoreLoadShaderFromMemory( lua_State *L ) { } } state->shaders[i] = malloc( sizeof( Shader ) ); - *state->shaders[i] = LoadShaderFromMemory( lua_tostring( L, -2 ), lua_tostring( L, -1 ) ); + // *state->shaders[i] = LoadShaderFromMemory( lua_tostring( L, -2 ), lua_tostring( L, -1 ) ); + *state->shaders[i] = LoadShaderFromMemory( vs, fs ); lua_pushinteger( L, i ); checkShaderRealloc( i ); @@ -986,6 +997,63 @@ int lcoreGetShaderLocationAttrib( lua_State *L ) { } /* +> success = RL_SetShaderLocationIndex( Shader shader, int shaderLocationIndex, int location ) + +Set shader location index + +- Failure return false +- Success return true +*/ +int lcoreSetShaderLocationIndex( lua_State *L ) { + if ( !lua_isnumber( L, -3 ) || !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) { + TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SetShaderLocationIndex( Shader shader, int shaderLocationIndex, int location )" ); + lua_pushboolean( L, false ); + return 1; + } + int location = lua_tointeger( L, -1 ); + lua_pop( L, 1 ); + int shaderLocationIndex = lua_tointeger( L, -1 ); + lua_pop( L, 1 ); + size_t shaderId = lua_tointeger( L, -1 ); + + if ( !validShader( shaderId ) ) { + lua_pushboolean( L, false ); + return 1; + } + state->shaders[ shaderId ]->locs[ shaderLocationIndex ] = location; + lua_pushboolean( L, true ); + + return 1; +} + +/* +> location = RL_GetShaderLocationIndex( Shader shader, int shaderLocationIndex ) + +Get shader location index + +- Failure return false +- Success return int +*/ +int lcoreGetShaderLocationIndex( lua_State *L ) { + if ( !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) { + TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetShaderLocationIndex( Shader shader, int shaderLocationIndex )" ); + lua_pushboolean( L, false ); + return 1; + } + int shaderLocationIndex = lua_tointeger( L, -1 ); + lua_pop( L, 1 ); + size_t shaderId = lua_tointeger( L, -1 ); + + if ( !validShader( shaderId ) ) { + lua_pushboolean( L, false ); + return 1; + } + lua_pushinteger( L, state->shaders[ shaderId ]->locs[ shaderLocationIndex ] ); + + return 1; +} + +/* > success = RL_SetShaderValueMatrix( Shader shader, int locIndex, Matrix mat ) Set shader uniform value ( matrix 4x4 ) diff --git a/src/lights.c b/src/lights.c new file mode 100644 index 0000000..381361e --- /dev/null +++ b/src/lights.c @@ -0,0 +1,108 @@ +#include "main.h" +#include "state.h" +#include "lua_core.h" +#include "core.h" +#include "lights.h" + +#define RLIGHTS_IMPLEMENTATION +#include "rlights.h" + +static void checkLightRealloc( int i ) { + if ( i == state->lightCount ) { + state->lightCount++; + } + + if ( state->lightCount == state->lightAlloc ) { + state->lightAlloc += ALLOC_PAGE_SIZE; + state->lights = realloc( state->lights, state->lightAlloc * sizeof( Light* ) ); + + for ( i = state->lightCount; i < state->lightAlloc; i++ ) { + state->lights[i] = NULL; + } + } +} + +bool validLight( size_t id ) { + if ( id < 0 || state->lightCount < id || state->lights[ id ] == NULL ) { + TraceLog( LOG_WARNING, "%s %d", "Invalid light", id ); + return false; + } + else { + return true; + } +} + +/* +## Lights - Basics +*/ + +/* +> light = RL_CreateLight( int type, Vector3 position, Vector3 target, Color color, Shader shader ) + +Create a light and get shader locations + +- Failure return -1 +- Success return int +*/ +int llightsCreateLight( lua_State *L ) { + if ( !lua_isnumber( L, -5 ) || !lua_istable( L, -4 ) || !lua_istable( L, -3 ) || !lua_istable( L, -2 ) || !lua_isnumber( L, -1 ) ) { + TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_CreateLight( int type, Vector3 position, Vector3 target, Color color, Shader shader )" ); + lua_pushinteger( L, -1 ); + return 1; + } + size_t shaderId = lua_tointeger( L, -1 ); + lua_pop( L, 1 ); + Color color = uluaGetColor( L ); + lua_pop( L, 1 ); + Vector3 target = uluaGetVector3( L ); + lua_pop( L, 1 ); + Vector3 position = uluaGetVector3( L ); + lua_pop( L, 1 ); + int type = lua_tointeger( L, -1 ); + + int i = 0; + + for ( i = 0; i < state->lightCount; i++ ) { + if ( state->lights[i] == NULL ) { + break; + } + } + state->lights[i] = malloc( sizeof( Light ) ); + *state->lights[i] = CreateLight( type, position, target, color, *state->shaders[ shaderId ] ); + lua_pushinteger( L, i ); + checkLightRealloc( i ); + + return 1; +} + +/* +> success = RL_UpdateLightValues( Shader shader, Light light ) + +Send light properties to shader + +- Failure return false +- Success return true +*/ +int llightsUpdateLightValues( lua_State *L ) { + if ( !lua_isnumber( L, -2 ) || !lua_isnumber( L, -1 ) ) { + TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UpdateLightValues( Shader shader, Light light )" ); + lua_pushboolean( L, false ); + return 1; + } + size_t lightId = lua_tointeger( L, -1 ); + lua_pop( L, 1 ); + size_t shaderId = lua_tointeger( L, -1 ); + + if ( !validLight( lightId ) ) { + lua_pushboolean( L, false ); + return 1; + } + if ( !validShader( shaderId ) ) { + lua_pushboolean( L, false ); + return 1; + } + UpdateLightValues( *state->shaders[ shaderId ], *state->lights[ lightId ] ); + lua_pushboolean( L, true ); + + return 1; +} diff --git a/src/lua_core.c b/src/lua_core.c index 25cce86..09a4094 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -9,7 +9,7 @@ #include "audio.h" #include "rmath.h" #include "rgui.h" -#include "raygui.h" +#include "lights.h" static void assignGlobalInt( int value, const char *name ) { lua_State *L = state->luaState; @@ -289,6 +289,9 @@ void defineGlobals() { assignGlobalInt( HUEBAR_PADDING, "HUEBAR_PADDING" ); assignGlobalInt( HUEBAR_SELECTOR_HEIGHT, "HUEBAR_SELECTOR_HEIGHT" ); assignGlobalInt( HUEBAR_SELECTOR_OVERFLOW, "HUEBAR_SELECTOR_OVERFLOW" ); + /* LightType */ + assignGlobalInt( LIGHT_DIRECTIONAL, "LIGHT_DIRECTIONAL" ); + assignGlobalInt( LIGHT_POINT, "LIGHT_POINT" ); /*DOC_END*/ } @@ -530,6 +533,8 @@ void luaRegister() { lua_register( L, "RL_EndShaderMode", lcoreEndShaderMode ); lua_register( L, "RL_GetShaderLocation", lcoreGetShaderLocation ); lua_register( L, "RL_GetShaderLocationAttrib", lcoreGetShaderLocationAttrib ); + lua_register( L, "RL_SetShaderLocationIndex", lcoreSetShaderLocationIndex ); + lua_register( L, "RL_GetShaderLocationIndex", lcoreGetShaderLocationIndex ); lua_register( L, "RL_SetShaderValueMatrix", lcoreSetShaderValueMatrix ); lua_register( L, "RL_SetShaderValueTexture", lcoreSetShaderValueTexture ); lua_register( L, "RL_SetShaderValue", lcoreSetShaderValue ); @@ -879,6 +884,10 @@ void luaRegister() { lua_register( L, "RL_GuiSetIconPixel", lguiGuiSetIconPixel ); lua_register( L, "RL_GuiClearIconPixel", lguiGuiClearIconPixel ); lua_register( L, "RL_GuiCheckIconPixel", lguiGuiCheckIconPixel ); + /* Lights */ + /* Basics. */ + lua_register( L, "RL_CreateLight", llightsCreateLight ); + lua_register( L, "RL_UpdateLightValues", llightsUpdateLightValues ); } /* Lua util functions. */ diff --git a/src/models.c b/src/models.c index 483d0f8..3fe2e45 100644 --- a/src/models.c +++ b/src/models.c @@ -1321,7 +1321,7 @@ int lmodelsCreateMaterial( lua_State *L ) { } } else if ( strcmp( "shader", (char*)lua_tostring( L, -2 ) ) == 0 && lua_isnumber( L, -1 ) ) { - /* TODO Shader when implemented. */ + state->materials[i]->shader = *state->shaders[ lua_tointeger( L, -1 ) ]; } lua_pop( L, 1 ); } diff --git a/src/state.c b/src/state.c index d9b2ad4..cd7629b 100644 --- a/src/state.c +++ b/src/state.c @@ -61,6 +61,10 @@ bool stateInit( const char *exePath ) { state->shaderAlloc = ALLOC_PAGE_SIZE; state->shaderCount = 0; state->shaders = malloc( state->shaderAlloc * sizeof( Shader* ) ); + /* Lights. */ + state->lightAlloc = ALLOC_PAGE_SIZE; + state->lightCount = 0; + state->lights = malloc( state->lightAlloc * sizeof( Light* ) ); for ( int i = 0; i < ALLOC_PAGE_SIZE; i++ ) { state->images[i] = NULL; @@ -72,6 +76,7 @@ bool stateInit( const char *exePath ) { state->models[i] = NULL; state->animations[i] = NULL; state->shaders[i] = NULL; + state->lights[i] = NULL; /* The ones we want to save the first. */ if ( 0 < i ) { @@ -152,6 +157,9 @@ void stateFree() { } for ( int i = 0; i < state->materialCount; ++i ) { if ( state->materials[i] != NULL ) { + /* Prevent unloading shader that would result in double free when freeing shaders. */ + state->materials[i]->shader.id = rlGetShaderIdDefault(); + UnloadMaterial( *state->materials[i] ); free( state->materials[i] ); } @@ -168,6 +176,11 @@ void stateFree() { free( state->shaders[i] ); } } + for ( int i = 0; i < state->lightCount; ++i ) { + if ( state->lights[i] != NULL ) { + free( state->lights[i] ); + } + } if ( IsAudioDeviceReady() ) { CloseAudioDevice(); @@ -190,5 +203,6 @@ void stateFree() { free( state->models ); free( state->animations ); free( state->shaders ); + free( state->lights ); free( state ); } |
