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.

121 lines
5.2 KiB

  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_ORBITAL); // Set an orbital camera mode
  55. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  56. SetCameraTarget(camera.target); // Set internal camera target to match our camera target
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. UpdateCamera(&camera); // Update internal camera and our camera
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. Begin3dMode(camera);
  71. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  72. DrawLight(spotLight); // Draw spot light
  73. DrawLight(dirLight); // Draw directional light
  74. DrawLight(pointLight); // Draw point light
  75. DrawGrid(10, 1.0f); // Draw a grid
  76. End3dMode();
  77. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  78. DrawFPS(10, 10);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. UnloadMaterial(material); // Unload material and assigned textures
  85. UnloadModel(dwarf); // Unload model
  86. // Destroy all created lights
  87. DestroyLight(pointLight);
  88. DestroyLight(dirLight);
  89. DestroyLight(spotLight);
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }