From be6d237b9ebbe245de4384c17b84e75dab0f4981 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 29 Mar 2019 20:22:50 +0100 Subject: Review models examples --- examples/shaders/shaders_custom_uniform.c | 2 +- examples/shaders/shaders_model_shader.c | 4 ++-- examples/shaders/shaders_postprocessing.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index fbfd82d0..74b9e771 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -45,7 +45,7 @@ int main() Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map) - model.material.maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position diff --git a/examples/shaders/shaders_model_shader.c b/examples/shaders/shaders_model_shader.c index 6c64f0ef..2717c192 100644 --- a/examples/shaders/shaders_model_shader.c +++ b/examples/shaders/shaders_model_shader.c @@ -50,8 +50,8 @@ int main() // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); - model.material.shader = shader; // Set shader effect to 3d model - model.material.maps[MAP_DIFFUSE].texture = texture; // Bind texture to model + model.materials[0].shader = shader; // Set shader effect to 3d model + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index f8483563..7c146419 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -74,7 +74,7 @@ int main() Model model = LoadModel("resources/models/church.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map) - model.material.maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture + model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position -- cgit v1.2.3 From 129703fad18b478d015a520524d46ab4afa2cb79 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 11 Apr 2019 16:53:20 +0200 Subject: new example: shaders_texture_drawing --- .../resources/shaders/glsl330/cubes_panning.fs | 61 ++++++++++++++++++ examples/shaders/shaders_texture_drawing.c | 71 +++++++++++++++++++++ examples/shaders/shaders_texture_drawing.png | Bin 0 -> 16865 bytes 3 files changed, 132 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl330/cubes_panning.fs create mode 100644 examples/shaders/shaders_texture_drawing.c create mode 100644 examples/shaders/shaders_texture_drawing.png (limited to 'examples/shaders') 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 Binary files /dev/null and b/examples/shaders/shaders_texture_drawing.png differ -- cgit v1.2.3 From 2ddc4bacba8fc6809de924c3d6912d4812226231 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 6 May 2019 09:41:54 +0200 Subject: Example review --- .../resources/shaders/glsl100/palette-switch.fs | 29 ----------- .../resources/shaders/glsl100/palette_switch.fs | 29 +++++++++++ .../resources/shaders/glsl120/palette-switch.fs | 27 ---------- .../resources/shaders/glsl120/palette_switch.fs | 27 ++++++++++ .../resources/shaders/glsl330/palette-switch.fs | 30 ----------- .../resources/shaders/glsl330/palette_switch.fs | 30 +++++++++++ examples/shaders/shaders_palette_switch.c | 60 ++++++++-------------- 7 files changed, 106 insertions(+), 126 deletions(-) delete mode 100644 examples/shaders/resources/shaders/glsl100/palette-switch.fs create mode 100644 examples/shaders/resources/shaders/glsl100/palette_switch.fs delete mode 100644 examples/shaders/resources/shaders/glsl120/palette-switch.fs create mode 100644 examples/shaders/resources/shaders/glsl120/palette_switch.fs delete mode 100644 examples/shaders/resources/shaders/glsl330/palette-switch.fs create mode 100644 examples/shaders/resources/shaders/glsl330/palette_switch.fs (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl100/palette-switch.fs b/examples/shaders/resources/shaders/glsl100/palette-switch.fs deleted file mode 100644 index 65a7bd29..00000000 --- a/examples/shaders/resources/shaders/glsl100/palette-switch.fs +++ /dev/null @@ -1,29 +0,0 @@ -#version 100 - -precision mediump float; - -const int colors = 8; - -// Input vertex attributes (from vertex shader) -varying vec2 fragTexCoord; -varying vec4 fragColor; - -// Input uniform values -uniform sampler2D texture0; -uniform ivec3 palette[colors]; - -void main() -{ - // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; - - // Convert the (normalized) texel color RED component (GB would work, too) - // to the palette index by scaling up from [0, 1] to [0, 255]. - int index = int(texelColor.r * 255.0); - ivec3 color = palette[index]; - - // Calculate final fragment color. Note that the palette color components - // are defined in the range [0, 255] and need to be normalized to [0, 1] - // for OpenGL to work. - gl_FragColor = vec4(color / 255.0, texelColor.a); -} diff --git a/examples/shaders/resources/shaders/glsl100/palette_switch.fs b/examples/shaders/resources/shaders/glsl100/palette_switch.fs new file mode 100644 index 00000000..7f09137e --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/palette_switch.fs @@ -0,0 +1,29 @@ +#version 100 + +precision mediump float; + +const int colors = 8; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r * 255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + gl_FragColor = vec4(color / 255.0, texelColor.a); +} diff --git a/examples/shaders/resources/shaders/glsl120/palette-switch.fs b/examples/shaders/resources/shaders/glsl120/palette-switch.fs deleted file mode 100644 index b4384502..00000000 --- a/examples/shaders/resources/shaders/glsl120/palette-switch.fs +++ /dev/null @@ -1,27 +0,0 @@ -#version 120 - -const int colors = 8; - -// Input fragment attributes (from fragment shader) -varying vec2 fragTexCoord; -varying vec4 fragColor; - -// Input uniform values -uniform sampler2D texture0; -uniform ivec3 palette[colors]; - -void main() -{ - // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; - - // Convert the (normalized) texel color RED component (GB would work, too) - // to the palette index by scaling up from [0, 1] to [0, 255]. - int index = int(texelColor.r * 255.0); - ivec3 color = palette[index]; - - // Calculate final fragment color. Note that the palette color components - // are defined in the range [0, 255] and need to be normalized to [0, 1] - // for OpenGL to work. - gl_FragColor = vec4(color / 255.0, texelColor.a); -} diff --git a/examples/shaders/resources/shaders/glsl120/palette_switch.fs b/examples/shaders/resources/shaders/glsl120/palette_switch.fs new file mode 100644 index 00000000..ab3f79c7 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl120/palette_switch.fs @@ -0,0 +1,27 @@ +#version 120 + +const int colors = 8; + +// Input fragment attributes (from fragment shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r * 255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + gl_FragColor = vec4(color / 255.0, texelColor.a); +} diff --git a/examples/shaders/resources/shaders/glsl330/palette-switch.fs b/examples/shaders/resources/shaders/glsl330/palette-switch.fs deleted file mode 100644 index 61b532ed..00000000 --- a/examples/shaders/resources/shaders/glsl330/palette-switch.fs +++ /dev/null @@ -1,30 +0,0 @@ -#version 330 - -const int colors = 8; - -// Input fragment attributes (from fragment shader) -in vec2 fragTexCoord; -in vec4 fragColor; - -// Input uniform values -uniform sampler2D texture0; -uniform ivec3 palette[colors]; - -// Output fragment color -out vec4 finalColor; - -void main() -{ - // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord)*fragColor; - - // Convert the (normalized) texel color RED component (GB would work, too) - // to the palette index by scaling up from [0, 1] to [0, 255]. - int index = int(texelColor.r * 255.0); - ivec3 color = palette[index]; - - // Calculate final fragment color. Note that the palette color components - // are defined in the range [0, 255] and need to be normalized to [0, 1] - // for OpenGL to work. - finalColor = vec4(color / 255.0, texelColor.a); -} diff --git a/examples/shaders/resources/shaders/glsl330/palette_switch.fs b/examples/shaders/resources/shaders/glsl330/palette_switch.fs new file mode 100644 index 00000000..7c8a488c --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/palette_switch.fs @@ -0,0 +1,30 @@ +#version 330 + +const int colors = 8; + +// Input fragment attributes (from fragment shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform ivec3 palette[colors]; + +// Output fragment color +out vec4 finalColor; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture(texture0, fragTexCoord)*fragColor; + + // Convert the (normalized) texel color RED component (GB would work, too) + // to the palette index by scaling up from [0, 1] to [0, 255]. + int index = int(texelColor.r*255.0); + ivec3 color = palette[index]; + + // Calculate final fragment color. Note that the palette color components + // are defined in the range [0, 255] and need to be normalized to [0, 1] + // for OpenGL to work. + finalColor = vec4(color/255.0, texelColor.a); +} diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index d0b56190..a2fa0b27 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -29,7 +29,7 @@ #define VALUES_PER_COLOR 3 static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { - { + { // 3-BIT RGB 0, 0, 0, 255, 0, 0, 0, 255, 0, @@ -39,7 +39,7 @@ static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { 255, 255, 0, 255, 255, 255, }, - { + { // AMMO-8 (GameBoy-like) 4, 12, 6, 17, 35, 24, 30, 58, 41, @@ -49,7 +49,7 @@ static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { 190, 220, 127, 238, 255, 204, }, - { + { // RKBV (2-strip film) 21, 25, 26, 138, 76, 88, 217, 98, 117, @@ -85,8 +85,8 @@ int main() // NOTE: If uniform variable could not be found in the shader, function returns -1 int paletteLoc = GetShaderLocation(shader, "palette"); - // Initial index not set, will be automatically bounded below. - int currentPalette = -1; + int currentPalette = 0; + int lineHeight = screenHeight/COLORS_PER_PALETTE; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -96,21 +96,15 @@ int main() { // Update //---------------------------------------------------------------------------------- - int paletteIndex = currentPalette; - if (IsKeyPressed(KEY_RIGHT)) paletteIndex++; - else if (IsKeyPressed(KEY_LEFT)) paletteIndex--; + if (IsKeyPressed(KEY_RIGHT)) currentPalette++; + else if (IsKeyPressed(KEY_LEFT)) currentPalette--; - if (paletteIndex >= MAX_PALETTES) paletteIndex = 0; - else if (paletteIndex < 0) paletteIndex = MAX_PALETTES - 1; + if (currentPalette >= MAX_PALETTES) currentPalette = 0; + else if (currentPalette < 0) currentPalette = MAX_PALETTES - 1; // Send new value to the shader to be used on drawing. - // Note that we are sending RGB triplets w/o the alpha channel *only* if the current - // palette index has changed (in order to save performances). - if (currentPalette != paletteIndex) - { - currentPalette = paletteIndex; - SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE); - } + // NOTE: We are sending RGB triplets w/o the alpha channel + SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE); //---------------------------------------------------------------------------------- // Draw @@ -121,33 +115,19 @@ int main() BeginShaderMode(shader); - // Draw horizontal screen-wide rectangles with increasing "palette index". - // The used palette index is encoded in the RGB components of the pixel. - int linesPerRectangle = screenHeight / COLORS_PER_PALETTE; - int leftover = screenHeight % COLORS_PER_PALETTE; - int y = 0; - - for (int i = 0; i < COLORS_PER_PALETTE; ++i) + for (int i = 0; i < COLORS_PER_PALETTE; i++) { - int height = linesPerRectangle; - - if (leftover > 0) - { - height += 1; - leftover -= 1; - } - - DrawRectangle(0, y, screenWidth, height, (Color){ i, i, i, 255 }); - - y += height; + // Draw horizontal screen-wide rectangles with increasing "palette index" + // The used palette index is encoded in the RGB components of the pixel + DrawRectangle(0, lineHeight*i, GetScreenWidth(), lineHeight, (Color){ i, i, i, 255 }); } EndShaderMode(); - DrawText("CURRENT PALETTE:", 10, 15, 20, RAYWHITE); - DrawText(paletteText[currentPalette], 240, 15, 20, RED); - DrawText("< >", 540, 10, 30, DARKBLUE); - + DrawText("< >", 10, 10, 30, DARKBLUE); + DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE); + DrawText(paletteText[currentPalette], 300, 15, 20, RED); + DrawFPS(700, 15); EndDrawing(); @@ -158,7 +138,7 @@ int main() //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; -- cgit v1.2.3 From 49a49e492ab58745ce7d7bbb3f8cb5b315f12de0 Mon Sep 17 00:00:00 2001 From: eggmund Date: Sun, 12 May 2019 16:50:56 +0100 Subject: Added julia set shader example. --- .../resources/shaders/glsl330/julia_shader.fs | 86 +++++++++ examples/shaders/shaders_julia_set.c | 213 +++++++++++++++++++++ examples/shaders/shaders_julia_set.png | Bin 0 -> 337885 bytes 3 files changed, 299 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl330/julia_shader.fs create mode 100644 examples/shaders/shaders_julia_set.c create mode 100644 examples/shaders/shaders_julia_set.png (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl330/julia_shader.fs b/examples/shaders/resources/shaders/glsl330/julia_shader.fs new file mode 100644 index 00000000..b1331d84 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/julia_shader.fs @@ -0,0 +1,86 @@ +#version 330 + +// Input vertex attributes (from vertex shader) + +uniform vec2 screenDims; // Dimensions of the screen +uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c +uniform vec2 offset; // Offset of the scale. +uniform float zoom; // Zoom of the scale. + +// Output fragment color +out vec4 finalColor; + +const int MAX_ITERATIONS = 255; // Max iterations to do. + +// Square a complex number +vec2 complexSquare(vec2 z) +{ + return vec2( + z.x * z.x - z.y * z.y, + z.x * z.y * 2.0 + ); +} + +// Convert Hue Saturation Value color into RGB +vec3 hsv2rgb(vec3 c) +{ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + + +void main() +{ + // The pixel coordinates scaled so they are on the mandelbrot scale. + vec2 z = vec2(((gl_FragCoord.x + offset.x)/screenDims.x) * 2.5 * zoom, + ((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y) * 1.5 * zoom); // y also flipped due to opengl + int iterations = 0; + + /* + Julia sets use a function z^2 + c, where c is a constant. + This function is iterated until the nature of the point is determined. + + If the magnitude of the number becomes greater than 2, then from that point onward + the number will get bigger and bigger, and will never get smaller (tends towards infinity). + 2^2 = 4, 4^2 = 8 and so on. + So at 2 we stop iterating. + + If the number is below 2, we keep iterating. + But when do we stop iterating if the number is always below 2 (it converges)? + That is what MAX_ITERATIONS is for. + Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can + then map to a color. + + We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + */ + for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) + { + z = complexSquare(z) + c; // Iterate function + if (dot(z, z) > 4.0) + { + break; + } + } + + // Another few iterations decreases errors in the smoothing calculation. + // See http://linas.org/art-gallery/escape/escape.html for more information. + z = complexSquare(z) + c; + z = complexSquare(z) + c; + + // This last part smooths the color (again see link above). + float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0)); + + // Normalize the value so it is between 0 and 1. + float norm = smoothVal/float(MAX_ITERATIONS); + + // If in set, color black. 0.999 allows for some float accuracy error. + if (norm > 0.999) + { + finalColor = vec4(0.0, 0.0, 0.0, 1.0); + } else + { + finalColor = vec4(hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); + } +} diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c new file mode 100644 index 00000000..676cff57 --- /dev/null +++ b/examples/shaders/shaders_julia_set.c @@ -0,0 +1,213 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Render julia sets using a shader. +* +* 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). +* +* 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) +* +* Author: eggmund (https://github.com/eggmund) +* +********************************************************************************************/ + +#include "raylib.h" +#include // For memcpy + +// Speed when using auto +const float AUTO_SPEED = 0.0005; + +// A few good julia sets +const float POINTS_OF_INTEREST[6][2] = +{ + {-0.348827, 0.607167}, + {-0.786268, 0.169728}, + {-0.8, 0.156}, + {0.285, 0.0}, + {-0.835, -0.2321}, + {-0.70176, -0.3842}, +}; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 1280; + int screenHeight = 720; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia set renderer"); + + // If julia set is rendered for this frame. + bool rendered = false; + + bool showControls = true; + + // Multiplier of speed to change c value. Set to 3 to start off with. + int incrementSpeed = 3; + + // Offset and zoom to draw the julia set at. (centered on screen and 2 times smaller) + float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; + float zoom = 1.6; + + // c constant to use in z^2 + c + float c[2]; + // Copy a point of interest into the c variable. 4 bytes per float (32 bits). + memcpy(c, &POINTS_OF_INTEREST[0], 8); + + // Load julia set shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); + + // Get variable (uniform) location on the shader to connect with the program + // NOTE: If uniform variable could not be found in the shader, function returns -1 + // The location of c will be stored since we will need to change this whenever c changes + int cLoc = GetShaderLocation(shader, "c"); + + // Tell the shader what the screen dimensions, zoom, offset and c are + float screenDims[2] = { (float)screenWidth, (float)screenHeight }; + SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); + SetShaderValue(shader, GetShaderLocation(shader, "zoom"), &zoom, UNIFORM_FLOAT); + SetShaderValue(shader, GetShaderLocation(shader, "offset"), offset, UNIFORM_VEC2); + + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + + // Create a RenderTexture2D to be used for render to texture + RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); + + SetTargetFPS(60); // Set the window to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + + // Get input + //---------------------------------------------------------------------------------- + + // Press 0 - 4 to reset c to a point of interest. + if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE) || IsKeyPressed(KEY_FOUR) || IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX)) + { + if (IsKeyPressed(KEY_ONE)) + { + memcpy(c, &POINTS_OF_INTEREST[0], 8); + } + else if (IsKeyPressed(KEY_TWO)) + { + memcpy(c, &POINTS_OF_INTEREST[1], 8); + } + else if (IsKeyPressed(KEY_THREE)) + { + memcpy(c, &POINTS_OF_INTEREST[2], 8); + } + else if (IsKeyPressed(KEY_FOUR)) + { + memcpy(c, &POINTS_OF_INTEREST[3], 8); + } + else if (IsKeyPressed(KEY_FIVE)) + { + memcpy(c, &POINTS_OF_INTEREST[4], 8); + } + else if (IsKeyPressed(KEY_SIX)) + { + memcpy(c, &POINTS_OF_INTEREST[5], 8); + } + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + rendered = false; // c value has changed, so render the set again. + } + + // Press "r" to stop changing c + if (IsKeyPressed(KEY_R)) + { + incrementSpeed = 0; + } + + // Toggle whether or not to show controls + if (IsKeyPressed(KEY_H)) + { + showControls = !showControls; + } + + // Scroll to change c increment speed. + int mouseMv = GetMouseWheelMove(); // Get the amount the mouse has moved this frame + if (mouseMv != 0) + { + if (IsKeyDown(KEY_LEFT_SHIFT)) + { + incrementSpeed += mouseMv * 10; + } + else + { + incrementSpeed += mouseMv; + } + rendered = false; + } + + if (incrementSpeed != 0) + { + float amount = GetFrameTime() * incrementSpeed * AUTO_SPEED; + c[0] += amount; + c[1] += amount; + + // Update the c value in the shader. + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + rendered = false; + } + + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(BLACK); // Clear the screen of the previous frame. + + // If the c value has changed, redraw the julia set using the shader, onto the render texture. + if (!rendered) + { + BeginTextureMode(target); // Enable drawing to texture + + ClearBackground(BLACK); // Clear the last frame drawn on the texture. + + // Draw a rectangle in shader mode. This acts as a canvas for the shader to draw on. + BeginShaderMode(shader); + DrawRectangle(0, 0, screenWidth, screenHeight, BLACK); + EndShaderMode(); + + EndTextureMode(); + + rendered = true; // The set is now rendered, so do not compute it again until it next changes. + } + + // Draw the saved texture (rendered julia set). + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, target.texture.height }, (Vector2){ 0, 0 }, WHITE); + + // Print information. + DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 20, RAYWHITE ); + + if (showControls) + { + DrawText("Press keys 1 - 6 to change point of interest.", 10, screenHeight - 88, 20, RAYWHITE); + DrawText("Use the scroll wheel to auto increment the c value. Hold shift while scrolling to increase speed by 10.", 10, screenHeight - 66, 20, RAYWHITE); + DrawText("Press 'r' to reset speed.", 10, screenHeight - 44, 20, RAYWHITE); + DrawText("Press 'h' to hide these controls.", 10, screenHeight - 22, 20, RAYWHITE); + } + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/shaders/shaders_julia_set.png b/examples/shaders/shaders_julia_set.png new file mode 100644 index 00000000..5117ed0d Binary files /dev/null and b/examples/shaders/shaders_julia_set.png differ -- cgit v1.2.3 From c25154064d2a83e46ffe1184385b408e5b8bca23 Mon Sep 17 00:00:00 2001 From: eggmund Date: Sun, 12 May 2019 17:14:52 +0100 Subject: Fixed small error in comments --- examples/shaders/shaders_julia_set.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 676cff57..c408d7e1 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -48,7 +48,7 @@ int main() // Multiplier of speed to change c value. Set to 3 to start off with. int incrementSpeed = 3; - // Offset and zoom to draw the julia set at. (centered on screen and 2 times smaller) + // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; float zoom = 1.6; -- cgit v1.2.3 From aea3b2df65685e589ee60ba94a65a69787c0a480 Mon Sep 17 00:00:00 2001 From: hmmmmmmmm Date: Sun, 12 May 2019 17:31:04 +0100 Subject: Fixed another small comment error --- examples/shaders/shaders_julia_set.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index c408d7e1..381cd33e 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -89,7 +89,7 @@ int main() // Get input //---------------------------------------------------------------------------------- - // Press 0 - 4 to reset c to a point of interest. + // Press 1 - 6 to reset c to a point of interest. if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE) || IsKeyPressed(KEY_FOUR) || IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX)) { if (IsKeyPressed(KEY_ONE)) -- cgit v1.2.3 From 6804c2189e07a661ef975146c288619589c90731 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 14 May 2019 00:07:34 +0200 Subject: new examples: shaders_texture_waves --- examples/shaders/resources/shaders/glsl330/wave.fs | 37 +++++++ examples/shaders/resources/space.png | Bin 0 -> 22517 bytes examples/shaders/shaders_texture_waves.c | 113 +++++++++++++++++++++ examples/shaders/shaders_texture_waves.png | Bin 0 -> 86453 bytes 4 files changed, 150 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl330/wave.fs create mode 100644 examples/shaders/resources/space.png create mode 100644 examples/shaders/shaders_texture_waves.c create mode 100644 examples/shaders/shaders_texture_waves.png (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl330/wave.fs b/examples/shaders/resources/shaders/glsl330/wave.fs new file mode 100644 index 00000000..f139f395 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/wave.fs @@ -0,0 +1,37 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// Output fragment color +out vec4 finalColor; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + finalColor = texture(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/shaders/resources/space.png b/examples/shaders/resources/space.png new file mode 100644 index 00000000..41129739 Binary files /dev/null and b/examples/shaders/resources/space.png differ diff --git a/examples/shaders/shaders_texture_waves.c b/examples/shaders/shaders_texture_waves.c new file mode 100644 index 00000000..03c62deb --- /dev/null +++ b/examples/shaders/shaders_texture_waves.c @@ -0,0 +1,113 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Texture Waves +* +* 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), to test this example +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders +* raylib comes with shaders ready for both versions, check raylib/shaders install folder +* +* 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) +* +* Copyright (c) 2019 Anata (creator) and Ramon Santamaria (review) (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +// ------------------------------------------------------------------------------------------------------------- +// Main Entry point +// ------------------------------------------------------------------------------------------------------------- +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture waves"); + + // Load space texture to apply shaders + Texture2D space = LoadTexture("resources/space.png"); + + // Load shader and setup location points and values + Shader wave = LoadShader(0, FormatText("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); + + float screenSizeLoc = GetShaderLocation(wave, "size"); + float secondsLoc = GetShaderLocation(wave, "secondes"); + float freqXLoc = GetShaderLocation(wave, "freqX"); + float freqYLoc = GetShaderLocation(wave, "freqY"); + float ampXLoc = GetShaderLocation(wave, "ampX"); + float ampYLoc = GetShaderLocation(wave, "ampY"); + float speedXLoc = GetShaderLocation(wave, "speedX"); + float speedYLoc = GetShaderLocation(wave, "speedY"); + + float screenSize[2] = { 800, 450 }; + + // Shader uniform values that can be updated at any time + float freqX = 25.0f; + float freqY = 25.0f; + float ampX = 5.0f; + float ampY = 5.0f; + float speedX = 8.0f; + float speedY = 8.0f; + + SetShaderValue(wave, screenSizeLoc, &screenSize, UNIFORM_VEC2); + SetShaderValue(wave, freqXLoc, &freqX, UNIFORM_FLOAT); + SetShaderValue(wave, freqYLoc, &freqY, UNIFORM_FLOAT); + SetShaderValue(wave, ampXLoc, &X, UNIFORM_FLOAT); + SetShaderValue(wave, ampYLoc, &Y, UNIFORM_FLOAT); + SetShaderValue(wave, speedXLoc, &speedX, UNIFORM_FLOAT); + SetShaderValue(wave, speedYLoc, &speedY, UNIFORM_FLOAT); + + float seconds = 0.0f; + + SetTargetFPS(60); + // ------------------------------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + seconds += GetFrameTime(); + + SetShaderValue(wave, secondsLoc, &seconds, UNIFORM_FLOAT); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginShaderMode(wave); + + DrawTexture(space, 0, 0, WHITE); + DrawTexture(space, space.width, 0, WHITE); + + EndShaderMode(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(wave); // Unload shader + UnloadTexture(space); // Unload texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/shaders/shaders_texture_waves.png b/examples/shaders/shaders_texture_waves.png new file mode 100644 index 00000000..99781a17 Binary files /dev/null and b/examples/shaders/shaders_texture_waves.png differ -- cgit v1.2.3 From 2edec8ae288ba70630021b330fe61c9005bc03d9 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 14 May 2019 00:08:21 +0200 Subject: Some example tweaks --- examples/shaders/shaders_raymarching.c | 8 +++++++- examples/shaders/shaders_texture_drawing.c | 12 +++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_raymarching.c b/examples/shaders/shaders_raymarching.c index e5c58a1d..f68222b5 100644 --- a/examples/shaders/shaders_raymarching.c +++ b/examples/shaders/shaders_raymarching.c @@ -18,6 +18,12 @@ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + int main() { // Initialization @@ -37,7 +43,7 @@ int main() // Load raymarching shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, "resources/shaders/glsl330/raymarching.fs"); + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION)); // Get shader locations for required uniforms int viewEyeLoc = GetShaderLocation(shader, "viewEye"); diff --git a/examples/shaders/shaders_texture_drawing.c b/examples/shaders/shaders_texture_drawing.c index cb8a9c1e..f5758273 100644 --- a/examples/shaders/shaders_texture_drawing.c +++ b/examples/shaders/shaders_texture_drawing.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [textures] example - Shader texture drawing +* raylib [textures] example - Texture drawing * * This example illustrates how to draw on a blank texture using a shader * @@ -13,6 +13,12 @@ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + int main() { // Initialization @@ -20,14 +26,14 @@ int main() int screenWidth = 800; int screenHeight = 450; - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shader texture drawing"); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - 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"); + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/cubes_panning.fs", GLSL_VERSION)); float time = 0.0f; int timeLoc = GetShaderLocation(shader, "uTime"); -- cgit v1.2.3 From 424d3ca8d9c5d612606444b2a2099cfad37f1888 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 14 May 2019 15:34:23 +0200 Subject: examples review Redesigns, deletes and renames Also noted authors propertly on contributed examples --- examples/shaders/shaders_julia_set.c | 208 ++++++++++++----------------- examples/shaders/shaders_palette_switch.c | 4 +- examples/shaders/shaders_texture_drawing.c | 2 + examples/shaders/shaders_texture_waves.c | 4 +- 4 files changed, 95 insertions(+), 123 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 381cd33e..c4dac6d2 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [shaders] example - Render julia sets using a shader. +* raylib [shaders] example - julia sets * * 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. @@ -10,73 +10,68 @@ * 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) * -* Author: eggmund (https://github.com/eggmund) +* Example contributed by eggmund (@eggmund) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 eggmund (@eggmund) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ #include "raylib.h" -#include // For memcpy -// Speed when using auto -const float AUTO_SPEED = 0.0005; +#include "raymath.h" // A few good julia sets const float POINTS_OF_INTEREST[6][2] = { - {-0.348827, 0.607167}, - {-0.786268, 0.169728}, - {-0.8, 0.156}, - {0.285, 0.0}, - {-0.835, -0.2321}, - {-0.70176, -0.3842}, + { -0.348827, 0.607167 }, + { -0.786268, 0.169728 }, + { -0.8, 0.156 }, + { 0.285, 0.0 }, + { -0.835, -0.2321 }, + { -0.70176, -0.3842 }, }; int main() { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 1280; - int screenHeight = 720; - - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia set renderer"); - - // If julia set is rendered for this frame. - bool rendered = false; - - bool showControls = true; - - // Multiplier of speed to change c value. Set to 3 to start off with. - int incrementSpeed = 3; - - // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) - float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; - float zoom = 1.6; + const int screenWidth = 800; + const int screenHeight = 450; - // c constant to use in z^2 + c - float c[2]; - // Copy a point of interest into the c variable. 4 bytes per float (32 bits). - memcpy(c, &POINTS_OF_INTEREST[0], 8); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets"); // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); - // Get variable (uniform) location on the shader to connect with the program + // c constant to use in z^2 + c + float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; + + // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) + float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; + float zoom = 1.6f; + + // Get variable (uniform) locations on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 - // The location of c will be stored since we will need to change this whenever c changes int cLoc = GetShaderLocation(shader, "c"); + int zoomLoc = GetShaderLocation(shader, "zoom"); + int offsetLoc = GetShaderLocation(shader, "offset"); // Tell the shader what the screen dimensions, zoom, offset and c are float screenDims[2] = { (float)screenWidth, (float)screenHeight }; SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); - SetShaderValue(shader, GetShaderLocation(shader, "zoom"), &zoom, UNIFORM_FLOAT); - SetShaderValue(shader, GetShaderLocation(shader, "offset"), offset, UNIFORM_VEC2); - - SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); + SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); + // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); + int incrementSpeed = 3; // Multiplier of speed to change c value + bool showControls = true; // Show controls + bool pause = false; // Pause animation + SetTargetFPS(60); // Set the window to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -86,115 +81,86 @@ int main() // Update //---------------------------------------------------------------------------------- - // Get input - //---------------------------------------------------------------------------------- - - // Press 1 - 6 to reset c to a point of interest. - if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE) || IsKeyPressed(KEY_FOUR) || IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX)) + // Press [1 - 6] to reset c to a point of interest + if (IsKeyPressed(KEY_ONE) || + IsKeyPressed(KEY_TWO) || + IsKeyPressed(KEY_THREE) || + IsKeyPressed(KEY_FOUR) || + IsKeyPressed(KEY_FIVE) || + IsKeyPressed(KEY_SIX)) { - if (IsKeyPressed(KEY_ONE)) - { - memcpy(c, &POINTS_OF_INTEREST[0], 8); - } - else if (IsKeyPressed(KEY_TWO)) - { - memcpy(c, &POINTS_OF_INTEREST[1], 8); - } - else if (IsKeyPressed(KEY_THREE)) - { - memcpy(c, &POINTS_OF_INTEREST[2], 8); - } - else if (IsKeyPressed(KEY_FOUR)) - { - memcpy(c, &POINTS_OF_INTEREST[3], 8); - } - else if (IsKeyPressed(KEY_FIVE)) - { - memcpy(c, &POINTS_OF_INTEREST[4], 8); - } - else if (IsKeyPressed(KEY_SIX)) - { - memcpy(c, &POINTS_OF_INTEREST[5], 8); - } - SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); - rendered = false; // c value has changed, so render the set again. - } + if (IsKeyPressed(KEY_ONE)) c[0] = POINTS_OF_INTEREST[0][0], c[1] = POINTS_OF_INTEREST[0][1]; + else if (IsKeyPressed(KEY_TWO)) c[0] = POINTS_OF_INTEREST[1][0], c[1] = POINTS_OF_INTEREST[1][1]; + else if (IsKeyPressed(KEY_THREE)) c[0] = POINTS_OF_INTEREST[2][0], c[1] = POINTS_OF_INTEREST[2][1]; + else if (IsKeyPressed(KEY_FOUR)) c[0] = POINTS_OF_INTEREST[3][0], c[1] = POINTS_OF_INTEREST[3][1]; + else if (IsKeyPressed(KEY_FIVE)) c[0] = POINTS_OF_INTEREST[4][0], c[1] = POINTS_OF_INTEREST[4][1]; + else if (IsKeyPressed(KEY_SIX)) c[0] = POINTS_OF_INTEREST[5][0], c[1] = POINTS_OF_INTEREST[5][1]; - // Press "r" to stop changing c - if (IsKeyPressed(KEY_R)) - { - incrementSpeed = 0; + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); } - // Toggle whether or not to show controls - if (IsKeyPressed(KEY_H)) + if (IsKeyPressed(KEY_P)) pause = !pause; // Pause animation (c change) + if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls + + if (!pause) { - showControls = !showControls; - } + if (IsKeyDown(KEY_RIGHT)) incrementSpeed++; + else if (IsKeyDown(KEY_LEFT)) incrementSpeed--; - // Scroll to change c increment speed. - int mouseMv = GetMouseWheelMove(); // Get the amount the mouse has moved this frame - if (mouseMv != 0) - { - if (IsKeyDown(KEY_LEFT_SHIFT)) - { - incrementSpeed += mouseMv * 10; - } - else + // Use mouse wheel to change zoom + zoom -= (float)GetMouseWheelMove()/10.f; + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); + + // Use mouse button to change offset + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { - incrementSpeed += mouseMv; + // TODO: Logic is not correct, the idea is getting zoom focus to pointed area + Vector2 mousePos = GetMousePosition(); + + offset[0] = mousePos.x -(float)screenWidth; + offset[1] = mousePos.y -(float)screenHeight; + + SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); } - rendered = false; - } - if (incrementSpeed != 0) - { - float amount = GetFrameTime() * incrementSpeed * AUTO_SPEED; + // Increment c value with time + float amount = GetFrameTime()*incrementSpeed*0.0005f; c[0] += amount; c[1] += amount; - // Update the c value in the shader. SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); - rendered = false; } - //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(BLACK); // Clear the screen of the previous frame. + ClearBackground(BLACK); // Clear the screen of the previous frame. - // If the c value has changed, redraw the julia set using the shader, onto the render texture. - if (!rendered) - { - BeginTextureMode(target); // Enable drawing to texture - - ClearBackground(BLACK); // Clear the last frame drawn on the texture. - - // Draw a rectangle in shader mode. This acts as a canvas for the shader to draw on. - BeginShaderMode(shader); - DrawRectangle(0, 0, screenWidth, screenHeight, BLACK); - EndShaderMode(); - - EndTextureMode(); - - rendered = true; // The set is now rendered, so do not compute it again until it next changes. - } - - // Draw the saved texture (rendered julia set). - DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, target.texture.height }, (Vector2){ 0, 0 }, WHITE); + // Using a render texture to draw Julia set + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture + + // Draw a rectangle in shader mode + // NOTE: This acts as a canvas for the shader to draw on + BeginShaderMode(shader); + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndShaderMode(); + EndTextureMode(); + + // Draw the saved texture (rendered julia set) + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, Vector2Zero(), WHITE); - // Print information. - DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 20, RAYWHITE ); + // Draw information + //DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 10, RAYWHITE); if (showControls) { - DrawText("Press keys 1 - 6 to change point of interest.", 10, screenHeight - 88, 20, RAYWHITE); - DrawText("Use the scroll wheel to auto increment the c value. Hold shift while scrolling to increase speed by 10.", 10, screenHeight - 66, 20, RAYWHITE); - DrawText("Press 'r' to reset speed.", 10, screenHeight - 44, 20, RAYWHITE); - DrawText("Press 'h' to hide these controls.", 10, screenHeight - 22, 20, RAYWHITE); + DrawText("Press keys [1 - 6] to change point of interest", 10, GetScreenHeight() - 60, 10, RAYWHITE); + DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, GetScreenHeight() - 45, 10, RAYWHITE); + DrawText("Press KEY_P to pause movement animation", 10, GetScreenHeight() - 30, 10, RAYWHITE); + DrawText("Press KEY_F1 to toggle these controls", 10, GetScreenHeight() - 15, 10, RAYWHITE); } EndDrawing(); diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index a2fa0b27..fa0bafaf 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -12,7 +12,9 @@ * This example has been created using raylib 2.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2019 Ramon Santamaria (@raysan5) +* Example contributed by Marco Lizza (@MarcoLizza) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Marco Lizza (@MarcoLizza) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/shaders/shaders_texture_drawing.c b/examples/shaders/shaders_texture_drawing.c index f5758273..3a540098 100644 --- a/examples/shaders/shaders_texture_drawing.c +++ b/examples/shaders/shaders_texture_drawing.c @@ -7,6 +7,8 @@ * 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) * +* Example contributed by Michał Ciesielski and reviewed by Ramon Santamaria (@raysan5) +* * Copyright (c) 2019 Michał Ciesielski and Ramon Santamaria (@raysan5) * ********************************************************************************************/ diff --git a/examples/shaders/shaders_texture_waves.c b/examples/shaders/shaders_texture_waves.c index 03c62deb..bc677c78 100644 --- a/examples/shaders/shaders_texture_waves.c +++ b/examples/shaders/shaders_texture_waves.c @@ -12,7 +12,9 @@ * 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) * -* Copyright (c) 2019 Anata (creator) and Ramon Santamaria (review) (@raysan5) +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5) * ********************************************************************************************/ -- cgit v1.2.3 From d878a0aecb762b2f6fe3f444ae9024afc104c8d3 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 15 May 2019 12:53:13 +0200 Subject: Update shaders_julia_set.c --- examples/shaders/shaders_julia_set.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index c4dac6d2..a6df0e84 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -18,8 +18,6 @@ #include "raylib.h" -#include "raymath.h" - // A few good julia sets const float POINTS_OF_INTEREST[6][2] = { @@ -48,7 +46,8 @@ int main() float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) - float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; + float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; + float targetOffset[2] = { offset[0], offset[1] }; float zoom = 1.6f; // Get variable (uniform) locations on the shader to connect with the program @@ -107,21 +106,25 @@ int main() if (IsKeyDown(KEY_RIGHT)) incrementSpeed++; else if (IsKeyDown(KEY_LEFT)) incrementSpeed--; - // Use mouse wheel to change zoom - zoom -= (float)GetMouseWheelMove()/10.f; - SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); - // Use mouse button to change offset if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) { + if (IsKeyDown(KEY_LEFT_SHIFT)) zoom -= 0.002f; + else zoom += 0.002f; + // TODO: Logic is not correct, the idea is getting zoom focus to pointed area Vector2 mousePos = GetMousePosition(); - - offset[0] = mousePos.x -(float)screenWidth; - offset[1] = mousePos.y -(float)screenHeight; - SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); + targetOffset[0] = mousePos.x -(float)screenWidth; + targetOffset[1] = mousePos.y -(float)screenHeight; } + + // Slowly move camera to targetOffset + offset[0] += GetFrameTime()*2.0f*(targetOffset[0] - offset[0]); + offset[1] += GetFrameTime()*2.0f*(targetOffset[1] - offset[1]); + + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); + SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); // Increment c value with time float amount = GetFrameTime()*incrementSpeed*0.0005f; @@ -150,7 +153,7 @@ int main() EndTextureMode(); // Draw the saved texture (rendered julia set) - DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, Vector2Zero(), WHITE); + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); // Draw information //DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 10, RAYWHITE); -- cgit v1.2.3 From 0b18133e46e72d7f51df7ddbbb81e9547bf5f4ba Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 15 May 2019 13:10:00 +0200 Subject: Update shaders_julia_set.c --- examples/shaders/shaders_julia_set.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index a6df0e84..24bf6f79 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -47,9 +47,10 @@ int main() // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; - float targetOffset[2] = { offset[0], offset[1] }; float zoom = 1.6f; + Vector2 offsetSpeed = { 0.0f, 0.0f }; + // Get variable (uniform) locations on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 int cLoc = GetShaderLocation(shader, "c"); @@ -106,23 +107,24 @@ int main() if (IsKeyDown(KEY_RIGHT)) incrementSpeed++; else if (IsKeyDown(KEY_LEFT)) incrementSpeed--; - // Use mouse button to change offset - if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) + // TODO: The idea is to zoom and move around with mouse + // Probably offset movement should be proportional to zoom level + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { - if (IsKeyDown(KEY_LEFT_SHIFT)) zoom -= 0.002f; - else zoom += 0.002f; - - // TODO: Logic is not correct, the idea is getting zoom focus to pointed area + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom += 0.003f; + if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= 0.003f; + Vector2 mousePos = GetMousePosition(); - targetOffset[0] = mousePos.x -(float)screenWidth; - targetOffset[1] = mousePos.y -(float)screenHeight; + offsetSpeed.x = mousePos.x -(float)screenWidth/2; + offsetSpeed.y = mousePos.y -(float)screenHeight/2; + + // Slowly move camera to targetOffset + offset[0] += GetFrameTime()*offsetSpeed.x*0.8f; + offset[1] += GetFrameTime()*offsetSpeed.y*0.8f; } - - // Slowly move camera to targetOffset - offset[0] += GetFrameTime()*2.0f*(targetOffset[0] - offset[0]); - offset[1] += GetFrameTime()*2.0f*(targetOffset[1] - offset[1]); - + else offsetSpeed = (Vector2){ 0.0f, 0.0f }; + SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); -- cgit v1.2.3 From 998b4180e1d3a9b6188699fd811c9b3abe537eac Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 15 May 2019 17:00:50 +0200 Subject: Update example! --- examples/shaders/shaders_julia_set.c | 24 +++++++++++------------- examples/shaders/shaders_julia_set.png | Bin 337885 -> 357391 bytes 2 files changed, 11 insertions(+), 13 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 24bf6f79..36eceb23 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -68,7 +68,7 @@ int main() // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); - int incrementSpeed = 3; // Multiplier of speed to change c value + int incrementSpeed = 0; // Multiplier of speed to change c value bool showControls = true; // Show controls bool pause = false; // Pause animation @@ -99,20 +99,20 @@ int main() SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); } - if (IsKeyPressed(KEY_P)) pause = !pause; // Pause animation (c change) + if (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change) if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls if (!pause) { - if (IsKeyDown(KEY_RIGHT)) incrementSpeed++; - else if (IsKeyDown(KEY_LEFT)) incrementSpeed--; + if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++; + else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--; // TODO: The idea is to zoom and move around with mouse // Probably offset movement should be proportional to zoom level if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { - if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom += 0.003f; - if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= 0.003f; + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom -= 0.003f; + if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom += 0.003f; Vector2 mousePos = GetMousePosition(); @@ -157,15 +157,13 @@ int main() // Draw the saved texture (rendered julia set) DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); - // Draw information - //DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 10, RAYWHITE); - if (showControls) { - DrawText("Press keys [1 - 6] to change point of interest", 10, GetScreenHeight() - 60, 10, RAYWHITE); - DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, GetScreenHeight() - 45, 10, RAYWHITE); - DrawText("Press KEY_P to pause movement animation", 10, GetScreenHeight() - 30, 10, RAYWHITE); - DrawText("Press KEY_F1 to toggle these controls", 10, GetScreenHeight() - 15, 10, RAYWHITE); + DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE); + DrawText("Press KEY_F1 to toggle these controls", 10, 30, 10, RAYWHITE); + DrawText("Press KEYS [1 - 6] to change point of interest", 10, 45, 10, RAYWHITE); + DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE); + DrawText("Press KEY_SPACE to pause movement animation", 10, 75, 10, RAYWHITE); } EndDrawing(); diff --git a/examples/shaders/shaders_julia_set.png b/examples/shaders/shaders_julia_set.png index 5117ed0d..b769c3f6 100644 Binary files a/examples/shaders/shaders_julia_set.png and b/examples/shaders/shaders_julia_set.png differ -- cgit v1.2.3 From a7c5e3cab70e00a287c561d06cef9fbb7b7e49ce Mon Sep 17 00:00:00 2001 From: eggmund Date: Wed, 15 May 2019 17:55:19 +0100 Subject: Updating julia set example. Now dividing by the zoom instead of multiplying (in the shader), so zoom works as expected. Also zoom increase/decrease is now scaled depending on the current zoom. --- examples/shaders/resources/shaders/glsl330/julia_shader.fs | 12 ++++++------ examples/shaders/shaders_julia_set.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl330/julia_shader.fs b/examples/shaders/resources/shaders/glsl330/julia_shader.fs index b1331d84..8e6815a5 100644 --- a/examples/shaders/resources/shaders/glsl330/julia_shader.fs +++ b/examples/shaders/resources/shaders/glsl330/julia_shader.fs @@ -15,10 +15,10 @@ const int MAX_ITERATIONS = 255; // Max iterations to do. // Square a complex number vec2 complexSquare(vec2 z) { - return vec2( - z.x * z.x - z.y * z.y, - z.x * z.y * 2.0 - ); + return vec2( + z.x * z.x - z.y * z.y, + z.x * z.y * 2.0 + ); } // Convert Hue Saturation Value color into RGB @@ -33,8 +33,8 @@ vec3 hsv2rgb(vec3 c) void main() { // The pixel coordinates scaled so they are on the mandelbrot scale. - vec2 z = vec2(((gl_FragCoord.x + offset.x)/screenDims.x) * 2.5 * zoom, - ((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y) * 1.5 * zoom); // y also flipped due to opengl + vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x) * 2.5)/zoom, + (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y) * 1.5)/zoom); // y also flipped due to opengl int iterations = 0; /* diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 36eceb23..f984d5e3 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -40,14 +40,14 @@ int main() // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); + Shader shader = LoadShader(0, "julia_shader.fs"); // c constant to use in z^2 + c float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; - float zoom = 1.6f; + float zoom = 1.0f; Vector2 offsetSpeed = { 0.0f, 0.0f }; @@ -111,8 +111,8 @@ int main() // Probably offset movement should be proportional to zoom level if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { - if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom -= 0.003f; - if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom += 0.003f; + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom += zoom * 0.003f; + if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= zoom * 0.003f; Vector2 mousePos = GetMousePosition(); -- cgit v1.2.3 From eb7beb59b6ee8d3881b231fdb8590935d1779ad1 Mon Sep 17 00:00:00 2001 From: eggmund Date: Wed, 15 May 2019 17:59:03 +0100 Subject: Ammended comment. Also changed path to shader back to what it was originally. --- examples/shaders/shaders_julia_set.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index f984d5e3..e063d54e 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -40,12 +40,12 @@ int main() // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, "julia_shader.fs"); + Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); // c constant to use in z^2 + c float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; - // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller) + // Offset and zoom to draw the julia set at. (centered on screen and default size) float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; float zoom = 1.0f; -- cgit v1.2.3 From f1ffb3f573a74113e44501dab07283cfb120acd2 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 May 2019 10:05:14 +0200 Subject: Review shaders for GLSL 100 --- .../resources/shaders/glsl100/cubes_panning.fs | 60 +++ .../shaders/resources/shaders/glsl100/depth.fs | 26 ++ .../shaders/resources/shaders/glsl100/julia_set.fs | 82 ++++ .../resources/shaders/glsl100/palette_switch.fs | 2 +- .../resources/shaders/glsl100/raymarching.fs | 431 +++++++++++++++++++++ examples/shaders/resources/shaders/glsl100/wave.fs | 36 ++ .../shaders/resources/shaders/glsl330/julia_set.fs | 82 ++++ .../resources/shaders/glsl330/julia_shader.fs | 86 ---- .../resources/shaders/glsl330/raymarching.fs | 23 +- examples/shaders/shaders_julia_set.c | 8 +- 10 files changed, 747 insertions(+), 89 deletions(-) create mode 100644 examples/shaders/resources/shaders/glsl100/cubes_panning.fs create mode 100644 examples/shaders/resources/shaders/glsl100/depth.fs create mode 100644 examples/shaders/resources/shaders/glsl100/julia_set.fs create mode 100644 examples/shaders/resources/shaders/glsl100/raymarching.fs create mode 100644 examples/shaders/resources/shaders/glsl100/wave.fs create mode 100644 examples/shaders/resources/shaders/glsl330/julia_set.fs delete mode 100644 examples/shaders/resources/shaders/glsl330/julia_shader.fs (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl100/cubes_panning.fs b/examples/shaders/resources/shaders/glsl100/cubes_panning.fs new file mode 100644 index 00000000..1b1ab15c --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/cubes_panning.fs @@ -0,0 +1,60 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// 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); + + gl_FragColor = vec4(color, alpha); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl100/depth.fs b/examples/shaders/resources/shaders/glsl100/depth.fs new file mode 100644 index 00000000..f6a14eb2 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/depth.fs @@ -0,0 +1,26 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; // Depth texture +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +void main() +{ + float zNear = 0.01; // camera z near + float zFar = 10.0; // camera z far + float z = texture2D(texture0, fragTexCoord).x; + + // Linearize depth value + float depth = (2.0*zNear)/(zFar + zNear - z*(zFar - zNear)); + + // Calculate final fragment color + gl_FragColor = vec4(depth, depth, depth, 1.0f); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl100/julia_set.fs b/examples/shaders/resources/shaders/glsl100/julia_set.fs new file mode 100644 index 00000000..9b8a0d03 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/julia_set.fs @@ -0,0 +1,82 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +uniform vec2 screenDims; // Dimensions of the screen +uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c +uniform vec2 offset; // Offset of the scale. +uniform float zoom; // Zoom of the scale. + +const int MAX_ITERATIONS = 255; // Max iterations to do. + +// Square a complex number +vec2 ComplexSquare(vec2 z) +{ + return vec2( + z.x * z.x - z.y * z.y, + z.x * z.y * 2.0 + ); +} + +// Convert Hue Saturation Value (HSV) color into RGB +vec3 Hsv2rgb(vec3 c) +{ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + + +void main() +{ + // The pixel coordinates scaled so they are on the mandelbrot scale + // y also flipped due to opengl + vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x)*2.5)/zoom, + (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y)*1.5)/zoom); + + int iterations = 0; + + /********************************************************************************************** + Julia sets use a function z^2 + c, where c is a constant. + This function is iterated until the nature of the point is determined. + + If the magnitude of the number becomes greater than 2, then from that point onward + the number will get bigger and bigger, and will never get smaller (tends towards infinity). + 2^2 = 4, 4^2 = 8 and so on. + So at 2 we stop iterating. + + If the number is below 2, we keep iterating. + But when do we stop iterating if the number is always below 2 (it converges)? + That is what MAX_ITERATIONS is for. + Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can + then map to a color. + + We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + *************************************************************************************************/ + for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) + { + z = ComplexSquare(z) + c; // Iterate function + + if (dot(z, z) > 4.0) break; + } + + // Another few iterations decreases errors in the smoothing calculation. + // See http://linas.org/art-gallery/escape/escape.html for more information. + z = ComplexSquare(z) + c; + z = ComplexSquare(z) + c; + + // This last part smooths the color (again see link above). + float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0)); + + // Normalize the value so it is between 0 and 1. + float norm = smoothVal/float(MAX_ITERATIONS); + + // If in set, color black. 0.999 allows for some float accuracy error. + if (norm > 0.999) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); + else gl_FragColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); +} diff --git a/examples/shaders/resources/shaders/glsl100/palette_switch.fs b/examples/shaders/resources/shaders/glsl100/palette_switch.fs index 7f09137e..3861d4c1 100644 --- a/examples/shaders/resources/shaders/glsl100/palette_switch.fs +++ b/examples/shaders/resources/shaders/glsl100/palette_switch.fs @@ -15,7 +15,7 @@ uniform ivec3 palette[colors]; void main() { // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + vec4 texelColor = texture2D(texture0, fragTexCoord) * fragColor; // Convert the (normalized) texel color RED component (GB would work, too) // to the palette index by scaling up from [0, 1] to [0, 255]. diff --git a/examples/shaders/resources/shaders/glsl100/raymarching.fs b/examples/shaders/resources/shaders/glsl100/raymarching.fs new file mode 100644 index 00000000..4ae71297 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/raymarching.fs @@ -0,0 +1,431 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +uniform vec3 viewEye; +uniform vec3 viewCenter; +uniform vec3 viewUp; +uniform float deltaTime; +uniform float runTime; +uniform vec2 resolution; + +// The MIT License +// Copyright © 2013 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +// A list of useful distance function to simple primitives, and an example on how to +// do some interesting boolean operations, repetition and displacement. +// +// More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm + +#define AA 1 // make this 1 is your machine is too slow + +//------------------------------------------------------------------ + +float sdPlane( vec3 p ) +{ + return p.y; +} + +float sdSphere( vec3 p, float s ) +{ + return length(p)-s; +} + +float sdBox( vec3 p, vec3 b ) +{ + vec3 d = abs(p) - b; + return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); +} + +float sdEllipsoid( in vec3 p, in vec3 r ) +{ + return (length( p/r ) - 1.0) * min(min(r.x,r.y),r.z); +} + +float udRoundBox( vec3 p, vec3 b, float r ) +{ + return length(max(abs(p)-b,0.0))-r; +} + +float sdTorus( vec3 p, vec2 t ) +{ + return length( vec2(length(p.xz)-t.x,p.y) )-t.y; +} + +float sdHexPrism( vec3 p, vec2 h ) +{ + vec3 q = abs(p); +#if 0 + return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x); +#else + float d1 = q.z-h.y; + float d2 = max((q.x*0.866025+q.y*0.5),q.y)-h.x; + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +#endif +} + +float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) +{ + vec3 pa = p-a, ba = b-a; + float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); + return length( pa - ba*h ) - r; +} + +float sdEquilateralTriangle( in vec2 p ) +{ + const float k = sqrt(3.0); + p.x = abs(p.x) - 1.0; + p.y = p.y + 1.0/k; + if( p.x + k*p.y > 0.0 ) p = vec2( p.x - k*p.y, -k*p.x - p.y )/2.0; + p.x += 2.0 - 2.0*clamp( (p.x+2.0)/2.0, 0.0, 1.0 ); + return -length(p)*sign(p.y); +} + +float sdTriPrism( vec3 p, vec2 h ) +{ + vec3 q = abs(p); + float d1 = q.z-h.y; +#if 1 + // distance bound + float d2 = max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5; +#else + // correct distance + h.x *= 0.866025; + float d2 = sdEquilateralTriangle(p.xy/h.x)*h.x; +#endif + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +} + +float sdCylinder( vec3 p, vec2 h ) +{ + vec2 d = abs(vec2(length(p.xz),p.y)) - h; + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +float sdCone( in vec3 p, in vec3 c ) +{ + vec2 q = vec2( length(p.xz), p.y ); + float d1 = -q.y-c.z; + float d2 = max( dot(q,c.xy), q.y); + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +} + +float sdConeSection( in vec3 p, in float h, in float r1, in float r2 ) +{ + float d1 = -p.y - h; + float q = p.y - h; + float si = 0.5*(r1-r2)/h; + float d2 = max( sqrt( dot(p.xz,p.xz)*(1.0-si*si)) + q*si - r2, q ); + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +} + +float sdPryamid4(vec3 p, vec3 h ) // h = { cos a, sin a, height } +{ + // Tetrahedron = Octahedron - Cube + float box = sdBox( p - vec3(0,-2.0*h.z,0), vec3(2.0*h.z) ); + + float d = 0.0; + d = max( d, abs( dot(p, vec3( -h.x, h.y, 0 )) )); + d = max( d, abs( dot(p, vec3( h.x, h.y, 0 )) )); + d = max( d, abs( dot(p, vec3( 0, h.y, h.x )) )); + d = max( d, abs( dot(p, vec3( 0, h.y,-h.x )) )); + float octa = d - h.z; + return max(-box,octa); // Subtraction + } + +float length2( vec2 p ) +{ + return sqrt( p.x*p.x + p.y*p.y ); +} + +float length6( vec2 p ) +{ + p = p*p*p; p = p*p; + return pow( p.x + p.y, 1.0/6.0 ); +} + +float length8( vec2 p ) +{ + p = p*p; p = p*p; p = p*p; + return pow( p.x + p.y, 1.0/8.0 ); +} + +float sdTorus82( vec3 p, vec2 t ) +{ + vec2 q = vec2(length2(p.xz)-t.x,p.y); + return length8(q)-t.y; +} + +float sdTorus88( vec3 p, vec2 t ) +{ + vec2 q = vec2(length8(p.xz)-t.x,p.y); + return length8(q)-t.y; +} + +float sdCylinder6( vec3 p, vec2 h ) +{ + return max( length6(p.xz)-h.x, abs(p.y)-h.y ); +} + +//------------------------------------------------------------------ + +float opS( float d1, float d2 ) +{ + return max(-d2,d1); +} + +vec2 opU( vec2 d1, vec2 d2 ) +{ + return (d1.x0.0 ) tmax = min( tmax, tp1 ); + float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 ); + else tmax = min( tmax, tp2 ); } +#endif + + float t = tmin; + float m = -1.0; + for( int i=0; i<64; i++ ) + { + float precis = 0.0005*t; + vec2 res = map( ro+rd*t ); + if( res.xtmax ) break; + t += res.x; + m = res.y; + } + + if( t>tmax ) m=-1.0; + return vec2( t, m ); +} + + +float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax ) +{ + float res = 1.0; + float t = mint; + for( int i=0; i<16; i++ ) + { + float h = map( ro + rd*t ).x; + res = min( res, 8.0*h/t ); + t += clamp( h, 0.02, 0.10 ); + if( h<0.001 || t>tmax ) break; + } + return clamp( res, 0.0, 1.0 ); +} + +vec3 calcNormal( in vec3 pos ) +{ + vec2 e = vec2(1.0,-1.0)*0.5773*0.0005; + return normalize( e.xyy*map( pos + e.xyy ).x + + e.yyx*map( pos + e.yyx ).x + + e.yxy*map( pos + e.yxy ).x + + e.xxx*map( pos + e.xxx ).x ); + /* + vec3 eps = vec3( 0.0005, 0.0, 0.0 ); + vec3 nor = vec3( + map(pos+eps.xyy).x - map(pos-eps.xyy).x, + map(pos+eps.yxy).x - map(pos-eps.yxy).x, + map(pos+eps.yyx).x - map(pos-eps.yyx).x ); + return normalize(nor); + */ +} + +float calcAO( in vec3 pos, in vec3 nor ) +{ + float occ = 0.0; + float sca = 1.0; + for( int i=0; i<5; i++ ) + { + float hr = 0.01 + 0.12*float(i)/4.0; + vec3 aopos = nor * hr + pos; + float dd = map( aopos ).x; + occ += -(dd-hr)*sca; + sca *= 0.95; + } + return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ); +} + +// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm +float checkersGradBox( in vec2 p ) +{ + // filter kernel + vec2 w = fwidth(p) + 0.001; + // analytical integral (box filter) + vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w; + // xor pattern + return 0.5 - 0.5*i.x*i.y; +} + +vec3 render( in vec3 ro, in vec3 rd ) +{ + vec3 col = vec3(0.7, 0.9, 1.0) +rd.y*0.8; + vec2 res = castRay(ro,rd); + float t = res.x; + float m = res.y; + if( m>-0.5 ) + { + vec3 pos = ro + t*rd; + vec3 nor = calcNormal( pos ); + vec3 ref = reflect( rd, nor ); + + // material + col = 0.45 + 0.35*sin( vec3(0.05,0.08,0.10)*(m-1.0) ); + if( m<1.5 ) + { + + float f = checkersGradBox( 5.0*pos.xz ); + col = 0.3 + f*vec3(0.1); + } + + // lighting + float occ = calcAO( pos, nor ); + vec3 lig = normalize( vec3(cos(-0.4 * runTime), sin(0.7 * runTime), -0.6) ); + vec3 hal = normalize( lig-rd ); + float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 ); + float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); + float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); + float dom = smoothstep( -0.1, 0.1, ref.y ); + float fre = pow( clamp(1.0+dot(nor,rd),0.0,1.0), 2.0 ); + + dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); + dom *= calcSoftshadow( pos, ref, 0.02, 2.5 ); + + float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0)* + dif * + (0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 )); + + vec3 lin = vec3(0.0); + lin += 1.30*dif*vec3(1.00,0.80,0.55); + lin += 0.40*amb*vec3(0.40,0.60,1.00)*occ; + lin += 0.50*dom*vec3(0.40,0.60,1.00)*occ; + lin += 0.50*bac*vec3(0.25,0.25,0.25)*occ; + lin += 0.25*fre*vec3(1.00,1.00,1.00)*occ; + col = col*lin; + col += 10.00*spe*vec3(1.00,0.90,0.70); + + col = mix( col, vec3(0.8,0.9,1.0), 1.0-exp( -0.0002*t*t*t ) ); + } + + return vec3( clamp(col,0.0,1.0) ); +} + +mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) +{ + vec3 cw = normalize(ta-ro); + vec3 cp = vec3(sin(cr), cos(cr),0.0); + vec3 cu = normalize( cross(cw,cp) ); + vec3 cv = normalize( cross(cu,cw) ); + return mat3( cu, cv, cw ); +} + +void main() +{ + vec3 tot = vec3(0.0); +#if AA>1 + for( int m=0; m1 + } + tot /= float(AA*AA); +#endif + + gl_FragColor = vec4( tot, 1.0 ); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl100/wave.fs b/examples/shaders/resources/shaders/glsl100/wave.fs new file mode 100644 index 00000000..bcc156cc --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/wave.fs @@ -0,0 +1,36 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + gl_FragColor = texture2D(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/shaders/resources/shaders/glsl330/julia_set.fs b/examples/shaders/resources/shaders/glsl330/julia_set.fs new file mode 100644 index 00000000..0e58716f --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/julia_set.fs @@ -0,0 +1,82 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Output fragment color +out vec4 finalColor; + +uniform vec2 screenDims; // Dimensions of the screen +uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c +uniform vec2 offset; // Offset of the scale. +uniform float zoom; // Zoom of the scale. + +const int MAX_ITERATIONS = 255; // Max iterations to do. + +// Square a complex number +vec2 ComplexSquare(vec2 z) +{ + return vec2( + z.x * z.x - z.y * z.y, + z.x * z.y * 2.0 + ); +} + +// Convert Hue Saturation Value (HSV) color into RGB +vec3 Hsv2rgb(vec3 c) +{ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +void main() +{ + // The pixel coordinates scaled so they are on the mandelbrot scale + // y also flipped due to opengl + vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x)*2.5)/zoom, + (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y)*1.5)/zoom); + + int iterations = 0; + + /********************************************************************************************** + Julia sets use a function z^2 + c, where c is a constant. + This function is iterated until the nature of the point is determined. + + If the magnitude of the number becomes greater than 2, then from that point onward + the number will get bigger and bigger, and will never get smaller (tends towards infinity). + 2^2 = 4, 4^2 = 8 and so on. + So at 2 we stop iterating. + + If the number is below 2, we keep iterating. + But when do we stop iterating if the number is always below 2 (it converges)? + That is what MAX_ITERATIONS is for. + Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can + then map to a color. + + We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + *************************************************************************************************/ + for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) + { + z = ComplexSquare(z) + c; // Iterate function + + if (dot(z, z) > 4.0) break; + } + + // Another few iterations decreases errors in the smoothing calculation. + // See http://linas.org/art-gallery/escape/escape.html for more information. + z = ComplexSquare(z) + c; + z = ComplexSquare(z) + c; + + // This last part smooths the color (again see link above). + float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0)); + + // Normalize the value so it is between 0 and 1. + float norm = smoothVal/float(MAX_ITERATIONS); + + // If in set, color black. 0.999 allows for some float accuracy error. + if (norm > 0.999) finalColor = vec4(0.0, 0.0, 0.0, 1.0); + else finalColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); +} diff --git a/examples/shaders/resources/shaders/glsl330/julia_shader.fs b/examples/shaders/resources/shaders/glsl330/julia_shader.fs deleted file mode 100644 index 8e6815a5..00000000 --- a/examples/shaders/resources/shaders/glsl330/julia_shader.fs +++ /dev/null @@ -1,86 +0,0 @@ -#version 330 - -// Input vertex attributes (from vertex shader) - -uniform vec2 screenDims; // Dimensions of the screen -uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c -uniform vec2 offset; // Offset of the scale. -uniform float zoom; // Zoom of the scale. - -// Output fragment color -out vec4 finalColor; - -const int MAX_ITERATIONS = 255; // Max iterations to do. - -// Square a complex number -vec2 complexSquare(vec2 z) -{ - return vec2( - z.x * z.x - z.y * z.y, - z.x * z.y * 2.0 - ); -} - -// Convert Hue Saturation Value color into RGB -vec3 hsv2rgb(vec3 c) -{ - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} - - -void main() -{ - // The pixel coordinates scaled so they are on the mandelbrot scale. - vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x) * 2.5)/zoom, - (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y) * 1.5)/zoom); // y also flipped due to opengl - int iterations = 0; - - /* - Julia sets use a function z^2 + c, where c is a constant. - This function is iterated until the nature of the point is determined. - - If the magnitude of the number becomes greater than 2, then from that point onward - the number will get bigger and bigger, and will never get smaller (tends towards infinity). - 2^2 = 4, 4^2 = 8 and so on. - So at 2 we stop iterating. - - If the number is below 2, we keep iterating. - But when do we stop iterating if the number is always below 2 (it converges)? - That is what MAX_ITERATIONS is for. - Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can - then map to a color. - - We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. - And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). - */ - for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) - { - z = complexSquare(z) + c; // Iterate function - if (dot(z, z) > 4.0) - { - break; - } - } - - // Another few iterations decreases errors in the smoothing calculation. - // See http://linas.org/art-gallery/escape/escape.html for more information. - z = complexSquare(z) + c; - z = complexSquare(z) + c; - - // This last part smooths the color (again see link above). - float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0)); - - // Normalize the value so it is between 0 and 1. - float norm = smoothVal/float(MAX_ITERATIONS); - - // If in set, color black. 0.999 allows for some float accuracy error. - if (norm > 0.999) - { - finalColor = vec4(0.0, 0.0, 0.0, 1.0); - } else - { - finalColor = vec4(hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); - } -} diff --git a/examples/shaders/resources/shaders/glsl330/raymarching.fs b/examples/shaders/resources/shaders/glsl330/raymarching.fs index 5ec9a02a..7c9fbcb1 100644 --- a/examples/shaders/resources/shaders/glsl330/raymarching.fs +++ b/examples/shaders/resources/shaders/glsl330/raymarching.fs @@ -1,5 +1,10 @@ #version 330 +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Output fragment color out vec4 finalColor; uniform vec3 viewEye; @@ -11,7 +16,23 @@ uniform vec2 resolution; // The MIT License // Copyright © 2013 Inigo Quilez -// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. // A list of useful distance function to simple primitives, and an example on how to // do some interesting boolean operations, repetition and displacement. diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index e063d54e..58163059 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -18,6 +18,12 @@ #include "raylib.h" +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + // A few good julia sets const float POINTS_OF_INTEREST[6][2] = { @@ -40,7 +46,7 @@ int main() // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs"); + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/julia_set.fs", GLSL_VERSION)); // c constant to use in z^2 + c float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; -- cgit v1.2.3 From 9fd410b8a87ede4b40be60ec5043090e2a86c6fe Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 May 2019 17:07:59 +0200 Subject: Review shader to use provided texture coordinates Now shader uses `fragTexCoord` that are the full screen texture coordinates normalized, instead of `gl_fragCoord`, the unnormalized screen coordinates --- .../shaders/resources/shaders/glsl100/julia_set.fs | 39 +++++++++++----------- .../shaders/resources/shaders/glsl330/julia_set.fs | 39 +++++++++++----------- examples/shaders/shaders_julia_set.c | 23 +++++++------ 3 files changed, 51 insertions(+), 50 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl100/julia_set.fs b/examples/shaders/resources/shaders/glsl100/julia_set.fs index 9b8a0d03..149a559c 100644 --- a/examples/shaders/resources/shaders/glsl100/julia_set.fs +++ b/examples/shaders/resources/shaders/glsl100/julia_set.fs @@ -33,31 +33,30 @@ vec3 Hsv2rgb(vec3 c) void main() { - // The pixel coordinates scaled so they are on the mandelbrot scale - // y also flipped due to opengl - vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x)*2.5)/zoom, - (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y)*1.5)/zoom); - - int iterations = 0; - /********************************************************************************************** - Julia sets use a function z^2 + c, where c is a constant. - This function is iterated until the nature of the point is determined. + Julia sets use a function z^2 + c, where c is a constant. + This function is iterated until the nature of the point is determined. - If the magnitude of the number becomes greater than 2, then from that point onward - the number will get bigger and bigger, and will never get smaller (tends towards infinity). - 2^2 = 4, 4^2 = 8 and so on. - So at 2 we stop iterating. + If the magnitude of the number becomes greater than 2, then from that point onward + the number will get bigger and bigger, and will never get smaller (tends towards infinity). + 2^2 = 4, 4^2 = 8 and so on. + So at 2 we stop iterating. - If the number is below 2, we keep iterating. - But when do we stop iterating if the number is always below 2 (it converges)? - That is what MAX_ITERATIONS is for. - Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can - then map to a color. + If the number is below 2, we keep iterating. + But when do we stop iterating if the number is always below 2 (it converges)? + That is what MAX_ITERATIONS is for. + Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can + then map to a color. - We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. - And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). *************************************************************************************************/ + + // The pixel coordinates are scaled so they are on the mandelbrot scale + // NOTE: fragTexCoord already comes as normalized screen coordinates but offset must be normalized before scaling and zoom + vec2 z = vec2((fragTexCoord.x + offset.x/screenDims.x)*2.5/zoom, (fragTexCoord.y + offset.y/screenDims.y)*1.5/zoom); + + int iterations = 0; for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) { z = ComplexSquare(z) + c; // Iterate function diff --git a/examples/shaders/resources/shaders/glsl330/julia_set.fs b/examples/shaders/resources/shaders/glsl330/julia_set.fs index 0e58716f..f68367ea 100644 --- a/examples/shaders/resources/shaders/glsl330/julia_set.fs +++ b/examples/shaders/resources/shaders/glsl330/julia_set.fs @@ -33,31 +33,30 @@ vec3 Hsv2rgb(vec3 c) void main() { - // The pixel coordinates scaled so they are on the mandelbrot scale - // y also flipped due to opengl - vec2 z = vec2((((gl_FragCoord.x + offset.x)/screenDims.x)*2.5)/zoom, - (((screenDims.y - gl_FragCoord.y + offset.y)/screenDims.y)*1.5)/zoom); - - int iterations = 0; - /********************************************************************************************** - Julia sets use a function z^2 + c, where c is a constant. - This function is iterated until the nature of the point is determined. + Julia sets use a function z^2 + c, where c is a constant. + This function is iterated until the nature of the point is determined. - If the magnitude of the number becomes greater than 2, then from that point onward - the number will get bigger and bigger, and will never get smaller (tends towards infinity). - 2^2 = 4, 4^2 = 8 and so on. - So at 2 we stop iterating. + If the magnitude of the number becomes greater than 2, then from that point onward + the number will get bigger and bigger, and will never get smaller (tends towards infinity). + 2^2 = 4, 4^2 = 8 and so on. + So at 2 we stop iterating. - If the number is below 2, we keep iterating. - But when do we stop iterating if the number is always below 2 (it converges)? - That is what MAX_ITERATIONS is for. - Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can - then map to a color. + If the number is below 2, we keep iterating. + But when do we stop iterating if the number is always below 2 (it converges)? + That is what MAX_ITERATIONS is for. + Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can + then map to a color. - We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. - And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). *************************************************************************************************/ + + // The pixel coordinates are scaled so they are on the mandelbrot scale + // NOTE: fragTexCoord already comes as normalized screen coordinates but offset must be normalized before scaling and zoom + vec2 z = vec2((fragTexCoord.x + offset.x/screenDims.x)*2.5/zoom, (fragTexCoord.y + offset.y/screenDims.y)*1.5/zoom); + + int iterations = 0; for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) { z = ComplexSquare(z) + c; // Iterate function diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 58163059..6c45f843 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -117,8 +117,8 @@ int main() // Probably offset movement should be proportional to zoom level if (IsMouseButtonDown(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) { - if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom += zoom * 0.003f; - if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= zoom * 0.003f; + if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) zoom += zoom*0.003f; + if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= zoom*0.003f; Vector2 mousePos = GetMousePosition(); @@ -153,16 +153,19 @@ int main() BeginTextureMode(target); // Enable drawing to texture ClearBackground(BLACK); // Clear the render texture - // Draw a rectangle in shader mode - // NOTE: This acts as a canvas for the shader to draw on - BeginShaderMode(shader); - DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); - EndShaderMode(); + // Draw a rectangle in shader mode to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); EndTextureMode(); - // Draw the saved texture (rendered julia set) - DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); - + // Draw the saved texture and rendered julia set with shader + // NOTE: We do not invert texture on Y, already considered inside shader + BeginShaderMode(shader); + DrawTexture(target.texture, 0, 0, WHITE); + EndShaderMode(); + if (showControls) { DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE); -- cgit v1.2.3 From ce87d2ced4dbe60f97d3ed48635231c222482b18 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 16 May 2019 17:23:31 +0200 Subject: new example: shaders_eratosthenes Contributed by ProfJski --- .../resources/shaders/glsl100/eratosthenes.fs | 58 +++++++++++++ .../resources/shaders/glsl330/eratosthenes.fs | 59 +++++++++++++ examples/shaders/shaders_eratosthenes.c | 94 +++++++++++++++++++++ examples/shaders/shaders_eratosthenes.png | Bin 0 -> 619472 bytes 4 files changed, 211 insertions(+) create mode 100644 examples/shaders/resources/shaders/glsl100/eratosthenes.fs create mode 100644 examples/shaders/resources/shaders/glsl330/eratosthenes.fs create mode 100644 examples/shaders/shaders_eratosthenes.c create mode 100644 examples/shaders/shaders_eratosthenes.png (limited to 'examples/shaders') diff --git a/examples/shaders/resources/shaders/glsl100/eratosthenes.fs b/examples/shaders/resources/shaders/glsl100/eratosthenes.fs new file mode 100644 index 00000000..0d598cac --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/eratosthenes.fs @@ -0,0 +1,58 @@ +#version 100 + +precision mediump float; + +/************************************************************************************* + + The Sieve of Eratosthenes -- a simple shader by ProfJski + An early prime number sieve: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + + The screen is divided into a square grid of boxes, each representing an integer value. + Each integer is tested to see if it is a prime number. Primes are colored white. + Non-primes are colored with a color that indicates the smallest factor which evenly divdes our integer. + + You can change the scale variable to make a larger or smaller grid. + Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers. + + WARNING: If you make scale too large, your GPU may bog down! + +***************************************************************************************/ + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Make a nice spectrum of colors based on counter and maxSize +vec4 Colorizer(float counter, float maxSize) +{ + float red = 0.0, green = 0.0, blue = 0.0; + float normsize = counter/maxSize; + + red = smoothstep(0.3, 0.7, normsize); + green = sin(3.14159*normsize); + blue = 1.0 - smoothstep(0.0, 0.4, normsize); + + return vec4(0.8*red, 0.8*green, 0.8*blue, 1.0); +} + +void main() +{ + vec4 color = vec4(1.0); + float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid. + int value = int(scale*floor(fragTexCoord.y*scale) + floor(fragTexCoord.x*scale)); // Group pixels into boxes representing integer values + + if ((value == 0) || (value == 1) || (value == 2)) gl_FragColor = vec4(1.0); + else + { + for (int i = 2; (i < max(2, sqrt(value) + 1)); i++) + { + if ((value - i*floor(value/i)) == 0) + { + color = Colorizer(float(i), scale); + //break; // Uncomment to color by the largest factor instead + } + } + + gl_FragColor = color; + } +} diff --git a/examples/shaders/resources/shaders/glsl330/eratosthenes.fs b/examples/shaders/resources/shaders/glsl330/eratosthenes.fs new file mode 100644 index 00000000..a6390b74 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/eratosthenes.fs @@ -0,0 +1,59 @@ +#version 330 + +/************************************************************************************* + + The Sieve of Eratosthenes -- a simple shader by ProfJski + An early prime number sieve: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + + The screen is divided into a square grid of boxes, each representing an integer value. + Each integer is tested to see if it is a prime number. Primes are colored white. + Non-primes are colored with a color that indicates the smallest factor which evenly divdes our integer. + + You can change the scale variable to make a larger or smaller grid. + Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers. + + WARNING: If you make scale too large, your GPU may bog down! + +***************************************************************************************/ + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Output fragment color +out vec4 finalColor; + +// Make a nice spectrum of colors based on counter and maxSize +vec4 Colorizer(float counter, float maxSize) +{ + float red = 0.0, green = 0.0, blue = 0.0; + float normsize = counter/maxSize; + + red = smoothstep(0.3, 0.7, normsize); + green = sin(3.14159*normsize); + blue = 1.0 - smoothstep(0.0, 0.4, normsize); + + return vec4(0.8*red, 0.8*green, 0.8*blue, 1.0); +} + +void main() +{ + vec4 color = vec4(1.0); + float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid. + int value = int(scale*floor(fragTexCoord.y*scale)+floor(fragTexCoord.x*scale)); // Group pixels into boxes representing integer values + + if ((value == 0) || (value == 1) || (value == 2)) finalColor = vec4(1.0); + else + { + for (int i = 2; (i < max(2, sqrt(value) + 1)); i++) + { + if ((value - i*floor(value/i)) == 0) + { + color = Colorizer(float(i), scale); + //break; // Uncomment to color by the largest factor instead + } + } + + finalColor = color; + } +} diff --git a/examples/shaders/shaders_eratosthenes.c b/examples/shaders/shaders_eratosthenes.c new file mode 100644 index 00000000..bfe516b5 --- /dev/null +++ b/examples/shaders/shaders_eratosthenes.c @@ -0,0 +1,94 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Sieve of Eratosthenes +* +* Sieve of Eratosthenes, the earliest known (ancient Greek) prime number sieve. +* +* "Sift the twos and sift the threes, +* The Sieve of Eratosthenes. +* When the multiples sublime, +* the numbers that are left are prime." +* +* 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). +* +* 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 ProfJski and reviewed by Ramon Santamaria (@raysan5) +* +* Copyright (c) 2019 ProfJski and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Sieve of Eratosthenes"); + + RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); + + // Load Eratosthenes shader + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION)); + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Nothing to do here, everything is happening in the shader + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture + + // Draw a rectangle in shader mode to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader) + + BeginShaderMode(shader); + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) + DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); + EndShaderMode(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload texture + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/shaders/shaders_eratosthenes.png b/examples/shaders/shaders_eratosthenes.png new file mode 100644 index 00000000..acd7fc75 Binary files /dev/null and b/examples/shaders/shaders_eratosthenes.png differ -- cgit v1.2.3 From 970f1e8ff15424ca2d96d89bf2871f0246835e9b Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 17 May 2019 01:17:40 +0200 Subject: examples review --- examples/shaders/shaders_palette_switch.c | 2 +- examples/shaders/shaders_palette_switch.png | Bin 0 -> 15463 bytes examples/shaders/shaders_texture_waves.c | 50 +++++++++++++--------------- 3 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 examples/shaders/shaders_palette_switch.png (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index fa0bafaf..4d651412 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -81,7 +81,7 @@ int main() // Load shader to be used on some parts drawing // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/palette-switch.fs", GLSL_VERSION)); + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/palette_switch.fs", GLSL_VERSION)); // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 diff --git a/examples/shaders/shaders_palette_switch.png b/examples/shaders/shaders_palette_switch.png new file mode 100644 index 00000000..7eb3eaf3 Binary files /dev/null and b/examples/shaders/shaders_palette_switch.png differ diff --git a/examples/shaders/shaders_texture_waves.c b/examples/shaders/shaders_texture_waves.c index bc677c78..e0026d36 100644 --- a/examples/shaders/shaders_texture_waves.c +++ b/examples/shaders/shaders_texture_waves.c @@ -38,22 +38,19 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture waves"); - // Load space texture to apply shaders - Texture2D space = LoadTexture("resources/space.png"); + // Load texture texture to apply shaders + Texture2D texture = LoadTexture("resources/space.png"); // Load shader and setup location points and values - Shader wave = LoadShader(0, FormatText("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); + Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); - float screenSizeLoc = GetShaderLocation(wave, "size"); - float secondsLoc = GetShaderLocation(wave, "secondes"); - float freqXLoc = GetShaderLocation(wave, "freqX"); - float freqYLoc = GetShaderLocation(wave, "freqY"); - float ampXLoc = GetShaderLocation(wave, "ampX"); - float ampYLoc = GetShaderLocation(wave, "ampY"); - float speedXLoc = GetShaderLocation(wave, "speedX"); - float speedYLoc = GetShaderLocation(wave, "speedY"); - - float screenSize[2] = { 800, 450 }; + int secondsLoc = GetShaderLocation(shader, "secondes"); + int freqXLoc = GetShaderLocation(shader, "freqX"); + int freqYLoc = GetShaderLocation(shader, "freqY"); + int ampXLoc = GetShaderLocation(shader, "ampX"); + int ampYLoc = GetShaderLocation(shader, "ampY"); + int speedXLoc = GetShaderLocation(shader, "speedX"); + int speedYLoc = GetShaderLocation(shader, "speedY"); // Shader uniform values that can be updated at any time float freqX = 25.0f; @@ -63,13 +60,14 @@ int main() float speedX = 8.0f; float speedY = 8.0f; - SetShaderValue(wave, screenSizeLoc, &screenSize, UNIFORM_VEC2); - SetShaderValue(wave, freqXLoc, &freqX, UNIFORM_FLOAT); - SetShaderValue(wave, freqYLoc, &freqY, UNIFORM_FLOAT); - SetShaderValue(wave, ampXLoc, &X, UNIFORM_FLOAT); - SetShaderValue(wave, ampYLoc, &Y, UNIFORM_FLOAT); - SetShaderValue(wave, speedXLoc, &speedX, UNIFORM_FLOAT); - SetShaderValue(wave, speedYLoc, &speedY, UNIFORM_FLOAT); + float screenSize[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() }; + SetShaderValue(shader, GetShaderLocation(shader, "size"), &screenSize, UNIFORM_VEC2); + SetShaderValue(shader, freqXLoc, &freqX, UNIFORM_FLOAT); + SetShaderValue(shader, freqYLoc, &freqY, UNIFORM_FLOAT); + SetShaderValue(shader, ampXLoc, &X, UNIFORM_FLOAT); + SetShaderValue(shader, ampYLoc, &Y, UNIFORM_FLOAT); + SetShaderValue(shader, speedXLoc, &speedX, UNIFORM_FLOAT); + SetShaderValue(shader, speedYLoc, &speedY, UNIFORM_FLOAT); float seconds = 0.0f; @@ -83,7 +81,7 @@ int main() //---------------------------------------------------------------------------------- seconds += GetFrameTime(); - SetShaderValue(wave, secondsLoc, &seconds, UNIFORM_FLOAT); + SetShaderValue(shader, secondsLoc, &seconds, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- // Draw @@ -92,10 +90,10 @@ int main() ClearBackground(RAYWHITE); - BeginShaderMode(wave); + BeginShaderMode(shader); - DrawTexture(space, 0, 0, WHITE); - DrawTexture(space, space.width, 0, WHITE); + DrawTexture(texture, 0, 0, WHITE); + DrawTexture(texture, texture.width, 0, WHITE); EndShaderMode(); @@ -105,8 +103,8 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(wave); // Unload shader - UnloadTexture(space); // Unload texture + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- -- cgit v1.2.3 From c1594fa445d038e40bae22eb69dfa04cc406aca4 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 18 May 2019 01:31:48 +0200 Subject: Tweaks --- examples/shaders/shaders_julia_set.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 6c45f843..ecaa5b6e 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -64,7 +64,7 @@ int main() int offsetLoc = GetShaderLocation(shader, "offset"); // Tell the shader what the screen dimensions, zoom, offset and c are - float screenDims[2] = { (float)screenWidth, (float)screenHeight }; + float screenDims[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() }; SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); -- cgit v1.2.3 From b525039e0ab8bcaa2fd6bde34c72a6405f88ae49 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 20 May 2019 16:36:42 +0200 Subject: Review ALL examples --- examples/shaders/shaders_custom_uniform.c | 38 +++++++++++------------ examples/shaders/shaders_eratosthenes.c | 8 ++--- examples/shaders/shaders_julia_set.c | 49 +++++++++++++++--------------- examples/shaders/shaders_model_shader.c | 19 +++++------- examples/shaders/shaders_palette_switch.c | 10 +++--- examples/shaders/shaders_postprocessing.c | 44 +++++++++++++-------------- examples/shaders/shaders_raymarching.c | 12 ++++---- examples/shaders/shaders_shapes_textures.c | 36 +++++++++++----------- examples/shaders/shaders_texture_drawing.c | 19 ++++++------ examples/shaders/shaders_texture_waves.c | 25 +++++++-------- 10 files changed, 127 insertions(+), 133 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index 74b9e771..1c82bba2 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -24,13 +24,13 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - + 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 - custom uniform variable"); @@ -48,20 +48,20 @@ int main() model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - + // Load postprocessing shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/swirl.fs", GLSL_VERSION)); - + // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 int swirlCenterLoc = GetShaderLocation(shader, "center"); - + float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 }; - + // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); - + // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode @@ -80,7 +80,7 @@ int main() // Send new value to the shader to be used on drawing SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); - + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- @@ -89,9 +89,9 @@ int main() BeginDrawing(); ClearBackground(RAYWHITE); - + BeginTextureMode(target); // Enable drawing to texture - + ClearBackground(RAYWHITE); // Clear texture background BeginMode3D(camera); // Begin 3d mode drawing @@ -101,21 +101,21 @@ int main() DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - + DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); - + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - + BeginShaderMode(shader); - + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); - + EndShaderMode(); - + // Draw some 2d text over drawn texture DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY); - + DrawFPS(10, 10); EndDrawing(); diff --git a/examples/shaders/shaders_eratosthenes.c b/examples/shaders/shaders_eratosthenes.c index bfe516b5..068fc26c 100644 --- a/examples/shaders/shaders_eratosthenes.c +++ b/examples/shaders/shaders_eratosthenes.c @@ -31,7 +31,7 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- @@ -46,11 +46,11 @@ int main() // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION)); - SetTargetFPS(60); + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -64,7 +64,7 @@ int main() ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture - ClearBackground(BLACK); // Clear the render texture + ClearBackground(BLACK); // Clear the render texture // Draw a rectangle in shader mode to be used as shader canvas // NOTE: Rectangle uses font white character texture coordinates, diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index ecaa5b6e..e64b622b 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -26,7 +26,7 @@ // A few good julia sets const float POINTS_OF_INTEREST[6][2] = -{ +{ { -0.348827, 0.607167 }, { -0.786268, 0.169728 }, { -0.8, 0.156 }, @@ -35,7 +35,7 @@ const float POINTS_OF_INTEREST[6][2] = { -0.70176, -0.3842 }, }; -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- @@ -47,16 +47,16 @@ int main() // Load julia set shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/julia_set.fs", GLSL_VERSION)); - + // c constant to use in z^2 + c float c[2] = { POINTS_OF_INTEREST[0][0], POINTS_OF_INTEREST[0][1] }; - + // Offset and zoom to draw the julia set at. (centered on screen and default size) float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 }; float zoom = 1.0f; - + Vector2 offsetSpeed = { 0.0f, 0.0f }; - + // Get variable (uniform) locations on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 int cLoc = GetShaderLocation(shader, "c"); @@ -64,35 +64,34 @@ int main() int offsetLoc = GetShaderLocation(shader, "offset"); // Tell the shader what the screen dimensions, zoom, offset and c are - float screenDims[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() }; + float screenDims[2] = { (float)screenWidth, (float)screenHeight }; SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2); - + SetShaderValue(shader, cLoc, c, UNIFORM_VEC2); SetShaderValue(shader, zoomLoc, &zoom, UNIFORM_FLOAT); SetShaderValue(shader, offsetLoc, offset, UNIFORM_VEC2); // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); - - int incrementSpeed = 0; // Multiplier of speed to change c value - bool showControls = true; // Show controls - bool pause = false; // Pause animation - SetTargetFPS(60); // Set the window to run at 60 frames-per-second + int incrementSpeed = 0; // Multiplier of speed to change c value + bool showControls = true; // Show controls + bool pause = false; // Pause animation + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- - // Press [1 - 6] to reset c to a point of interest - if (IsKeyPressed(KEY_ONE) || - IsKeyPressed(KEY_TWO) || - IsKeyPressed(KEY_THREE) || - IsKeyPressed(KEY_FOUR) || - IsKeyPressed(KEY_FIVE) || + if (IsKeyPressed(KEY_ONE) || + IsKeyPressed(KEY_TWO) || + IsKeyPressed(KEY_THREE) || + IsKeyPressed(KEY_FOUR) || + IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX)) { if (IsKeyPressed(KEY_ONE)) c[0] = POINTS_OF_INTEREST[0][0], c[1] = POINTS_OF_INTEREST[0][1]; @@ -107,7 +106,7 @@ int main() if (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change) if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls - + if (!pause) { if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++; @@ -121,10 +120,10 @@ int main() if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) zoom -= zoom*0.003f; Vector2 mousePos = GetMousePosition(); - + offsetSpeed.x = mousePos.x -(float)screenWidth/2; offsetSpeed.y = mousePos.y -(float)screenHeight/2; - + // Slowly move camera to targetOffset offset[0] += GetFrameTime()*offsetSpeed.x*0.8f; offset[1] += GetFrameTime()*offsetSpeed.y*0.8f; @@ -148,7 +147,7 @@ int main() BeginDrawing(); ClearBackground(BLACK); // Clear the screen of the previous frame. - + // Using a render texture to draw Julia set BeginTextureMode(target); // Enable drawing to texture ClearBackground(BLACK); // Clear the render texture @@ -165,7 +164,7 @@ int main() BeginShaderMode(shader); DrawTexture(target.texture, 0, 0, WHITE); EndShaderMode(); - + if (showControls) { DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE); diff --git a/examples/shaders/shaders_model_shader.c b/examples/shaders/shaders_model_shader.c index 2717c192..8224a337 100644 --- a/examples/shaders/shaders_model_shader.c +++ b/examples/shaders/shaders_model_shader.c @@ -24,13 +24,13 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - + 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 - model shader"); @@ -45,16 +45,16 @@ int main() Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture - + // Load shader for model // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); model.materials[0].shader = shader; // Set shader effect to 3d model model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model - + Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - + SetCameraMode(camera, CAMERA_FREE); // Set an orbital camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second @@ -81,11 +81,8 @@ int main() DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); - + DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY); - - DrawText(FormatText("Camera position: (%.2f, %.2f, %.2f)", camera.position.x, camera.position.y, camera.position.z), 600, 20, 10, BLACK); - DrawText(FormatText("Camera target: (%.2f, %.2f, %.2f)", camera.target.x, camera.target.y, camera.target.z), 600, 40, 10, GRAY); DrawFPS(10, 10); diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index 4d651412..6bc27827 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -69,12 +69,12 @@ static const char *paletteText[] = { "RKBV (2-strip film)" }; -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; + const int screenWidth = 800; + const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color palette switch"); @@ -117,7 +117,7 @@ int main() BeginShaderMode(shader); - for (int i = 0; i < COLORS_PER_PALETTE; i++) + for (int i = 0; i < COLORS_PER_PALETTE; i++) { // Draw horizontal screen-wide rectangles with increasing "palette index" // The used palette index is encoded in the RGB components of the pixel @@ -129,7 +129,7 @@ int main() DrawText("< >", 10, 10, 30, DARKBLUE); DrawText("CURRENT PALETTE:", 60, 15, 20, RAYWHITE); DrawText(paletteText[currentPalette], 300, 15, 20, RED); - + DrawFPS(700, 15); EndDrawing(); diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index 7c146419..018b8d11 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -58,31 +58,31 @@ static const char *postproShaderText[] = { //"FXAA" }; -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - + 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 - postprocessing shader"); // Define the camera to look into our 3d world Camera camera = {{ 2.0f, 3.0f, 2.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; - + Model model = LoadModel("resources/models/church.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map) model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set model diffuse texture Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position - + // Load all postpro shaders // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER) // NOTE 2: We load the correct shader depending on GLSL version Shader shaders[MAX_POSTPRO_SHADERS]; - + // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); shaders[FX_POSTERIZATION] = LoadShader(0, FormatText("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION)); @@ -96,12 +96,12 @@ int main() shaders[FX_SOBEL] = LoadShader(0, FormatText("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION)); shaders[FX_BLOOM] = LoadShader(0, FormatText("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION)); shaders[FX_BLUR] = LoadShader(0, FormatText("resources/shaders/glsl%i/blur.fs", GLSL_VERSION)); - + int currentShader = FX_GRAYSCALE; // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); - + // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode @@ -114,10 +114,10 @@ int main() // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera); // Update camera - + if (IsKeyPressed(KEY_RIGHT)) currentShader++; else if (IsKeyPressed(KEY_LEFT)) currentShader--; - + if (currentShader >= MAX_POSTPRO_SHADERS) currentShader = 0; else if (currentShader < 0) currentShader = MAX_POSTPRO_SHADERS - 1; //---------------------------------------------------------------------------------- @@ -129,7 +129,7 @@ int main() ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture - + ClearBackground(RAYWHITE); // Clear texture background BeginMode3D(camera); // Begin 3d mode drawing @@ -139,26 +139,26 @@ int main() DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - + // Render previously generated texture using selected postpro shader BeginShaderMode(shaders[currentShader]); - + // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE); - + EndShaderMode(); - + // Draw 2d shapes and text over drawn texture DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f)); - + DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); - + DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK); DrawText(postproShaderText[currentShader], 330, 15, 20, RED); DrawText("< >", 540, 10, 30, DARKBLUE); - + DrawFPS(700, 15); EndDrawing(); @@ -167,10 +167,10 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- - + // Unload all postpro shaders for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]); - + UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model UnloadRenderTexture(target); // Unload render texture diff --git a/examples/shaders/shaders_raymarching.c b/examples/shaders/shaders_raymarching.c index f68222b5..34091792 100644 --- a/examples/shaders/shaders_raymarching.c +++ b/examples/shaders/shaders_raymarching.c @@ -24,13 +24,13 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; - + const int screenWidth = 800; + const int screenHeight = 450; + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes"); Camera camera = { 0 }; @@ -44,7 +44,7 @@ int main() // Load raymarching shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION)); - + // Get shader locations for required uniforms int viewEyeLoc = GetShaderLocation(shader, "viewEye"); int viewCenterLoc = GetShaderLocation(shader, "viewCenter"); @@ -72,7 +72,7 @@ int main() float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z }; float cameraUp[3] = { camera.up.x, camera.up.y, camera.up.z }; - float deltaTime = GetFrameTime(); + float deltaTime = GetFrameTime(); runTime += deltaTime; // Set shader required uniform values diff --git a/examples/shaders/shaders_shapes_textures.c b/examples/shaders/shaders_shapes_textures.c index 5ee5d560..cf53bf99 100644 --- a/examples/shaders/shaders_shapes_textures.c +++ b/examples/shaders/shaders_shapes_textures.c @@ -24,23 +24,23 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; + const int screenWidth = 800; + const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders"); - + Texture2D fudesumi = LoadTexture("resources/fudesumi.png"); // Load shader to be used on some parts drawing - // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version + // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version // NOTE 2: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); - SetTargetFPS(60); + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop @@ -56,19 +56,19 @@ int main() BeginDrawing(); ClearBackground(RAYWHITE); - + // Start drawing with default shader DrawText("USING DEFAULT SHADER", 20, 40, 10, RED); - + DrawCircle(80, 120, 35, DARKBLUE); DrawCircleGradient(80, 220, 60, GREEN, SKYBLUE); DrawCircleLines(80, 340, 80, DARKBLUE); - + // Activate our custom shader to be applied on next shapes/textures drawings BeginShaderMode(shader); - + DrawText("USING CUSTOM SHADER", 190, 40, 10, RED); DrawRectangle(250 - 60, 90, 120, 60, RED); @@ -77,29 +77,29 @@ int main() // Activate our default shader for next drawings EndShaderMode(); - + DrawText("USING DEFAULT SHADER", 370, 40, 10, RED); - + DrawTriangle((Vector2){430, 80}, (Vector2){430 - 60, 150}, (Vector2){430 + 60, 150}, VIOLET); - + DrawTriangleLines((Vector2){430, 160}, (Vector2){430 - 20, 230}, (Vector2){430 + 20, 230}, DARKBLUE); DrawPoly((Vector2){430, 320}, 6, 80, 0, BROWN); - + // Activate our custom shader to be applied on next shapes/textures drawings BeginShaderMode(shader); DrawTexture(fudesumi, 500, -30, WHITE); // Using custom shader - + // Activate our default shader for next drawings EndShaderMode(); - + DrawText("(c) Fudesumi sprite by Eiden Marsal", 380, screenHeight - 20, 10, GRAY); - + EndDrawing(); //---------------------------------------------------------------------------------- } @@ -108,7 +108,7 @@ int main() //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(fudesumi); // Unload texture - + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- diff --git a/examples/shaders/shaders_texture_drawing.c b/examples/shaders/shaders_texture_drawing.c index 3a540098..697000bc 100644 --- a/examples/shaders/shaders_texture_drawing.c +++ b/examples/shaders/shaders_texture_drawing.c @@ -21,12 +21,12 @@ #define GLSL_VERSION 100 #endif -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- - int screenWidth = 800; - int screenHeight = 450; + const int screenWidth = 800; + const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture drawing"); @@ -41,17 +41,18 @@ int main() int timeLoc = GetShaderLocation(shader, "uTime"); SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT); - SetTargetFPS(60); - //-------------------------------------------------------------------------------------- + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + // ------------------------------------------------------------------------------------------------------------- - while (!WindowShouldClose()) + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- time = GetTime(); SetShaderValue(shader, timeLoc, &time, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- - + // Draw //---------------------------------------------------------------------------------- BeginDrawing(); @@ -61,13 +62,13 @@ int main() 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); diff --git a/examples/shaders/shaders_texture_waves.c b/examples/shaders/shaders_texture_waves.c index e0026d36..07186d37 100644 --- a/examples/shaders/shaders_texture_waves.c +++ b/examples/shaders/shaders_texture_waves.c @@ -26,10 +26,7 @@ #define GLSL_VERSION 100 #endif -// ------------------------------------------------------------------------------------------------------------- -// Main Entry point -// ------------------------------------------------------------------------------------------------------------- -int main() +int main(void) { // Initialization //-------------------------------------------------------------------------------------- @@ -69,13 +66,13 @@ int main() SetShaderValue(shader, speedXLoc, &speedX, UNIFORM_FLOAT); SetShaderValue(shader, speedYLoc, &speedY, UNIFORM_FLOAT); - float seconds = 0.0f; - - SetTargetFPS(60); - // ------------------------------------------------------------------------------------------------------------- - + float seconds = 0.0f; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + // ------------------------------------------------------------------------------------------------------------- + // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -86,9 +83,9 @@ int main() // Draw //---------------------------------------------------------------------------------- - BeginDrawing(); + BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); BeginShaderMode(shader); @@ -97,7 +94,7 @@ int main() EndShaderMode(); - EndDrawing(); + EndDrawing(); //---------------------------------------------------------------------------------- } @@ -109,5 +106,5 @@ int main() CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- - return 0; + return 0; } -- cgit v1.2.3 From 87774a0a21f8d2998b4dc13e989005270476ae92 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 27 May 2019 00:18:15 +0200 Subject: Review variables initialization --- examples/shaders/shaders_postprocessing.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/shaders') diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index 018b8d11..ed9da8bb 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -70,7 +70,7 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); // Define the camera to look into our 3d world - Camera camera = {{ 2.0f, 3.0f, 2.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; + Camera camera = { { 2.0f, 3.0f, 2.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; Model model = LoadModel("resources/models/church.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map) @@ -81,7 +81,7 @@ int main(void) // Load all postpro shaders // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER) // NOTE 2: We load the correct shader depending on GLSL version - Shader shaders[MAX_POSTPRO_SHADERS]; + Shader shaders[MAX_POSTPRO_SHADERS] = { 0 }; // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader shaders[FX_GRAYSCALE] = LoadShader(0, FormatText("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION)); -- cgit v1.2.3