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.

174 lines
6.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - lightmap
  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. * Example contributed by Jussi Viitala (@nullstare) and reviewed by Ramon Santamaria (@raysan5)
  11. *
  12. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  13. * BSD-like license that allows static linking with closed source software
  14. *
  15. * Copyright (c) 2019-2024 Jussi Viitala (@nullstare) and Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "raylib.h"
  21. #include "raymath.h"
  22. #include "rlgl.h"
  23. #if defined(PLATFORM_DESKTOP)
  24. #define GLSL_VERSION 330
  25. #else // PLATFORM_ANDROID, PLATFORM_WEB
  26. #define GLSL_VERSION 100
  27. #endif
  28. #define MAP_SIZE 10
  29. //------------------------------------------------------------------------------------
  30. // Program main entry point
  31. //------------------------------------------------------------------------------------
  32. int main(void)
  33. {
  34. // Initialization
  35. //--------------------------------------------------------------------------------------
  36. const int screenWidth = 800;
  37. const int screenHeight = 450;
  38. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  39. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - lightmap");
  40. // Define the camera to look into our 3d world
  41. Camera camera = { 0 };
  42. camera.position = (Vector3){ 4.0f, 6.0f, 8.0f }; // Camera position
  43. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  44. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  45. camera.fovy = 45.0f; // Camera field-of-view Y
  46. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  47. Mesh mesh = GenMeshPlane((float)MAP_SIZE, (float)MAP_SIZE, 1, 1);
  48. // GenMeshPlane doesn't generate texcoords2 so we will upload them separately
  49. mesh.texcoords2 = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
  50. // X // Y
  51. mesh.texcoords2[0] = 0.0f; mesh.texcoords2[1] = 0.0f;
  52. mesh.texcoords2[2] = 1.0f; mesh.texcoords2[3] = 0.0f;
  53. mesh.texcoords2[4] = 0.0f; mesh.texcoords2[5] = 1.0f;
  54. mesh.texcoords2[6] = 1.0f; mesh.texcoords2[7] = 1.0f;
  55. // Load a new texcoords2 attributes buffer
  56. mesh.vboId[SHADER_LOC_VERTEX_TEXCOORD02] = rlLoadVertexBuffer(mesh.texcoords2, mesh.vertexCount*2*sizeof(float), false);
  57. rlEnableVertexArray(mesh.vaoId);
  58. // Index 5 is for texcoords2
  59. rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
  60. rlEnableVertexAttribute(5);
  61. rlDisableVertexArray();
  62. // Load lightmap shader
  63. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lightmap.vs", GLSL_VERSION),
  64. TextFormat("resources/shaders/glsl%i/lightmap.fs", GLSL_VERSION));
  65. Texture texture = LoadTexture("resources/cubicmap_atlas.png");
  66. Texture light = LoadTexture("resources/spark_flame.png");
  67. GenTextureMipmaps(&texture);
  68. SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
  69. RenderTexture lightmap = LoadRenderTexture(MAP_SIZE, MAP_SIZE);
  70. SetTextureFilter(lightmap.texture, TEXTURE_FILTER_TRILINEAR);
  71. Material material = LoadMaterialDefault();
  72. material.shader = shader;
  73. material.maps[MATERIAL_MAP_ALBEDO].texture = texture;
  74. material.maps[MATERIAL_MAP_METALNESS].texture = lightmap.texture;
  75. // Drawing to lightmap
  76. BeginTextureMode(lightmap);
  77. ClearBackground(BLACK);
  78. BeginBlendMode(BLEND_ADDITIVE);
  79. DrawTexturePro(
  80. light,
  81. (Rectangle){ 0, 0, light.width, light.height },
  82. (Rectangle){ 0, 0, 20, 20 },
  83. (Vector2){ 10.0, 10.0 },
  84. 0.0,
  85. RED
  86. );
  87. DrawTexturePro(
  88. light,
  89. (Rectangle){ 0, 0, light.width, light.height },
  90. (Rectangle){ 8, 4, 20, 20 },
  91. (Vector2){ 10.0, 10.0 },
  92. 0.0,
  93. BLUE
  94. );
  95. DrawTexturePro(
  96. light,
  97. (Rectangle){ 0, 0, light.width, light.height },
  98. (Rectangle){ 8, 8, 10, 10 },
  99. (Vector2){ 5.0, 5.0 },
  100. 0.0,
  101. GREEN
  102. );
  103. BeginBlendMode(BLEND_ALPHA);
  104. EndTextureMode();
  105. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  106. //--------------------------------------------------------------------------------------
  107. // Main game loop
  108. while (!WindowShouldClose()) // Detect window close button or ESC key
  109. {
  110. // Update
  111. //----------------------------------------------------------------------------------
  112. UpdateCamera(&camera, CAMERA_ORBITAL);
  113. //----------------------------------------------------------------------------------
  114. // Draw
  115. //----------------------------------------------------------------------------------
  116. BeginDrawing();
  117. ClearBackground(RAYWHITE);
  118. BeginMode3D(camera);
  119. DrawMesh(mesh, material, MatrixIdentity());
  120. EndMode3D();
  121. DrawFPS(10, 10);
  122. DrawTexturePro(
  123. lightmap.texture,
  124. (Rectangle){ 0, 0, -MAP_SIZE, -MAP_SIZE },
  125. (Rectangle){ GetRenderWidth() - MAP_SIZE*8 - 10, 10, MAP_SIZE*8, MAP_SIZE*8 },
  126. (Vector2){ 0.0, 0.0 },
  127. 0.0,
  128. WHITE);
  129. DrawText("lightmap", GetRenderWidth() - 66, 16 + MAP_SIZE*8, 10, GRAY);
  130. DrawText("10x10 pixels", GetRenderWidth() - 76, 30 + MAP_SIZE*8, 10, GRAY);
  131. EndDrawing();
  132. //----------------------------------------------------------------------------------
  133. }
  134. // De-Initialization
  135. //--------------------------------------------------------------------------------------
  136. UnloadMesh(mesh); // Unload the mesh
  137. UnloadShader(shader); // Unload shader
  138. UnloadTexture(texture); // Unload texture
  139. UnloadTexture(light); // Unload texture
  140. CloseWindow(); // Close window and OpenGL context
  141. //--------------------------------------------------------------------------------------
  142. return 0;
  143. }