REVIEWED: Example shaders_normalmap, it crashes #5032

This commit is contained in:
Ray
2025-08-07 17:28:25 +02:00
parent 47d1cc200d
commit 54473e2c2b
2 changed files with 29 additions and 26 deletions

View File

@@ -19,12 +19,13 @@
********************************************************************************************/
#include <raylib.h>
#include <raymath.h>
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
//------------------------------------------------------------------------------------
@@ -34,14 +35,16 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(800, 450, "Normal Map");
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - normal map");
Camera camera = {0};
camera.position = (Vector3){0.0f, 2.0f, -4.0f};
camera.target = (Vector3){0.0f, 0.0f, 0.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
Camera camera = { 0 };
camera.position = (Vector3){ 0.0f, 2.0f, -4.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
@@ -52,12 +55,13 @@ int main(void)
// Get some required shader locations
shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(shader, "normalMap");
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
// shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
// This example uses just 1 point light.
Vector3 lightPosition = {0.0f, 1.0f, 0.0f};
// This example uses just 1 point light
Vector3 lightPosition = { 0.0f, 1.0f, 0.0f };
int lightPosLoc = GetShaderLocation(shader, "lightPos");
// Load a plane model that has proper normals and tangents
@@ -91,26 +95,25 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Move the light around on the X and Z axis using WASD keys
Vector3 direction = {0};
if (IsKeyDown(KEY_W)) direction = Vector3Add(direction, (Vector3){0.0f, 0.0f, 1.0f});
if (IsKeyDown(KEY_S)) direction = Vector3Add(direction, (Vector3){0.0f, 0.0f, -1.0f});
if (IsKeyDown(KEY_D)) direction = Vector3Add(direction, (Vector3){-1.0f, 0.0f, 0.0f});
if (IsKeyDown(KEY_A)) direction = Vector3Add(direction, (Vector3){1.0f, 0.0f, 0.0f});
Vector3 direction = { 0 };
if (IsKeyDown(KEY_W)) direction = Vector3Add(direction, (Vector3){ 0.0f, 0.0f, 1.0f });
if (IsKeyDown(KEY_S)) direction = Vector3Add(direction, (Vector3){ 0.0f, 0.0f, -1.0f });
if (IsKeyDown(KEY_D)) direction = Vector3Add(direction, (Vector3){ -1.0f, 0.0f, 0.0f });
if (IsKeyDown(KEY_A)) direction = Vector3Add(direction, (Vector3){ 1.0f, 0.0f, 0.0f });
direction = Vector3Normalize(direction);
lightPosition = Vector3Add(lightPosition, Vector3Scale(direction, GetFrameTime() * 3.0f));
lightPosition = Vector3Add(lightPosition, Vector3Scale(direction, GetFrameTime()*3.0f));
// Increase/Decrease the specular exponent(shininess)
if (IsKeyDown(KEY_UP)) specularExponent = Clamp(specularExponent + 40.0f * GetFrameTime(), 2.0f, 128.0f);
if (IsKeyDown(KEY_DOWN)) specularExponent = Clamp(specularExponent - 40.0f * GetFrameTime(), 2.0f, 128.0f);
if (IsKeyDown(KEY_UP)) specularExponent = Clamp(specularExponent + 40.0f*GetFrameTime(), 2.0f, 128.0f);
if (IsKeyDown(KEY_DOWN)) specularExponent = Clamp(specularExponent - 40.0f*GetFrameTime(), 2.0f, 128.0f);
// Toggle normal map on and off
if (IsKeyPressed(KEY_N)) useNormalMap = !useNormalMap;
// Spin plane model at a constant rate
plane.transform = MatrixRotateY(GetTime() * 0.5f);
plane.transform = MatrixRotateY(GetTime()*0.5f);
// Update shader values
float lightPos[3] = {lightPosition.x, lightPosition.y, lightPosition.z};
@@ -138,19 +141,19 @@ int main(void)
EndShaderMode();
//Draw sphere to show light position
// Draw sphere to show light position
DrawSphereWires(lightPosition, 0.2f, 8, 8, ORANGE);
EndMode3D();
Color textColor = (useNormalMap) ? DARKGREEN : RED;
const char *toggleStr = (useNormalMap) ? "On" : "Off";
DrawText(TextFormat("Use key [N] to toggle normal map: %s", toggleStr), 10, 30, 20, textColor);
DrawText(TextFormat("Use key [N] to toggle normal map: %s", toggleStr), 10, 30, 10, textColor);
int yOffset = 24;
DrawText("Use keys [W][A][S][D] to move the light", 10, 30 + yOffset * 1, 20, BLACK);
DrawText("Use keys [Up][Down] to change specular exponent", 10, 30 + yOffset * 2, 20, BLACK);
DrawText(TextFormat("Specular Exponent: %.2f", specularExponent), 10, 30 + yOffset * 3, 20, BLUE);
DrawText("Use keys [W][A][S][D] to move the light", 10, 30 + yOffset*1, 10, BLACK);
DrawText("Use keys [Up][Down] to change specular exponent", 10, 30 + yOffset*2, 10, BLACK);
DrawText(TextFormat("Specular Exponent: %.2f", specularExponent), 10, 30 + yOffset*3, 10, BLUE);
DrawFPS(10, 10);

View File

@@ -22,7 +22,7 @@
//------------------------------------------------------------------------------------
// Module functions declaration
//------------------------------------------------------------------------------------
void NormalizeKernel(float *kernel, int size);
static void NormalizeKernel(float *kernel, int size);
//------------------------------------------------------------------------------------
// Program main entry point