standard_lighting.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if defined(PLATFORM_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  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;
  31. Material material;
  32. Light spotLight;
  33. Light dirLight;
  34. Light pointLight;
  35. //----------------------------------------------------------------------------------
  36. // Module Functions Declaration
  37. //----------------------------------------------------------------------------------
  38. void UpdateDrawFrame(void); // Update and Draw one frame
  39. //----------------------------------------------------------------------------------
  40. // Main Enry Point
  41. //----------------------------------------------------------------------------------
  42. int main()
  43. {
  44. // Initialization
  45. //--------------------------------------------------------------------------------------
  46. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  47. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
  48. dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  49. material = LoadStandardMaterial();
  50. material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
  51. material.texNormal = LoadTexture("resources/model/dwarf_normal.png"); // Load model normal texture
  52. material.texSpecular = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
  53. material.colDiffuse = WHITE;
  54. material.colAmbient = (Color){0, 0, 10, 255};
  55. material.colSpecular = WHITE;
  56. material.glossiness = 50.0f;
  57. dwarf.material = material; // Apply material to model
  58. spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
  59. spotLight->target = (Vector3){0.0f, 0.0f, 0.0f};
  60. spotLight->intensity = 2.0f;
  61. spotLight->diffuse = (Color){255, 100, 100, 255};
  62. spotLight->coneAngle = 60.0f;
  63. dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255});
  64. dirLight->target = (Vector3){1.0f, -2.0f, -2.0f};
  65. dirLight->intensity = 2.0f;
  66. dirLight->diffuse = (Color){100, 255, 100, 255};
  67. pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255});
  68. pointLight->intensity = 2.0f;
  69. pointLight->diffuse = (Color){100, 100, 255, 255};
  70. pointLight->radius = 3.0f;
  71. // Setup orbital camera
  72. SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode
  73. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  74. SetCameraTarget(camera.target); // Set internal camera target to match our camera target
  75. #if defined(PLATFORM_WEB)
  76. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  77. #else
  78. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  79. //--------------------------------------------------------------------------------------
  80. // Main game loop
  81. while (!WindowShouldClose()) // Detect window close button or ESC key
  82. {
  83. UpdateDrawFrame();
  84. }
  85. #endif
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadMaterial(material); // Unload material and assigned textures
  89. UnloadModel(dwarf); // Unload model
  90. // Destroy all created lights
  91. DestroyLight(pointLight);
  92. DestroyLight(dirLight);
  93. DestroyLight(spotLight);
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }
  98. //----------------------------------------------------------------------------------
  99. // Module Functions Definition
  100. //----------------------------------------------------------------------------------
  101. void UpdateDrawFrame(void)
  102. {
  103. // Update
  104. //----------------------------------------------------------------------------------
  105. UpdateCamera(&camera); // Update internal camera and our camera
  106. //----------------------------------------------------------------------------------
  107. // Draw
  108. //----------------------------------------------------------------------------------
  109. BeginDrawing();
  110. ClearBackground(RAYWHITE);
  111. Begin3dMode(camera);
  112. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  113. DrawLight(spotLight); // Draw spot light
  114. DrawLight(dirLight); // Draw directional light
  115. DrawLight(pointLight); // Draw point light
  116. DrawGrid(10, 1.0f); // Draw a grid
  117. End3dMode();
  118. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  119. DrawFPS(10, 10);
  120. EndDrawing();
  121. //----------------------------------------------------------------------------------
  122. }