diff --git a/src/dusk/display/display.c b/src/dusk/display/display.c index ff8ca75..1bb12ef 100644 --- a/src/dusk/display/display.c +++ b/src/dusk/display/display.c @@ -22,6 +22,8 @@ display_t DISPLAY = { 0 }; +texture_t TEXTURE; + errorret_t displayInit(void) { memoryZero(&DISPLAY, sizeof(DISPLAY)); @@ -36,6 +38,19 @@ errorret_t displayInit(void) { errorChain(textInit()); errorChain(screenInit()); + color_t pixels[2 * 2] = { + COLOR_RED, COLOR_GREEN, + COLOR_BLUE, COLOR_WHITE + }; + errorChain(textureInit( + &TEXTURE, + 2, 2, + TEXTURE_FORMAT_RGBA, + (texturedata_t){ + .rgbaColors = pixels + } + )); + errorOk(); } @@ -76,10 +91,13 @@ errorret_t displayUpdate(void) { errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model)); - + errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &TEXTURE)); errorChain(spriteBatchPush( - NULL, - 0.0f, 0.0f, 100, 100, COLOR_WHITE, 0, 0, 1, 1 + 0.0f, 0.0f, + 1.0f, 1.0f, + COLOR_WHITE, + 0.0f, 0.0f, + 1.0f, 1.0f )); errorChain(spriteBatchFlush()); diff --git a/src/dusk/display/shader/shader.c b/src/dusk/display/shader/shader.c index d7a40c2..2d36f95 100644 --- a/src/dusk/display/shader/shader.c +++ b/src/dusk/display/shader/shader.c @@ -32,6 +32,18 @@ errorret_t shaderSetMatrix( errorOk(); } +errorret_t shaderSetTexture( + shader_t *shader, + const char_t *name, + texture_t *texture +) { + assertNotNull(shader, "Shader cannot be null"); + assertStrLenMin(name, 1, "Uniform name cannot be empty"); + assertNotNull(texture, "Texture cannot be null"); + errorChain(shaderSetTexturePlatform(shader, name, texture)); + errorOk(); +} + // errorret_t shaderSetColor( // shader_t *shader, // const char_t *name, diff --git a/src/dusk/display/shader/shaderunlit.h b/src/dusk/display/shader/shaderunlit.h index 752c732..cd82b99 100644 --- a/src/dusk/display/shader/shaderunlit.h +++ b/src/dusk/display/shader/shaderunlit.h @@ -11,6 +11,7 @@ #define SHADER_UNLIT_PROJECTION "u_Proj" #define SHADER_UNLIT_VIEW "u_View" #define SHADER_UNLIT_MODEL "u_Model" +#define SHADER_UNLIT_TEXTURE "u_Texture" // #define SHADER_UNLIT_COLOR "u_Color" extern shaderdefinition_t SHADER_UNLIT_DEFINITION; diff --git a/src/dusk/display/spritebatch/spritebatch.c b/src/dusk/display/spritebatch/spritebatch.c index 5015bd5..744e25e 100644 --- a/src/dusk/display/spritebatch/spritebatch.c +++ b/src/dusk/display/spritebatch/spritebatch.c @@ -25,7 +25,6 @@ errorret_t spriteBatchInit() { } errorret_t spriteBatchPush( - texture_t *texture, const float_t minX, const float_t minY, const float_t maxX, @@ -37,7 +36,6 @@ errorret_t spriteBatchPush( const float_t v1 ) { errorChain(spriteBatchPush3D( - texture, (vec3){ minX, minY, 0 }, (vec3){ maxX, maxY, 0 }, color, @@ -48,7 +46,6 @@ errorret_t spriteBatchPush( } errorret_t spriteBatchPush3D( - texture_t *texture, const vec3 min, const vec3 max, const color_t color, @@ -56,12 +53,8 @@ errorret_t spriteBatchPush3D( const vec2 uv1 ) { // Need to flush? - if( - SPRITEBATCH.currentTexture != texture || - SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX - ) { + if(SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX) { errorChain(spriteBatchFlush()); - SPRITEBATCH.currentTexture = texture; } size_t vertexOffset = SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT; @@ -75,7 +68,6 @@ errorret_t spriteBatchPush3D( void spriteBatchClear() { SPRITEBATCH.spriteCount = 0; - SPRITEBATCH.currentTexture = NULL; } errorret_t spriteBatchFlush() { @@ -85,7 +77,6 @@ errorret_t spriteBatchFlush() { size_t vertexCount = QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount; errorChain(meshFlush(&SPRITEBATCH.mesh, 0, vertexCount)); - errorChain(textureBind(SPRITEBATCH.currentTexture)); errorChain(meshDraw(&SPRITEBATCH.mesh, 0, vertexCount)); spriteBatchClear(); errorOk(); diff --git a/src/dusk/display/spritebatch/spritebatch.h b/src/dusk/display/spritebatch/spritebatch.h index a22ec78..1342a50 100644 --- a/src/dusk/display/spritebatch/spritebatch.h +++ b/src/dusk/display/spritebatch/spritebatch.h @@ -7,16 +7,13 @@ #pragma once #include "display/mesh/quad.h" -#include "display/texture/texture.h" -#define SPRITEBATCH_SPRITES_MAX 1 +#define SPRITEBATCH_SPRITES_MAX 16 #define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT) - typedef struct { mesh_t mesh; int32_t spriteCount; - texture_t *currentTexture; } spritebatch_t; // Have to define these seperately because of alignment in certain platforms. @@ -39,7 +36,6 @@ errorret_t spriteBatchInit(); * Currently changing texture pointer will cause the buffer to flush but this is * also likely to change in the future. * - * @param texture The texture to use for the sprite. * @param minX The minimum x coordinate of the sprite. * @param minY The minimum y coordinate of the sprite. * @param maxX The maximum x coordinate of the sprite. @@ -52,7 +48,6 @@ errorret_t spriteBatchInit(); * @return An error code indicating success or failure. */ errorret_t spriteBatchPush( - texture_t *texture, const float_t minX, const float_t minY, const float_t maxX, @@ -68,7 +63,6 @@ errorret_t spriteBatchPush( * Pushes a 3D sprite to the batch. This is like spriteBatchPush but takes * 3D coordinates instead of 2D. * - * @param texture The texture to use for the sprite. * @param min The minimum (x,y,z) coordinate of the sprite. * @param max The maximum (x,y,z) coordinate of the sprite. * @param color The color to tint the sprite with. @@ -77,7 +71,6 @@ errorret_t spriteBatchPush( * @return An error code indicating success or failure. */ errorret_t spriteBatchPush3D( - texture_t *texture, const vec3 min, const vec3 max, const color_t color, diff --git a/src/dusk/display/text/text.c b/src/dusk/display/text/text.c index 440639f..37e6b4b 100644 --- a/src/dusk/display/text/text.c +++ b/src/dusk/display/text/text.c @@ -46,7 +46,7 @@ errorret_t textDrawChar( tilesetTileGetUV(tileset, tileIndex, uv); errorChain(spriteBatchPush( - texture, + // texture, x, y, x + tileset->tileWidth, y + tileset->tileHeight, diff --git a/src/dusk/display/texture/texture.c b/src/dusk/display/texture/texture.c index 7a6b2e5..49c6d65 100644 --- a/src/dusk/display/texture/texture.c +++ b/src/dusk/display/texture/texture.c @@ -11,8 +11,6 @@ #include "util/math.h" #include "display/display.h" -texture_t *TEXTURE_BOUND = NULL; - errorret_t textureInit( texture_t *texture, const int32_t width, @@ -34,18 +32,9 @@ errorret_t textureInit( errorOk(); } -errorret_t textureBind(texture_t *texture) { - errorChain(textureBindPlatform(texture)); - errorOk(); -} - errorret_t textureDispose(texture_t *texture) { assertNotNull(texture, "Texture cannot be NULL"); - if(TEXTURE_BOUND == texture) { - textureBind(NULL); - } - errorChain(textureDisposePlatform(texture)); errorOk(); } \ No newline at end of file diff --git a/src/dusk/display/texture/texture.h b/src/dusk/display/texture/texture.h index c760057..a7fdbff 100644 --- a/src/dusk/display/texture/texture.h +++ b/src/dusk/display/texture/texture.h @@ -13,9 +13,6 @@ #ifndef textureInitPlatform #error "textureInitPlatform should not be defined." #endif -#ifndef textureBindPlatform - #error "textureBindPlatform should not be defined." -#endif #ifndef textureDisposePlatform #error "textureDisposePlatform should not be defined." #endif @@ -28,8 +25,6 @@ typedef union texturedata_u { color_t *rgbaColors; } texturedata_t; -extern texture_t *TEXTURE_BOUND; - /** * Initializes a texture. * @@ -47,13 +42,6 @@ errorret_t textureInit( const texturedata_t data ); -/** - * Binds a texture for rendering. Providing NULL will unbind any texture. - * - * @param texture The texture to bind. - */ -errorret_t textureBind(texture_t *texture); - /** * Disposes a texture. * diff --git a/src/dusk/map/map.c b/src/dusk/map/map.c index 572bd32..1c0a7c4 100644 --- a/src/dusk/map/map.c +++ b/src/dusk/map/map.c @@ -176,7 +176,7 @@ void mapUpdate() { void mapRender() { if(!mapIsLoaded()) return; - textureBind(NULL); + // textureBind(NULL); for(chunkindex_t i = 0; i < MAP_CHUNK_COUNT; i++) { mapChunkRender(&MAP.chunks[i]); } diff --git a/src/dusk/script/module/display/modulespritebatch.c b/src/dusk/script/module/display/modulespritebatch.c index ce96b53..3daa7ae 100644 --- a/src/dusk/script/module/display/modulespritebatch.c +++ b/src/dusk/script/module/display/modulespritebatch.c @@ -32,32 +32,20 @@ int moduleSpriteBatchClear(lua_State *L) { int moduleSpriteBatchPush(lua_State *L) { assertNotNull(L, "Lua state is null"); - // Texture pointer or Nil for no texture - if(!lua_isuserdata(L, 1) && !lua_isnil(L, 1)) { - return luaL_error(L, "First argument must be a texture or nil"); - } - - // If texture is not nil, check it's a texture userdata - texture_t *tex = NULL; - if(lua_isuserdata(L, 1)) { - tex = (texture_t *)luaL_checkudata(L, 1, "texture_mt"); - assertNotNull(tex, "Texture pointer cannot be NULL"); - } - // MinX, MinY, MaxX, MaxY if( - !lua_isnumber(L, 2) || !lua_isnumber(L, 3) || !lua_isnumber(L, 4) || - !lua_isnumber(L, 5) + !lua_isnumber(L, 1) || !lua_isnumber(L, 2) || !lua_isnumber(L, 3) || + !lua_isnumber(L, 4) ) { return luaL_error(L, "Sprite coordinates must be numbers"); } // Color (struct) or nil for white color_t *color = NULL; - if(lua_gettop(L) < 6 || lua_isnil(L, 6)) { + if(lua_gettop(L) < 5 || lua_isnil(L, 5)) { // Allow NULL - } else if(lua_isuserdata(L, 6)) { - color = (color_t*)luaL_checkudata(L, 6, "color_mt"); + } else if(lua_isuserdata(L, 5)) { + color = (color_t*)luaL_checkudata(L, 5, "color_mt"); } else { return luaL_error(L, "Sprite color must be a color struct or nil"); } @@ -68,31 +56,30 @@ int moduleSpriteBatchPush(lua_State *L) { float_t u1 = 1.0f; float_t v1 = 1.0f; - if(lua_gettop(L) >= 8) { - if(!lua_isnumber(L, 7) || !lua_isnumber(L, 8)) { + if(lua_gettop(L) >= 7) { + if(!lua_isnumber(L, 6) || !lua_isnumber(L, 7)) { return luaL_error(L, "Sprite UV min coordinates must be numbers"); } - u0 = (float_t)lua_tonumber(L, 7); - v0 = (float_t)lua_tonumber(L, 8); + u0 = (float_t)lua_tonumber(L, 6); + v0 = (float_t)lua_tonumber(L, 7); } - if(lua_gettop(L) >= 10) { - if(!lua_isnumber(L, 9) || !lua_isnumber(L, 10)) { + if(lua_gettop(L) >= 9) { + if(!lua_isnumber(L, 8) || !lua_isnumber(L, 9)) { return luaL_error(L, "Sprite UV max coordinates must be numbers"); } - u1 = (float_t)lua_tonumber(L, 9); - v1 = (float_t)lua_tonumber(L, 10); + u1 = (float_t)lua_tonumber(L, 8); + v1 = (float_t)lua_tonumber(L, 9); } - float_t minX = (float_t)lua_tonumber(L, 2); - float_t minY = (float_t)lua_tonumber(L, 3); - float_t maxX = (float_t)lua_tonumber(L, 4); - float_t maxY = (float_t)lua_tonumber(L, 5); + float_t minX = (float_t)lua_tonumber(L, 1); + float_t minY = (float_t)lua_tonumber(L, 2); + float_t maxX = (float_t)lua_tonumber(L, 3); + float_t maxY = (float_t)lua_tonumber(L, 4); errorret_t ret = spriteBatchPush( - tex, minX, minY, maxX, diff --git a/src/duskgl/display/shader/shadergl.c b/src/duskgl/display/shader/shadergl.c index 148a306..10e0baa 100644 --- a/src/duskgl/display/shader/shadergl.c +++ b/src/duskgl/display/shader/shadergl.c @@ -203,6 +203,32 @@ errorret_t shaderSetMatrixGL( errorOk(); } +errorret_t shaderSetTextureGL( + shadergl_t *shader, + const char_t *name, + texture_t *texture +) { + assertNotNull(shader, "Shader cannot be null"); + assertNotNull(texture, "Texture cannot be null"); + assertStrLenMin(name, 1, "Uniform name cannot be empty"); + + #ifdef DUSK_OPENGL_LEGACY + assertUnreachable("Cannot set textures on legacy opengl."); + #else + GLint location; + errorChain(shaderParamGetLocationGL(shader, name, &location)); + + glActiveTexture(GL_TEXTURE0); + errorChain(errorGLCheck()); + glBindTexture(GL_TEXTURE_2D, texture->id); + errorChain(errorGLCheck()); + glUniform1i(location, 0); + errorChain(errorGLCheck()); + #endif + + errorOk(); +} + // errorret_t shaderSetColorGL( // shadergl_t *shader, // const char_t *name, diff --git a/src/duskgl/display/shader/shadergl.h b/src/duskgl/display/shader/shadergl.h index 0c6fa12..a3d2cea 100644 --- a/src/duskgl/display/shader/shadergl.h +++ b/src/duskgl/display/shader/shadergl.h @@ -7,7 +7,7 @@ #pragma once #include "error/errorgl.h" -#include "display/color.h" +#include "display/texture/texture.h" typedef struct { #ifdef DUSK_OPENGL_LEGACY @@ -100,11 +100,11 @@ errorret_t shaderSetMatrixGL( * @param color The color data to set. * @return An errorret_t indicating success or failure. */ -// errorret_t shaderSetTextureGL( -// shadergl_t *shader, -// const char_t *name, -// texture_t *texture -// ); +errorret_t shaderSetTextureGL( + shadergl_t *shader, + const char_t *name, + texture_t *texture +); /** * Sets a color uniform parameter in the shader. diff --git a/src/duskgl/display/shader/shaderplatform.h b/src/duskgl/display/shader/shaderplatform.h index 2c5364a..5c6e8ef 100644 --- a/src/duskgl/display/shader/shaderplatform.h +++ b/src/duskgl/display/shader/shaderplatform.h @@ -14,6 +14,6 @@ typedef shaderdefinitiongl_t shaderdefinitionplatform_t; #define shaderInitPlatform shaderInitGL #define shaderBindPlatform shaderBindGL #define shaderSetMatrixPlatform shaderSetMatrixGL -// #define shaderSetTexturePlatform shaderSetTextureGL +#define shaderSetTexturePlatform shaderSetTextureGL // #define shaderSetColorPlatform shaderSetColorGL #define shaderDisposePlatform shaderDisposeGL \ No newline at end of file diff --git a/src/duskgl/display/shader/shaderunlitgl.c b/src/duskgl/display/shader/shaderunlitgl.c index 7bc9123..3d7c33d 100644 --- a/src/duskgl/display/shader/shaderunlitgl.c +++ b/src/duskgl/display/shader/shaderunlitgl.c @@ -20,17 +20,21 @@ "uniform mat4 u_View;\n" "uniform mat4 u_Model;\n" "out vec4 v_Color;\n" + "out vec2 v_TexCoord;\n" "void main() {\n" " gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" " v_Color = aColor;\n" + " v_TexCoord = aTexCoord;\n" "}\n", .frag = "#version 330 core\n" + "uniform sampler2D u_Texture;\n" "in vec4 v_Color;\n" + "in vec2 v_TexCoord;\n" "out vec4 FragColor;\n" "void main() {\n" - " FragColor = v_Color;\n" + " FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n" "}\n" }; #endif \ No newline at end of file diff --git a/src/duskgl/display/texture/texturegl.c b/src/duskgl/display/texture/texturegl.c index 1c7108a..9b890a6 100644 --- a/src/duskgl/display/texture/texturegl.c +++ b/src/duskgl/display/texture/texturegl.c @@ -63,19 +63,6 @@ errorret_t textureInitGL( errorOk(); } -errorret_t textureBindGL(texturegl_t *texture) { - if(texture == NULL) { - glBindTexture(GL_TEXTURE_2D, 0); - errorChain(errorGLCheck()); - errorOk(); - } - - assertTrue(texture->id != 0, "Texture ID must be valid"); - glBindTexture(GL_TEXTURE_2D, texture->id); - errorChain(errorGLCheck()); - errorOk(); -} - errorret_t textureDisposeGL(texturegl_t *texture) { assertNotNull(texture, "Texture cannot be NULL"); assertTrue(texture->id != 0, "Texture ID must be valid"); diff --git a/src/duskgl/display/texture/texturegl.h b/src/duskgl/display/texture/texturegl.h index 7d620d5..bfe408a 100644 --- a/src/duskgl/display/texture/texturegl.h +++ b/src/duskgl/display/texture/texturegl.h @@ -40,14 +40,6 @@ errorret_t textureInitGL( const texturedata_t data ); -/** - * Binds a texture for rendering. Providing NULL will unbind any texture. - * - * @param texture The texture to bind. - * @return An error if the texture failed to bind, otherwise success. - */ -errorret_t textureBindGL(texturegl_t *texture); - /** * Disposes a texture. * diff --git a/src/duskgl/display/texture/textureplatform.h b/src/duskgl/display/texture/textureplatform.h index c540531..922f755 100644 --- a/src/duskgl/display/texture/textureplatform.h +++ b/src/duskgl/display/texture/textureplatform.h @@ -12,5 +12,4 @@ typedef textureformatgl_t textureformatplatform_t; typedef texturegl_t textureplatform_t; #define textureInitPlatform textureInitGL -#define textureBindPlatform textureBindGL #define textureDisposePlatform textureDisposeGL \ No newline at end of file