shaders_lightmap_rendering.c 7.0 KB

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