shaders_standard_lighting.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.3 (www.raylib.com)
  13. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  14. *
  15. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 800;
  24. int screenHeight = 450;
  25. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  26. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  27. // Define the camera to look into our 3d world
  28. Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  29. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  30. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  31. Material material = LoadStandardMaterial();
  32. material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
  33. material.texNormal = LoadTexture("resources/model/dwarf_normal.png"); // Load model normal texture
  34. material.texSpecular = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
  35. material.colDiffuse = WHITE;
  36. material.colAmbient = (Color){0, 0, 10, 255};
  37. material.colSpecular = WHITE;
  38. material.glossiness = 50.0f;
  39. dwarf.material = material; // Apply material to model
  40. Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
  41. spotLight->target = (Vector3){0.0f, 0.0f, 0.0f};
  42. spotLight->intensity = 2.0f;
  43. spotLight->diffuse = (Color){255, 100, 100, 255};
  44. spotLight->coneAngle = 60.0f;
  45. Light dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255});
  46. dirLight->target = (Vector3){1.0f, -2.0f, -2.0f};
  47. dirLight->intensity = 2.0f;
  48. dirLight->diffuse = (Color){100, 255, 100, 255};
  49. Light pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255});
  50. pointLight->intensity = 2.0f;
  51. pointLight->diffuse = (Color){100, 100, 255, 255};
  52. pointLight->radius = 3.0f;
  53. // Setup orbital camera
  54. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  55. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  56. //--------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. UpdateCamera(&camera); // Update camera
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. Begin3dMode(camera);
  69. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  70. DrawLight(spotLight); // Draw spot light
  71. DrawLight(dirLight); // Draw directional light
  72. DrawLight(pointLight); // Draw point light
  73. DrawGrid(10, 1.0f); // Draw a grid
  74. End3dMode();
  75. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  76. DrawFPS(10, 10);
  77. EndDrawing();
  78. //----------------------------------------------------------------------------------
  79. }
  80. // De-Initialization
  81. //--------------------------------------------------------------------------------------
  82. UnloadMaterial(material); // Unload material and assigned textures
  83. UnloadModel(dwarf); // Unload model
  84. // Destroy all created lights
  85. DestroyLight(pointLight);
  86. DestroyLight(dirLight);
  87. DestroyLight(spotLight);
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }