summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorchriscamacho <[email protected]>2019-09-22 20:28:50 +0100
committerRay <[email protected]>2019-09-22 21:28:50 +0200
commita679b0ccc015295ed8c305605b0bf19112f949f3 (patch)
tree6763eee4263e93a2c1ab05964236f02bf5339db5 /examples
parentacedf4a0d5f2cb721dc12cf56a176fe420e63d2e (diff)
downloadraylib-a679b0ccc015295ed8c305605b0bf19112f949f3.tar.gz
raylib-a679b0ccc015295ed8c305605b0bf19112f949f3.zip
contributed simple shader example (#973)
Contributed simple shader example
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile2
-rw-r--r--examples/shaders/resources/mask.pngbin0 -> 74820 bytes
-rw-r--r--examples/shaders/resources/plasma.pngbin0 -> 581880 bytes
-rw-r--r--examples/shaders/resources/shaders/glsl330/mask.fs21
-rw-r--r--examples/shaders/resources/shaders/glsl330/mask.vs21
-rw-r--r--examples/shaders/shaders_simple.c153
6 files changed, 197 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile
index 278cdee3..c4e7c57c 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -437,6 +437,8 @@ EXAMPLES = \
shaders/shaders_julia_set \
shaders/shaders_eratosthenes \
shaders/shaders_basic_lighting \
+ shaders/shaders_fog \
+ shaders/shaders_simple \
audio/audio_module_playing \
audio/audio_music_stream \
audio/audio_raw_stream \
diff --git a/examples/shaders/resources/mask.png b/examples/shaders/resources/mask.png
new file mode 100644
index 00000000..06a25978
--- /dev/null
+++ b/examples/shaders/resources/mask.png
Binary files differ
diff --git a/examples/shaders/resources/plasma.png b/examples/shaders/resources/plasma.png
new file mode 100644
index 00000000..01c2d883
--- /dev/null
+++ b/examples/shaders/resources/plasma.png
Binary files differ
diff --git a/examples/shaders/resources/shaders/glsl330/mask.fs b/examples/shaders/resources/shaders/glsl330/mask.fs
new file mode 100644
index 00000000..a0627909
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/mask.fs
@@ -0,0 +1,21 @@
+#version 330
+
+// Input vertex attributes (from vertex shader)
+in vec2 fragTexCoord;
+
+// Input uniform values
+uniform sampler2D texture0;
+uniform sampler2D mask;
+uniform int frame;
+
+// Output fragment color
+out vec4 finalColor;
+
+void main()
+{
+ vec4 maskColour = texture(mask, fragTexCoord+vec2(sin(-frame/150.0)/10.0,cos(-frame/170.0)/10.0));
+ if (maskColour.r < 0.25) discard;
+ vec4 texelColor = texture(texture0, fragTexCoord+vec2(sin(frame/90.0)/8.0,cos(frame/60.0)/8.0));
+
+ finalColor = texelColor * maskColour;
+}
diff --git a/examples/shaders/resources/shaders/glsl330/mask.vs b/examples/shaders/resources/shaders/glsl330/mask.vs
new file mode 100644
index 00000000..66a15161
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/mask.vs
@@ -0,0 +1,21 @@
+#version 330
+
+// Input vertex attributes
+in vec3 vertexPosition;
+in vec2 vertexTexCoord;
+
+// Input uniform values
+uniform mat4 mvp;
+uniform mat4 matModel;
+
+// Output vertex attributes (to fragment shader)
+out vec2 fragTexCoord;
+
+void main()
+{
+ // Send vertex attributes to fragment shader
+ fragTexCoord = vertexTexCoord;
+
+ // Calculate final vertex position
+ gl_Position = mvp*vec4(vertexPosition, 1.0);
+}
diff --git a/examples/shaders/shaders_simple.c b/examples/shaders/shaders_simple.c
new file mode 100644
index 00000000..cf57e126
--- /dev/null
+++ b/examples/shaders/shaders_simple.c
@@ -0,0 +1,153 @@
+/*******************************************************************************************
+*
+* raylib [shaders] example - demonstrates how you can use your own simple shaders in raylib
+*
+* This example has been created using raylib 2.5 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
+*
+* Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
+*
+********************************************************************************************
+*
+* after a model is loaded it has a default material, this material can be modified in place
+* rather than creating one from scratch...
+* While all of the MAPs have particular names, they can be used for any purpose
+* Three of the MAP are applied as cubic maps (see below)
+*
+********************************************************************************************/
+
+
+#include <stddef.h>
+
+#include "raylib.h"
+#include "raymath.h"
+
+
+#define screenWidth 1280
+#define screenHeight 720
+
+
+int main(void)
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ InitWindow(screenWidth, screenHeight, "raylib - simple shader");
+
+ // Define the camera to look into our 3d world
+ Camera camera = { 0 };
+ camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
+ camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
+ camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
+ camera.fovy = 45.0f;
+ camera.type = CAMERA_PERSPECTIVE;
+
+ // three models to show the shader on
+ Mesh torus = GenMeshTorus(.3, 1, 16, 32);
+ Model model1 = LoadModelFromMesh(torus);
+
+ Mesh cube = GenMeshCube(.8,.8,.8);
+ Model model2 = LoadModelFromMesh(cube);
+
+ // this one un shaded just so we can see the gaps in the other two
+ Mesh sphere = GenMeshSphere(1, 16, 16);
+ Model model3 = LoadModelFromMesh(sphere);
+
+ // load the shader
+ Shader shader = LoadShader("resources/shaders/glsl330/mask.vs",
+ "resources/shaders/glsl330/mask.fs");
+
+ // apply the diffuse texture (colour map)
+ Texture tex = LoadTexture("resources/plasma.png");
+ model1.materials[0].maps[MAP_DIFFUSE].texture = tex;
+ model2.materials[0].maps[MAP_DIFFUSE].texture = tex;
+
+ // using MAP_EMISSION as a spare slot to use for 2nd texture
+ // dont use MAP_IRRADIANCE, MAP_PREFILTER, or MAP_CUBEMAP
+ // as they are bound as cube maps (which don't see to work at all on my machine!)
+ Texture maskTx = LoadTexture("resources/mask.png");
+ model1.materials[0].maps[MAP_EMISSION].texture = maskTx;
+ model2.materials[0].maps[MAP_EMISSION].texture = maskTx;
+ shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(shader, "mask");
+
+ // frame is incremented each frame to animate the shader
+ int shaderFrame = GetShaderLocation(shader, "frame");
+
+ // apply the shader to the two models
+ model1.materials[0].shader = shader;
+ model2.materials[0].shader = shader;
+
+
+ // frame counter
+ int frame = 0;
+
+ // model rotation
+ Vector3 ang = { 0 };
+
+ SetTargetFPS(60); // Set to run at 60 frames-per-second
+ //--------------------------------------------------------------------------------------
+
+
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+
+ frame ++;
+ ang.x += 0.01;
+ ang.y += 0.005;
+ ang.z -= 0.0025;
+
+ // animate the shader
+ SetShaderValue(shader, shaderFrame, &frame, UNIFORM_INT);
+
+ // rotate one of the models
+ model1.transform = MatrixRotateXYZ(ang);
+
+ UpdateCamera(&camera);
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(DARKBLUE);
+
+ BeginMode3D(camera);
+
+ DrawModel(model1, (Vector3){0.5,0,0}, 1, WHITE);
+ DrawModelEx(model2, (Vector3){-.5,0,0}, (Vector3){1,1,0}, 50, (Vector3){1,1,1}, WHITE);
+ DrawModel(model3,(Vector3){0,0,-1.5}, 1, WHITE);
+ DrawGrid(10, 1.0f); // Draw a grid
+
+ EndMode3D();
+
+ DrawFPS(10, 10);
+
+ int l = MeasureText(FormatText("Frame %i", frame), 20);
+ DrawRectangle(16, 698, l+8, 42, BLUE);
+ DrawText(FormatText("Frame %i", frame), 20, 700, 20, WHITE);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+
+ UnloadModel(model1);
+ UnloadModel(model2);
+ UnloadModel(model3);
+ UnloadTexture(tex);
+ UnloadTexture(maskTx);
+ UnloadShader(shader);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+