aboutsummaryrefslogtreecommitdiff
path: root/examples/shaders
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2019-04-11 16:53:20 +0200
committerRay <raysan5@gmail.com>2019-04-11 16:53:20 +0200
commit129703fad18b478d015a520524d46ab4afa2cb79 (patch)
tree4155ff93f5f118ae50f11f7dcb6cc7644664f085 /examples/shaders
parent6fc97643bf564ec2f616ba3114230c78ed1c265e (diff)
downloadraylib-129703fad18b478d015a520524d46ab4afa2cb79.tar.gz
raylib-129703fad18b478d015a520524d46ab4afa2cb79.zip
new example: shaders_texture_drawing
Diffstat (limited to 'examples/shaders')
-rw-r--r--examples/shaders/resources/shaders/glsl330/cubes_panning.fs61
-rw-r--r--examples/shaders/shaders_texture_drawing.c71
-rw-r--r--examples/shaders/shaders_texture_drawing.pngbin0 -> 16865 bytes
3 files changed, 132 insertions, 0 deletions
diff --git a/examples/shaders/resources/shaders/glsl330/cubes_panning.fs b/examples/shaders/resources/shaders/glsl330/cubes_panning.fs
new file mode 100644
index 00000000..c92418a4
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl330/cubes_panning.fs
@@ -0,0 +1,61 @@
+#version 330
+
+// Input vertex attributes (from vertex shader)
+in vec2 fragTexCoord;
+in vec4 fragColor;
+
+// Output fragment color
+out vec4 finalColor;
+
+// Custom variables
+#define PI 3.14159265358979323846
+uniform float uTime = 0.0;
+
+float divisions = 5.0;
+float angle = 0.0;
+
+vec2 VectorRotateTime(vec2 v, float speed)
+{
+ float time = uTime*speed;
+ float localTime = fract(time); // The time domain this works on is 1 sec.
+
+ if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0;
+ else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4*sin(2*PI*localTime - PI/2);
+ else if ((localTime >= 0.50) && (localTime < 0.75)) angle = PI*0.25;
+ else if ((localTime >= 0.75) && (localTime < 1.00)) angle = PI/4*sin(2*PI*localTime);
+
+ // Rotate vector by angle
+ v -= 0.5;
+ v = mat2(cos(angle), -sin(angle), sin(angle), cos(angle))*v;
+ v += 0.5;
+
+ return v;
+}
+
+float Rectangle(in vec2 st, in float size, in float fill)
+{
+ float roundSize = 0.5 - size/2.0;
+ float left = step(roundSize, st.x);
+ float top = step(roundSize, st.y);
+ float bottom = step(roundSize, 1.0 - st.y);
+ float right = step(roundSize, 1.0 - st.x);
+
+ return (left*bottom*right*top)*fill;
+}
+
+void main()
+{
+ vec2 fragPos = fragTexCoord;
+ fragPos.xy += uTime/9.0;
+
+ fragPos *= divisions;
+ vec2 ipos = floor(fragPos); // Get the integer coords
+ vec2 fpos = fract(fragPos); // Get the fractional coords
+
+ fpos = VectorRotateTime(fpos, 0.2);
+
+ float alpha = Rectangle(fpos, 0.216, 1.0);
+ vec3 color = vec3(0.3, 0.3, 0.3);
+
+ finalColor = vec4(color, alpha);
+} \ No newline at end of file
diff --git a/examples/shaders/shaders_texture_drawing.c b/examples/shaders/shaders_texture_drawing.c
new file mode 100644
index 00000000..cb8a9c1e
--- /dev/null
+++ b/examples/shaders/shaders_texture_drawing.c
@@ -0,0 +1,71 @@
+/*******************************************************************************************
+*
+* raylib [textures] example - Shader texture drawing
+*
+* This example illustrates how to draw on a blank texture using a shader
+*
+* This example has been created using raylib 2.0 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2019 MichaƂ Ciesielski and Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shader texture drawing");
+
+ Image imBlank = GenImageColor(1024, 1024, BLANK);
+ Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader
+ UnloadImage(imBlank);
+
+ // NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
+ Shader shader = LoadShader(0, "resources/shaders/glsl330/cubes_panning.fs");
+
+ float time = 0.0f;
+ int timeLoc = GetShaderLocation(shader, "uTime");
+ SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT);
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+ while (!WindowShouldClose())
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ time = GetTime();
+ SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT);
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings
+ DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all magic happens on shader
+ EndShaderMode(); // Disable our custom shader, return to default shader
+
+ DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadShader(shader);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
diff --git a/examples/shaders/shaders_texture_drawing.png b/examples/shaders/shaders_texture_drawing.png
new file mode 100644
index 00000000..12df6fae
--- /dev/null
+++ b/examples/shaders/shaders_texture_drawing.png
Binary files differ