shaders_lightmap_rendering.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 10
  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. SetTextureFilter(lightmap.texture, TEXTURE_FILTER_TRILINEAR);
  75. Material material = LoadMaterialDefault();
  76. material.shader = shader;
  77. material.maps[MATERIAL_MAP_ALBEDO].texture = texture;
  78. material.maps[MATERIAL_MAP_METALNESS].texture = lightmap.texture;
  79. // Drawing to lightmap
  80. BeginTextureMode(lightmap);
  81. ClearBackground(BLACK);
  82. BeginBlendMode(BLEND_ADDITIVE);
  83. DrawTexturePro(
  84. light,
  85. (Rectangle){ 0, 0, light.width, light.height },
  86. (Rectangle){ 0, 0, 20, 20 },
  87. (Vector2){ 10.0, 10.0 },
  88. 0.0,
  89. RED
  90. );
  91. DrawTexturePro(
  92. light,
  93. (Rectangle){ 0, 0, light.width, light.height },
  94. (Rectangle){ 8, 4, 20, 20 },
  95. (Vector2){ 10.0, 10.0 },
  96. 0.0,
  97. BLUE
  98. );
  99. DrawTexturePro(
  100. light,
  101. (Rectangle){ 0, 0, light.width, light.height },
  102. (Rectangle){ 8, 8, 10, 10 },
  103. (Vector2){ 5.0, 5.0 },
  104. 0.0,
  105. GREEN
  106. );
  107. BeginBlendMode(BLEND_ALPHA);
  108. EndTextureMode();
  109. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  110. //--------------------------------------------------------------------------------------
  111. // Main game loop
  112. while (!WindowShouldClose()) // Detect window close button or ESC key
  113. {
  114. // Update
  115. //----------------------------------------------------------------------------------
  116. UpdateCamera(&camera, CAMERA_ORBITAL);
  117. //----------------------------------------------------------------------------------
  118. // Draw
  119. //----------------------------------------------------------------------------------
  120. BeginDrawing();
  121. ClearBackground(RAYWHITE);
  122. BeginMode3D(camera);
  123. DrawMesh(mesh, material, MatrixIdentity());
  124. EndMode3D();
  125. DrawFPS(10, 10);
  126. DrawTexturePro(
  127. lightmap.texture,
  128. (Rectangle){ 0, 0, -MAP_SIZE, -MAP_SIZE },
  129. (Rectangle){ GetRenderWidth() - MAP_SIZE*8 - 10, 10, MAP_SIZE*8, MAP_SIZE*8 },
  130. (Vector2){ 0.0, 0.0 },
  131. 0.0,
  132. WHITE);
  133. DrawText("lightmap", GetRenderWidth() - 66, 16 + MAP_SIZE*8, 10, GRAY);
  134. DrawText("10x10 pixels", GetRenderWidth() - 76, 30 + MAP_SIZE*8, 10, GRAY);
  135. EndDrawing();
  136. //----------------------------------------------------------------------------------
  137. }
  138. // De-Initialization
  139. //--------------------------------------------------------------------------------------
  140. UnloadMesh(mesh); // Unload the mesh
  141. UnloadShader(shader); // Unload shader
  142. UnloadTexture(texture); // Unload texture
  143. UnloadTexture(light); // Unload texture
  144. CloseWindow(); // Close window and OpenGL context
  145. //--------------------------------------------------------------------------------------
  146. return 0;
  147. }