This commit is contained in:
Konstantin8105 2022-11-22 18:50:35 +03:00
parent 0bec81c656
commit fe6d2c0ed3
190 changed files with 104835 additions and 5 deletions

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

View file

@ -0,0 +1,26 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
// Output vertex attributes (to fragment shader)
out vec2 fragTexCoord;
out vec4 fragColor;
// NOTE: Add here your custom variables
void main()
{
// Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
// Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,47 @@
#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;
// NOTE: Add here your custom variables
// NOTE: Render size values should be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float radius = 250.0;
float angle = 0.8;
uniform vec2 center = vec2(200.0, 200.0);
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
tc -= center;
float dist = length(tc);
if (dist < radius)
{
float percent = (radius - dist)/radius;
float theta = percent*percent*angle*8.0;
float s = sin(theta);
float c = cos(theta);
tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c)));
}
tc += center;
vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;;
finalColor = vec4(color.rgb, 1.0);;
}

View file

@ -0,0 +1,100 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable Multi Sampling Anti Aliasing 4x (if available)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable")
camera := rl.Camera{}
camera.Position = rl.NewVector3(8.0, 8.0, 8.0)
camera.Target = rl.NewVector3(0.0, 1.5, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
obj := rl.LoadModel("barracks.obj") // Load OBJ model
texture := rl.LoadTexture("barracks_diffuse.png") // Load model texture
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set obj model diffuse texture
position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position
shader := rl.LoadShader("", "glsl330/swirl.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
swirlCenterLoc := rl.GetShaderLocation(shader, "center")
swirlCenter := make([]float32, 2)
swirlCenter[0] = float32(screenWidth) / 2
swirlCenter[1] = float32(screenHeight) / 2
// Create a RenderTexture2D to be used for render to texture
target := rl.LoadRenderTexture(screenWidth, screenHeight)
// Setup orbital camera
rl.SetCameraMode(camera, rl.CameraOrbital) // Set an orbital camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
//----------------------------------------------------------------------------------
mousePosition := rl.GetMousePosition()
swirlCenter[0] = mousePosition.X
swirlCenter[1] = float32(screenHeight) - mousePosition.Y
// Send new value to the shader to be used on drawing
rl.SetShaderValue(shader, swirlCenterLoc, swirlCenter, rl.ShaderUniformVec2)
rl.UpdateCamera(&camera) // Update camera
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginTextureMode(target) // Enable drawing to texture
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(obj, position, 0.5, rl.White) // Draw 3d model with texture
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, rl.Red)
rl.EndTextureMode() // End drawing to texture (now we have a texture available for next passes)
rl.BeginShaderMode(shader)
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
rl.DrawTextureRec(target.Texture, rl.NewRectangle(0, 0, float32(target.Texture.Width), float32(-target.Texture.Height)), rl.NewVector2(0, 0), rl.White)
rl.EndShaderMode()
rl.DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.UnloadShader(shader) // Unload shader
rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(obj) // Unload model
rl.UnloadRenderTexture(target) // Unload render texture
rl.CloseWindow()
}

View file

@ -0,0 +1,36 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
in mat4 instanceTransform;
// Input uniform values
uniform mat4 mvp;
uniform mat4 matNormal;
// Output vertex attributes (to fragment shader)
out vec3 fragPosition;
out vec2 fragTexCoord;
out vec4 fragColor;
out vec3 fragNormal;
// NOTE: Add here your custom variables
void main()
{
// Compute MVP for current instance
mat4 mvpi = mvp*instanceTransform;
// Send vertex attributes to fragment shader
fragPosition = vec3(mvpi*vec4(vertexPosition, 1.0));
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
// Calculate final vertex position
gl_Position = mvpi*vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,82 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec3 fragPosition;
in vec2 fragTexCoord;
in vec4 fragColor;
in vec3 fragNormal;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// NOTE: Add here your custom variables
#define MAX_LIGHTS 4
#define LIGHT_DIRECTIONAL 0
#define LIGHT_POINT 1
struct MaterialProperty {
vec3 color;
int useSampler;
sampler2D sampler;
};
struct Light {
int enabled;
int type;
vec3 position;
vec3 target;
vec4 color;
};
// Input lighting values
uniform Light lights[MAX_LIGHTS];
uniform vec4 ambient;
uniform vec3 viewPos;
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord);
vec3 lightDot = vec3(0.0);
vec3 normal = normalize(fragNormal);
vec3 viewD = normalize(viewPos - fragPosition);
vec3 specular = vec3(0.0);
// NOTE: Implement here your fragment shader code
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled == 1)
{
vec3 light = vec3(0.0);
if (lights[i].type == LIGHT_DIRECTIONAL)
{
light = -normalize(lights[i].target - lights[i].position);
}
if (lights[i].type == LIGHT_POINT)
{
light = normalize(lights[i].position - fragPosition);
}
float NdotL = max(dot(normal, light), 0.0);
lightDot += lights[i].color.rgb*NdotL;
float specCo = 0.0;
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine
specular += specCo;
}
}
finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
finalColor += texelColor*(ambient/10.0)*colDiffuse;
// Gamma correction
finalColor = pow(finalColor, vec4(1.0/2.2));
}

View file

@ -0,0 +1,92 @@
package main
import (
"fmt"
"reflect"
"unsafe"
rl "github.com/gen2brain/raylib-go/raylib"
)
type LightType int32
const (
LightTypeDirectional LightType = iota
LightTypePoint
)
type Light struct {
shader rl.Shader
lightType LightType
position rl.Vector3
target rl.Vector3
color rl.Color
enabled int32
// shader locations
enabledLoc int32
typeLoc int32
posLoc int32
targetLoc int32
colorLoc int32
}
const maxLightsCount = 4
var lightCount = 0
func NewLight(
lightType LightType,
position, target rl.Vector3,
color rl.Color,
shader rl.Shader) Light {
light := Light{
shader: shader,
}
if lightCount < maxLightsCount {
light.enabled = 1
light.lightType = lightType
light.position = position
light.target = target
light.color = color
light.enabledLoc = rl.GetShaderLocation(shader, fmt.Sprintf("lights[%d].enabled", lightCount))
light.typeLoc = rl.GetShaderLocation(shader, fmt.Sprintf("lights[%d].type", lightCount))
light.posLoc = rl.GetShaderLocation(shader, fmt.Sprintf("lights[%d].position", lightCount))
light.targetLoc = rl.GetShaderLocation(shader, fmt.Sprintf("lights[%d].target", lightCount))
light.colorLoc = rl.GetShaderLocation(shader, fmt.Sprintf("lights[%d].color", lightCount))
light.UpdateValues()
lightCount++
}
return light
}
// Send light properties to shader
func (lt *Light) UpdateValues() {
// not pretty -_-
// need nicer api
sh := &reflect.SliceHeader{
Len: 4,
Cap: 4,
}
// Send to shader light enabled state and type
sh.Data = uintptr(unsafe.Pointer(&lt.enabled))
rl.SetShaderValue(lt.shader, lt.enabledLoc, *(*[]float32)(unsafe.Pointer(sh)), rl.ShaderUniformInt)
// Send to shader light position values
sh.Data = uintptr(unsafe.Pointer(&lt.lightType))
rl.SetShaderValue(lt.shader, lt.posLoc, []float32{lt.position.X, lt.position.Y, lt.position.Z}, rl.ShaderUniformVec3)
// Send to shader light target target values
rl.SetShaderValue(lt.shader, lt.targetLoc, []float32{lt.target.X, lt.target.Y, lt.target.Z}, rl.ShaderUniformVec3)
// Send to shader light color values
rl.SetShaderValue(lt.shader, lt.colorLoc,
[]float32{float32(lt.color.R) / 255, float32(lt.color.G) / 255, float32(lt.color.B) / 255, float32(lt.color.A) / 255},
rl.ShaderUniformVec4)
}

View file

@ -0,0 +1,253 @@
package main
import (
"fmt"
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
const MAX_INSTANCES = 100000
func main() {
var (
screenWidth = int32(800) // Framebuffer width
screenHeight = int32(450) // Framebuffer height
fps = 60 // Frames per second
speed = 30 // Speed of jump animation
groups = 2 // Count of separate groups jumping around
amp = float32(10) // Maximum amplitude of jump
variance = float32(0.8) // Global variance in jump height
loop = float32(0.0) // Individual cube's computed loop timer
textPositionY int32 = 300
framesCounter = 0
)
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable Multi Sampling Anti Aliasing 4x (if available)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mesh instancing")
// Define the camera to look into our 3d world
camera := rl.Camera{
Position: rl.NewVector3(-125.0, 125.0, -125.0),
Target: rl.NewVector3(0.0, 0.0, 0.0),
Up: rl.NewVector3(0.0, 1.0, 0.0),
Fovy: 45.0,
Projection: rl.CameraPerspective,
}
cube := rl.GenMeshCube(1.0, 1.0, 1.0)
rotations := make([]rl.Matrix, MAX_INSTANCES) // Rotation state of instances
rotationsInc := make([]rl.Matrix, MAX_INSTANCES) // Per-frame rotation animation of instances
translations := make([]rl.Matrix, MAX_INSTANCES) // Locations of instances
// Scatter random cubes around
for i := 0; i < MAX_INSTANCES; i++ {
x := float32(rl.GetRandomValue(-50, 50))
y := float32(rl.GetRandomValue(-50, 50))
z := float32(rl.GetRandomValue(-50, 50))
translations[i] = rl.MatrixTranslate(x, y, z)
x = float32(rl.GetRandomValue(0, 360))
y = float32(rl.GetRandomValue(0, 360))
z = float32(rl.GetRandomValue(0, 360))
axis := rl.Vector3Normalize(rl.NewVector3(x, y, z))
angle := float32(rl.GetRandomValue(0, 10)) * rl.Deg2rad
rotationsInc[i] = rl.MatrixRotate(axis, angle)
rotations[i] = rl.MatrixIdentity()
}
transforms := make([]rl.Matrix, MAX_INSTANCES)
shader := rl.LoadShader("glsl330/base_lighting_instanced.vs", "glsl330/lighting.fs")
shader.UpdateLocation(rl.LocMatrixMvp, rl.GetShaderLocation(shader, "mvp"))
shader.UpdateLocation(rl.LocVectorView, rl.GetShaderLocation(shader, "viewPos"))
shader.UpdateLocation(rl.LocMatrixModel, rl.GetShaderLocationAttrib(shader, "instanceTransform"))
// ambient light level
ambientLoc := rl.GetShaderLocation(shader, "ambient")
rl.SetShaderValue(shader, ambientLoc, []float32{0.2, 0.2, 0.2, 1.0}, rl.ShaderUniformVec4)
NewLight(LightTypeDirectional, rl.NewVector3(50.0, 50.0, 0.0), rl.Vector3Zero(), rl.White, shader)
material := rl.LoadMaterialDefault()
material.Shader = shader
mmap := material.GetMap(rl.MapDiffuse)
mmap.Color = rl.Red
rl.SetCameraMode(camera, rl.CameraOrbital)
rl.SetTargetFPS(int32(fps))
for !rl.WindowShouldClose() {
// Update
//----------------------------------------------------------------------------------
textPositionY = 300
framesCounter++
if rl.IsKeyDown(rl.KeyUp) {
amp += 0.5
}
if rl.IsKeyDown(rl.KeyDown) {
if amp <= 1 {
amp = 1
} else {
amp -= 1
}
}
if rl.IsKeyDown(rl.KeyLeft) {
if variance < 0 {
variance = 0
} else {
variance -= 0.01
}
}
if rl.IsKeyDown(rl.KeyRight) {
if variance > 1 {
variance = 1
} else {
variance += 0.01
}
}
if rl.IsKeyDown(rl.KeyOne) {
groups = 1
}
if rl.IsKeyDown(rl.KeyTwo) {
groups = 2
}
if rl.IsKeyDown(rl.KeyThree) {
groups = 3
}
if rl.IsKeyDown(rl.KeyFour) {
groups = 4
}
if rl.IsKeyDown(rl.KeyFive) {
groups = 5
}
if rl.IsKeyDown(rl.KeySix) {
groups = 6
}
if rl.IsKeyDown(rl.KeySeven) {
groups = 7
}
if rl.IsKeyDown(rl.KeyEight) {
groups = 8
}
if rl.IsKeyDown(rl.KeyNine) {
groups = 9
}
if rl.IsKeyDown(rl.KeyW) {
groups = 7
amp = 25
speed = 18
variance = float32(0.70)
}
if rl.IsKeyDown(rl.KeyEqual) {
if float32(speed) <= float32(fps)*0.25 {
speed = int(float32(fps) * 0.25)
} else {
speed = int(float32(speed) * 0.95)
}
}
if rl.IsKeyDown(rl.KeyKpAdd) {
if float32(speed) <= float32(fps)*0.25 {
speed = int(float32(fps) * 0.25)
} else {
speed = int(float32(speed) * 0.95)
}
}
if rl.IsKeyDown(rl.KeyMinus) {
speed = int(math.Max(float64(speed)*1.02, float64(speed)+1))
}
if rl.IsKeyDown(rl.KeyKpSubtract) {
speed = int(math.Max(float64(speed)*1.02, float64(speed)+1))
}
// Update the light shader with the camera view position
rl.SetShaderValue(shader, shader.GetLocation(rl.LocVectorView),
[]float32{camera.Position.X, camera.Position.Y, camera.Position.Z}, rl.ShaderUniformVec3)
// Apply per-instance transformations
for i := 0; i < MAX_INSTANCES; i++ {
rotations[i] = rl.MatrixMultiply(rotations[i], rotationsInc[i])
transforms[i] = rl.MatrixMultiply(rotations[i], translations[i])
// Get the animation cycle's framesCounter for this instance
loop = float32((framesCounter+int(float32(i%groups)/float32(groups)*float32(speed)))%speed) / float32(speed)
// Calculate the y according to loop cycle
y := float32(math.Sin(float64(loop)*rl.Pi*2)) * amp *
((1 - variance) + (float32(variance) * float32(i%(groups*10)) / float32(groups*10)))
// Clamp to floor
if y < 0 {
y = 0
}
transforms[i] = rl.MatrixMultiply(transforms[i], rl.MatrixTranslate(0.0, y, 0.0))
}
rl.UpdateCamera(&camera) // Update camera
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
rl.BeginDrawing()
{
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
//rl.DrawMesh(cube, material, rl.MatrixIdentity())
rl.DrawMeshInstanced(cube, material, transforms, MAX_INSTANCES)
rl.EndMode3D()
rl.DrawText("A CUBE OF DANCING CUBES!", 490, 10, 20, rl.Maroon)
rl.DrawText("PRESS KEYS:", 10, textPositionY, 20, rl.Black)
textPositionY += 25
rl.DrawText("1 - 9", 10, textPositionY, 10, rl.Black)
rl.DrawText(": Number of groups", 50, textPositionY, 10, rl.Black)
rl.DrawText(fmt.Sprintf(": %d", groups), 160, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("UP", 10, textPositionY, 10, rl.Black)
rl.DrawText(": increase amplitude", 50, textPositionY, 10, rl.Black)
rl.DrawText(fmt.Sprintf(": %.2f", amp), 160, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("DOWN", 10, textPositionY, 10, rl.Black)
rl.DrawText(": decrease amplitude", 50, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("LEFT", 10, textPositionY, 10, rl.Black)
rl.DrawText(": decrease variance", 50, textPositionY, 10, rl.Black)
rl.DrawText(fmt.Sprintf(": %.2f", variance), 160, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("RIGHT", 10, textPositionY, 10, rl.Black)
rl.DrawText(": increase variance", 50, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("+/=", 10, textPositionY, 10, rl.Black)
rl.DrawText(": increase speed", 50, textPositionY, 10, rl.Black)
rl.DrawText(fmt.Sprintf(": %d = %f loops/sec", speed, float32(fps)/float32(speed)), 160, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("-", 10, textPositionY, 10, rl.Black)
rl.DrawText(": decrease speed", 50, textPositionY, 10, rl.Black)
textPositionY += 15
rl.DrawText("W", 10, textPositionY, 10, rl.Black)
rl.DrawText(": Wild setup!", 50, textPositionY, 10, rl.Black)
rl.DrawFPS(10, 10)
}
rl.EndDrawing()
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
rl.CloseWindow() // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

View file

@ -0,0 +1,26 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
// Output vertex attributes (to fragment shader)
out vec2 fragTexCoord;
out vec4 fragColor;
// NOTE: Add here your custom variables
void main()
{
// Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
// Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,26 @@
#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;
// NOTE: Add here your custom variables
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
// Calculate final fragment color
finalColor = vec4(gray, gray, gray, texelColor.a);
}

View file

@ -0,0 +1,66 @@
package main
import (
"fmt"
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable Multi Sampling Anti Aliasing 4x (if available)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader")
camera := rl.Camera{}
camera.Position = rl.NewVector3(3.0, 3.0, 3.0)
camera.Target = rl.NewVector3(0.0, 1.5, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
dwarf := rl.LoadModel("dwarf.obj") // Load OBJ model
texture := rl.LoadTexture("dwarf_diffuse.png") // Load model texture
shader := rl.LoadShader("glsl330/base.vs", "glsl330/grayscale.fs") // Load model shader
rl.SetMaterialTexture(dwarf.Materials, rl.MapDiffuse, texture)
dwarf.Materials.Shader = shader // Set shader effect to 3d model
position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position
rl.SetCameraMode(camera, rl.CameraFree) // Set free camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(dwarf, position, 2.0, rl.White) // Draw 3d model with texture
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.DrawText("(c) Dwarf 3D model by David Moreno", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.DrawText(fmt.Sprintf("Camera position: (%.2f, %.2f, %.2f)", camera.Position.X, camera.Position.Y, camera.Position.Z), 600, 20, 10, rl.Black)
rl.DrawText(fmt.Sprintf("Camera target: (%.2f, %.2f, %.2f)", camera.Target.X, camera.Target.Y, camera.Target.Z), 600, 40, 10, rl.Gray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.UnloadShader(shader) // Unload shader
rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(dwarf) // Unload model
rl.CloseWindow()
}

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB

View file

@ -0,0 +1,40 @@
#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;
// NOTE: Add here your custom variables
const vec2 size = vec2(800, 450); // render size
const float samples = 5.0; // pixels per axis; higher = bigger glow, worse performance
const float quality = 2.5; // lower = smaller glow, better quality
void main()
{
vec4 sum = vec4(0);
vec2 sizeFactor = vec2(1)/size*quality;
// Texel color fetching from texture sampler
vec4 source = texture(texture0, fragTexCoord);
const int range = 2; // should be = (samples - 1)/2;
for (int x = -range; x <= range; x++)
{
for (int y = -range; y <= range; y++)
{
sum += texture(texture0, fragTexCoord + vec2(x, y)*sizeFactor);
}
}
// Calculate final fragment color
finalColor = ((sum/(samples*samples)) + source)*colDiffuse;
}

View file

@ -0,0 +1,35 @@
#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;
// NOTE: Add here your custom variables
// NOTE: Render size values must be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float offset[3] = float[](0.0, 1.3846153846, 3.2307692308);
float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703);
void main()
{
// Texel color fetching from texture sampler
vec3 texelColor = texture(texture0, fragTexCoord).rgb*weight[0];
for (int i = 1; i < 3; i++)
{
texelColor += texture(texture0, fragTexCoord + vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];
texelColor += texture(texture0, fragTexCoord - vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];
}
finalColor = vec4(texelColor, 1.0);
}

View file

@ -0,0 +1,48 @@
#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;
// NOTE: Add here your custom variables
float hatchOffsetY = 5.0;
float lumThreshold01 = 0.9;
float lumThreshold02 = 0.7;
float lumThreshold03 = 0.5;
float lumThreshold04 = 0.3;
void main()
{
vec3 tc = vec3(1.0, 1.0, 1.0);
float lum = length(texture(texture0, fragTexCoord).rgb);
if (lum < lumThreshold01)
{
if (mod(gl_FragCoord.x + gl_FragCoord.y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
}
if (lum < lumThreshold02)
{
if (mod(gl_FragCoord.x - gl_FragCoord.y, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
}
if (lum < lumThreshold03)
{
if (mod(gl_FragCoord.x + gl_FragCoord.y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
}
if (lum < lumThreshold04)
{
if (mod(gl_FragCoord.x - gl_FragCoord.y - hatchOffsetY, 10.0) == 0.0) tc = vec3(0.0, 0.0, 0.0);
}
finalColor = vec4(tc, 1.0);
}

View file

@ -0,0 +1,59 @@
#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;
// NOTE: Add here your custom variables
// NOTE: Render size values must be passed from code
const float renderWidth = 800.0;
const float renderHeight = 450.0;
float stitchingSize = 6.0;
uniform int invert = 0;
vec4 PostFX(sampler2D tex, vec2 uv)
{
vec4 c = vec4(0.0);
float size = stitchingSize;
vec2 cPos = uv * vec2(renderWidth, renderHeight);
vec2 tlPos = floor(cPos / vec2(size, size));
tlPos *= size;
int remX = int(mod(cPos.x, size));
int remY = int(mod(cPos.y, size));
if (remX == 0 && remY == 0) tlPos = cPos;
vec2 blPos = tlPos;
blPos.y += (size - 1.0);
if ((remX == remY) || (((int(cPos.x) - int(blPos.x)) == (int(blPos.y) - int(cPos.y)))))
{
if (invert == 1) c = vec4(0.2, 0.15, 0.05, 1.0);
else c = texture(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
}
else
{
if (invert == 1) c = texture(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
else c = vec4(0.0, 0.0, 0.0, 1.0);
}
return c;
}
void main()
{
vec3 tc = PostFX(texture0, fragTexCoord).rgb;
finalColor = vec4(tc, 1.0);
}

View file

@ -0,0 +1,34 @@
#version 330
in vec2 fragTexCoord;
out vec4 fragColor;
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// NOTE: Add here your custom variables
void main()
{
vec4 color = texture(texture0, fragTexCoord);
color += texture(texture0, fragTexCoord + 0.001);
color += texture(texture0, fragTexCoord + 0.003);
color += texture(texture0, fragTexCoord + 0.005);
color += texture(texture0, fragTexCoord + 0.007);
color += texture(texture0, fragTexCoord + 0.009);
color += texture(texture0, fragTexCoord + 0.011);
color += texture(texture0, fragTexCoord - 0.001);
color += texture(texture0, fragTexCoord - 0.003);
color += texture(texture0, fragTexCoord - 0.005);
color += texture(texture0, fragTexCoord - 0.007);
color += texture(texture0, fragTexCoord - 0.009);
color += texture(texture0, fragTexCoord - 0.011);
color.rgb = vec3((color.r + color.g + color.b)/3.0);
color = color/9.5;
fragColor = color;
}

View file

@ -0,0 +1,40 @@
#version 330
in vec2 fragTexCoord;
out vec4 fragColor;
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// NOTE: Add here your custom variables
const float PI = 3.1415926535;
void main()
{
float aperture = 178.0;
float apertureHalf = 0.5 * aperture * (PI / 180.0);
float maxFactor = sin(apertureHalf);
vec2 uv = vec2(0);
vec2 xy = 2.0 * fragTexCoord.xy - 1.0;
float d = length(xy);
if (d < (2.0 - maxFactor))
{
d = length(xy * maxFactor);
float z = sqrt(1.0 - d * d);
float r = atan(d, z) / PI;
float phi = atan(xy.y, xy.x);
uv.x = r * cos(phi) + 0.5;
uv.y = r * sin(phi) + 0.5;
}
else
{
uv = fragTexCoord.xy;
}
fragColor = texture(texture0, uv);
}

View file

@ -0,0 +1,26 @@
#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;
// NOTE: Add here your custom variables
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
// Calculate final fragment color
finalColor = vec4(gray, gray, gray, texelColor.a);
}

View file

@ -0,0 +1,33 @@
#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;
// NOTE: Add here your custom variables
// NOTE: Render size values must be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
uniform float pixelWidth = 5.0;
uniform float pixelHeight = 5.0;
void main()
{
float dx = pixelWidth*(1.0/renderWidth);
float dy = pixelHeight*(1.0/renderHeight);
vec2 coord = vec2(dx*floor(fragTexCoord.x/dx), dy*floor(fragTexCoord.y/dy));
vec3 tc = texture(texture0, coord).rgb;
finalColor = vec4(tc, 1.0);
}

View file

@ -0,0 +1,31 @@
#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;
// NOTE: Add here your custom variables
float gamma = 0.6;
float numColors = 8.0;
void main()
{
// Texel color fetching from texture sampler
vec3 texelColor = texture(texture0, fragTexCoord.xy).rgb;
texelColor = pow(texelColor, vec3(gamma, gamma, gamma));
texelColor = texelColor*numColors;
texelColor = floor(texelColor);
texelColor = texelColor/numColors;
texelColor = pow(texelColor, vec3(1.0/gamma));
finalColor = vec4(texelColor, 1.0);
}

View file

@ -0,0 +1,32 @@
#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;
// NOTE: Add here your custom variables
void main()
{
// Texel color fetching from texture sampler
vec3 texelColor = texture(texture0, fragTexCoord).rgb;
vec3 colors[3];
colors[0] = vec3(0.0, 0.0, 1.0);
colors[1] = vec3(1.0, 1.0, 0.0);
colors[2] = vec3(1.0, 0.0, 0.0);
float lum = (texelColor.r + texelColor.g + texelColor.b)/3.0;
int ix = (lum < 0.5)? 0:1;
vec3 tc = mix(colors[ix], colors[ix + 1], (lum - float(ix)*0.5)/0.5);
finalColor = vec4(tc, 1.0);
}

View file

@ -0,0 +1,49 @@
#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;
// NOTE: Add here your custom variables
// NOTE: Render size values must be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float offset = 0.0;
uniform float time;
void main()
{
float frequency = renderHeight/3.0;
/*
// Scanlines method 1
float tval = 0; //time
vec2 uv = 0.5 + (fragTexCoord - 0.5)*(0.9 + 0.01*sin(0.5*tval));
vec4 color = texture(texture0, fragTexCoord);
color = clamp(color*0.5 + 0.5*color*color*1.2, 0.0, 1.0);
color *= 0.5 + 0.5*16.0*uv.x*uv.y*(1.0 - uv.x)*(1.0 - uv.y);
color *= vec4(0.8, 1.0, 0.7, 1);
color *= 0.9 + 0.1*sin(10.0*tval + uv.y*1000.0);
color *= 0.97 + 0.03*sin(110.0*tval);
fragColor = color;
*/
// Scanlines method 2
float globalPos = (fragTexCoord.y + offset) * frequency;
float wavePos = cos((fract(globalPos) - 0.5)*3.14);
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord);
finalColor = mix(vec4(0.0, 0.3, 0.0, 0.0), texelColor, wavePos);
}

View file

@ -0,0 +1,41 @@
#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;
// NOTE: Add here your custom variables
uniform vec2 resolution = vec2(800, 450);
void main()
{
float x = 1.0/resolution.x;
float y = 1.0/resolution.y;
vec4 horizEdge = vec4(0.0);
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0;
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y ))*2.0;
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y ))*2.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0;
vec4 vertEdge = vec4(0.0);
vertEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0;
vertEdge -= texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y - y))*2.0;
vertEdge -= texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0;
vertEdge += texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0;
vertEdge += texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y + y))*2.0;
vertEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0;
vec3 edge = sqrt((horizEdge.rgb*horizEdge.rgb) + (vertEdge.rgb*vertEdge.rgb));
finalColor = vec4(edge, texture2D(texture0, fragTexCoord).a);
}

View file

@ -0,0 +1,148 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
const MaxPostproShaders = 12
const (
FxGrayscale = iota
FxPosterization
FxDreamVision
FxPixelizer
FxCrossHatching
FxCrossStitching
FxPredatorView
FxScanlines
FxFisheye
FxSobel
FxBloom
FxBlur
)
var postproShaderText = []string{
"GRAYSCALE",
"POSTERIZATION",
"DREAM_VISION",
"PIXELIZER",
"CROSS_HATCHING",
"CROSS_STITCHING",
"PREDATOR_VIEW",
"SCANLINES",
"FISHEYE",
"SOBEL",
"BLOOM",
"BLUR",
}
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint) // Enable Multi Sampling Anti Aliasing 4x (if available)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader")
camera := rl.Camera{}
camera.Position = rl.NewVector3(2.0, 3.0, 2.0)
camera.Target = rl.NewVector3(0.0, 1.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
obj := rl.LoadModel("church.obj") // Load OBJ model
texture := rl.LoadTexture("church_diffuse.png") // Load model texture
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set obj model diffuse texture
position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position
// Load all postpro shaders
// NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER)
shaders := make([]rl.Shader, MaxPostproShaders)
shaders[FxGrayscale] = rl.LoadShader("", "glsl330/grayscale.fs")
shaders[FxPosterization] = rl.LoadShader("", "glsl330/posterization.fs")
shaders[FxDreamVision] = rl.LoadShader("", "glsl330/dream_vision.fs")
shaders[FxPixelizer] = rl.LoadShader("", "glsl330/pixelizer.fs")
shaders[FxCrossHatching] = rl.LoadShader("", "glsl330/cross_hatching.fs")
shaders[FxCrossStitching] = rl.LoadShader("", "glsl330/cross_stitching.fs")
shaders[FxPredatorView] = rl.LoadShader("", "glsl330/predator.fs")
shaders[FxScanlines] = rl.LoadShader("", "glsl330/scanlines.fs")
shaders[FxFisheye] = rl.LoadShader("", "glsl330/fisheye.fs")
shaders[FxSobel] = rl.LoadShader("", "glsl330/sobel.fs")
shaders[FxBlur] = rl.LoadShader("", "glsl330/blur.fs")
shaders[FxBloom] = rl.LoadShader("", "glsl330/bloom.fs")
currentShader := FxGrayscale
// Create a RenderTexture2D to be used for render to texture
target := rl.LoadRenderTexture(screenWidth, screenHeight)
rl.SetCameraMode(camera, rl.CameraOrbital) // Set free camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
if rl.IsKeyPressed(rl.KeyRight) {
currentShader++
} else if rl.IsKeyPressed(rl.KeyLeft) {
currentShader--
}
if currentShader >= MaxPostproShaders {
currentShader = 0
} else if currentShader < 0 {
currentShader = MaxPostproShaders - 1
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginTextureMode(target) // Enable drawing to texture
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(obj, position, 0.1, rl.White) // Draw 3d model with texture
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.EndTextureMode() // End drawing to texture (now we have a texture available for next passes)
// Render previously generated texture using selected postpro shader
rl.BeginShaderMode(shaders[currentShader])
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
rl.DrawTextureRec(target.Texture, rl.NewRectangle(0, 0, float32(target.Texture.Width), float32(-target.Texture.Height)), rl.NewVector2(0, 0), rl.White)
rl.EndShaderMode()
rl.DrawRectangle(0, 9, 580, 30, rl.Fade(rl.LightGray, 0.7))
rl.DrawText("(c) Church 3D model by Alberto Cano", screenWidth-200, screenHeight-20, 10, rl.DarkGray)
rl.DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, rl.Black)
rl.DrawText(postproShaderText[currentShader], 330, 15, 20, rl.Red)
rl.DrawText("< >", 540, 10, 30, rl.DarkBlue)
rl.DrawFPS(700, 15)
rl.EndDrawing()
}
// Unload all postpro shaders
for i := 0; i < MaxPostproShaders; i++ {
rl.UnloadShader(shaders[i])
}
rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(obj) // Unload model
rl.UnloadRenderTexture(target) // Unload render texture
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

View file

@ -0,0 +1,26 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
// Output vertex attributes (to fragment shader)
out vec2 fragTexCoord;
out vec4 fragColor;
// NOTE: Add here your custom variables
void main()
{
// Send vertex attributes to fragment shader
fragTexCoord = vertexTexCoord;
fragColor = vertexColor;
// Calculate final vertex position
gl_Position = mvp*vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,26 @@
#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;
// NOTE: Add here your custom variables
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
// Calculate final fragment color
finalColor = vec4(gray, gray, gray, texelColor.a);
}

View file

@ -0,0 +1,74 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shapes and texture shaders")
fudesumi := rl.LoadTexture("fudesumi.png")
// NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
shader := rl.LoadShader("glsl330/base.vs", "glsl330/grayscale.fs")
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
// Start drawing with default shader
rl.DrawText("USING DEFAULT SHADER", 20, 40, 10, rl.Red)
rl.DrawCircle(80, 120, 35, rl.DarkBlue)
rl.DrawCircleGradient(80, 220, 60, rl.Green, rl.SkyBlue)
rl.DrawCircleLines(80, 340, 80, rl.DarkBlue)
// Activate our custom shader to be applied on next shapes/textures drawings
rl.BeginShaderMode(shader)
rl.DrawText("USING CUSTOM SHADER", 190, 40, 10, rl.Red)
rl.DrawRectangle(250-60, 90, 120, 60, rl.Red)
rl.DrawRectangleGradientH(250-90, 170, 180, 130, rl.Maroon, rl.Gold)
rl.DrawRectangleLines(250-40, 320, 80, 60, rl.Orange)
// Activate our default shader for next drawings
rl.EndShaderMode()
rl.DrawText("USING DEFAULT SHADER", 370, 40, 10, rl.Red)
rl.DrawTriangle(rl.NewVector2(430, 80),
rl.NewVector2(430-60, 150),
rl.NewVector2(430+60, 150), rl.Violet)
rl.DrawTriangleLines(rl.NewVector2(430, 160),
rl.NewVector2(430-20, 230),
rl.NewVector2(430+20, 230), rl.DarkBlue)
rl.DrawPoly(rl.NewVector2(430, 320), 6, 80, 0, rl.Brown)
// Activate our custom shader to be applied on next shapes/textures drawings
rl.BeginShaderMode(shader)
rl.DrawTexture(fudesumi, 500, -30, rl.White) // Using custom shader
// Activate our default shader for next drawings
rl.EndShaderMode()
rl.DrawText("(c) Fudesumi sprite by Eiden Marsal", 380, screenHeight-20, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadShader(shader) // Unload shader
rl.UnloadTexture(fudesumi) // Unload texture
rl.CloseWindow() // Close window and OpenGL context
}