1 | /******************************************************************************************* |
2 | * |
3 | * raylib [shaders] example - basic lighting |
4 | * |
5 | * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, |
6 | * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. |
7 | * |
8 | * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3). |
9 | * |
10 | * This example has been created using raylib 2.5 (www.raylib.com) |
11 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
12 | * |
13 | * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5) |
14 | * |
15 | * Chris Camacho (@codifies - http://bedroomcoders.co.uk/) notes: |
16 | * |
17 | * This is based on the PBR lighting example, but greatly simplified to aid learning... |
18 | * actually there is very little of the PBR example left! |
19 | * When I first looked at the bewildering complexity of the PBR example I feared |
20 | * I would never understand how I could do simple lighting with raylib however its |
21 | * a testement to the authors of raylib (including rlights.h) that the example |
22 | * came together fairly quickly. |
23 | * |
24 | * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5) |
25 | * |
26 | ********************************************************************************************/ |
27 | |
28 | #include "raylib.h" |
29 | |
30 | #include "raymath.h" |
31 | |
32 | #define RLIGHTS_IMPLEMENTATION |
33 | #include "rlights.h" |
34 | |
35 | #if defined(PLATFORM_DESKTOP) |
36 | #define GLSL_VERSION 330 |
37 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
38 | #define GLSL_VERSION 100 |
39 | #endif |
40 | |
41 | int main(void) |
42 | { |
43 | // Initialization |
44 | //-------------------------------------------------------------------------------------- |
45 | const int screenWidth = 800; |
46 | const int screenHeight = 450; |
47 | |
48 | SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) |
49 | InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting" ); |
50 | |
51 | // Define the camera to look into our 3d world |
52 | Camera camera = { 0 }; |
53 | camera.position = (Vector3){ 2.0f, 2.0f, 6.0f }; // Camera position |
54 | camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point |
55 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) |
56 | camera.fovy = 45.0f; // Camera field-of-view Y |
57 | camera.type = CAMERA_PERSPECTIVE; // Camera mode type |
58 | |
59 | // Load models |
60 | Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); |
61 | Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); |
62 | Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); |
63 | |
64 | // Load models texture |
65 | Texture texture = LoadTexture("resources/texel_checker.png" ); |
66 | |
67 | // Assign texture to default model material |
68 | modelA.materials[0].maps[MAP_DIFFUSE].texture = texture; |
69 | modelB.materials[0].maps[MAP_DIFFUSE].texture = texture; |
70 | modelC.materials[0].maps[MAP_DIFFUSE].texture = texture; |
71 | |
72 | Shader shader = LoadShader(FormatText("resources/shaders/glsl%i/base_lighting.vs" , GLSL_VERSION), |
73 | FormatText("resources/shaders/glsl%i/lighting.fs" , GLSL_VERSION)); |
74 | |
75 | // Get some shader loactions |
76 | shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel" ); |
77 | shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos" ); |
78 | |
79 | // ambient light level |
80 | int ambientLoc = GetShaderLocation(shader, "ambient" ); |
81 | SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4); |
82 | |
83 | float angle = 6.282f; |
84 | |
85 | // All models use the same shader |
86 | modelA.materials[0].shader = shader; |
87 | modelB.materials[0].shader = shader; |
88 | modelC.materials[0].shader = shader; |
89 | |
90 | // Using 4 point lights, white, red, green and blue |
91 | Light lights[MAX_LIGHTS] = { 0 }; |
92 | lights[0] = CreateLight(LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), WHITE, shader); |
93 | lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), RED, shader); |
94 | lights[2] = CreateLight(LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), GREEN, shader); |
95 | lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), BLUE, shader); |
96 | |
97 | SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode |
98 | |
99 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
100 | //-------------------------------------------------------------------------------------- |
101 | |
102 | // Main game loop |
103 | while (!WindowShouldClose()) // Detect window close button or ESC key |
104 | { |
105 | // Update |
106 | //---------------------------------------------------------------------------------- |
107 | if (IsKeyPressed(KEY_W)) { lights[0].enabled = !lights[0].enabled; } |
108 | if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; } |
109 | if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; } |
110 | if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; } |
111 | |
112 | UpdateCamera(&camera); // Update camera |
113 | |
114 | // Make the lights do differing orbits |
115 | angle -= 0.02f; |
116 | lights[0].position.x = cosf(angle)*4.0f; |
117 | lights[0].position.z = sinf(angle)*4.0f; |
118 | lights[1].position.x = cosf(-angle*0.6f)*4.0f; |
119 | lights[1].position.z = sinf(-angle*0.6f)*4.0f; |
120 | lights[2].position.y = cosf(angle*0.2f)*4.0f; |
121 | lights[2].position.z = sinf(angle*0.2f)*4.0f; |
122 | lights[3].position.y = cosf(-angle*0.35f)*4.0f; |
123 | lights[3].position.z = sinf(-angle*0.35f)*4.0f; |
124 | |
125 | UpdateLightValues(shader, lights[0]); |
126 | UpdateLightValues(shader, lights[1]); |
127 | UpdateLightValues(shader, lights[2]); |
128 | UpdateLightValues(shader, lights[3]); |
129 | |
130 | // Rotate the torus |
131 | modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f)); |
132 | modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f)); |
133 | |
134 | // Update the light shader with the camera view position |
135 | float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; |
136 | SetShaderValue(shader, shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3); |
137 | //---------------------------------------------------------------------------------- |
138 | |
139 | // Draw |
140 | //---------------------------------------------------------------------------------- |
141 | BeginDrawing(); |
142 | |
143 | ClearBackground(RAYWHITE); |
144 | |
145 | BeginMode3D(camera); |
146 | |
147 | // Draw the three models |
148 | DrawModel(modelA, Vector3Zero(), 1.0f, WHITE); |
149 | DrawModel(modelB, (Vector3){-1.6,0,0}, 1.0f, WHITE); |
150 | DrawModel(modelC, (Vector3){ 1.6,0,0}, 1.0f, WHITE); |
151 | |
152 | // Draw markers to show where the lights are |
153 | if (lights[0].enabled) { DrawSphereEx(lights[0].position, 0.2f, 8, 8, WHITE); } |
154 | if (lights[1].enabled) { DrawSphereEx(lights[1].position, 0.2f, 8, 8, RED); } |
155 | if (lights[2].enabled) { DrawSphereEx(lights[2].position, 0.2f, 8, 8, GREEN); } |
156 | if (lights[3].enabled) { DrawSphereEx(lights[3].position, 0.2f, 8, 8, BLUE); } |
157 | |
158 | DrawGrid(10, 1.0f); |
159 | |
160 | EndMode3D(); |
161 | |
162 | DrawFPS(10, 10); |
163 | |
164 | DrawText("Use keys RGBW to toggle lights" , 10, 30, 20, DARKGRAY); |
165 | |
166 | EndDrawing(); |
167 | //---------------------------------------------------------------------------------- |
168 | } |
169 | |
170 | // De-Initialization |
171 | //-------------------------------------------------------------------------------------- |
172 | UnloadModel(modelA); // Unload the modelA |
173 | UnloadModel(modelB); // Unload the modelB |
174 | UnloadModel(modelC); // Unload the modelC |
175 | |
176 | UnloadTexture(texture); // Unload the texture |
177 | UnloadShader(shader); // Unload shader |
178 | |
179 | CloseWindow(); // Close window and OpenGL context |
180 | //-------------------------------------------------------------------------------------- |
181 | |
182 | return 0; |
183 | } |
184 | |
185 | |