19
19
********************************************************************************************/
20
20
21
21
#include <raylib.h>
22
+
22
23
#include <raymath.h>
23
24
24
25
#if defined(PLATFORM_DESKTOP )
25
- #define GLSL_VERSION 330
26
- #else // PLATFORM_ANDROID, PLATFORM_WEB
27
- #define GLSL_VERSION 100
26
+ #define GLSL_VERSION 330
27
+ #else // PLATFORM_ANDROID, PLATFORM_WEB
28
+ #define GLSL_VERSION 100
28
29
#endif
29
30
30
31
//------------------------------------------------------------------------------------
@@ -34,14 +35,16 @@ int main(void)
34
35
{
35
36
// Initialization
36
37
//--------------------------------------------------------------------------------------
38
+ const int screenWidth = 800 ;
39
+ const int screenHeight = 450 ;
37
40
38
41
SetConfigFlags (FLAG_MSAA_4X_HINT );
39
- InitWindow (800 , 450 , "Normal Map " );
42
+ InitWindow (screenWidth , screenHeight , "raylib [shaders] example - normal map " );
40
43
41
- Camera camera = {0 };
42
- camera .position = (Vector3 ){0.0f , 2.0f , -4.0f };
43
- camera .target = (Vector3 ){0.0f , 0.0f , 0.0f };
44
- camera .up = (Vector3 ){0.0f , 1.0f , 0.0f };
44
+ Camera camera = { 0 };
45
+ camera .position = (Vector3 ){ 0.0f , 2.0f , -4.0f };
46
+ camera .target = (Vector3 ){ 0.0f , 0.0f , 0.0f };
47
+ camera .up = (Vector3 ){ 0.0f , 1.0f , 0.0f };
45
48
camera .fovy = 45.0f ;
46
49
camera .projection = CAMERA_PERSPECTIVE ;
47
50
@@ -52,12 +55,13 @@ int main(void)
52
55
// Get some required shader locations
53
56
shader .locs [SHADER_LOC_MAP_NORMAL ] = GetShaderLocation (shader , "normalMap" );
54
57
shader .locs [SHADER_LOC_VECTOR_VIEW ] = GetShaderLocation (shader , "viewPos" );
58
+
55
59
// NOTE: "matModel" location name is automatically assigned on shader loading,
56
60
// no need to get the location again if using that uniform name
57
61
// shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
58
62
59
- // This example uses just 1 point light.
60
- Vector3 lightPosition = {0.0f , 1.0f , 0.0f };
63
+ // This example uses just 1 point light
64
+ Vector3 lightPosition = { 0.0f , 1.0f , 0.0f };
61
65
int lightPosLoc = GetShaderLocation (shader , "lightPos" );
62
66
63
67
// Load a plane model that has proper normals and tangents
@@ -91,26 +95,25 @@ int main(void)
91
95
{
92
96
// Update
93
97
//----------------------------------------------------------------------------------
94
-
95
98
// Move the light around on the X and Z axis using WASD keys
96
- Vector3 direction = {0 };
97
- if (IsKeyDown (KEY_W )) direction = Vector3Add (direction , (Vector3 ){0.0f , 0.0f , 1.0f });
98
- if (IsKeyDown (KEY_S )) direction = Vector3Add (direction , (Vector3 ){0.0f , 0.0f , -1.0f });
99
- if (IsKeyDown (KEY_D )) direction = Vector3Add (direction , (Vector3 ){-1.0f , 0.0f , 0.0f });
100
- if (IsKeyDown (KEY_A )) direction = Vector3Add (direction , (Vector3 ){1.0f , 0.0f , 0.0f });
99
+ Vector3 direction = { 0 };
100
+ if (IsKeyDown (KEY_W )) direction = Vector3Add (direction , (Vector3 ){ 0.0f , 0.0f , 1.0f });
101
+ if (IsKeyDown (KEY_S )) direction = Vector3Add (direction , (Vector3 ){ 0.0f , 0.0f , -1.0f });
102
+ if (IsKeyDown (KEY_D )) direction = Vector3Add (direction , (Vector3 ){ -1.0f , 0.0f , 0.0f });
103
+ if (IsKeyDown (KEY_A )) direction = Vector3Add (direction , (Vector3 ){ 1.0f , 0.0f , 0.0f });
101
104
102
105
direction = Vector3Normalize (direction );
103
- lightPosition = Vector3Add (lightPosition , Vector3Scale (direction , GetFrameTime () * 3.0f ));
106
+ lightPosition = Vector3Add (lightPosition , Vector3Scale (direction , GetFrameTime ()* 3.0f ));
104
107
105
108
// Increase/Decrease the specular exponent(shininess)
106
- if (IsKeyDown (KEY_UP )) specularExponent = Clamp (specularExponent + 40.0f * GetFrameTime (), 2.0f , 128.0f );
107
- if (IsKeyDown (KEY_DOWN )) specularExponent = Clamp (specularExponent - 40.0f * GetFrameTime (), 2.0f , 128.0f );
109
+ if (IsKeyDown (KEY_UP )) specularExponent = Clamp (specularExponent + 40.0f * GetFrameTime (), 2.0f , 128.0f );
110
+ if (IsKeyDown (KEY_DOWN )) specularExponent = Clamp (specularExponent - 40.0f * GetFrameTime (), 2.0f , 128.0f );
108
111
109
112
// Toggle normal map on and off
110
113
if (IsKeyPressed (KEY_N )) useNormalMap = !useNormalMap ;
111
114
112
115
// Spin plane model at a constant rate
113
- plane .transform = MatrixRotateY (GetTime () * 0.5f );
116
+ plane .transform = MatrixRotateY (GetTime ()* 0.5f );
114
117
115
118
// Update shader values
116
119
float lightPos [3 ] = {lightPosition .x , lightPosition .y , lightPosition .z };
@@ -138,19 +141,19 @@ int main(void)
138
141
139
142
EndShaderMode ();
140
143
141
- //Draw sphere to show light position
144
+ // Draw sphere to show light position
142
145
DrawSphereWires (lightPosition , 0.2f , 8 , 8 , ORANGE );
143
146
144
147
EndMode3D ();
145
148
146
149
Color textColor = (useNormalMap ) ? DARKGREEN : RED ;
147
150
const char * toggleStr = (useNormalMap ) ? "On" : "Off" ;
148
- DrawText (TextFormat ("Use key [N] to toggle normal map: %s" , toggleStr ), 10 , 30 , 20 , textColor );
151
+ DrawText (TextFormat ("Use key [N] to toggle normal map: %s" , toggleStr ), 10 , 30 , 10 , textColor );
149
152
150
153
int yOffset = 24 ;
151
- DrawText ("Use keys [W][A][S][D] to move the light" , 10 , 30 + yOffset * 1 , 20 , BLACK );
152
- DrawText ("Use keys [Up][Down] to change specular exponent" , 10 , 30 + yOffset * 2 , 20 , BLACK );
153
- DrawText (TextFormat ("Specular Exponent: %.2f" , specularExponent ), 10 , 30 + yOffset * 3 , 20 , BLUE );
154
+ DrawText ("Use keys [W][A][S][D] to move the light" , 10 , 30 + yOffset * 1 , 10 , BLACK );
155
+ DrawText ("Use keys [Up][Down] to change specular exponent" , 10 , 30 + yOffset * 2 , 10 , BLACK );
156
+ DrawText (TextFormat ("Specular Exponent: %.2f" , specularExponent ), 10 , 30 + yOffset * 3 , 10 , BLUE );
154
157
155
158
DrawFPS (10 , 10 );
156
159
0 commit comments