You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
19 KiB

пре 7 година
пре 7 година
пре 7 година
  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.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
  87. material.texNormal = LoadTexture("resources/model/dwarf_normal.png"); // Load model normal texture
  88. material.texSpecular = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
  89. material.colDiffuse = WHITE;
  90. material.colAmbient = (Color){0, 0, 10, 255};
  91. material.colSpecular = WHITE;
  92. material.glossiness = 50.0f;
  93. dwarf.material = material; // Apply material to model
  94. Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
  95. spotLight->target = (Vector3){0.0f, 0.0f, 0.0f};
  96. spotLight->intensity = 2.0f;
  97. spotLight->diffuse = (Color){255, 100, 100, 255};
  98. spotLight->coneAngle = 60.0f;
  99. Light dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255});
  100. dirLight->target = (Vector3){1.0f, -2.0f, -2.0f};
  101. dirLight->intensity = 2.0f;
  102. dirLight->diffuse = (Color){100, 255, 100, 255};
  103. Light pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255});
  104. pointLight->intensity = 2.0f;
  105. pointLight->diffuse = (Color){100, 100, 255, 255};
  106. pointLight->radius = 3.0f;
  107. // Set shader lights values for enabled lights
  108. // NOTE: If values are not changed in real time, they can be set at initialization!!!
  109. SetShaderLightsValues(material.shader);
  110. //SetShaderActive(0);
  111. // Setup orbital camera
  112. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  113. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  114. //--------------------------------------------------------------------------------------
  115. // Main game loop
  116. while (!WindowShouldClose()) // Detect window close button or ESC key
  117. {
  118. // Update
  119. //----------------------------------------------------------------------------------
  120. UpdateCamera(&camera); // Update camera
  121. //----------------------------------------------------------------------------------
  122. // Draw
  123. //----------------------------------------------------------------------------------
  124. BeginDrawing();
  125. ClearBackground(RAYWHITE);
  126. Begin3dMode(camera);
  127. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  128. DrawLight(spotLight); // Draw spot light
  129. DrawLight(dirLight); // Draw directional light
  130. DrawLight(pointLight); // Draw point light
  131. DrawGrid(10, 1.0f); // Draw a grid
  132. End3dMode();
  133. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  134. DrawFPS(10, 10);
  135. EndDrawing();
  136. //----------------------------------------------------------------------------------
  137. }
  138. // De-Initialization
  139. //--------------------------------------------------------------------------------------
  140. UnloadMaterial(material); // Unload material and assigned textures
  141. UnloadModel(dwarf); // Unload model
  142. // Destroy all created lights
  143. DestroyLight(pointLight);
  144. DestroyLight(dirLight);
  145. DestroyLight(spotLight);
  146. // Unload lights
  147. if (lightsCount > 0)
  148. {
  149. for (int i = 0; i < lightsCount; i++) free(lights[i]);
  150. lightsCount = 0;
  151. }
  152. CloseWindow(); // Close window and OpenGL context
  153. //--------------------------------------------------------------------------------------
  154. return 0;
  155. }
  156. //--------------------------------------------------------------------------------------------
  157. // Module Functions Definitions
  158. //--------------------------------------------------------------------------------------------
  159. // Create a new light, initialize it and add to pool
  160. Light CreateLight(int type, Vector3 position, Color diffuse)
  161. {
  162. Light light = NULL;
  163. if (lightsCount < MAX_LIGHTS)
  164. {
  165. // Allocate dynamic memory
  166. light = (Light)malloc(sizeof(LightData));
  167. // Initialize light values with generic values
  168. light->id = lightsCount;
  169. light->type = type;
  170. light->enabled = true;
  171. light->position = position;
  172. light->target = (Vector3){ 0.0f, 0.0f, 0.0f };
  173. light->intensity = 1.0f;
  174. light->diffuse = diffuse;
  175. // Add new light to the array
  176. lights[lightsCount] = light;
  177. // Increase enabled lights count
  178. lightsCount++;
  179. }
  180. else
  181. {
  182. // NOTE: Returning latest created light to avoid crashes
  183. light = lights[lightsCount];
  184. }
  185. return light;
  186. }
  187. // Destroy a light and take it out of the list
  188. void DestroyLight(Light light)
  189. {
  190. if (light != NULL)
  191. {
  192. int lightId = light->id;
  193. // Free dynamic memory allocation
  194. free(lights[lightId]);
  195. // Remove *obj from the pointers array
  196. for (int i = lightId; i < lightsCount; i++)
  197. {
  198. // Resort all the following pointers of the array
  199. if ((i + 1) < lightsCount)
  200. {
  201. lights[i] = lights[i + 1];
  202. lights[i]->id = lights[i + 1]->id;
  203. }
  204. }
  205. // Decrease enabled physic objects count
  206. lightsCount--;
  207. }
  208. }
  209. // Draw light in 3D world
  210. void DrawLight(Light light)
  211. {
  212. switch (light->type)
  213. {
  214. case LIGHT_POINT:
  215. {
  216. DrawSphereWires(light->position, 0.3f*light->intensity, 8, 8, (light->enabled ? light->diffuse : GRAY));
  217. DrawCircle3D(light->position, light->radius, (Vector3){ 0, 0, 0 }, 0.0f, (light->enabled ? light->diffuse : GRAY));
  218. DrawCircle3D(light->position, light->radius, (Vector3){ 1, 0, 0 }, 90.0f, (light->enabled ? light->diffuse : GRAY));
  219. DrawCircle3D(light->position, light->radius, (Vector3){ 0, 1, 0 },90.0f, (light->enabled ? light->diffuse : GRAY));
  220. } break;
  221. case LIGHT_DIRECTIONAL:
  222. {
  223. DrawLine3D(light->position, light->target, (light->enabled ? light->diffuse : GRAY));
  224. DrawSphereWires(light->position, 0.3f*light->intensity, 8, 8, (light->enabled ? light->diffuse : GRAY));
  225. DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : GRAY));
  226. } break;
  227. case LIGHT_SPOT:
  228. {
  229. DrawLine3D(light->position, light->target, (light->enabled ? light->diffuse : GRAY));
  230. Vector3 dir = VectorSubtract(light->target, light->position);
  231. VectorNormalize(&dir);
  232. DrawCircle3D(light->position, 0.5f, dir, 0.0f, (light->enabled ? light->diffuse : GRAY));
  233. //DrawCylinderWires(light->position, 0.0f, 0.3f*light->coneAngle/50, 0.6f, 5, (light->enabled ? light->diffuse : GRAY));
  234. DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : GRAY));
  235. } break;
  236. default: break;
  237. }
  238. }
  239. // Get shader locations for lights (up to MAX_LIGHTS)
  240. static void GetShaderLightsLocations(Shader shader)
  241. {
  242. char locName[32] = "lights[x].\0";
  243. char locNameUpdated[64];
  244. for (int i = 0; i < MAX_LIGHTS; i++)
  245. {
  246. locName[7] = '0' + i;
  247. strcpy(locNameUpdated, locName);
  248. strcat(locNameUpdated, "enabled\0");
  249. lightsLocs[i][0] = GetShaderLocation(shader, locNameUpdated);
  250. locNameUpdated[0] = '\0';
  251. strcpy(locNameUpdated, locName);
  252. strcat(locNameUpdated, "type\0");
  253. lightsLocs[i][1] = GetShaderLocation(shader, locNameUpdated);
  254. locNameUpdated[0] = '\0';
  255. strcpy(locNameUpdated, locName);
  256. strcat(locNameUpdated, "position\0");
  257. lightsLocs[i][2] = GetShaderLocation(shader, locNameUpdated);
  258. locNameUpdated[0] = '\0';
  259. strcpy(locNameUpdated, locName);
  260. strcat(locNameUpdated, "direction\0");
  261. lightsLocs[i][3] = GetShaderLocation(shader, locNameUpdated);
  262. locNameUpdated[0] = '\0';
  263. strcpy(locNameUpdated, locName);
  264. strcat(locNameUpdated, "radius\0");
  265. lightsLocs[i][4] = GetShaderLocation(shader, locNameUpdated);
  266. locNameUpdated[0] = '\0';
  267. strcpy(locNameUpdated, locName);
  268. strcat(locNameUpdated, "diffuse\0");
  269. lightsLocs[i][5] = GetShaderLocation(shader, locNameUpdated);
  270. locNameUpdated[0] = '\0';
  271. strcpy(locNameUpdated, locName);
  272. strcat(locNameUpdated, "intensity\0");
  273. lightsLocs[i][6] = GetShaderLocation(shader, locNameUpdated);
  274. locNameUpdated[0] = '\0';
  275. strcpy(locNameUpdated, locName);
  276. strcat(locNameUpdated, "coneAngle\0");
  277. lightsLocs[i][7] = GetShaderLocation(shader, locNameUpdated);
  278. }
  279. }
  280. // Set shader uniform values for lights
  281. // NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0
  282. // TODO: Replace glUniform1i(), glUniform1f(), glUniform3f(), glUniform4f():
  283. //SetShaderValue(Shader shader, int uniformLoc, float *value, int size)
  284. //SetShaderValuei(Shader shader, int uniformLoc, int *value, int size)
  285. static void SetShaderLightsValues(Shader shader)
  286. {
  287. int tempInt[8] = { 0 };
  288. float tempFloat[8] = { 0.0f };
  289. for (int i = 0; i < MAX_LIGHTS; i++)
  290. {
  291. if (i < lightsCount)
  292. {
  293. tempInt[0] = lights[i]->enabled;
  294. SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], lights[i]->enabled);
  295. tempInt[0] = lights[i]->type;
  296. SetShaderValuei(shader, lightsLocs[i][1], tempInt, 1); //glUniform1i(lightsLocs[i][1], lights[i]->type);
  297. tempFloat[0] = (float)lights[i]->diffuse.r/255.0f;
  298. tempFloat[1] = (float)lights[i]->diffuse.g/255.0f;
  299. tempFloat[2] = (float)lights[i]->diffuse.b/255.0f;
  300. tempFloat[3] = (float)lights[i]->diffuse.a/255.0f;
  301. SetShaderValue(shader, lightsLocs[i][5], tempFloat, 4);
  302. //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);
  303. tempFloat[0] = lights[i]->intensity;
  304. SetShaderValue(shader, lightsLocs[i][6], tempFloat, 1);
  305. switch (lights[i]->type)
  306. {
  307. case LIGHT_POINT:
  308. {
  309. tempFloat[0] = lights[i]->position.x;
  310. tempFloat[1] = lights[i]->position.y;
  311. tempFloat[2] = lights[i]->position.z;
  312. SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3);
  313. tempFloat[0] = lights[i]->radius;
  314. SetShaderValue(shader, lightsLocs[i][4], tempFloat, 1);
  315. //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
  316. //glUniform1f(lightsLocs[i][4], lights[i]->radius);
  317. } break;
  318. case LIGHT_DIRECTIONAL:
  319. {
  320. Vector3 direction = VectorSubtract(lights[i]->target, lights[i]->position);
  321. VectorNormalize(&direction);
  322. tempFloat[0] = direction.x;
  323. tempFloat[1] = direction.y;
  324. tempFloat[2] = direction.z;
  325. SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3);
  326. //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z);
  327. } break;
  328. case LIGHT_SPOT:
  329. {
  330. tempFloat[0] = lights[i]->position.x;
  331. tempFloat[1] = lights[i]->position.y;
  332. tempFloat[2] = lights[i]->position.z;
  333. SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3);
  334. //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
  335. Vector3 direction = VectorSubtract(lights[i]->target, lights[i]->position);
  336. VectorNormalize(&direction);
  337. tempFloat[0] = direction.x;
  338. tempFloat[1] = direction.y;
  339. tempFloat[2] = direction.z;
  340. SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3);
  341. //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z);
  342. tempFloat[0] = lights[i]->coneAngle;
  343. SetShaderValue(shader, lightsLocs[i][7], tempFloat, 1);
  344. //glUniform1f(lightsLocs[i][7], lights[i]->coneAngle);
  345. } break;
  346. default: break;
  347. }
  348. }
  349. else
  350. {
  351. tempInt[0] = 0;
  352. SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], 0); // Light disabled
  353. }
  354. }
  355. }
  356. // Calculate vector length
  357. float VectorLength(const Vector3 v)
  358. {
  359. float length;
  360. length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
  361. return length;
  362. }
  363. // Normalize provided vector
  364. void VectorNormalize(Vector3 *v)
  365. {
  366. float length, ilength;
  367. length = VectorLength(*v);
  368. if (length == 0.0f) length = 1.0f;
  369. ilength = 1.0f/length;
  370. v->x *= ilength;
  371. v->y *= ilength;
  372. v->z *= ilength;
  373. }
  374. // Substract two vectors
  375. Vector3 VectorSubtract(Vector3 v1, Vector3 v2)
  376. {
  377. Vector3 result;
  378. result.x = v1.x - v2.x;
  379. result.y = v1.y - v2.y;
  380. result.z = v1.z - v2.z;
  381. return result;
  382. }