Initial commit

This commit is contained in:
2022-01-20 21:58:50 +08:00
commit b44d41ec66
86 changed files with 5415 additions and 0 deletions

82
cmd/main.go Normal file
View File

@ -0,0 +1,82 @@
package main
import (
"fmt"
"runtime"
"time"
"edgaru089.ml/go/gl01/internal/game"
_ "edgaru089.ml/go/gl01/internal/render/gpu_preference"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
const (
//windowWidth = 852
//windowHeight = 480
windowWidth = 1600
windowHeight = 900
)
func init() {
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, 1)
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
win, err := glfw.CreateWindow(windowWidth, windowHeight, "Gl01", nil, nil)
if err != nil {
panic(err)
}
win.MakeContextCurrent()
glfw.SwapInterval(1)
err = gl.Init()
if err != nil {
panic(err)
}
fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))
game := game.NewGame()
game.Init(win)
gl.ClearColor(0.6, 0.8, 1.0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
win.SwapBuffers()
winClock := time.Now()
fpsClock := time.Now()
fpsCounter := 0
for !win.ShouldClose() {
deltaTime := time.Since(winClock)
winClock = time.Now()
game.Update(win, deltaTime)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
game.Render(win)
win.SwapBuffers()
glfw.PollEvents()
if time.Since(fpsClock) >= time.Second {
win.SetTitle(fmt.Sprintf("Gl01 %dFPS", fpsCounter))
fpsCounter = 0
fpsClock = time.Now()
} else {
fpsCounter++
}
}
}

138
cmd/main.go.old Normal file
View File

@ -0,0 +1,138 @@
package main
import (
"fmt"
"runtime"
"time"
"unsafe"
"github.com/Edgaru089/gl01/internal/asset"
"github.com/Edgaru089/gl01/internal/render"
"github.com/Edgaru089/gl01/internal/world"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
)
const (
windowWidth = 800
windowHeight = 600
)
func init() {
runtime.LockOSThread()
}
func main() {
asset.InitWorldTextureAtlas()
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, 0)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
win, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil)
if err != nil {
panic(err)
}
win.MakeContextCurrent()
glfw.SwapInterval(1)
err = gl.Init()
if err != nil {
panic(err)
}
fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))
shader, err := render.NewShader(asset.WorldShaderVert, asset.WorldShaderFrag)
if err != nil {
panic(err)
}
model := mgl32.Ident4()
shader.SetUniformMat4("model", model)
view := mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
shader.SetUniformMat4("view", view)
projection := mgl32.Perspective(mgl32.DegToRad(45), float32(windowWidth)/float32(windowHeight), 0.1, 10)
shader.SetUniformMat4("projection", projection)
// Texture
asset.InitWorldTextureAtlas()
worldTexture := render.NewTextureRGBA(asset.WorldTextureAtlas.Image)
shader.SetUniformTexture("tex", worldTexture)
gl.BindFragDataLocation(shader.Handle(), 0, gl.Str("outputColor\x00"))
var c, cm world.Chunk
c.SetBlock(0, 0, 0, 2)
c.SetBlock(0, 0, 2, 1)
cm.SetChunkID(-1, -1)
cm.SetBlock(15, 0, 15, 1)
cm.SetBlock(15, 1, 15, 2)
var arr []world.Vertex
arr = c.AppendVertex(arr)
arr = cm.AppendVertex(arr)
// Vertex Array
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
// Vertex Buffer
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(world.Vertex{}))*len(arr), gl.Ptr(arr), gl.STATIC_DRAW)
vertAttrib := uint32(gl.GetAttribLocation(shader.Handle(), gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.World))))
texCoordAttrib := uint32(gl.GetAttribLocation(shader.Handle(), gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Texture))))
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.ClearColor(1, 1, 1, 1)
angle := 0.0
prevTime := time.Now()
for !win.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Angle update
angle += time.Since(prevTime).Seconds()
prevTime = time.Now()
model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
// Render
// Shader variable
shader.UseProgram()
shader.BindTextures()
shader.SetUniformMat4("model", model)
gl.BindVertexArray(vao)
//gl.ActiveTexture(gl.TEXTURE1)
//gl.BindTexture(gl.TEXTURE_2D, texture)
gl.DrawArrays(gl.TRIANGLES, 0, int32(len(arr)))
win.SwapBuffers()
glfw.PollEvents()
}
}

94
cmd/main.go.old2 Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"fmt"
"runtime"
"time"
"github.com/Edgaru089/gl01/internal/render"
"github.com/Edgaru089/gl01/internal/util/itype"
"github.com/Edgaru089/gl01/internal/world"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
const (
windowWidth = 852
windowHeight = 480
)
func init() {
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, 0)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
win, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil)
if err != nil {
panic(err)
}
win.MakeContextCurrent()
glfw.SwapInterval(1)
err = gl.Init()
if err != nil {
panic(err)
}
fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))
var c, cm world.Chunk
c.SetBlock(0, 0, 0, 2)
c.SetBlock(1, 0, 2, 1)
c.SetBlock(2, 0, 2, 3)
cm.SetChunkID(-1, -1)
cm.SetBlock(15, 0, 15, 5)
cm.SetBlock(15, 1, 15, 6)
world.DefaultWorld.SetChunk(0, 0, &c)
world.DefaultWorld.SetChunk(-1, -1, &cm)
var rotY, rotZ float32 = 135, -45
render.DefaultWorldRenderer.LookAt(itype.Vec3f{3, 3, 3}, rotY, rotZ)
render.DefaultWorldRenderer.SetAspect(float32(windowWidth) / float32(windowHeight))
render.DefaultWorldRenderer.SetFovY(45)
render.DefaultWorldRenderer.Init(&world.DefaultWorld)
gl.ClearColor(1, 1, 1, 1)
winClock := time.Now()
for !win.ShouldClose() {
deltaTime := time.Since(winClock)
winClock = time.Now()
if win.GetKey(glfw.KeyLeft) == glfw.Press {
rotY += float32(deltaTime.Seconds()) * 100
}
if win.GetKey(glfw.KeyRight) == glfw.Press {
rotY -= float32(deltaTime.Seconds()) * 100
}
if win.GetKey(glfw.KeyUp) == glfw.Press {
rotZ += float32(deltaTime.Seconds()) * 100
}
if win.GetKey(glfw.KeyDown) == glfw.Press {
rotZ -= float32(deltaTime.Seconds()) * 100
}
render.DefaultWorldRenderer.LookAt(itype.Vec3f{3, 3, 3}, rotY, rotZ)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
render.DefaultWorldRenderer.Render(&world.DefaultWorld)
win.SwapBuffers()
glfw.PollEvents()
}
}