mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Day 9: Fix back-face culling bug+proper normals+mouse inputs
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
[Window][Debug##Default]
|
[Window][Debug##Default]
|
||||||
Pos=809,63
|
Pos=893,32
|
||||||
Size=449,377
|
Size=378,371
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
|
|||||||
@ -18,9 +18,17 @@ type mouseBtnState struct {
|
|||||||
IsDoubleClicked bool
|
IsDoubleClicked bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mouseMotionState struct {
|
||||||
|
XDelta int32
|
||||||
|
YDelta int32
|
||||||
|
XPos int32
|
||||||
|
YPos int32
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
keyMap = make(map[sdl.Keycode]*keyState)
|
keyMap = make(map[sdl.Keycode]*keyState)
|
||||||
mouseBtnMap = make(map[int]*mouseBtnState)
|
mouseBtnMap = make(map[int]*mouseBtnState)
|
||||||
|
mouseMotion = mouseMotionState{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func EventLoopStart() {
|
func EventLoopStart() {
|
||||||
@ -35,6 +43,9 @@ func EventLoopStart() {
|
|||||||
v.IsReleasedThisFrame = false
|
v.IsReleasedThisFrame = false
|
||||||
v.IsDoubleClicked = false
|
v.IsDoubleClicked = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mouseMotion.XDelta = 0
|
||||||
|
mouseMotion.YDelta = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||||
@ -59,12 +70,48 @@ func HandleMouseEvent(e *sdl.MouseButtonEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mb.State = int(e.State)
|
mb.State = int(e.State)
|
||||||
|
|
||||||
mb.IsDoubleClicked = e.Clicks == 2 && e.State == sdl.PRESSED
|
mb.IsDoubleClicked = e.Clicks == 2 && e.State == sdl.PRESSED
|
||||||
mb.IsPressedThisFrame = e.State == sdl.PRESSED
|
mb.IsPressedThisFrame = e.State == sdl.PRESSED
|
||||||
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleMouseMotion(e *sdl.MouseMotionEvent) {
|
||||||
|
|
||||||
|
mouseMotion.XPos = e.X
|
||||||
|
mouseMotion.YPos = e.Y
|
||||||
|
|
||||||
|
mouseMotion.XDelta = e.XRel
|
||||||
|
mouseMotion.YDelta = e.YRel
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetMousePos returns the window coordinates of the mouse
|
||||||
|
func GetMousePos() (x, y int32) {
|
||||||
|
return mouseMotion.XPos, mouseMotion.YPos
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetMouseMotion returns how many pixels were moved last frame
|
||||||
|
func GetMouseMotion() (xDelta, yDelta int32) {
|
||||||
|
return mouseMotion.XDelta, mouseMotion.YDelta
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMouseMotionNorm() (xDelta, yDelta int32) {
|
||||||
|
|
||||||
|
x, y := mouseMotion.XDelta, mouseMotion.YDelta
|
||||||
|
if x > 0 {
|
||||||
|
x = 1
|
||||||
|
} else if x < 0 {
|
||||||
|
x = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if y > 0 {
|
||||||
|
y = -1
|
||||||
|
} else if y < 0 {
|
||||||
|
y = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return x, y
|
||||||
|
}
|
||||||
|
|
||||||
func KeyClicked(kc sdl.Keycode) bool {
|
func KeyClicked(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks := keyMap[kc]
|
||||||
|
|||||||
61
main.go
61
main.go
@ -17,11 +17,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//TODO:
|
//TODO:
|
||||||
|
//Timing and deltatime
|
||||||
|
//Make a window/engine class
|
||||||
|
//Mesh class
|
||||||
//Abstract UI
|
//Abstract UI
|
||||||
//Asset loading
|
//Textures
|
||||||
|
//Proper Asset loading
|
||||||
//Rework buffers package
|
//Rework buffers package
|
||||||
//Interleaved or packed buffers (xyzxyzxyz OR xxxyyyzzz)
|
//Interleaved or packed buffers (xyzxyzxyz OR xxxyyyzzz)
|
||||||
//Timing and deltatime
|
//Audio
|
||||||
|
|
||||||
type ImguiInfo struct {
|
type ImguiInfo struct {
|
||||||
imCtx *imgui.Context
|
imCtx *imgui.Context
|
||||||
@ -47,6 +51,9 @@ var (
|
|||||||
projMat = &gglm.Mat4{}
|
projMat = &gglm.Mat4{}
|
||||||
|
|
||||||
imguiInfo *ImguiInfo
|
imguiInfo *ImguiInfo
|
||||||
|
|
||||||
|
camPos = gglm.NewVec3(0, 0, 10)
|
||||||
|
camForward = gglm.NewVec3(0, 0, 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -98,10 +105,8 @@ func main() {
|
|||||||
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||||
|
|
||||||
//Moves objects into the cameras view
|
//Moves objects into the cameras view
|
||||||
camPos := gglm.NewVec3(0, 0, 10)
|
camPos = gglm.NewVec3(0, 0, 10)
|
||||||
targetPos := gglm.NewVec3(0, 0, 0)
|
updateViewMat()
|
||||||
viewMat := gglm.LookAt(camPos, targetPos, gglm.NewVec3(0, 1, 0))
|
|
||||||
simpleShader.SetUnifMat4("viewMat", &viewMat.Mat4)
|
|
||||||
|
|
||||||
//Perspective/Depth
|
//Perspective/Depth
|
||||||
projMat := gglm.Perspective(45*gglm.Deg2Rad, float32(winWidth)/float32(winHeight), 0.1, 20)
|
projMat := gglm.Perspective(45*gglm.Deg2Rad, float32(winWidth)/float32(winHeight), 0.1, 20)
|
||||||
@ -122,6 +127,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateViewMat() {
|
||||||
|
targetPos := gglm.NewVec3(0, 0, 0)
|
||||||
|
viewMat := gglm.LookAt(camPos, targetPos, gglm.NewVec3(0, 1, 0))
|
||||||
|
simpleShader.SetUnifMat4("viewMat", &viewMat.Mat4)
|
||||||
|
}
|
||||||
|
|
||||||
func initSDL() error {
|
func initSDL() error {
|
||||||
|
|
||||||
err := sdl.Init(sdl.INIT_EVERYTHING)
|
err := sdl.Init(sdl.INIT_EVERYTHING)
|
||||||
@ -129,6 +140,8 @@ func initSDL() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sdl.ShowCursor(1)
|
||||||
|
|
||||||
sdl.GLSetAttribute(sdl.MAJOR_VERSION, 4)
|
sdl.GLSetAttribute(sdl.MAJOR_VERSION, 4)
|
||||||
sdl.GLSetAttribute(sdl.MINOR_VERSION, 1)
|
sdl.GLSetAttribute(sdl.MINOR_VERSION, 1)
|
||||||
|
|
||||||
@ -152,6 +165,12 @@ func initOpenGL() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gl.Enable(gl.DEPTH_TEST)
|
||||||
|
|
||||||
|
gl.Enable(gl.CULL_FACE)
|
||||||
|
gl.CullFace(gl.BACK)
|
||||||
|
gl.FrontFace(gl.CCW)
|
||||||
|
|
||||||
gl.ClearColor(0, 0, 0, 1)
|
gl.ClearColor(0, 0, 0, 1)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -240,6 +259,7 @@ func flattenFaces(faces []asig.Face) []uint32 {
|
|||||||
|
|
||||||
func loadBuffers() {
|
func loadBuffers() {
|
||||||
|
|
||||||
|
// scene, release, err := asig.ImportFile("./res/models/weird-cube.fbx", asig.PostProcessTriangulate)
|
||||||
scene, release, err := asig.ImportFile("./res/models/color-cube.fbx", asig.PostProcessTriangulate)
|
scene, release, err := asig.ImportFile("./res/models/color-cube.fbx", asig.PostProcessTriangulate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.ErrLog.Panicln("Failed to load model. Err: " + err.Error())
|
logging.ErrLog.Panicln("Failed to load model. Err: " + err.Error())
|
||||||
@ -357,6 +377,9 @@ func handleInputs() {
|
|||||||
case *sdl.MouseButtonEvent:
|
case *sdl.MouseButtonEvent:
|
||||||
input.HandleMouseEvent(e)
|
input.HandleMouseEvent(e)
|
||||||
|
|
||||||
|
case *sdl.MouseMotionEvent:
|
||||||
|
input.HandleMouseMotion(e)
|
||||||
|
|
||||||
case *sdl.WindowEvent:
|
case *sdl.WindowEvent:
|
||||||
|
|
||||||
//NOTE: SDL is not firing window resize, but is resizing the window by itself
|
//NOTE: SDL is not firing window resize, but is resizing the window by itself
|
||||||
@ -421,16 +444,20 @@ var lightColor1 gglm.Vec3 = *gglm.NewVec3(1, 1, 1)
|
|||||||
func runGameLogic() {
|
func runGameLogic() {
|
||||||
|
|
||||||
if input.KeyDown(sdl.K_w) {
|
if input.KeyDown(sdl.K_w) {
|
||||||
modelMat.Translate(gglm.NewVec3(0, 0, -0.1))
|
camPos.Data[1] += -0.1
|
||||||
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_s) {
|
if input.KeyDown(sdl.K_s) {
|
||||||
modelMat.Translate(gglm.NewVec3(0, 0, 0.1))
|
camPos.Data[1] += 0.1
|
||||||
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_d) {
|
if input.KeyDown(sdl.K_d) {
|
||||||
modelMat.Translate(gglm.NewVec3(0.1, 0, 0))
|
camPos.Data[0] += 0.1
|
||||||
|
updateViewMat()
|
||||||
}
|
}
|
||||||
if input.KeyDown(sdl.K_a) {
|
if input.KeyDown(sdl.K_a) {
|
||||||
modelMat.Translate(gglm.NewVec3(-0.1, 0, 00))
|
camPos.Data[0] += -0.1
|
||||||
|
updateViewMat()
|
||||||
}
|
}
|
||||||
|
|
||||||
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
simpleShader.SetUnifMat4("modelMat", &modelMat.Mat4)
|
||||||
@ -450,10 +477,6 @@ func runGameLogic() {
|
|||||||
time = currentTime
|
time = currentTime
|
||||||
|
|
||||||
imgui.NewFrame()
|
imgui.NewFrame()
|
||||||
if imgui.Button("Click Me!") {
|
|
||||||
logging.InfoLog.Println("Clicked!")
|
|
||||||
}
|
|
||||||
imgui.InputText("Name", &name)
|
|
||||||
|
|
||||||
if imgui.SliderFloat3("Ambient Color", &ambientColor.Data, 0, 1) {
|
if imgui.SliderFloat3("Ambient Color", &ambientColor.Data, 0, 1) {
|
||||||
simpleShader.SetUnifVec3("ambientLightColor", &ambientColor)
|
simpleShader.SetUnifVec3("ambientLightColor", &ambientColor)
|
||||||
@ -471,19 +494,25 @@ func runGameLogic() {
|
|||||||
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if imgui.SliderFloat3("Cam pos", &camPos.Data, -10, 10) {
|
||||||
|
updateViewMat()
|
||||||
|
}
|
||||||
|
|
||||||
imgui.Render()
|
imgui.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
func draw() {
|
func draw() {
|
||||||
|
|
||||||
gl.Disable(gl.SCISSOR_TEST)
|
gl.Disable(gl.SCISSOR_TEST)
|
||||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
gl.Enable(gl.CULL_FACE)
|
||||||
|
gl.Enable(gl.DEPTH_TEST)
|
||||||
|
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||||
|
|
||||||
simpleShader.Activate()
|
simpleShader.Activate()
|
||||||
|
|
||||||
//DRAW
|
//DRAW
|
||||||
bo.Activate()
|
bo.Activate()
|
||||||
gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_INT, gl.PtrOffset(0))
|
gl.DrawElements(gl.TRIANGLES, 60, gl.UNSIGNED_INT, gl.PtrOffset(0))
|
||||||
bo.Deactivate()
|
bo.Deactivate()
|
||||||
|
|
||||||
drawUI()
|
drawUI()
|
||||||
|
|||||||
BIN
res/models/weird-cube.fbx
Executable file
BIN
res/models/weird-cube.fbx
Executable file
Binary file not shown.
@ -16,7 +16,7 @@ uniform mat4 projMat;
|
|||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vertColor = vertColorIn;
|
vertColor = vertColorIn;
|
||||||
vertNormal = vertNormalIn;
|
vertNormal = mat3(transpose(inverse(modelMat))) * vertNormalIn;
|
||||||
fragPos = vec3(modelMat * vec4(vertPosIn, 1.0));
|
fragPos = vec3(modelMat * vec4(vertPosIn, 1.0));
|
||||||
|
|
||||||
gl_Position = projMat * viewMat * modelMat * vec4(vertPosIn, 1.0);
|
gl_Position = projMat * viewMat * modelMat * vec4(vertPosIn, 1.0);
|
||||||
|
|||||||
Reference in New Issue
Block a user