standard_lighting.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Standard lighting (materials and lights)
  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), to test this example
  9. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  10. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  11. *
  12. * This example has been created using raylib 1.7 (www.raylib.com)
  13. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  14. *
  15. * Copyright (c) 2016-2017 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #include <stdlib.h> // Required for: NULL
  20. #include <string.h> // Required for: strcpy()
  21. #include <math.h> // Required for: vector math
  22. //----------------------------------------------------------------------------------
  23. // Defines and Macros
  24. //----------------------------------------------------------------------------------
  25. #define MAX_LIGHTS 8 // Max lights supported by standard shader
  26. //----------------------------------------------------------------------------------
  27. // Types and Structures Definition
  28. //----------------------------------------------------------------------------------
  29. // Light type
  30. typedef struct LightData {
  31. unsigned int id; // Light unique id
  32. bool enabled; // Light enabled
  33. int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
  34. Vector3 position; // Light position
  35. Vector3 target; // Light direction: LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
  36. float radius; // Light attenuation radius light intensity reduced with distance (world distance)
  37. Color diffuse; // Light diffuse color
  38. float intensity; // Light intensity level
  39. float coneAngle; // Light cone max angle: LIGHT_SPOT
  40. } LightData, *Light;
  41. // Light types
  42. typedef enum { LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT } LightType;
  43. //----------------------------------------------------------------------------------
  44. // Global Variables Definition
  45. //----------------------------------------------------------------------------------
  46. static Light lights[MAX_LIGHTS]; // Lights pool
  47. static int lightsCount = 0; // Enabled lights counter
  48. static int lightsLocs[MAX_LIGHTS][8]; // Lights location points in shader: 8 possible points per light:
  49. // enabled, type, position, target, radius, diffuse, intensity, coneAngle
  50. //----------------------------------------------------------------------------------
  51. // Module Functions Declaration
  52. //----------------------------------------------------------------------------------
  53. static Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool
  54. static void DestroyLight(Light light); // Destroy a light and take it out of the list
  55. static void DrawLight(Light light); // Draw light in 3D world
  56. static void GetShaderLightsLocations(Shader shader); // Get shader locations for lights (up to MAX_LIGHTS)
  57. static void SetShaderLightsValues(Shader shader); // Set shader uniform values for lights
  58. // Vector3 math functions
  59. static float VectorLength(const Vector3 v); // Calculate vector length
  60. static void VectorNormalize(Vector3 *v); // Normalize provided vector
  61. static Vector3 VectorSubtract(Vector3 v1, Vector3 v2); // Substract two vectors
  62. //https://www.gamedev.net/topic/655969-speed-gluniform-vs-uniform-buffer-objects/
  63. //https://www.reddit.com/r/opengl/comments/4ri20g/is_gluniform_more_expensive_than_glprogramuniform/
  64. //http://cg.alexandra.dk/?p=3778 - AZDO
  65. //https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/BestPracticesforShaders/BestPracticesforShaders.html
  66. //------------------------------------------------------------------------------------
  67. // Program main entry point
  68. //------------------------------------------------------------------------------------
  69. int main()
  70. {
  71. // Initialization
  72. //--------------------------------------------------------------------------------------
  73. int screenWidth = 800;
  74. int screenHeight = 450;
  75. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  76. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  77. // Define the camera to look into our 3d world
  78. Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  79. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  80. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  81. Material material;// = LoadStandardMaterial();
  82. material.shader = LoadShader("resources/shaders/glsl330/standard.vs",
  83. "resources/shaders/glsl330/standard.fs");
  84. // Try to get lights location points (if available)
  85. GetShaderLightsLocations(material.shader);
  86. material.maps[MAP_DIFFUSE].texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
  87. material.maps[MAP_NORMAL].texture = LoadTexture("resources/model/dwarf_normal.png"); // Load model normal texture
  88. material.maps[MAP_SPECULAR].texture = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
  89. material.maps[MAP_DIFFUSE].color = WHITE;
  90. material.maps[MAP_SPECULAR].color = WHITE;
  91. dwarf.material = material; // Apply material to model
  92. Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
  93. spotLight->target = (Vector3){0.0f, 0.0f, 0.0f};
  94. spotLight->intensity = 2.0f;
  95. spotLight->diffuse = (Color){255, 100, 100, 255};
  96. spotLight->coneAngle = 60.0f;
  97. Light dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255});
  98. dirLight->target = (Vector3){1.0f, -2.0f, -2.0f};
  99. dirLight->intensity = 2.0f;
  100. dirLight->diffuse = (Color){100, 255, 100, 255};
  101. Light pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255});
  102. pointLight->intensity = 2.0f;
  103. pointLight->diffuse = (Color){100, 100, 255, 255};
  104. pointLight->radius = 3.0f;
  105. // Set shader lights values for enabled lights
  106. // NOTE: If values are not changed in real time, they can be set at initialization!!!
  107. SetShaderLightsValues(material.shader);
  108. //SetShaderActive(0);
  109. // Setup orbital camera
  110. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  111. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  112. //--------------------------------------------------------------------------------------
  113. // Main game loop
  114. while (!WindowShouldClose()) // Detect window close button or ESC key
  115. {
  116. // Update
  117. //----------------------------------------------------------------------------------
  118. UpdateCamera(&camera); // Update camera
  119. //----------------------------------------------------------------------------------
  120. // Draw
  121. //----------------------------------------------------------------------------------
  122. BeginDrawing();
  123. ClearBackground(RAYWHITE);
  124. BeginMode3D(camera);
  125. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  126. DrawLight(spotLight); // Draw spot light
  127. DrawLight(dirLight); // Draw directional light
  128. DrawLight(pointLight); // Draw point light
  129. DrawGrid(10, 1.0f); // Draw a grid
  130. EndMode3D();
  131. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  132. DrawFPS(10, 10);
  133. EndDrawing();
  134. //----------------------------------------------------------------------------------
  135. }
  136. // De-Initialization
  137. //--------------------------------------------------------------------------------------
  138. UnloadMaterial(material); // Unload material and assigned textures
  139. UnloadModel(dwarf); // Unload model
  140. // Destroy all created lights
  141. DestroyLight(pointLight);
  142. DestroyLight(dirLight);
  143. DestroyLight(spotLight);
  144. // Unload lights
  145. if (lightsCount > 0)
  146. {
  147. for (int i = 0; i < lightsCount; i++) free(lights[i]);
  148. lightsCount = 0;
  149. }
  150. CloseWindow(); // Close window and OpenGL context
  151. //--------------------------------------------------------------------------------------
  152. return 0;
  153. }
  154. //--------------------------------------------------------------------------------------------
  155. // Module Functions Definitions
  156. //--------------------------------------------------------------------------------------------
  157. // Create a new light, initialize it and add to pool
  158. Light CreateLight(int type, Vector3 position, Color diffuse)
  159. {
  160. Light light = NULL;
  161. if (lightsCount < MAX_LIGHTS)
  162. {
  163. // Allocate dynamic memory
  164. light = (Light)malloc(sizeof(LightData));
  165. // Initialize light values with generic values
  166. light->id = lightsCount;
  167. light->type = type;
  168. light->enabled = true;
  169. light->position = position;
  170. light->target = (Vector3){ 0.0f, 0.0f, 0.0f };
  171. light->intensity = 1.0f;
  172. light->diffuse = diffuse;
  173. // Add new light to the array
  174. lights[lightsCount] = light;
  175. // Increase enabled lights count
  176. lightsCount++;
  177. }
  178. else
  179. {
  180. // NOTE: Returning latest created light to avoid crashes
  181. light = lights[lightsCount];
  182. }
  183. return light;
  184. }
  185. // Destroy a light and take it out of the list
  186. void DestroyLight(Light light)
  187. {
  188. if (light != NULL)
  189. {
  190. int lightId = light->id;
  191. // Free dynamic memory allocation
  192. free(lights[lightId]);
  193. // Remove *obj from the pointers array
  194. for (int i = lightId; i < lightsCount; i++)
  195. {
  196. // Resort all the following pointers of the array
  197. if ((i + 1) < lightsCount)
  198. {
  199. lights[i] = lights[i + 1];
  200. lights[i]->id = lights[i + 1]->id;
  201. }
  202. }
  203. // Decrease enabled physic objects count
  204. lightsCount--;
  205. }
  206. }
  207. // Draw light in 3D world
  208. void DrawLight(Light light)
  209. {
  210. switch (light->type)
  211. {
  212. case LIGHT_POINT:
  213. {
  214. DrawSphereWires(light->position, 0.3f*light->intensity, 8, 8, (light->enabled ? light->diffuse : GRAY));
  215. DrawCircle3D(light->position, light->radius, (Vector3){ 0, 0, 0 }, 0.0f, (light->enabled ? light->diffuse : GRAY));
  216. DrawCircle3D(light->position, light->radius, (Vector3){ 1, 0, 0 }, 90.0f, (light->enabled ? light->diffuse : GRAY));
  217. DrawCircle3D(light->position, light->radius, (Vector3){ 0, 1, 0 },90.0f, (light->enabled ? light->diffuse : GRAY));
  218. } break;
  219. case LIGHT_DIRECTIONAL:
  220. {
  221. DrawLine3D(light->position, light->target, (light->enabled ? light->diffuse : GRAY));
  222. DrawSphereWires(light->position, 0.3f*light->intensity, 8, 8, (light->enabled ? light->diffuse : GRAY));
  223. DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : GRAY));
  224. } break;
  225. case LIGHT_SPOT:
  226. {
  227. DrawLine3D(light->position, light->target, (light->enabled ? light->diffuse : GRAY));
  228. Vector3 dir = VectorSubtract(light->target, light->position);
  229. VectorNormalize(&dir);
  230. DrawCircle3D(light->position, 0.5f, dir, 0.0f, (light->enabled ? light->diffuse : GRAY));
  231. //DrawCylinderWires(light->position, 0.0f, 0.3f*light->coneAngle/50, 0.6f, 5, (light->enabled ? light->diffuse : GRAY));
  232. DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : GRAY));
  233. } break;
  234. default: break;
  235. }
  236. }
  237. // Get shader locations for lights (up to MAX_LIGHTS)
  238. static void GetShaderLightsLocations(Shader shader)
  239. {
  240. char locName[32] = "lights[x].\0";
  241. char locNameUpdated[64];
  242. for (int i = 0; i < MAX_LIGHTS; i++)
  243. {
  244. locName[7] = '0' + i;
  245. strcpy(locNameUpdated, locName);
  246. strcat(locNameUpdated, "enabled\0");
  247. lightsLocs[i][0] = GetShaderLocation(shader, locNameUpdated);
  248. locNameUpdated[0] = '\0';
  249. strcpy(locNameUpdated, locName);
  250. strcat(locNameUpdated, "type\0");
  251. lightsLocs[i][1] = GetShaderLocation(shader, locNameUpdated);
  252. locNameUpdated[0] = '\0';
  253. strcpy(locNameUpdated, locName);
  254. strcat(locNameUpdated, "position\0");
  255. lightsLocs[i][2] = GetShaderLocation(shader, locNameUpdated);
  256. locNameUpdated[0] = '\0';
  257. strcpy(locNameUpdated, locName);
  258. strcat(locNameUpdated, "direction\0");
  259. lightsLocs[i][3] = GetShaderLocation(shader, locNameUpdated);
  260. locNameUpdated[0] = '\0';
  261. strcpy(locNameUpdated, locName);
  262. strcat(locNameUpdated, "radius\0");
  263. lightsLocs[i][4] = GetShaderLocation(shader, locNameUpdated);
  264. locNameUpdated[0] = '\0';
  265. strcpy(locNameUpdated, locName);
  266. strcat(locNameUpdated, "diffuse\0");
  267. lightsLocs[i][5] = GetShaderLocation(shader, locNameUpdated);
  268. locNameUpdated[0] = '\0';
  269. strcpy(locNameUpdated, locName);
  270. strcat(locNameUpdated, "intensity\0");
  271. lightsLocs[i][6] = GetShaderLocation(shader, locNameUpdated);
  272. locNameUpdated[0] = '\0';
  273. strcpy(locNameUpdated, locName);
  274. strcat(locNameUpdated, "coneAngle\0");
  275. lightsLocs[i][7] = GetShaderLocation(shader, locNameUpdated);
  276. }
  277. }
  278. // Set shader uniform values for lights
  279. // NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0
  280. // TODO: Replace glUniform1i(), glUniform1f(), glUniform3f(), glUniform4f():
  281. //SetShaderValue(Shader shader, int uniformLoc, float *value, int size)
  282. //SetShaderValuei(Shader shader, int uniformLoc, int *value, int size)
  283. static void SetShaderLightsValues(Shader shader)
  284. {
  285. int tempInt[8] = { 0 };
  286. float tempFloat[8] = { 0.0f };
  287. for (int i = 0; i < MAX_LIGHTS; i++)
  288. {
  289. if (i < lightsCount)
  290. {
  291. tempInt[0] = lights[i]->enabled;
  292. SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], lights[i]->enabled);
  293. tempInt[0] = lights[i]->type;
  294. SetShaderValuei(shader, lightsLocs[i][1], tempInt, 1); //glUniform1i(lightsLocs[i][1], lights[i]->type);
  295. tempFloat[0] = (float)lights[i]->diffuse.r/255.0f;
  296. tempFloat[1] = (float)lights[i]->diffuse.g/255.0f;
  297. tempFloat[2] = (float)lights[i]->diffuse.b/255.0f;
  298. tempFloat[3] = (float)lights[i]->diffuse.a/255.0f;
  299. SetShaderValue(shader, lightsLocs[i][5], tempFloat, 4);
  300. //glUniform4f(lightsLocs[i][5], (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255);
  301. tempFloat[0] = lights[i]->intensity;
  302. SetShaderValue(shader, lightsLocs[i][6], tempFloat, 1);
  303. switch (lights[i]->type)
  304. {
  305. case LIGHT_POINT:
  306. {
  307. tempFloat[0] = lights[i]->position.x;
  308. tempFloat[1] = lights[i]->position.y;
  309. tempFloat[2] = lights[i]->position.z;
  310. SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3);
  311. tempFloat[0] = lights[i]->radius;
  312. SetShaderValue(shader, lightsLocs[i][4], tempFloat, 1);
  313. //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
  314. //glUniform1f(lightsLocs[i][4], lights[i]->radius);
  315. } break;
  316. case LIGHT_DIRECTIONAL:
  317. {
  318. Vector3 direction = VectorSubtract(lights[i]->target, lights[i]->position);
  319. VectorNormalize(&direction);
  320. tempFloat[0] = direction.x;
  321. tempFloat[1] = direction.y;
  322. tempFloat[2] = direction.z;
  323. SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3);
  324. //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z);
  325. } break;
  326. case LIGHT_SPOT:
  327. {
  328. tempFloat[0] = lights[i]->position.x;
  329. tempFloat[1] = lights[i]->position.y;
  330. tempFloat[2] = lights[i]->position.z;
  331. SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3);
  332. //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
  333. Vector3 direction = VectorSubtract(lights[i]->target, lights[i]->position);
  334. VectorNormalize(&direction);
  335. tempFloat[0] = direction.x;
  336. tempFloat[1] = direction.y;
  337. tempFloat[2] = direction.z;
  338. SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3);
  339. //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z);
  340. tempFloat[0] = lights[i]->coneAngle;
  341. SetShaderValue(shader, lightsLocs[i][7], tempFloat, 1);
  342. //glUniform1f(lightsLocs[i][7], lights[i]->coneAngle);
  343. } break;
  344. default: break;
  345. }
  346. }
  347. else
  348. {
  349. tempInt[0] = 0;
  350. SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], 0); // Light disabled
  351. }
  352. }
  353. }
  354. // Calculate vector length
  355. float VectorLength(const Vector3 v)
  356. {
  357. float length;
  358. length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
  359. return length;
  360. }
  361. // Normalize provided vector
  362. void VectorNormalize(Vector3 *v)
  363. {
  364. float length, ilength;
  365. length = VectorLength(*v);
  366. if (length == 0.0f) length = 1.0f;
  367. ilength = 1.0f/length;
  368. v->x *= ilength;
  369. v->y *= ilength;
  370. v->z *= ilength;
  371. }
  372. // Substract two vectors
  373. Vector3 VectorSubtract(Vector3 v1, Vector3 v2)
  374. {
  375. Vector3 result;
  376. result.x = v1.x - v2.x;
  377. result.y = v1.y - v2.y;
  378. result.z = v1.z - v2.z;
  379. return result;
  380. }