Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions examples/shaders/ascii_rendering/ascii.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#version 330

// Input from the vertex shader
in vec2 fragTexCoord;

// Output color for the screen
out vec4 finalColor;

uniform sampler2D texture0;
uniform vec2 resolution;

// Fontsize less then 9 may be not complete
uniform float fontSize;

float GreyScale(in vec3 col)
{
return dot(col, vec3(0.2126, 0.7152, 0.0722));
}

float GetCharacter(int n, vec2 p)
{
p = floor(p*vec2(-4.0, 4.0) + 2.5);

// Check if the coordinate is inside the 5x5 grid (0 to 4)
if (clamp(p.x, 0.0, 4.0) == p.x && clamp(p.y, 0.0, 4.0) == p.y)
{
int a = int(round(p.x) + 5.0*round(p.y));
if (((n >> a) & 1) == 1)
{
return 1.0;
}
}

return 0.0; // The bit is off, or we are outside the grid
}

// -----------------------------------------------------------------------------
// Main shader logic
// -----------------------------------------------------------------------------

void main()
{
vec2 charPixelSize = vec2(fontSize, fontSize);
vec2 uvCellSize = charPixelSize/resolution;

// The cell size is based on the fontSize set by application
vec2 cellUV = floor(fragTexCoord/uvCellSize)*uvCellSize;

vec3 cellColor = texture(texture0, cellUV).rgb;

// Gray is used to define what character will be selected to draw
float gray = GreyScale(cellColor);

int n = 4096;

// Character set from https://www.shadertoy.com/view/lssGDj
// Create new bitmaps https://thrill-project.com/archiv/coding/bitmap/
if (gray > 0.2) n = 65600; // :
if (gray > 0.3) n = 18725316; // v
if (gray > 0.4) n = 15255086; // o
if (gray > 0.5) n = 13121101; // &
if (gray > 0.6) n = 15252014; // 8
if (gray > 0.7) n = 13195790; // @
if (gray > 0.8) n = 11512810; // #

vec2 localUV = (fragTexCoord - cellUV)/uvCellSize; // Range [0.0, 1.0]

vec2 p = localUV*2.0 - 1.0; // Range [-1.0, 1.0]

vec3 color = cellColor*GetCharacter(n, p);

finalColor = vec4(color, 1.0);
}
Binary file added examples/shaders/ascii_rendering/fudesumi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions examples/shaders/ascii_rendering/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"fmt"

rl "github.com/gen2brain/raylib-go/raylib"
)

func main() {
// Initialization
const screenWidth = 800
const screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - ascii rendering")

fudesumi := rl.LoadTexture("fudesumi.png")
raysan := rl.LoadTexture("raysan.png")
shader := rl.LoadShader("", "ascii.fs")

// These locations are used to send data to the GPU
resolutionLoc := rl.GetShaderLocation(shader, "resolution")
fontSizeLoc := rl.GetShaderLocation(shader, "fontSize")

// Set the character size for the ASCII effect (Fontsize should be 9 or more)
fontSize := float32(9.0)

// Send the updated values to the shader
resolution := []float32{float32(screenWidth), float32(screenHeight)}
rl.SetShaderValue(shader, resolutionLoc, resolution, rl.ShaderUniformVec2)

circlePos := rl.NewVector2(40.0, float32(screenHeight)*0.5)
circleSpeed := float32(1.0)

// RenderTexture to apply postprocessing
target := rl.LoadRenderTexture(screenWidth, screenHeight)

rl.SetTargetFPS(60)

// Main game loop
for !rl.WindowShouldClose() {
// Update
circlePos.X += circleSpeed
if circlePos.X > 200.0 || circlePos.X < 40.0 {
circleSpeed *= -1 // Revert speed
}

if rl.IsKeyPressed(rl.KeyLeft) && fontSize > 9.0 {
fontSize -= 1.0 // Reduce fontSize
}
if rl.IsKeyPressed(rl.KeyRight) && fontSize < 15.0 {
fontSize += 1.0 // Increase fontSize
}

// Set fontsize for the shader
rl.SetShaderValue(shader, fontSizeLoc, []float32{fontSize}, rl.ShaderUniformFloat)

// Draw
rl.BeginTextureMode(target)

rl.ClearBackground(rl.White)

// Draw scene in our render texture
rl.DrawTexture(fudesumi, 500, -30, rl.White)
rl.DrawTextureV(raysan, circlePos, rl.White)

rl.EndTextureMode()

rl.BeginDrawing()

rl.ClearBackground(rl.RayWhite)

rl.BeginShaderMode(shader)

// Draw scene texture (rendered earlier) to screen with vertical flip
srcRec := rl.NewRectangle(0, 0, float32(target.Texture.Width), -float32(target.Texture.Height))
rl.DrawTextureRec(target.Texture, srcRec, rl.Vector2Zero(), rl.White)

rl.EndShaderMode()

rl.DrawRectangle(0, 0, screenWidth, 40, rl.Black)
rl.DrawText(fmt.Sprintf("Ascii effect - FontSize:%2.0f - [Left] -1 [Right] +1 ", fontSize), 120, 10, 20, rl.LightGray)
rl.DrawFPS(10, 10)

rl.EndDrawing()
}

rl.UnloadTexture(fudesumi)
rl.UnloadTexture(raysan)
rl.UnloadShader(shader)
rl.UnloadRenderTexture(target)

rl.CloseWindow()
}
Binary file added examples/shaders/ascii_rendering/raysan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/shaders/color_correction/cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions examples/shaders/color_correction/color_correction.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

uniform float contrast;
uniform float saturation;
uniform float brightness;

// Output fragment color
out vec4 finalColor;

void main()
{
vec4 texel = texture(texture0, fragTexCoord); // Get texel color

// Apply contrast
texel.rgb = (texel.rgb - 0.5f)*(contrast/100.0f + 1.0f) + 0.5f;

// Apply brightness
texel.rgb = texel.rgb + brightness/100.0f;

// Apply saturation
float intensity = dot(texel.rgb, vec3(0.299f, 0.587f, 0.114f));
texel.rgb = (texel.rgb - intensity)*saturation/100.0f + texel.rgb;

// Output resulting color
finalColor = texel;
}
Binary file added examples/shaders/color_correction/fudesumi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
190 changes: 190 additions & 0 deletions examples/shaders/color_correction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package main

import (
"fmt"

rl "github.com/gen2brain/raylib-go/raylib"
)

const maxTextures = 4

func main() {
// Initialization
const screenWidth = 800
const screenHeight = 450

rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color correction")

textures := [maxTextures]rl.Texture2D{
rl.LoadTexture("parrots.png"),
rl.LoadTexture("cat.png"),
rl.LoadTexture("mandrill.png"),
rl.LoadTexture("fudesumi.png"),
}

shdrColorCorrection := rl.LoadShader("", "color_correction.fs")

imageIndex := int32(0)
resetButtonClicked := false

contrast := float32(0.0)
saturation := float32(0.0)
brightness := float32(0.0)

// Get shader locations
contrastLoc := rl.GetShaderLocation(shdrColorCorrection, "contrast")
saturationLoc := rl.GetShaderLocation(shdrColorCorrection, "saturation")
brightnessLoc := rl.GetShaderLocation(shdrColorCorrection, "brightness")

// Set initial shader values
rl.SetShaderValue(shdrColorCorrection, contrastLoc, []float32{contrast}, rl.ShaderUniformFloat)
rl.SetShaderValue(shdrColorCorrection, saturationLoc, []float32{saturation}, rl.ShaderUniformFloat)
rl.SetShaderValue(shdrColorCorrection, brightnessLoc, []float32{brightness}, rl.ShaderUniformFloat)

rl.SetTargetFPS(60)

// Main game loop
for !rl.WindowShouldClose() {
// Update
// Select texture to draw via keys
if rl.IsKeyPressed(rl.KeyOne) {
imageIndex = 0
} else if rl.IsKeyPressed(rl.KeyTwo) {
imageIndex = 1
} else if rl.IsKeyPressed(rl.KeyThree) {
imageIndex = 2
} else if rl.IsKeyPressed(rl.KeyFour) {
imageIndex = 3
}

// Reset values to 0
if rl.IsKeyPressed(rl.KeyR) || resetButtonClicked {
contrast = 0.0
saturation = 0.0
brightness = 0.0
}

// Send values to shader
rl.SetShaderValue(shdrColorCorrection, contrastLoc, []float32{contrast}, rl.ShaderUniformFloat)
rl.SetShaderValue(shdrColorCorrection, saturationLoc, []float32{saturation}, rl.ShaderUniformFloat)
rl.SetShaderValue(shdrColorCorrection, brightnessLoc, []float32{brightness}, rl.ShaderUniformFloat)

// Draw
rl.BeginDrawing()

rl.ClearBackground(rl.RayWhite)

rl.BeginShaderMode(shdrColorCorrection)

currTex := textures[imageIndex]
posX := int32(580/2) - currTex.Width/2
posY := int32(rl.GetScreenHeight()/2) - currTex.Height/2
rl.DrawTexture(currTex, posX, posY, rl.White)

rl.EndShaderMode()

rl.DrawLine(580, 0, 580, int32(rl.GetScreenHeight()), rl.NewColor(218, 218, 218, 255))
rl.DrawRectangle(580, 0, int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()), rl.NewColor(232, 232, 232, 255))

// Draw UI info text
rl.DrawText("Color Correction", 585, 40, 20, rl.Gray)

rl.DrawText("Picture", 602, 75, 10, rl.Gray)
rl.DrawText("Press [1] - [4] to Change Picture", 600, 230, 8, rl.Gray)
rl.DrawText("Press [R] to Reset Values", 600, 250, 8, rl.Gray)

// Draw GUI controls using pure Raylib helpers
drawGuiToggleGroup(rl.NewRectangle(645, 70, 20, 20), []string{"1", "2", "3", "4"}, &imageIndex)

drawGuiSliderBar(rl.NewRectangle(645, 100, 120, 20), "Contrast", fmt.Sprintf("%.0f", contrast), &contrast, -100.0, 100.0)
drawGuiSliderBar(rl.NewRectangle(645, 130, 120, 20), "Saturation", fmt.Sprintf("%.0f", saturation), &saturation, -100.0, 100.0)
drawGuiSliderBar(rl.NewRectangle(645, 160, 120, 20), "Brightness", fmt.Sprintf("%.0f", brightness), &brightness, -100.0, 100.0)

resetButtonClicked = drawGuiButton(rl.NewRectangle(645, 190, 40, 20), "Reset")

rl.DrawFPS(710, 10)

rl.EndDrawing()
}

for i := 0; i < maxTextures; i++ {
rl.UnloadTexture(textures[i])
}
rl.UnloadShader(shdrColorCorrection)

rl.CloseWindow()
}

func drawGuiToggleGroup(bounds rl.Rectangle, options []string, active *int32) {
mousePos := rl.GetMousePosition()
for i, option := range options {
rec := rl.NewRectangle(bounds.X+float32(i)*(bounds.Width+5), bounds.Y, bounds.Width, bounds.Height)
isSelected := *active == int32(i)
isHovered := rl.CheckCollisionPointRec(mousePos, rec)

bgColor := rl.LightGray
textColor := rl.DarkGray
if isSelected {
bgColor = rl.Blue
textColor = rl.White
} else if isHovered {
bgColor = rl.Gray
textColor = rl.White
}

rl.DrawRectangleRec(rec, bgColor)
rl.DrawRectangleLinesEx(rec, 1, rl.DarkGray)
rl.DrawText(option, int32(rec.X+rec.Width/2-3), int32(rec.Y+rec.Height/2-5), 10, textColor)

if isHovered && rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
*active = int32(i)
}
}
}

func drawGuiSliderBar(bounds rl.Rectangle, label, textRight string, value *float32, minValue, maxValue float32) {
rl.DrawText(label, int32(bounds.X-60), int32(bounds.Y+5), 10, rl.DarkGray)

rl.DrawRectangleRec(bounds, rl.LightGray)
rl.DrawRectangleLinesEx(bounds, 1, rl.DarkGray)

// Handle mouse dragging
mousePos := rl.GetMousePosition()
if rl.IsMouseButtonDown(rl.MouseButtonLeft) && rl.CheckCollisionPointRec(mousePos, bounds) {
pct := (mousePos.X - bounds.X) / bounds.Width
if pct < 0 {
pct = 0
}
if pct > 1 {
pct = 1
}
*value = minValue + pct*(maxValue-minValue)
}

// Draw filled bar
pct := (*value - minValue) / (maxValue - minValue)
fillWidth := bounds.Width * pct
if fillWidth > 0 {
rl.DrawRectangleRec(rl.NewRectangle(bounds.X, bounds.Y, fillWidth, bounds.Height), rl.SkyBlue)
}

rl.DrawText(textRight, int32(bounds.X+bounds.Width+10), int32(bounds.Y+5), 10, rl.DarkGray)
}

func drawGuiButton(bounds rl.Rectangle, text string) bool {
mousePos := rl.GetMousePosition()
isHovered := rl.CheckCollisionPointRec(mousePos, bounds)

bgColor := rl.LightGray
textColor := rl.DarkGray
if isHovered {
bgColor = rl.Gray
textColor = rl.White
}

rl.DrawRectangleRec(bounds, bgColor)
rl.DrawRectangleLinesEx(bounds, 1, rl.DarkGray)
rl.DrawText(text, int32(bounds.X+bounds.Width/2-13), int32(bounds.Y+bounds.Height/2-5), 10, textColor)

return isHovered && rl.IsMouseButtonPressed(rl.MouseButtonLeft)
}
Binary file added examples/shaders/color_correction/mandrill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/shaders/color_correction/parrots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading