shaders_lightmap.c 6.7 KB

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