summaryrefslogtreecommitdiffhomepage
path: root/examples/shaders
diff options
context:
space:
mode:
authorJussi Viitala <[email protected]>2023-05-04 21:21:35 +0300
committerGitHub <[email protected]>2023-05-04 20:21:35 +0200
commitabcbd9817e9163bd10008b42848dfa2d72ebb6c5 (patch)
tree2030c069461652196caf5f9fb7c64cc226e26145 /examples/shaders
parenta48bb6e1ed7b33190e486ba65b7875f0dff73701 (diff)
downloadraylib-abcbd9817e9163bd10008b42848dfa2d72ebb6c5.tar.gz
raylib-abcbd9817e9163bd10008b42848dfa2d72ebb6c5.zip
Lightmap example. (#3043)
Diffstat (limited to 'examples/shaders')
-rw-r--r--examples/shaders/resources/LICENSE.md2
-rw-r--r--examples/shaders/resources/cubicmap_atlas.pngbin0 -> 37160 bytes
-rw-r--r--examples/shaders/resources/shaders/glsl330/lightmap.fs20
-rw-r--r--examples/shaders/resources/shaders/glsl330/lightmap.vs27
-rw-r--r--examples/shaders/resources/spark_flame.pngbin0 -> 7537 bytes
-rw-r--r--examples/shaders/shaders_lightmap.c169
-rw-r--r--examples/shaders/shaders_lightmap.pngbin0 -> 229671 bytes
7 files changed, 218 insertions, 0 deletions
diff --git a/examples/shaders/resources/LICENSE.md b/examples/shaders/resources/LICENSE.md
index 96458eca..7210e34b 100644
--- a/examples/shaders/resources/LICENSE.md
+++ b/examples/shaders/resources/LICENSE.md
@@ -9,3 +9,5 @@
| raysan.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
| space.png | ❔ | ❔ | - |
| texel_checker.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Made with [UV Checker Map Maker](http://uvchecker.byvalle.com/) |
+| cubicmap.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
+| spark_flame.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Made with [EffectTextureMaker](https://mebiusbox.github.io/contents/EffectTextureMaker/) | \ No newline at end of file
diff --git a/examples/shaders/resources/cubicmap_atlas.png b/examples/shaders/resources/cubicmap_atlas.png
new file mode 100644
index 00000000..9fc404a7
--- /dev/null
+++ b/examples/shaders/resources/cubicmap_atlas.png
Binary files differ
diff --git a/examples/shaders/resources/shaders/glsl330/lightmap.fs b/examples/shaders/resources/shaders/glsl330/lightmap.fs
new file mode 100644
index 00000000..95558610
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/lightmap.fs
@@ -0,0 +1,20 @@
+#version 330
+// Input vertex attributes (from vertex shader)
+in vec2 fragTexCoord;
+in vec2 fragTexCoord2;
+in vec3 fragPosition;
+in vec4 fragColor;
+
+// Input uniform values
+uniform sampler2D texture0;
+uniform sampler2D texture1;
+
+// Output fragment color
+out vec4 finalColor;
+
+void main() {
+ // Texel color fetching from texture sampler
+ vec4 texelColor = texture( texture0, fragTexCoord );
+ vec4 texelColor2 = texture( texture1, fragTexCoord2 );
+ finalColor = texelColor * texelColor2;
+}
diff --git a/examples/shaders/resources/shaders/glsl330/lightmap.vs b/examples/shaders/resources/shaders/glsl330/lightmap.vs
new file mode 100644
index 00000000..00278eaa
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/lightmap.vs
@@ -0,0 +1,27 @@
+#version 330
+// Input vertex attributes
+in vec3 vertexPosition;
+in vec2 vertexTexCoord;
+in vec2 vertexTexCoord2;
+in vec4 vertexColor;
+
+// Input uniform values
+uniform mat4 mvp;
+uniform mat4 matModel;
+
+// Output vertex attributes (to fragment shader)
+out vec3 fragPosition;
+out vec2 fragTexCoord;
+out vec2 fragTexCoord2;
+out vec4 fragColor;
+
+void main() {
+ // Send vertex attributes to fragment shader
+ fragPosition = vec3( matModel * vec4( vertexPosition, 1.0 ) );
+ fragTexCoord = vertexTexCoord;
+ fragTexCoord2 = vertexTexCoord2;
+ fragColor = vertexColor;
+
+ // Calculate final vertex position
+ gl_Position = mvp * vec4( vertexPosition, 1.0 );
+}
diff --git a/examples/shaders/resources/spark_flame.png b/examples/shaders/resources/spark_flame.png
new file mode 100644
index 00000000..72cea2e9
--- /dev/null
+++ b/examples/shaders/resources/spark_flame.png
Binary files differ
diff --git a/examples/shaders/shaders_lightmap.c b/examples/shaders/shaders_lightmap.c
new file mode 100644
index 00000000..a37c9922
--- /dev/null
+++ b/examples/shaders/shaders_lightmap.c
@@ -0,0 +1,169 @@
+/*******************************************************************************************
+*
+* raylib [shaders] example - lightmap
+*
+* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
+* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
+*
+* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
+*
+* Example contributed by Jussi Viitala (@nullstare) and reviewed by Ramon Santamaria (@raysan5)
+*
+* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
+* BSD-like license that allows static linking with closed source software
+*
+* Copyright (c) 2019-2023 Jussi Viitala (@nullstare) and Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "raylib.h"
+#include "raymath.h"
+#include "rlgl.h"
+
+#if defined(PLATFORM_DESKTOP)
+ #define GLSL_VERSION 330
+#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
+ #define GLSL_VERSION 100
+#endif
+
+#define MAP_SIZE 10
+
+//------------------------------------------------------------------------------------
+// Program main entry point
+//------------------------------------------------------------------------------------
+int main(void)
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
+ InitWindow(screenWidth, screenHeight, "raylib [shaders] example - lightmap");
+
+ // Define the camera to look into our 3d world
+ Camera camera = { 0 };
+ camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position
+ camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
+ camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
+ camera.fovy = 45.0f; // Camera field-of-view Y
+ camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
+
+ Mesh mesh = GenMeshPlane((float)MAP_SIZE, (float)MAP_SIZE, 1, 1);
+
+ // GenMeshPlane doesn't generate texcoords2 so we will upload them separately
+ mesh.texcoords2 = (float *)RL_MALLOC(mesh.vertexCount * 2 * sizeof(float));
+
+ // X // Y
+ mesh.texcoords2[0] = 0.0f; mesh.texcoords2[1] = 0.0f;
+ mesh.texcoords2[2] = 1.0f; mesh.texcoords2[3] = 0.0f;
+ mesh.texcoords2[4] = 0.0f; mesh.texcoords2[5] = 1.0f;
+ mesh.texcoords2[6] = 1.0f; mesh.texcoords2[7] = 1.0f;
+
+ // Load a new texcoords2 attributes buffer
+ mesh.vboId[SHADER_LOC_VERTEX_TEXCOORD02] = rlLoadVertexBuffer(mesh.texcoords2, mesh.vertexCount*2*sizeof(float), false);
+ rlEnableVertexArray(mesh.vaoId);
+ // Index 5 is for texcoords2
+ rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
+ rlEnableVertexAttribute(5);
+ rlDisableVertexArray();
+
+ // Load lightmap shader
+ Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lightmap.vs", GLSL_VERSION),
+ TextFormat("resources/shaders/glsl%i/lightmap.fs", GLSL_VERSION));
+
+ Texture texture = LoadTexture("resources/cubicmap_atlas.png");
+ Texture light = LoadTexture("resources/spark_flame.png");
+
+ GenTextureMipmaps(&texture);
+ SetTextureFilter(texture, TEXTURE_FILTER_TRILINEAR);
+
+ RenderTexture lightmap = LoadRenderTexture(MAP_SIZE, MAP_SIZE);
+
+ SetTextureFilter(lightmap.texture, TEXTURE_FILTER_TRILINEAR);
+
+ Material material = LoadMaterialDefault();
+ material.shader = shader;
+ material.maps[MATERIAL_MAP_ALBEDO].texture = texture;
+ material.maps[MATERIAL_MAP_METALNESS].texture = lightmap.texture;
+
+ // Drawing to lightmap
+ BeginTextureMode(lightmap);
+ ClearBackground(BLACK);
+
+ BeginBlendMode(BLEND_ADDITIVE);
+ DrawTexturePro(
+ light,
+ (Rectangle){ 0, 0, light.width, light.height },
+ (Rectangle){ 0, 0, 20, 20 },
+ (Vector2){ 10.0, 10.0 },
+ 0.0,
+ RED
+ );
+ DrawTexturePro(
+ light,
+ (Rectangle){ 0, 0, light.width, light.height },
+ (Rectangle){ 8, 4, 20, 20 },
+ (Vector2){ 10.0, 10.0 },
+ 0.0,
+ BLUE
+ );
+ DrawTexturePro(
+ light,
+ (Rectangle){ 0, 0, light.width, light.height },
+ (Rectangle){ 8, 8, 10, 10 },
+ (Vector2){ 5.0, 5.0 },
+ 0.0,
+ GREEN
+ );
+ BeginBlendMode(BLEND_ALPHA);
+ EndTextureMode();
+
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ UpdateCamera(&camera, CAMERA_ORBITAL);
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+ ClearBackground(RAYWHITE);
+
+ BeginMode3D(camera);
+ DrawMesh(mesh, material, MatrixIdentity());
+ EndMode3D();
+
+ DrawFPS(10, 10);
+
+ DrawTexturePro(
+ lightmap.texture,
+ (Rectangle){ 0, 0, MAP_SIZE, MAP_SIZE },
+ (Rectangle){ 0, 36, MAP_SIZE * 4, MAP_SIZE * 4 },
+ (Vector2){ 0.0, 0.0 },
+ 0.0,
+ WHITE
+ );
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadMesh(mesh); // Unload the mesh
+ UnloadShader(shader); // Unload shader
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
diff --git a/examples/shaders/shaders_lightmap.png b/examples/shaders/shaders_lightmap.png
new file mode 100644
index 00000000..bdd8d3b9
--- /dev/null
+++ b/examples/shaders/shaders_lightmap.png
Binary files differ