From f74791ed7bcb3585c576f40766bf4e22b0923a7e Mon Sep 17 00:00:00 2001 From: Joshua Reisenauer Date: Mon, 23 May 2016 02:12:22 -0700 Subject: [PATCH 1/5] better build system --- CMakeLists.txt | 32 ++++++++++++++++++++++++++++++++ src/windows_compile.bat | 2 -- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 src/windows_compile.bat diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..8689e9add --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required (VERSION 3.0) +project (raylib) +SET(PLATFORM_TO_USE "PLATFORM_DESKTOP" CACHE STRING "Platform to compile for") +SET_PROPERTY(CACHE PLATFORM_TO_USE PROPERTY STRINGS PLATFORM_DESKTOP PLATFORM_RPI PLATFORM_WEB) + +set(CMAKE_C_FLAGS "-O1 -Wall -std=gnu99 -fgnu89-inline") + +IF(${PLATFORM_TO_USE} MATCHES "PLATFORM_DESKTOP") + + add_definitions(-DPLATFORM_DESKTOP, -DGRAPHICS_API_OPENGL_33) + include_directories("." "src/" "external/openal_soft/include" "external/glew/include" "external/glfw3/include") + +ENDIF() + +IF(${PLATFORM_TO_USE} MATCHES "PLATFORM_RPI") + + add_definitions(-DPLATFORM_RPI, -GRAPHICS_API_OPENGL_ES2) + include_directories("." "/opt/vc/include" "/opt/vc/include/interface/vmcs_host/linux" "/opt/vc/include/interface/vcos/pthreads") + +ENDIF() + +IF(${PLATFORM_TO_USE} MATCHES "PLATFORM_WEB") + + add_definitions(-DPLATFORM_WEB, -GRAPHICS_API_OPENGL_ES2) + include_directories("." "src/" "external/openal_soft/include" "external/glew/include" "external/glfw3/include") + +ENDIF() + + +file(GLOB SOURCES "src/*.c") +add_library(raylib STATIC ${SOURCES}) +install(TARGETS raylib DESTINATION lib/) \ No newline at end of file diff --git a/src/windows_compile.bat b/src/windows_compile.bat deleted file mode 100644 index f1d0fb29d..000000000 --- a/src/windows_compile.bat +++ /dev/null @@ -1,2 +0,0 @@ -set PATH=C:\raylib\MinGW\bin;%PATH% -mingw32-make \ No newline at end of file From 48374c85dd9d05bcc86a277f64db018323ac5183 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 23 May 2016 11:25:04 +0200 Subject: [PATCH 2/5] Some tweaks It seems there are some problems with DrawLights() --- examples/shaders_standard_lighting.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/shaders_standard_lighting.c b/examples/shaders_standard_lighting.c index 7a9cc086d..2dde71935 100644 --- a/examples/shaders_standard_lighting.c +++ b/examples/shaders_standard_lighting.c @@ -17,7 +17,6 @@ ********************************************************************************************/ #include "raylib.h" -#include "raymath.h" int main() { @@ -35,27 +34,27 @@ int main() Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model - Texture2D texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture Material material = LoadStandardMaterial(); - material.texDiffuse = texDiffuse; + material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture material.colDiffuse = (Color){255, 255, 255, 255}; material.colAmbient = (Color){0, 0, 10, 255}; material.colSpecular = (Color){255, 255, 255, 255}; material.glossiness = 50.0f; - dwarf.material = material; // Apply material to model + dwarf.material = material; // Apply material to model + Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255}); spotLight->target = (Vector3){0.0f, 0.0f, 0.0f}; spotLight->intensity = 2.0f; spotLight->diffuse = (Color){255, 100, 100, 255}; spotLight->coneAngle = 60.0f; - + Light dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255}); dirLight->target = (Vector3){1.0f, -2.0f, -2.0f}; dirLight->intensity = 2.0f; dirLight->diffuse = (Color){100, 255, 100, 255}; - + Light pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255}); pointLight->intensity = 2.0f; pointLight->diffuse = (Color){100, 100, 255, 255}; From d53b6f4381485785f7ece3c8004449c20b60ae03 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 25 May 2016 16:19:57 +0200 Subject: [PATCH 3/5] Renamed shader variables (more generic names) Now shader maps use a generic naming convention for any kind of texture maps (not only diffuse, normal or specular). Useful for custom shaders. --- src/rlgl.c | 26 ++++++++++++++------------ src/rlgl.h | 8 ++++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/rlgl.c b/src/rlgl.c index 85c0cae29..d319119f4 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1792,23 +1792,23 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform) // Set shader textures (diffuse, normal, specular) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, material.texDiffuse.id); - glUniform1i(material.shader.mapDiffuseLoc, 0); // Texture fits in active texture unit 0 + glUniform1i(material.shader.mapTexture0Loc, 0); // Diffuse texture fits in active texture unit 0 - if ((material.texNormal.id != 0) && (material.shader.mapNormalLoc != -1)) + if ((material.texNormal.id != 0) && (material.shader.mapTexture1Loc != -1)) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, material.texNormal.id); - glUniform1i(material.shader.mapNormalLoc, 1); // Texture fits in active texture unit 1 + glUniform1i(material.shader.mapTexture1Loc, 1); // Normal texture fits in active texture unit 1 // TODO: Upload to shader normalDepth //glUniform1f(???, material.normalDepth); } - if ((material.texSpecular.id != 0) && (material.shader.mapSpecularLoc != -1)) + if ((material.texSpecular.id != 0) && (material.shader.mapTexture2Loc != -1)) { glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, material.texSpecular.id); - glUniform1i(material.shader.mapSpecularLoc, 2); // Texture fits in active texture unit 2 + glUniform1i(material.shader.mapTexture2Loc, 2); // Specular texture fits in active texture unit 2 } if (vaoSupported) @@ -2569,19 +2569,19 @@ static void LoadDefaultShaderLocations(Shader *shader) // Get handles to GLSL input attibute locations shader->vertexLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_POSITION_NAME); shader->texcoordLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TEXCOORD_NAME); - shader->normalLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_NORMAL_NAME); - shader->colorLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_COLOR_NAME); - shader->tangentLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TANGENT_NAME); shader->texcoord2Loc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TEXCOORD2_NAME); + shader->normalLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_NORMAL_NAME); + shader->tangentLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_TANGENT_NAME); + shader->colorLoc = glGetAttribLocation(shader->id, DEFAULT_ATTRIB_COLOR_NAME); // Get handles to GLSL uniform locations (vertex shader) shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix"); // Get handles to GLSL uniform locations (fragment shader) shader->tintColorLoc = glGetUniformLocation(shader->id, "colDiffuse"); - shader->mapDiffuseLoc = glGetUniformLocation(shader->id, "texture0"); - shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1"); - shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2"); + shader->mapTexture0Loc = glGetUniformLocation(shader->id, "texture0"); + shader->mapTexture1Loc = glGetUniformLocation(shader->id, "texture1"); + shader->mapTexture2Loc = glGetUniformLocation(shader->id, "texture2"); } // Unload default shader @@ -2864,8 +2864,10 @@ static void DrawDefaultBuffers(void) Matrix matMVP = MatrixMultiply(modelview, projection); glUniformMatrix4fv(currentShader.mvpLoc, 1, false, MatrixToFloat(matMVP)); - glUniform1i(currentShader.mapDiffuseLoc, 0); glUniform4f(currentShader.tintColorLoc, 1.0f, 1.0f, 1.0f, 1.0f); + glUniform1i(currentShader.mapTexture0Loc, 0); + + // NOTE: Additional map textures not considered for default buffers drawing } // Draw lines buffers diff --git a/src/rlgl.h b/src/rlgl.h index 0765a8a7a..fa35dbc63 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -171,10 +171,10 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion; int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) int tintColorLoc; // Color uniform location point (fragment shader) - // Texture map locations - 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) + // Texture map locations (generic for any kind of map) + int mapTexture0Loc; // Map texture uniform location point (default-texture-unit = 0) + int mapTexture1Loc; // Map texture uniform location point (default-texture-unit = 1) + int mapTexture2Loc; // Map texture uniform location point (default-texture-unit = 2) } Shader; // Texture2D type From 3d6696f6c981a7a8f523d631926380eced475733 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 25 May 2016 16:21:13 +0200 Subject: [PATCH 4/5] Renamed shader variables (more generic names) --- src/raylib.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 9cd02fd8d..d0231be2b 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -369,6 +369,7 @@ typedef struct BoundingBox { // Vertex data definning a mesh typedef struct Mesh { int vertexCount; // number of vertices stored in arrays + int triangleCount; // number of triangles stored (indexed or not) float *vertices; // vertex position (XYZ - 3 components per vertex) (shader-location = 0) float *texcoords; // vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) float *texcoords2; // vertex second texture coordinates (useful for lightmaps) (shader-location = 5) @@ -376,8 +377,7 @@ typedef struct Mesh { float *tangents; // vertex tangents (XYZ - 3 components per vertex) (shader-location = 4) unsigned char *colors; // vertex colors (RGBA - 4 components per vertex) (shader-location = 3) unsigned short *indices; // vertex indices (in case vertex data comes indexed) - int triangleCount; // number of triangles stored (indexed or not) - + BoundingBox bounds; // mesh limits defined by min and max points unsigned int vaoId; // OpenGL Vertex Array Object id @@ -389,30 +389,30 @@ typedef struct Shader { unsigned int id; // Shader program id // Vertex attributes locations (default locations) - int vertexLoc; // Vertex attribute location point (default-location = 0) - int texcoordLoc; // Texcoord attribute location point (default-location = 1) - int normalLoc; // Normal attribute location point (default-location = 2) - int colorLoc; // Color attibute location point (default-location = 3) - int tangentLoc; // Tangent attribute location point (default-location = 4) + int vertexLoc; // Vertex attribute location point (default-location = 0) + int texcoordLoc; // Texcoord attribute location point (default-location = 1) int texcoord2Loc; // Texcoord2 attribute location point (default-location = 5) + int normalLoc; // Normal attribute location point (default-location = 2) + int tangentLoc; // Tangent attribute location point (default-location = 4) + int colorLoc; // Color attibute location point (default-location = 3) // Uniform locations int mvpLoc; // ModelView-Projection matrix uniform location point (vertex shader) int tintColorLoc; // Diffuse color uniform location point (fragment shader) - // Texture map locations - 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) + // Texture map locations (generic for any kind of map) + int mapTexture0Loc; // Map texture uniform location point (default-texture-unit = 0) + int mapTexture1Loc; // Map texture uniform location point (default-texture-unit = 1) + int mapTexture2Loc; // Map texture uniform location point (default-texture-unit = 2) } Shader; // Material type typedef struct Material { - Shader shader; // Standard shader (supports 3 map types: diffuse, normal, specular) + Shader shader; // Standard shader (supports 3 map textures) - Texture2D texDiffuse; // Diffuse texture - Texture2D texNormal; // Normal texture - Texture2D texSpecular; // Specular texture + Texture2D texDiffuse; // Diffuse texture (binded to shader mapTexture0Loc) + Texture2D texNormal; // Normal texture (binded to shader mapTexture1Loc) + Texture2D texSpecular; // Specular texture (binded to shader mapTexture2Loc) Color colDiffuse; // Diffuse color Color colAmbient; // Ambient color @@ -439,8 +439,8 @@ typedef struct LightData { Vector3 target; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target) float attenuation; // Lost of light intensity with distance (world distance) - Color diffuse; // Use Vector3 diffuse - float intensity; + Color diffuse; // Light color + float intensity; // Light intensity level float coneAngle; // Spot light max angle } LightData, *Light; From ea5b00528b0cb1e5cc1e7169a195b75915c8607a Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 29 May 2016 11:49:13 +0200 Subject: [PATCH 5/5] Improved render to texture Support render texture size different than screen size --- examples/shaders_custom_uniform.c | 2 ++ src/core.c | 38 ++++++++++++++++++++++++++----- src/rlgl.c | 14 ++++++++++++ src/rlgl.h | 1 + src/textures.c | 7 +++--- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/examples/shaders_custom_uniform.c b/examples/shaders_custom_uniform.c index 32dd7ff1c..740771439 100644 --- a/examples/shaders_custom_uniform.c +++ b/examples/shaders_custom_uniform.c @@ -89,6 +89,8 @@ int main() DrawGrid(10, 1.0f); // Draw a grid End3dMode(); + + DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) diff --git a/src/core.c b/src/core.c index a94ad48d0..08f9a7e23 100644 --- a/src/core.c +++ b/src/core.c @@ -562,9 +562,8 @@ void Begin2dMode(Camera2D camera) Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f); Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD); Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f); - Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f); - + Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation); rlMultMatrixf(MatrixToFloat(matTransform)); @@ -627,11 +626,24 @@ void BeginTextureMode(RenderTexture2D target) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - rlEnableRenderTexture(target.id); - - rlClearScreenBuffers(); // Clear render texture buffers + rlEnableRenderTexture(target.id); // Enable render target + rlClearScreenBuffers(); // Clear render texture buffers + + // Set viewport to framebuffer size + rlViewport(0, 0, target.texture.width, target.texture.height); + + rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix + rlLoadIdentity(); // Reset current matrix (PROJECTION) + + // Set orthographic projection to current framebuffer size + // NOTE: Configured top-left corner as (0, 0) + rlOrtho(0, target.texture.width, target.texture.height, 0, 0.0f, 1.0f); + + rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix rlLoadIdentity(); // Reset current matrix (MODELVIEW) + + //rlScalef(0.0f, -1.0f, 0.0f); // Flip Y-drawing (?) } // Ends drawing to render texture @@ -639,7 +651,21 @@ void EndTextureMode(void) { rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2) - rlDisableRenderTexture(); + rlDisableRenderTexture(); // Disable render target + + // Set viewport to default framebuffer size (screen size) + // TODO: consider possible viewport offsets + rlViewport(0, 0, GetScreenWidth(), GetScreenHeight()); + + rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix + rlLoadIdentity(); // Reset current matrix (PROJECTION) + + // Set orthographic projection to current framebuffer size + // NOTE: Configured top-left corner as (0, 0) + rlOrtho(0, GetScreenWidth(), GetScreenHeight(), 0, 0.0f, 1.0f); + + rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix + rlLoadIdentity(); // Reset current matrix (MODELVIEW) } // Set target FPS for the game diff --git a/src/rlgl.c b/src/rlgl.c index d319119f4..cc4c4c2f5 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -404,6 +404,12 @@ void rlOrtho(double left, double right, double bottom, double top, double near, #endif +// Set the viewport area (trasnformation from normalized device coordinates to window coordinates) +void rlViewport(int x, int y, int width, int height) +{ + glViewport(x, y, width, height); +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Vertex level operations //---------------------------------------------------------------------------------- @@ -725,17 +731,25 @@ void rlDisableTexture(void) #endif } +// Enable rendering to texture (fbo) void rlEnableRenderTexture(unsigned int id) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, id); + + //glDisable(GL_CULL_FACE); // Allow double side drawing for texture flipping + //glCullFace(GL_FRONT); #endif } +// Disable rendering to texture void rlDisableRenderTexture(void) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glBindFramebuffer(GL_FRAMEBUFFER, 0); + + //glEnable(GL_CULL_FACE); + //glCullFace(GL_BACK); #endif } diff --git a/src/rlgl.h b/src/rlgl.h index fa35dbc63..a3ba6cd5e 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -247,6 +247,7 @@ void rlScalef(float x, float y, float z); // Multiply the current matrix b void rlMultMatrixf(float *mat); // Multiply the current matrix by another matrix void rlFrustum(double left, double right, double bottom, double top, double near, double far); void rlOrtho(double left, double right, double bottom, double top, double near, double far); +void rlViewport(int x, int y, int width, int height); // Set the viewport area //------------------------------------------------------------------------------------ // Functions Declaration - Vertex level operations diff --git a/src/textures.c b/src/textures.c index 79047ab7e..439311f6b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1385,10 +1385,6 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint) { Rectangle destRec = { (int)position.x, (int)position.y, abs(sourceRec.width), abs(sourceRec.height) }; - - if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; - if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; - Vector2 origin = { 0, 0 }; DrawTexturePro(texture, sourceRec, destRec, origin, 0.0f, tint); @@ -1398,6 +1394,9 @@ void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Co // NOTE: origin is relative to destination rectangle size void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint) { + if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; + if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; + rlEnableTexture(texture.id); rlPushMatrix();