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