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

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -0,0 +1,48 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards")
camera := rl.Camera{}
camera.Position = rl.NewVector3(5.0, 4.0, 5.0)
camera.Target = rl.NewVector3(0.0, 2.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
bill := rl.LoadTexture("billboard.png") // Our texture billboard
billPosition := rl.NewVector3(0.0, 2.0, 0.0) // Position where draw billboard
rl.SetCameraMode(camera, rl.CameraOrbital) // Set an orbital camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateCamera(&camera) // Update camera
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawGrid(10, 1.0) // Draw a grid
rl.DrawBillboard(camera, bill, billPosition, 2.0, rl.White)
rl.EndMode3D()
rl.EndDrawing()
}
rl.UnloadTexture(bill) // Unload texture
rl.CloseWindow()
}

View file

@ -0,0 +1,110 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions")
camera := rl.Camera{}
camera.Position = rl.NewVector3(0.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
playerPosition := rl.NewVector3(0.0, 1.0, 2.0)
playerSize := rl.NewVector3(1.0, 2.0, 1.0)
playerColor := rl.Green
enemyBoxPos := rl.NewVector3(-4.0, 1.0, 0.0)
enemyBoxSize := rl.NewVector3(2.0, 2.0, 2.0)
enemySpherePos := rl.NewVector3(4.0, 0.0, 0.0)
enemySphereSize := float32(1.5)
collision := false
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
// Move player
if rl.IsKeyDown(rl.KeyRight) {
playerPosition.X += 0.2
} else if rl.IsKeyDown(rl.KeyLeft) {
playerPosition.X -= 0.2
} else if rl.IsKeyDown(rl.KeyDown) {
playerPosition.Z += 0.2
} else if rl.IsKeyDown(rl.KeyUp) {
playerPosition.Z -= 0.2
}
collision = false
// Check collisions player vs enemy-box
if rl.CheckCollisionBoxes(
rl.NewBoundingBox(
rl.NewVector3(playerPosition.X-playerSize.X/2, playerPosition.Y-playerSize.Y/2, playerPosition.Z-playerSize.Z/2),
rl.NewVector3(playerPosition.X+playerSize.X/2, playerPosition.Y+playerSize.Y/2, playerPosition.Z+playerSize.Z/2)),
rl.NewBoundingBox(
rl.NewVector3(enemyBoxPos.X-enemyBoxSize.X/2, enemyBoxPos.Y-enemyBoxSize.Y/2, enemyBoxPos.Z-enemyBoxSize.Z/2),
rl.NewVector3(enemyBoxPos.X+enemyBoxSize.X/2, enemyBoxPos.Y+enemyBoxSize.Y/2, enemyBoxPos.Z+enemyBoxSize.Z/2)),
) {
collision = true
}
// Check collisions player vs enemy-sphere
if rl.CheckCollisionBoxSphere(
rl.NewBoundingBox(
rl.NewVector3(playerPosition.X-playerSize.X/2, playerPosition.Y-playerSize.Y/2, playerPosition.Z-playerSize.Z/2),
rl.NewVector3(playerPosition.X+playerSize.X/2, playerPosition.Y+playerSize.Y/2, playerPosition.Z+playerSize.Z/2)),
enemySpherePos,
enemySphereSize,
) {
collision = true
}
if collision {
playerColor = rl.Red
} else {
playerColor = rl.Green
}
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
// Draw enemy-box
rl.DrawCube(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, rl.Gray)
rl.DrawCubeWires(enemyBoxPos, enemyBoxSize.X, enemyBoxSize.Y, enemyBoxSize.Z, rl.DarkGray)
// Draw enemy-sphere
rl.DrawSphere(enemySpherePos, enemySphereSize, rl.Gray)
rl.DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, rl.DarkGray)
// Draw player
rl.DrawCubeV(playerPosition, playerSize, playerColor)
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.DrawText("Move player with cursors to collide", 220, 40, 20, rl.Gray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View file

@ -0,0 +1,70 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing")
camera := rl.Camera{}
camera.Position = rl.NewVector3(16.0, 14.0, 16.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
image := rl.LoadImage("cubicmap.png") // Load cubicmap image (RAM)
cubicmap := rl.LoadTextureFromImage(image) // Convert image to texture to display (VRAM)
mesh := rl.GenMeshCubicmap(*image, rl.NewVector3(1.0, 1.0, 1.0))
model := rl.LoadModelFromMesh(mesh)
// NOTE: By default each cube is mapped to one part of texture atlas
texture := rl.LoadTexture("cubicmap_atlas.png") // Load map texture
rl.SetMaterialTexture(model.Materials, rl.MapDiffuse, texture) // Set map diffuse texture
mapPosition := rl.NewVector3(-16.0, 0.0, -8.0) // Set model position
rl.UnloadImage(image) // Unload cubicmap image from RAM, already uploaded to VRAM
rl.SetCameraMode(camera, rl.CameraOrbital) // Set an orbital camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
rl.UpdateCamera(&camera) // Update camera
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(model, mapPosition, 1.0, rl.White)
rl.EndMode3D()
rl.DrawTextureEx(cubicmap, rl.NewVector2(float32(screenWidth-cubicmap.Width*4-20), 20), 0.0, 4.0, rl.White)
rl.DrawRectangleLines(screenWidth-cubicmap.Width*4-20, 20, cubicmap.Width*4, cubicmap.Height*4, rl.Green)
rl.DrawText("cubicmap image used to", 658, 90, 10, rl.Gray)
rl.DrawText("generate map 3d model", 658, 104, 10, rl.Gray)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.UnloadTexture(cubicmap) // Unload cubicmap texture
rl.UnloadTexture(texture) // Unload map texture
rl.UnloadModel(model) // Unload map model
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View file

@ -0,0 +1,119 @@
/*******************************************************************************************
*
* raylib [models] example - first person maze
*
* This example has been created using raylib-go v0.0.0-20220104071325-2f072dc2d259 (https://github.com/gen2brain/raylib-go)
* raylib-go is licensed under an unmodified zlib/libpng license (https://github.com/gen2brain/raylib-go/blob/master/LICENSE)
*
* Original C version for Raylib 2.5 Copyright (c) 2019 Ramon Santamaria (@raysan5)
* Converted to Go by Michael Redman January 4, 2022
*
********************************************************************************************/
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
// Initialization
//--------------------------------------------------------------------------------------
var screenWidth int32 = 800
var screenHeight int32 = 450
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze")
// Define the camera to look into our 3d world
camera := rl.Camera{}
camera.Position = rl.NewVector3(0.2, 0.4, 0.2)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
imMap := rl.LoadImage("cubicmap.png") // Load cubicmap image (RAM)
cubicmap := rl.LoadTextureFromImage(imMap) // Convert image to texture to display (VRAM)
mesh := rl.GenMeshCubicmap(*imMap, rl.NewVector3(1.0, 1.0, 1.0))
model := rl.LoadModelFromMesh(mesh)
// NOTE: By default each cube is mapped to one part of texture atlas
texture := rl.LoadTexture("cubicmap_atlas.png") // load map texture
model.Materials.GetMap(rl.MapDiffuse).Texture = texture // Set map diffuse texture
// Get map image data to be used for collision detectio
mapPixels := rl.LoadImageColors(imMap)
rl.UnloadImage(imMap) // Unload image from RAM
mapPosition := rl.NewVector3(-16.0, 0.0, -8.0) // Set model position
rl.SetCameraMode(camera, rl.CameraFirstPerson) // Set camera mode
rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
for !rl.WindowShouldClose() { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
oldCamPos := camera.Position // Store old camera position
rl.UpdateCamera(&camera) // Update camera
// Check player collision (we simplify to 2D collision detection)
playerPos := rl.NewVector2(camera.Position.X, camera.Position.Z)
playerRadius := 0.1 // Collision radius (player is modelled as a cylinder for collision)
playerCellX := (int)(playerPos.X - mapPosition.X + 0.5)
playerCellY := (int)(playerPos.Y - mapPosition.Z + 0.5)
// Out-of-limits security check
if playerCellX < 0 {
playerCellX = 0
} else if playerCellX >= int(cubicmap.Width) {
playerCellX = int(cubicmap.Width) - 1
}
if playerCellY < 0 {
playerCellY = 0
} else if playerCellY >= int(cubicmap.Height) {
playerCellY = int(cubicmap.Height) - 1
}
// Check map collisions using image data and player position
// TODO: Improvement: Just check player surrounding cells for collision
for y := 0; y < int(cubicmap.Height); y++ {
for x := 0; x < int(cubicmap.Width); x++ {
// Collision: white pixel, only check R channel
if mapPixels[y*int(cubicmap.Width)+x].R == 255 && (rl.CheckCollisionCircleRec(playerPos, float32(playerRadius), rl.NewRectangle(float32(mapPosition.X-0.5+float32(x)), float32(mapPosition.Z-0.5+float32(y)), 1.0, 1.0))) {
// Collision detected, reset camera position
camera.Position = oldCamPos
}
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(model, mapPosition, 1.0, rl.White) // Draw maze map
rl.EndMode3D()
rl.DrawTextureEx(cubicmap, rl.NewVector2(float32(rl.GetScreenWidth())-float32(cubicmap.Width)*4.0-20, 20.0), 0.0, 4.0, rl.White)
rl.DrawRectangleLines(int32(rl.GetScreenWidth())-cubicmap.Width*4-20, 20, cubicmap.Width*4, cubicmap.Height*4, rl.Green)
// Draw player position radar
rl.DrawRectangle(int32(rl.GetScreenWidth()-int(cubicmap.Width*4)-20+(playerCellX*4)), int32(20+playerCellY*4), 4, 4, rl.Red)
rl.DrawFPS(10, 10)
rl.EndDrawing()
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
rl.UnloadTexture(cubicmap) // Unload cubicmap texture
rl.UnloadTexture(texture) // Unload map texture
rl.UnloadModel(model) // Unload map model
rl.CloseWindow() // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

View file

@ -0,0 +1,52 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes")
camera := rl.Camera{}
camera.Position = rl.NewVector3(0.0, 10.0, 10.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawCube(rl.NewVector3(-4.0, 0.0, 2.0), 2.0, 5.0, 2.0, rl.Red)
rl.DrawCubeWires(rl.NewVector3(-4.0, 0.0, 2.0), 2.0, 5.0, 2.0, rl.Gold)
rl.DrawCubeWires(rl.NewVector3(-4.0, 0.0, -2.0), 3.0, 6.0, 2.0, rl.Maroon)
rl.DrawSphere(rl.NewVector3(-1.0, 0.0, -2.0), 1.0, rl.Green)
rl.DrawSphereWires(rl.NewVector3(1.0, 0.0, 2.0), 2.0, 16, 16, rl.Lime)
rl.DrawCylinder(rl.NewVector3(4.0, 0.0, -2.0), 1.0, 2.0, 3.0, 4, rl.SkyBlue)
rl.DrawCylinderWires(rl.NewVector3(4.0, 0.0, -2.0), 1.0, 2.0, 3.0, 4, rl.DarkBlue)
rl.DrawCylinderWires(rl.NewVector3(4.5, -1.0, 2.0), 1.0, 1.0, 2.0, 6, rl.Brown)
rl.DrawCylinder(rl.NewVector3(1.0, 0.0, -4.0), 0.0, 1.5, 3.0, 8, rl.Gold)
rl.DrawCylinderWires(rl.NewVector3(1.0, 0.0, -4.0), 0.0, 1.5, 3.0, 8, rl.Pink)
rl.DrawGrid(10, 1.0) // Draw a grid
rl.EndMode3D()
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.CloseWindow()
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View file

@ -0,0 +1,68 @@
package main
import (
//"fmt"
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing")
camera := rl.Camera{}
camera.Position = rl.NewVector3(18.0, 16.0, 18.0)
camera.Target = rl.NewVector3(0.0, 0.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
image := rl.LoadImage("heightmap.png") // Load heightmap image (RAM)
texture := rl.LoadTextureFromImage(image) // Convert image to texture (VRAM)
mesh := rl.GenMeshHeightmap(*image, rl.NewVector3(16, 8, 16)) // Generate heightmap mesh (RAM and VRAM)
model := rl.LoadModelFromMesh(mesh) // Load model from generated mesh
rl.SetMaterialTexture(model.Materials, rl.MapDiffuse, texture) // Set map diffuse texture
mapPosition := rl.NewVector3(-8.0, 0.0, -8.0) // Set model position
rl.UnloadImage(image) // Unload heightmap image from RAM, already uploaded to VRAM
rl.SetCameraMode(camera, rl.CameraOrbital) // Set an orbital camera mode
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
// Update
rl.UpdateCamera(&camera) // Update camera
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(model, mapPosition, 1.0, rl.Red)
rl.DrawGrid(20, 1.0)
rl.EndMode3D()
rl.DrawTexture(texture, screenWidth-texture.Width-20, 20, rl.White)
rl.DrawRectangleLines(screenWidth-texture.Width-20, 20, texture.Width, texture.Height, rl.Green)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
rl.UnloadTexture(texture) // Unload map texture
rl.UnloadModel(model) // Unload map model
rl.CloseWindow()
}

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 434 KiB

View file

@ -0,0 +1,51 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [models] example - obj model loading")
camera := rl.Camera{}
camera.Position = rl.NewVector3(50.0, 50.0, 50.0)
camera.Target = rl.NewVector3(0.0, 10.0, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
obj := rl.LoadModel("castle.obj") // Load OBJ model
texture := rl.LoadTexture("castle_diffuse.png") // Load model texture
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set map diffuse texture
position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(obj, position, 1.0, rl.White) // Draw 3d model with texture
rl.DrawGrid(20, 10.0) // Draw a grid
rl.EndMode3D()
rl.DrawText("(c) Castle 3D model by Alberto Cano", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.EndDrawing()
}
rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(obj) // Unload model
rl.CloseWindow()
}