Review PR and comments

This commit is contained in:
Ray 2019-09-22 21:47:19 +02:00
parent a679b0ccc0
commit bdcb16e7bb
4 changed files with 52 additions and 65 deletions

View file

@ -100,9 +100,8 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// user must unload shaders and textures as they could be in use // Shaders and textures must be unloaded by user,
// by other models.... // they could be in use by other models
UnloadShader(model.materials[0].shader);
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

View file

@ -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 * After a model is loaded it has a default material, this material can be
* rather than creating one from scratch... * modified in place rather than creating one from scratch...
* While all of the MAPs have particular names, they can be used for any purpose * 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) * 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 // Load the shader
Shader shader = LoadShader("resources/shaders/glsl330/mask.vs", Shader shader = LoadShader("resources/shaders/glsl330/mask.vs", "resources/shaders/glsl330/mask.fs");
"resources/shaders/glsl330/mask.fs");
// apply the diffuse texture (colour map) // Load and apply the diffuse texture (colour map)
Texture tex = LoadTexture("resources/plasma.png"); Texture texDiffuse = LoadTexture("resources/plasma.png");
model1.materials[0].maps[MAP_DIFFUSE].texture = tex; model1.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
model2.materials[0].maps[MAP_DIFFUSE].texture = tex; model2.materials[0].maps[MAP_DIFFUSE].texture = texDiffuse;
// using MAP_EMISSION as a spare slot to use for 2nd texture // Using MAP_EMISSION as a spare slot to use for 2nd texture
// dont use MAP_IRRADIANCE, MAP_PREFILTER, or MAP_CUBEMAP // NOTE: Don't 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!) // as they are bound as cube maps
Texture maskTx = LoadTexture("resources/mask.png"); Texture texMask = LoadTexture("resources/mask.png");
model1.materials[0].maps[MAP_EMISSION].texture = maskTx; model1.materials[0].maps[MAP_EMISSION].texture = texMask;
model2.materials[0].maps[MAP_EMISSION].texture = maskTx; 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 // Frame is incremented each frame to animate the shader
int shaderFrame = GetShaderLocation(shader, "frame"); 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;
int framesCounter = 0;
// frame counter Vector3 rotation = { 0 }; // Model rotation angles
int frame = 0;
// model rotation
Vector3 ang = { 0 };
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 ++; // Send frames counter to shader for animation
ang.x += 0.01; SetShaderValue(shader, shaderFrame, &framesCounter, UNIFORM_INT);
ang.y += 0.005;
ang.z -= 0.0025;
// animate the shader // Rotate one of the models
SetShaderValue(shader, shaderFrame, &frame, UNIFORM_INT); model1.transform = MatrixRotateXYZ(rotation);
// rotate one of the models
model1.transform = MatrixRotateXYZ(ang);
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); DrawFPS(10, 10);
DrawRectangle(16, 698, l+8, 42, BLUE);
DrawText(FormatText("Frame %i", frame), 20, 700, 20, WHITE);
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);
CloseWindow(); // Close window and OpenGL context UnloadTexture(texDiffuse); // Unload default diffuse texture
UnloadTexture(texMask); // Unload texture mask
UnloadShader(shader); // Unload shader
CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

@ -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

View file

@ -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;