Browse Source

Review PR and comments

Ray 5 years ago
parent
commit
bdcb16e7bb
4 changed files with 54 additions and 67 deletions
  1. 3 3
      examples/models/models_material_pbr.c
  2. 47 61
      examples/shaders/shaders_simple.c
  3. 2 1
      src/models.c
  4. 2 2
      src/rlgl.h

+ 3 - 3
examples/models/models_material_pbr.c

@@ -100,9 +100,8 @@ int main(void)
     // De-Initialization
     // De-Initialization
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
 
 
-    // user must unload shaders and textures as they could be in use
-    // by other models....
-    UnloadShader(model.materials[0].shader);
+    // Shaders and textures must be unloaded by user, 
+    // they could be in use by other models
     UnloadTexture(model.materials[0].maps[MAP_ALBEDO].texture);
     UnloadTexture(model.materials[0].maps[MAP_ALBEDO].texture);
     UnloadTexture(model.materials[0].maps[MAP_NORMAL].texture);
     UnloadTexture(model.materials[0].maps[MAP_NORMAL].texture);
     UnloadTexture(model.materials[0].maps[MAP_METALNESS].texture);
     UnloadTexture(model.materials[0].maps[MAP_METALNESS].texture);
@@ -111,6 +110,7 @@ int main(void)
     UnloadTexture(model.materials[0].maps[MAP_IRRADIANCE].texture);
     UnloadTexture(model.materials[0].maps[MAP_IRRADIANCE].texture);
     UnloadTexture(model.materials[0].maps[MAP_PREFILTER].texture);
     UnloadTexture(model.materials[0].maps[MAP_PREFILTER].texture);
     UnloadTexture(model.materials[0].maps[MAP_BRDF].texture);
     UnloadTexture(model.materials[0].maps[MAP_BRDF].texture);
+    UnloadShader(model.materials[0].shader);
 
 
     UnloadModel(model);         // Unload model
     UnloadModel(model);         // Unload model
 
 

+ 47 - 61
examples/shaders/shaders_simple.c

@@ -1,6 +1,6 @@
 /*******************************************************************************************
 /*******************************************************************************************
 *
 *
-*   raylib [shaders] example - demonstrates how you can use your own simple shaders in raylib
+*   raylib [shaders] example - Simple shader
 *
 *
 *   This example has been created using raylib 2.5 (www.raylib.com)
 *   This example has been created using raylib 2.5 (www.raylib.com)
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
@@ -11,28 +11,23 @@
 *
 *
 ********************************************************************************************
 ********************************************************************************************
 *
 *
-* after a model is loaded it has a default material, this material can be modified in place
-* rather than creating one from scratch...
-* While all of the MAPs have particular names, they can be used for any purpose
-* Three of the MAP are applied as cubic maps (see below)
+*   After a model is loaded it has a default material, this material can be
+*   modified in place rather than creating one from scratch...
+*   While all of the maps have particular names, they can be used for any purpose
+*   except for three maps that are applied as cubic maps (see below)
 *
 *
 ********************************************************************************************/
 ********************************************************************************************/
 
 
-
-#include <stddef.h>
-
 #include "raylib.h"
 #include "raylib.h"
 #include "raymath.h"
 #include "raymath.h"
 
 
-
-#define screenWidth 1280
-#define screenHeight 720
-
-
 int main(void)
 int main(void)
 {
 {
     // Initialization
     // Initialization
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
+    const int screenWidth = 800;
+    const int screenHeight = 450;
+
     InitWindow(screenWidth, screenHeight, "raylib - simple shader");
     InitWindow(screenWidth, screenHeight, "raylib - simple shader");
 
 
     // Define the camera to look into our 3d world
     // Define the camera to look into our 3d world
@@ -43,71 +38,64 @@ int main(void)
     camera.fovy = 45.0f;
     camera.fovy = 45.0f;
     camera.type = CAMERA_PERSPECTIVE;
     camera.type = CAMERA_PERSPECTIVE;
 
 
-    // three models to show the shader on
+    // Define our three models to show the shader on
     Mesh torus = GenMeshTorus(.3, 1, 16, 32);
     Mesh torus = GenMeshTorus(.3, 1, 16, 32);
     Model model1 = LoadModelFromMesh(torus);
     Model model1 = LoadModelFromMesh(torus);
 
 
     Mesh cube = GenMeshCube(.8,.8,.8);
     Mesh cube = GenMeshCube(.8,.8,.8);
     Model model2 = LoadModelFromMesh(cube);
     Model model2 = LoadModelFromMesh(cube);
 
 
-    // this one un shaded just so we can see the gaps in the other two
+    // Generate model to be shaded just to see the gaps in the other two
     Mesh sphere = GenMeshSphere(1, 16, 16);
     Mesh sphere = GenMeshSphere(1, 16, 16);
     Model model3 = LoadModelFromMesh(sphere);
     Model model3 = LoadModelFromMesh(sphere);
 
 
-    // load the shader
-    Shader shader = LoadShader("resources/shaders/glsl330/mask.vs",
-                                "resources/shaders/glsl330/mask.fs");
-
-    // apply the diffuse texture (colour map)
-    Texture tex = LoadTexture("resources/plasma.png");
-    model1.materials[0].maps[MAP_DIFFUSE].texture = tex;
-    model2.materials[0].maps[MAP_DIFFUSE].texture = tex;
-
-    // using MAP_EMISSION as a spare slot to use for 2nd texture
-    // dont use MAP_IRRADIANCE, MAP_PREFILTER, or  MAP_CUBEMAP
-    // as they are bound as cube maps (which don't see to work at all on my machine!)
-    Texture maskTx = LoadTexture("resources/mask.png");
-    model1.materials[0].maps[MAP_EMISSION].texture = maskTx;
-    model2.materials[0].maps[MAP_EMISSION].texture = maskTx;
+    // Load the shader
+    Shader shader = LoadShader("resources/shaders/glsl330/mask.vs", "resources/shaders/glsl330/mask.fs");
+
+    // Load and apply the diffuse texture (colour map)
+    Texture texDiffuse = LoadTexture("resources/plasma.png");
+    model1.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
+    model2.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
+
+    // Using MAP_EMISSION as a spare slot to use for 2nd texture
+    // NOTE: Don't use MAP_IRRADIANCE, MAP_PREFILTER or  MAP_CUBEMAP
+    // as they are bound as cube maps
+    Texture texMask = LoadTexture("resources/mask.png");
+    model1.materials[0].maps[MAP_EMISSION].texture = texMask;
+    model2.materials[0].maps[MAP_EMISSION].texture = texMask;
     shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
     shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
 
 
-    // frame is incremented each frame to animate the shader
-    int shaderFrame = GetShaderLocation(shader, "frame");
+    // Frame is incremented each frame to animate the shader
+    int shaderFrame = GetShaderLocation(shader, "framesCounter");
 
 
-    // apply the shader to the two models
+    // Apply the shader to the two models
     model1.materials[0].shader = shader;
     model1.materials[0].shader = shader;
     model2.materials[0].shader = shader;
     model2.materials[0].shader = shader;
 
 
-
-    // frame counter
-    int frame = 0;
-
-    // model rotation
-    Vector3 ang = { 0 };
+    int framesCounter = 0;
+    Vector3 rotation = { 0 };       // Model rotation angles
 
 
     SetTargetFPS(60);               // Set  to run at 60 frames-per-second
     SetTargetFPS(60);               // Set  to run at 60 frames-per-second
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
 
 
-
-
     // Main game loop
     // Main game loop
     while (!WindowShouldClose())    // Detect window close button or ESC key
     while (!WindowShouldClose())    // Detect window close button or ESC key
     {
     {
         // Update
         // Update
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
+        framesCounter++;
+        rotation.x += 0.01f;
+        rotation.y += 0.005f;
+        rotation.z -= 0.0025f;
 
 
-        frame ++;
-        ang.x += 0.01;
-        ang.y += 0.005;
-        ang.z -= 0.0025;
-
-        // animate the shader
-        SetShaderValue(shader, shaderFrame, &frame, UNIFORM_INT);
+        // Send frames counter to shader for animation
+        SetShaderValue(shader, shaderFrame, &framesCounter, UNIFORM_INT);
 
 
-        // rotate one of the models
-        model1.transform = MatrixRotateXYZ(ang);
+        // Rotate one of the models
+        model1.transform = MatrixRotateXYZ(rotation);
 
 
         UpdateCamera(&camera);
         UpdateCamera(&camera);
+        //----------------------------------------------------------------------------------
 
 
         // Draw
         // Draw
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
@@ -124,11 +112,10 @@ int main(void)
 
 
             EndMode3D();
             EndMode3D();
 
 
-            DrawFPS(10, 10);
+            DrawRectangle(16, 698, MeasureText(FormatText("Frame: %i", framesCounter), 20) + 8, 42, BLUE);
+            DrawText(FormatText("Frame: %i", framesCounter), 20, 700, 20, WHITE);
 
 
-            int l = MeasureText(FormatText("Frame %i", frame), 20);
-            DrawRectangle(16, 698, l+8, 42, BLUE);
-            DrawText(FormatText("Frame %i", frame), 20, 700, 20, WHITE);
+            DrawFPS(10, 10);
 
 
         EndDrawing();
         EndDrawing();
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
@@ -136,18 +123,17 @@ int main(void)
 
 
     // De-Initialization
     // De-Initialization
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
-
     UnloadModel(model1);
     UnloadModel(model1);
     UnloadModel(model2);
     UnloadModel(model2);
     UnloadModel(model3);
     UnloadModel(model3);
-    UnloadTexture(tex);
-    UnloadTexture(maskTx);
-    UnloadShader(shader);
+    
+    UnloadTexture(texDiffuse);  // Unload default diffuse texture
+    UnloadTexture(texMask);     // Unload texture mask
+    
+    UnloadShader(shader);       // Unload shader
 
 
-    CloseWindow();        // Close window and OpenGL context
+    CloseWindow();              // Close window and OpenGL context
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
 
 
     return 0;
     return 0;
 }
 }
-
-

+ 2 - 1
src/models.c

@@ -2921,7 +2921,8 @@ static Model LoadOBJ(const char *fileName)
         tinyobj_attrib_free(&attrib);
         tinyobj_attrib_free(&attrib);
         tinyobj_shapes_free(meshes, meshCount);
         tinyobj_shapes_free(meshes, meshCount);
         tinyobj_materials_free(materials, materialCount);
         tinyobj_materials_free(materials, materialCount);
-        RL_FREE(data); // oh ray how did you miss this...! :-p
+
+        RL_FREE(data);
     }
     }
 
 
     // NOTE: At this point we have all model data loaded
     // NOTE: At this point we have all model data loaded

+ 2 - 2
src/rlgl.h

@@ -2967,8 +2967,8 @@ char *LoadText(const char *fileName)
 Shader LoadShader(const char *vsFileName, const char *fsFileName)
 Shader LoadShader(const char *vsFileName, const char *fsFileName)
 {
 {
     Shader shader = { 0 };
     Shader shader = { 0 };
-    // double allocation causing leak (allocation done in LoadShaderCode)
-    //shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int));
+    
+    // NOTE: Shader.locs is allocated by LoadShaderCode()
 
 
     char *vShaderStr = NULL;
     char *vShaderStr = NULL;
     char *fShaderStr = NULL;
     char *fShaderStr = NULL;