diff --git a/src/rlgl.c b/src/rlgl.c index 8f3b42621..773d76bdf 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -33,20 +33,21 @@ #include <string.h> // Declares strcmp(), strlen(), strtok() #if defined(GRAPHICS_API_OPENGL_11) - #ifdef __APPLE__ // OpenGL include for OSX + #ifdef __APPLE__ // OpenGL include for OSX #include <OpenGL/gl.h> #else - #include <GL/gl.h> // Basic OpenGL include + #include <GL/gl.h> // Basic OpenGL include #endif #endif #if defined(GRAPHICS_API_OPENGL_33) #define GLEW_STATIC - #ifdef __APPLE__ // OpenGL include for OSX + #ifdef __APPLE__ // OpenGL include for OSX #include <OpenGL/gl3.h> #else - #include <GL/glew.h> // Extensions loading lib - //#include "glad.h" // TODO: Other extensions loading lib? --> REVIEW + #include <GL/glew.h> // Extensions loading lib + //#include "glad.h" // TODO: Test glad extensions loading lib + //#include "gl_core_3_3.h" // TODO: Test glLoadGen extension loading lib: ERRORS #endif #endif @@ -56,6 +57,10 @@ #include <GLES2/gl2ext.h> #endif +#if defined(RLGL_STANDALONE) + #include <stdarg.h> // Used for functions with variable number of parameters (TraceLog()) +#endif + //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- @@ -177,6 +182,10 @@ typedef struct { unsigned char a; } pixel; +#if defined(RLGL_STANDALONE) +typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; +#endif + //---------------------------------------------------------------------------------- // Global Variables Definition //---------------------------------------------------------------------------------- @@ -250,6 +259,9 @@ static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays; // NOTE: It's required in shapes and models modules! unsigned int whiteTexture; +// Save screen size data (render size), required for postpro quad +static int screenWidth, screenHeight; + //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- @@ -273,6 +285,10 @@ static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight); static char** StringSplit(char *baseString, const char delimiter, int *numExt); #endif +#if defined(RLGL_STANDALONE) +static void TraceLog(int msgType, const char *text, ...); +#endif + //---------------------------------------------------------------------------------- // Module Functions Definition - Matrix operations //---------------------------------------------------------------------------------- @@ -846,12 +862,12 @@ void rlglInit(void) #if defined(GRAPHICS_API_OPENGL_33) // Initialize extensions using GLEW glewExperimental = 1; // Needed for core profile - GLenum error = glewInit(); if (error != GLEW_OK) TraceLog(ERROR, "Failed to initialize GLEW - Error Code: %s\n", glewGetErrorString(error)); - + if (glewIsSupported("GL_VERSION_3_3")) + //if (ogl_LoadFunctions() != ogl_LOAD_FAILED) { TraceLog(INFO, "OpenGL 3.3 Core profile"); @@ -995,6 +1011,7 @@ void rlglInit(void) } // Init postpro system +// NOTE: Uses global variables screenWidth and screenHeight void rlglInitPostpro(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) @@ -1005,13 +1022,13 @@ void rlglInitPostpro(void) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, GetScreenWidth(), GetScreenHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); // Create the renderbuffer that will serve as the depth attachment for the framebuffer. glGenRenderbuffers(1, &fboDepthTexture); glBindRenderbuffer(GL_RENDERBUFFER, fboDepthTexture); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, GetScreenWidth(), GetScreenHeight()); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, screenWidth, screenHeight); // NOTE: We can also use a texture for depth buffer (GL_ARB_depth_texture/GL_OES_depth_texture extensions) // A renderbuffer is simpler than a texture and could offer better performance on embedded devices @@ -1062,8 +1079,8 @@ void rlglInitPostpro(void) quadData.vertexCount = 6; - float w = GetScreenWidth(); - float h = GetScreenHeight(); + float w = screenWidth; + float h = screenHeight; float quadPositions[6*3] = { w, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, h, 0.0, 0, h, 0.0, w, h, 0.0, w, 0.0, 0.0 }; float quadTexcoords[6*2] = { 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0 }; @@ -1141,7 +1158,20 @@ void rlglClose(void) { glDeleteFramebuffers(1, &fbo); - UnloadModel(postproQuad); + // Unload postpro quad model data +#if defined(GRAPHICS_API_OPENGL_11) + free(postproQuad.mesh.vertices); + free(postproQuad.mesh.texcoords); + free(postproQuad.mesh.normals); +#endif + + rlDeleteBuffers(postproQuad.mesh.vboId[0]); + rlDeleteBuffers(postproQuad.mesh.vboId[1]); + rlDeleteBuffers(postproQuad.mesh.vboId[2]); + + rlDeleteVertexArrays(postproQuad.mesh.vaoId); + + TraceLog(INFO, "Unloaded postpro quad data"); } free(draws); @@ -1312,7 +1342,7 @@ void rlglDrawPostpro(void) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, 0); - rlglDrawModel(postproQuad, (Vector3){0,0,0}, 0.0f, (Vector3){0,0,0}, (Vector3){1.0f, 1.0f, 1.0f}, WHITE, false); + rlglDrawModel(postproQuad, (Vector3){0,0,0}, 0.0f, (Vector3){0,0,0}, (Vector3){1.0f, 1.0f, 1.0f}, (Color){ 255, 255, 255, 255 }, false); #endif } @@ -1459,17 +1489,23 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r } // Initialize Graphics Device (OpenGL stuff) +// NOTE: Stores global variables screenWidth and screenHeight void rlglInitGraphics(int offsetX, int offsetY, int width, int height) { + // Save screen size data (global vars), required on postpro quad + // NOTE: Size represents render size, it could differ from screen size! + screenWidth = width; + screenHeight = height; + // NOTE: Required! viewport must be recalculated if screen resized! glViewport(offsetX/2, offsetY/2, width - offsetX, height - offsetY); // Set viewport width and height // NOTE: Don't confuse glViewport with the transformation matrix // NOTE: glViewport just defines the area of the context that you will actually draw to. - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, depth buffer is used for 3D glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color (black) //glClearDepth(1.0f); // Clear depth buffer (default) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers, depth buffer is used for 3D glEnable(GL_DEPTH_TEST); // Enables depth testing (required for 3D) glDepthFunc(GL_LEQUAL); // Type of depth testing to apply @@ -2216,6 +2252,8 @@ unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr) void UnloadShader(Shader shader) { rlDeleteShader(shader.id); + + TraceLog(INFO, "[SHDR ID %i] Unloaded shader program data", shader.id); } // Set custom shader to be used on batch draw @@ -2252,6 +2290,7 @@ void SetCustomShader(Shader shader) } // Set postprocessing shader +// NOTE: Uses global variables screenWidth and screenHeight void SetPostproShader(Shader shader) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) @@ -2265,8 +2304,8 @@ void SetPostproShader(Shader shader) Texture2D texture; texture.id = fboColorTexture; - texture.width = GetScreenWidth(); - texture.height = GetScreenHeight(); + texture.width = screenWidth; + texture.height = screenHeight; SetShaderMapDiffuse(&postproQuad.shader, texture); @@ -3072,12 +3111,9 @@ static pixel *GenNextMipmap(pixel *srcData, int srcWidth, int srcHeight) #endif #if defined(RLGL_STANDALONE) - -typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType; - // Output a trace log message // NOTE: Expected msgType: (0)Info, (1)Error, (2)Warning -void TraceLog(int msgType, const char *text, ...) +static void TraceLog(int msgType, const char *text, ...) { va_list args; va_start(args, text); diff --git a/src/rlgl.h b/src/rlgl.h index 723b3f17a..c85039035 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -36,6 +36,10 @@ #include "utils.h" // Required for function TraceLog() #endif +#if defined(RLGL_STANDALONE) + #define RAYMATH_STANDALONE +#endif + #include "raymath.h" // Required for data type Matrix and Matrix functions // Select desired OpenGL version @@ -89,9 +93,26 @@ typedef enum { RL_LINES, RL_TRIANGLES, RL_QUADS } DrawMode; typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; #ifdef RLGL_STANDALONE + #ifndef __cplusplus + // Boolean type + typedef enum { false, true } bool; + #endif + + // byte type + typedef unsigned char byte; + + // Color type, RGBA (32bit) + typedef struct Color { + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a; + } Color; + // Texture formats (support depends on OpenGL version) typedef enum { UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha) + UNCOMPRESSED_GRAY_ALPHA, UNCOMPRESSED_R5G6B5, // 16 bpp UNCOMPRESSED_R8G8B8, // 24 bpp UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) @@ -106,7 +127,8 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; COMPRESSED_ETC2_EAC_RGBA, // 8 bpp COMPRESSED_PVRT_RGB, // 4 bpp COMPRESSED_PVRT_RGBA, // 4 bpp - /*COMPRESSED_ASTC_RGBA_4x4*/ // 8 bpp + COMPRESSED_ASTC_4x4_RGBA, // 8 bpp + COMPRESSED_ASTC_8x8_RGBA // 2 bpp } TextureFormat; // VertexData type @@ -123,21 +145,36 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; // Shader type typedef struct Shader { - unsigned int id; // Shader program id + unsigned int id; // Shader program id + // TODO: This should be Texture2D objects + unsigned int texDiffuseId; // Diffuse texture id + unsigned int texNormalId; // Normal texture id + unsigned int texSpecularId; // Specular texture id + // Variable attributes - unsigned int vertexLoc; // Vertex attribute location point (vertex shader) - unsigned int texcoordLoc; // Texcoord attribute location point (vertex shader) - unsigned int normalLoc; // Normal attribute location point (vertex shader) - unsigned int colorLoc; // Color attibute location point (vertex shader) + int vertexLoc; // Vertex attribute location point (vertex shader) + int texcoordLoc; // Texcoord attribute location point (vertex shader) + int normalLoc; // Normal attribute location point (vertex shader) + int colorLoc; // Color attibute location point (vertex shader) // Uniforms - unsigned int projectionLoc; // Projection matrix uniform location point (vertex shader) - unsigned int modelviewLoc; // ModeView matrix uniform location point (vertex shader) - unsigned int textureLoc; // Texture uniform location point (fragment shader) - unsigned int tintColorLoc; // Color uniform location point (fragment shader) + int projectionLoc; // Projection matrix uniform location point (vertex shader) + int modelviewLoc; // ModeView matrix uniform location point (vertex shader) + int tintColorLoc; // Color uniform location point (fragment shader) + + int mapDiffuseLoc; // Diffuse map texture uniform location point (fragment shader) + int mapNormalLoc; // Normal map texture uniform location point (fragment shader) + int mapSpecularLoc; // Specular map texture uniform location point (fragment shader) } Shader; + // Texture2D type + typedef struct Texture2D { + unsigned int id; // Texture id + int width; + int height; + } Texture2D; + // 3d Model type typedef struct Model { VertexData mesh; @@ -145,13 +182,6 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; Texture2D texture; Shader shader; } Model; - - // Texture2D type - typedef struct Texture2D { - unsigned int id; // Texture id - int width; - int height; - } Texture2D; #endif #ifdef __cplusplus @@ -231,6 +261,29 @@ void PrintProjectionMatrix(void); // DEBUG: Print projection matrix void PrintModelviewMatrix(void); // DEBUG: Print modelview matrix #endif +#if defined(RLGL_STANDALONE) +//------------------------------------------------------------------------------------ +// Shaders System Functions (Module: rlgl) +// NOTE: This functions are useless when using OpenGL 1.1 +//------------------------------------------------------------------------------------ +Shader LoadShader(char *vsFileName, char *fsFileName); // Load a custom shader and bind default locations +unsigned int LoadShaderProgram(char *vShaderStr, char *fShaderStr); // Load a custom shader and return program id +void UnloadShader(Shader shader); // Unload a custom shader from memory +void SetPostproShader(Shader shader); // Set fullscreen postproduction shader +void SetCustomShader(Shader shader); // Set custom shader to be used in batch draw +void SetDefaultShader(void); // Set default shader to be used in batch draw +void SetModelShader(Model *model, Shader shader); // Link a shader to a model +bool IsPosproShaderEnabled(void); // Check if postprocessing shader is enabled + +int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location +void SetShaderValue(Shader shader, int uniformLoc, float *value, int size); // Set shader uniform value (float) +void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size); // Set shader uniform value (int) +void SetShaderMapDiffuse(Shader *shader, Texture2D texture); // Default diffuse shader map texture assignment +void SetShaderMapNormal(Shader *shader, const char *uniformName, Texture2D texture); // Normal map texture shader assignment +void SetShaderMapSpecular(Shader *shader, const char *uniformName, Texture2D texture); // Specular map texture shader assignment +void SetShaderMap(Shader *shader, int mapLocation, Texture2D texture, int textureUnit); // TODO: Generic shader map assignment +#endif + #ifdef __cplusplus } #endif