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.

184 lines
8.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - basic lighting
  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).
  9. *
  10. * This example has been created using raylib 2.5 (www.raylib.com)
  11. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  12. *
  13. * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
  14. *
  15. * Chris Camacho (@codifies - http://bedroomcoders.co.uk/) notes:
  16. *
  17. * This is based on the PBR lighting example, but greatly simplified to aid learning...
  18. * actually there is very little of the PBR example left!
  19. * When I first looked at the bewildering complexity of the PBR example I feared
  20. * I would never understand how I could do simple lighting with raylib however its
  21. * a testement to the authors of raylib (including rlights.h) that the example
  22. * came together fairly quickly.
  23. *
  24. * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  25. *
  26. ********************************************************************************************/
  27. #include "raylib.h"
  28. #include "raymath.h"
  29. #define RLIGHTS_IMPLEMENTATION
  30. #include "rlights.h"
  31. #if defined(PLATFORM_DESKTOP)
  32. #define GLSL_VERSION 330
  33. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  34. #define GLSL_VERSION 100
  35. #endif
  36. int main(void)
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. const int screenWidth = 800;
  41. const int screenHeight = 450;
  42. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  43. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
  44. // Define the camera to look into our 3d world
  45. Camera camera = { 0 };
  46. camera.position = (Vector3){ 2.0f, 2.0f, 6.0f }; // Camera position
  47. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  48. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  49. camera.fovy = 45.0f; // Camera field-of-view Y
  50. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  51. // Load models
  52. Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32));
  53. Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  54. Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32));
  55. // Load models texture
  56. Texture texture = LoadTexture("resources/texel_checker.png");
  57. // Assign texture to default model material
  58. modelA.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  59. modelB.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  60. modelC.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  61. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting.vs", GLSL_VERSION),
  62. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  63. // Get some shader loactions
  64. shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  65. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  66. // ambient light level
  67. int ambientLoc = GetShaderLocation(shader, "ambient");
  68. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  69. float angle = 6.282f;
  70. // All models use the same shader
  71. modelA.materials[0].shader = shader;
  72. modelB.materials[0].shader = shader;
  73. modelC.materials[0].shader = shader;
  74. // Using 4 point lights, white, red, green and blue
  75. Light lights[MAX_LIGHTS] = { 0 };
  76. lights[0] = CreateLight(LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), WHITE, shader);
  77. lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 4, 2, 4 }, Vector3Zero(), RED, shader);
  78. lights[2] = CreateLight(LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), GREEN, shader);
  79. lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 0, 4, 2 }, Vector3Zero(), BLUE, shader);
  80. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  81. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  82. //--------------------------------------------------------------------------------------
  83. // Main game loop
  84. while (!WindowShouldClose()) // Detect window close button or ESC key
  85. {
  86. // Update
  87. //----------------------------------------------------------------------------------
  88. if (IsKeyPressed(KEY_W)) { lights[0].enabled = !lights[0].enabled; }
  89. if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
  90. if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
  91. if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
  92. UpdateCamera(&camera); // Update camera
  93. // Make the lights do differing orbits
  94. angle -= 0.02f;
  95. lights[0].position.x = cosf(angle)*4.0f;
  96. lights[0].position.z = sinf(angle)*4.0f;
  97. lights[1].position.x = cosf(-angle*0.6f)*4.0f;
  98. lights[1].position.z = sinf(-angle*0.6f)*4.0f;
  99. lights[2].position.y = cosf(angle*0.2f)*4.0f;
  100. lights[2].position.z = sinf(angle*0.2f)*4.0f;
  101. lights[3].position.y = cosf(-angle*0.35f)*4.0f;
  102. lights[3].position.z = sinf(-angle*0.35f)*4.0f;
  103. UpdateLightValues(shader, lights[0]);
  104. UpdateLightValues(shader, lights[1]);
  105. UpdateLightValues(shader, lights[2]);
  106. UpdateLightValues(shader, lights[3]);
  107. // Rotate the torus
  108. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f));
  109. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f));
  110. // Update the light shader with the camera view position
  111. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  112. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  113. //----------------------------------------------------------------------------------
  114. // Draw
  115. //----------------------------------------------------------------------------------
  116. BeginDrawing();
  117. ClearBackground(RAYWHITE);
  118. BeginMode3D(camera);
  119. // Draw the three models
  120. DrawModel(modelA, Vector3Zero(), 1.0f, WHITE);
  121. DrawModel(modelB, (Vector3){-1.6,0,0}, 1.0f, WHITE);
  122. DrawModel(modelC, (Vector3){ 1.6,0,0}, 1.0f, WHITE);
  123. // Draw markers to show where the lights are
  124. if (lights[0].enabled) { DrawSphereEx(lights[0].position, 0.2f, 8, 8, WHITE); }
  125. if (lights[1].enabled) { DrawSphereEx(lights[1].position, 0.2f, 8, 8, RED); }
  126. if (lights[2].enabled) { DrawSphereEx(lights[2].position, 0.2f, 8, 8, GREEN); }
  127. if (lights[3].enabled) { DrawSphereEx(lights[3].position, 0.2f, 8, 8, BLUE); }
  128. DrawGrid(10, 1.0f);
  129. EndMode3D();
  130. DrawFPS(10, 10);
  131. DrawText("Use keys RGBW to toggle lights", 10, 30, 20, DARKGRAY);
  132. EndDrawing();
  133. //----------------------------------------------------------------------------------
  134. }
  135. // De-Initialization
  136. //--------------------------------------------------------------------------------------
  137. UnloadModel(modelA); // Unload the modelA
  138. UnloadModel(modelB); // Unload the modelB
  139. UnloadModel(modelC); // Unload the modelC
  140. UnloadTexture(texture); // Unload the texture
  141. UnloadShader(shader); // Unload shader
  142. CloseWindow(); // Close window and OpenGL context
  143. //--------------------------------------------------------------------------------------
  144. return 0;
  145. }