shaders_basic_lighting.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Blinn-Phong lighting
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define SHININESS_SPEED 1.0f
  13. #define LIGHT_SPEED 0.25f
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. SetConfigFlags(FLAG_MSAA_4X_HINT);
  21. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - blinn-phong lighting");
  22. SetTargetFPS(60);
  23. // Camera initialization
  24. Camera camera = {{ 8.0f, 8.0f, 8.0f }, { 0.0f, 3.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }};
  25. // Model initialization
  26. Vector3 position = { 0.0f, 0.0f, 0.0f };
  27. Model model = LoadModel("resources/model/dwarf.obj");
  28. Shader shader = LoadShader("resources/shaders/phong.vs", "resources/shaders/phong.fs");
  29. SetModelShader(&model, shader);
  30. // Shader locations initialization
  31. int lIntensityLoc = GetShaderLocation(shader, "light_intensity");
  32. int lAmbientLoc = GetShaderLocation(shader, "light_ambientColor");
  33. int lDiffuseLoc = GetShaderLocation(shader, "light_diffuseColor");
  34. int lSpecularLoc = GetShaderLocation(shader, "light_specularColor");
  35. int lSpecIntensityLoc = GetShaderLocation(shader, "light_specIntensity");
  36. int mAmbientLoc = GetShaderLocation(shader, "mat_ambientColor");
  37. int mSpecularLoc = GetShaderLocation(shader, "mat_specularColor");
  38. int mGlossLoc = GetShaderLocation(shader, "mat_glossiness");
  39. // Camera and light vectors shader locations
  40. int cameraLoc = GetShaderLocation(shader, "cameraPos");
  41. int lightLoc = GetShaderLocation(shader, "lightPos");
  42. // Light and material definitions
  43. Light light;
  44. Material matBlinn;
  45. // Light initialization
  46. light.position = (Vector3){ 4.0f, 2.0f, 0.0f };
  47. light.direction = (Vector3){ 5.0f, 1.0f, 1.0f };
  48. light.intensity = 1.0f;
  49. light.diffuse = WHITE;
  50. light.ambient = (Color){ 150, 75, 0, 255 };
  51. light.specular = WHITE;
  52. light.specIntensity = 1.0f;
  53. // Material initialization
  54. matBlinn.diffuse = WHITE;
  55. matBlinn.ambient = (Color){ 50, 50, 50, 255 };
  56. matBlinn.specular = WHITE;
  57. matBlinn.glossiness = 50.0f;
  58. // Setup camera
  59. SetCameraMode(CAMERA_FREE); // Set camera mode
  60. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  61. SetCameraTarget(camera.target); // Set internal camera target to match our camera target
  62. //--------------------------------------------------------------------------------------
  63. // Main game loop
  64. while (!WindowShouldClose()) // Detect window close button or ESC key
  65. {
  66. // Update
  67. //----------------------------------------------------------------------------------
  68. // Update camera position
  69. UpdateCamera(&camera);
  70. // Glossiness input control
  71. if(IsKeyDown(KEY_UP)) matBlinn.glossiness += SHININESS_SPEED;
  72. else if(IsKeyDown(KEY_DOWN))
  73. {
  74. matBlinn.glossiness -= SHININESS_SPEED;
  75. if( matBlinn.glossiness < 0) matBlinn.glossiness = 0.0f;
  76. }
  77. // Light X movement
  78. if (IsKeyDown(KEY_D)) light.position.x += LIGHT_SPEED;
  79. else if(IsKeyDown(KEY_A)) light.position.x -= LIGHT_SPEED;
  80. // Light Y movement
  81. if (IsKeyDown(KEY_LEFT_SHIFT)) light.position.y += LIGHT_SPEED;
  82. else if (IsKeyDown(KEY_LEFT_CONTROL)) light.position.y -= LIGHT_SPEED;
  83. // Light Z movement
  84. if (IsKeyDown(KEY_S)) light.position.z += LIGHT_SPEED;
  85. else if (IsKeyDown(KEY_W)) light.position.z -= LIGHT_SPEED;
  86. // Send light values to shader
  87. SetShaderValue(shader, lIntensityLoc, &light.intensity, 1);
  88. SetShaderValue(shader, lAmbientLoc, ColorToFloat(light.ambient), 3);
  89. SetShaderValue(shader, lDiffuseLoc, ColorToFloat(light.diffuse), 3);
  90. SetShaderValue(shader, lSpecularLoc, ColorToFloat(light.specular), 3);
  91. SetShaderValue(shader, lSpecIntensityLoc, &light.specIntensity, 1);
  92. // Send material values to shader
  93. SetShaderValue(shader, mAmbientLoc, ColorToFloat(matBlinn.ambient), 3);
  94. SetShaderValue(shader, mSpecularLoc, ColorToFloat(matBlinn.specular), 3);
  95. SetShaderValue(shader, mGlossLoc, &matBlinn.glossiness, 1);
  96. // Send camera and light transform values to shader
  97. SetShaderValue(shader, cameraLoc, VectorToFloat(camera.position), 3);
  98. SetShaderValue(shader, lightLoc, VectorToFloat(light.position), 3);
  99. //----------------------------------------------------------------------------------
  100. // Draw
  101. //----------------------------------------------------------------------------------
  102. BeginDrawing();
  103. ClearBackground(RAYWHITE);
  104. Begin3dMode(camera);
  105. DrawModel(model, position, 4.0f, matBlinn.diffuse);
  106. DrawSphere(light.position, 0.5f, GOLD);
  107. DrawGrid(20, 1.0f);
  108. End3dMode();
  109. DrawFPS(10, 10); // Draw FPS
  110. EndDrawing();
  111. //----------------------------------------------------------------------------------
  112. }
  113. // De-Initialization
  114. //--------------------------------------------------------------------------------------
  115. UnloadShader(shader);
  116. UnloadModel(model);
  117. CloseWindow(); // Close window and OpenGL context
  118. //--------------------------------------------------------------------------------------
  119. return 0;
  120. }