add more examples
This commit is contained in:
parent
032749cff8
commit
1500124ca1
4 changed files with 267 additions and 0 deletions
BIN
examples/shaders/texture_tiling/cubicmap_atlas.png
Normal file
BIN
examples/shaders/texture_tiling/cubicmap_atlas.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
65
examples/shaders/texture_tiling/main.go
Normal file
65
examples/shaders/texture_tiling/main.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
screenWidth := int32(800)
|
||||||
|
screenHeight := int32(450)
|
||||||
|
|
||||||
|
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture tiling")
|
||||||
|
|
||||||
|
cam := rl.Camera3D{}
|
||||||
|
cam.Position = rl.NewVector3(4, 4, 4)
|
||||||
|
cam.Target = rl.NewVector3(0, 0.5, 0)
|
||||||
|
cam.Up = rl.NewVector3(0, 1, 0)
|
||||||
|
cam.Fovy = 45
|
||||||
|
cam.Projection = rl.CameraPerspective
|
||||||
|
|
||||||
|
cube := rl.GenMeshCube(1, 1, 1)
|
||||||
|
model := rl.LoadModelFromMesh(cube)
|
||||||
|
|
||||||
|
texture := rl.LoadTexture("cubicmap_atlas.png")
|
||||||
|
rl.SetMaterialTexture(model.Materials, rl.MapDiffuse, texture)
|
||||||
|
|
||||||
|
tiling := []float32{3, 3}
|
||||||
|
shader := rl.LoadShader("", "tiling.fs")
|
||||||
|
rl.SetShaderValue(shader, rl.GetShaderLocation(shader, "tiling"), tiling, rl.ShaderUniformVec2)
|
||||||
|
model.Materials.Shader = shader
|
||||||
|
|
||||||
|
rl.DisableCursor()
|
||||||
|
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
rl.UpdateCamera(&cam, rl.CameraOrbital)
|
||||||
|
|
||||||
|
if rl.IsKeyPressed(rl.KeyZ) {
|
||||||
|
cam.Target = rl.NewVector3(0, 0.5, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.BeginDrawing()
|
||||||
|
|
||||||
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
|
||||||
|
rl.BeginMode3D(cam)
|
||||||
|
|
||||||
|
rl.BeginShaderMode(shader)
|
||||||
|
rl.DrawModel(model, rl.Vector3Zero(), 2, rl.White)
|
||||||
|
rl.EndShaderMode()
|
||||||
|
|
||||||
|
rl.DrawGrid(10, 1)
|
||||||
|
|
||||||
|
rl.EndMode3D()
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.UnloadShader(shader)
|
||||||
|
rl.UnloadModel(model)
|
||||||
|
rl.UnloadTexture(texture)
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
14
examples/shaders/texture_tiling/tiling.fs
Normal file
14
examples/shaders/texture_tiling/tiling.fs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#version 330 core
|
||||||
|
|
||||||
|
uniform sampler2D diffuseMap;
|
||||||
|
uniform vec2 tiling;
|
||||||
|
|
||||||
|
in vec2 fragTexCoord;
|
||||||
|
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec2 texCoord = fragTexCoord * tiling;
|
||||||
|
fragColor = texture(diffuseMap, texCoord);
|
||||||
|
}
|
188
examples/textures/mouse_painting/main.go
Normal file
188
examples/textures/mouse_painting/main.go
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
screenW = int32(800)
|
||||||
|
screenH = int32(450)
|
||||||
|
|
||||||
|
colors = []rl.Color{rl.RayWhite, rl.Yellow, rl.Gold, rl.Pink, rl.Red, rl.Maroon, rl.Green, rl.Lime, rl.DarkGreen, rl.SkyBlue, rl.Blue, rl.DarkBlue, rl.Purple, rl.Violet, rl.DarkPurple, rl.Beige, rl.Brown, rl.DarkBrown, rl.LightGray, rl.Gray, rl.DarkGray, rl.Black}
|
||||||
|
|
||||||
|
colorRecs []rl.Rectangle
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
rl.InitWindow(screenW, screenH, "raylib [textures] example - mouse painting")
|
||||||
|
|
||||||
|
for i := 0; i < len(colors); i++ {
|
||||||
|
colorRecs = append(colorRecs, rl.NewRectangle(10+(30*float32(i))+(2*float32(i)), 10, 30, 30))
|
||||||
|
}
|
||||||
|
colorSelected, colorMouseHover := 1, 0
|
||||||
|
colorPrev := colorSelected
|
||||||
|
brushSize := float32(20)
|
||||||
|
mousePressed := false
|
||||||
|
|
||||||
|
btnSaveRec := rl.NewRectangle(750, 10, 40, 30)
|
||||||
|
btnSaveMouseHover, showSaveMsg := false, false
|
||||||
|
saveMsgCount := 0
|
||||||
|
|
||||||
|
target := rl.LoadRenderTexture(screenW, screenH)
|
||||||
|
|
||||||
|
rl.BeginTextureMode(target)
|
||||||
|
rl.ClearBackground(colors[0])
|
||||||
|
rl.EndTextureMode()
|
||||||
|
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
for !rl.WindowShouldClose() {
|
||||||
|
|
||||||
|
mousePos := rl.GetMousePosition()
|
||||||
|
|
||||||
|
if rl.IsKeyPressed(rl.KeyRight) {
|
||||||
|
colorSelected++
|
||||||
|
} else if rl.IsKeyPressed(rl.KeyLeft) {
|
||||||
|
colorSelected--
|
||||||
|
}
|
||||||
|
|
||||||
|
if colorSelected >= len(colors) {
|
||||||
|
colorSelected = len(colors) - 1
|
||||||
|
} else if colorSelected < 0 {
|
||||||
|
colorSelected = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(colorRecs); i++ {
|
||||||
|
if rl.CheckCollisionPointRec(mousePos, colorRecs[i]) {
|
||||||
|
colorMouseHover = i
|
||||||
|
} else {
|
||||||
|
colorMouseHover = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if colorMouseHover >= 0 && rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
|
||||||
|
colorSelected = colorMouseHover
|
||||||
|
colorPrev = colorSelected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsKeyPressed(rl.KeyUp) {
|
||||||
|
brushSize += 5
|
||||||
|
} else if rl.IsKeyPressed(rl.KeyDown) {
|
||||||
|
brushSize -= 5
|
||||||
|
}
|
||||||
|
brushSize += rl.GetMouseWheelMove() * 5
|
||||||
|
if brushSize < 2 {
|
||||||
|
brushSize = 2
|
||||||
|
}
|
||||||
|
if brushSize > 50 {
|
||||||
|
brushSize = 50
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsKeyPressed(rl.KeyC) {
|
||||||
|
rl.BeginTextureMode(target)
|
||||||
|
rl.ClearBackground(colors[0])
|
||||||
|
rl.EndTextureMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsMouseButtonDown(rl.MouseButtonLeft) {
|
||||||
|
rl.BeginTextureMode(target)
|
||||||
|
if mousePos.Y > 50 {
|
||||||
|
rl.DrawCircle(int32(mousePos.X), int32(mousePos.Y), brushSize, colors[colorSelected])
|
||||||
|
}
|
||||||
|
rl.EndTextureMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.IsMouseButtonDown(rl.MouseButtonRight) {
|
||||||
|
if !mousePressed {
|
||||||
|
colorPrev = colorSelected
|
||||||
|
colorSelected = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
mousePressed = true
|
||||||
|
|
||||||
|
rl.BeginTextureMode(target)
|
||||||
|
if mousePos.Y > 50 {
|
||||||
|
rl.DrawCircle(int32(mousePos.X), int32(mousePos.Y), brushSize, colors[0])
|
||||||
|
}
|
||||||
|
rl.EndTextureMode()
|
||||||
|
} else if rl.IsMouseButtonReleased(rl.MouseButtonRight) && mousePressed {
|
||||||
|
colorSelected = colorPrev
|
||||||
|
mousePressed = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if rl.CheckCollisionPointRec(mousePos, btnSaveRec) {
|
||||||
|
btnSaveMouseHover = true
|
||||||
|
} else {
|
||||||
|
btnSaveMouseHover = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if btnSaveMouseHover && rl.IsMouseButtonReleased(rl.MouseButtonLeft) || rl.IsKeyPressed(rl.KeyS) {
|
||||||
|
image := rl.LoadImageFromTexture(target.Texture)
|
||||||
|
rl.ImageFlipVertical(image)
|
||||||
|
rl.ExportImage(*image, "raylib_mouse_painting.png")
|
||||||
|
rl.UnloadImage(image)
|
||||||
|
showSaveMsg = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if showSaveMsg {
|
||||||
|
saveMsgCount++
|
||||||
|
if saveMsgCount > 240 {
|
||||||
|
showSaveMsg = false
|
||||||
|
saveMsgCount = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.BeginDrawing()
|
||||||
|
rl.ClearBackground(rl.RayWhite)
|
||||||
|
|
||||||
|
rl.DrawTextureRec(target.Texture, rl.NewRectangle(0, 0, float32(target.Texture.Width), -float32(target.Texture.Height)), rl.Vector2Zero(), rl.White)
|
||||||
|
|
||||||
|
if mousePos.Y > 50 {
|
||||||
|
if rl.IsMouseButtonDown(rl.MouseButtonRight) {
|
||||||
|
rl.DrawCircleLines(int32(mousePos.X), int32(mousePos.Y), brushSize, rl.Gray)
|
||||||
|
} else {
|
||||||
|
rl.DrawCircle(rl.GetMouseX(), rl.GetMouseY(), brushSize, colors[colorSelected])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangle(0, 0, int32(rl.GetScreenWidth()), 50, rl.RayWhite)
|
||||||
|
rl.DrawLine(0, 50, int32(rl.GetScreenWidth()), 50, rl.LightGray)
|
||||||
|
|
||||||
|
for i := 0; i < len(colors); i++ {
|
||||||
|
rl.DrawRectangleRec(colorRecs[i], colors[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangleLines(10, 10, 30, 30, rl.LightGray)
|
||||||
|
|
||||||
|
if colorMouseHover >= 0 {
|
||||||
|
rl.DrawRectangleRec(colorRecs[colorMouseHover], rl.Fade(rl.White, 0.6))
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawRectangleLinesEx(rl.NewRectangle(colorRecs[colorSelected].X-2, colorRecs[colorSelected].Y-2, colorRecs[colorSelected].Width+4, colorRecs[colorSelected].Height+4), 2, rl.Black)
|
||||||
|
|
||||||
|
if btnSaveMouseHover {
|
||||||
|
rl.DrawRectangleLinesEx(btnSaveRec, 2, rl.Red)
|
||||||
|
rl.DrawText("SAVE!", 755, 20, 10, rl.Red)
|
||||||
|
} else {
|
||||||
|
rl.DrawRectangleLinesEx(btnSaveRec, 2, rl.Black)
|
||||||
|
rl.DrawText("SAVE!", 755, 20, 10, rl.Black)
|
||||||
|
}
|
||||||
|
|
||||||
|
if showSaveMsg {
|
||||||
|
rl.DrawRectangle(0, 0, int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()), rl.Fade(rl.RayWhite, 0.8))
|
||||||
|
rl.DrawRectangle(0, screenH/2-40, int32(rl.GetScreenWidth()), 80, rl.Black)
|
||||||
|
txt := "IMG SAVED"
|
||||||
|
txtlen := rl.MeasureText(txt, 20)
|
||||||
|
rl.DrawText(txt, screenW/2-txtlen/2, screenH/2-10, 20, rl.RayWhite)
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.DrawText("hold left mouse to draw right mouse to erase | mouse wheel up/down arrows change brush size | right/left arrows change color | c key to clear", 10, screenH-15, 10, rl.Black)
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.UnloadRenderTexture(target)
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue