Stuff draws correctly with different sizes and colors. Trying to pull out the draw() function failed.

main
Sean Hickey 2021-09-02 00:40:16 -07:00
parent ccc61e9a07
commit 286efb85c5
5 changed files with 222 additions and 62 deletions

159
main.go
View File

@ -57,8 +57,8 @@ func run() {
// Capture the mouse for movement
sdl.SetRelativeMouseMode(true)
// Init and configure global settings
gl.Init()
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gameWindow, err := game_window.NewGameWindow(GameTitle)
if err != nil {
@ -66,24 +66,23 @@ func run() {
}
defer gameWindow.Destroy()
camera := gl_objects.NewCamera()
camera := gl_objects.NewCamera(gameWindow.GLProgram)
camera.Position = mgl32.Vec3{0, 0, 10}
camera.Draw(gameWindow.GLProgram)
modelUniform := gl.GetUniformLocation(gameWindow.GLProgram, gl.Str("model\x00"))
//gl.BindFragDataLocation(gameWindow.GLProgram, 0, gl.Str("outputColor\x00"))
texture, err := gl_helpers.NewTexture("square.png")
textureId, err := gl_helpers.NewTexture("square.png")
if err != nil {
log.Fatal(err)
}
box := gl_objects.NewBox(6.0, 4.0, 2.0, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{1, 1, 1}, true)
vertices := box.GetVertexArray()
log.Println(vertices)
box := gl_objects.NewBox(6.0, 4.0, 2.0, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{1, 0, 0})
//box.GLInit(gameWindow.GLProgram)
//box.SetTexture(texture)
/*
*/
vertexArray := box.VertexArray
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
@ -91,28 +90,32 @@ func run() {
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)
gl.BufferData(gl.ARRAY_BUFFER, len(vertexArray)*4, gl.Ptr(vertexArray), gl.STATIC_DRAW)
var sizeofFloat int32 = 4
stride := gl_objects.VertexSize * sizeofFloat
vertAttrib := uint32(gl.GetAttribLocation(gameWindow.GLProgram, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointerWithOffset(vertAttrib, 3, gl.FLOAT, false, 8*4, 0)
gl.VertexAttribPointerWithOffset(vertAttrib, 3, gl.FLOAT, false, stride, uintptr(0))
vertColorAttrib := uint32(gl.GetAttribLocation(gameWindow.GLProgram, gl.Str("vertColor\x00")))
gl.EnableVertexAttribArray(vertColorAttrib)
gl.VertexAttribPointerWithOffset(vertColorAttrib, 3, gl.FLOAT, false, 8*4, 3*4)
gl.VertexAttribPointerWithOffset(vertColorAttrib, 3, gl.FLOAT, false, stride, uintptr(3*sizeofFloat))
texCoordAttrib := uint32(gl.GetAttribLocation(gameWindow.GLProgram, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, 8*4, 6*4)
// Configure global settings
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, stride, uintptr(6*sizeofFloat))
/*
*/
model := mgl32.Ident4()
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
// Global options after setup is all done
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
running := true
var angle float32 = 0.0
for running {
@ -120,21 +123,27 @@ func run() {
model = mgl32.Ident4()
camera.Update()
camera.Draw(gameWindow.GLProgram)
camera.Draw()
angle += 0.01
// TODO can rotate each object separately
angle += 0.00
model = mgl32.HomogRotate3D(angle, mgl32.Vec3{0, 1, 0})
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
// I think this is what I might need to abstract out into each Draw()
//box.Update()
//box.Draw()
/*
*/
gl.BindVertexArray(vao)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.BindTexture(gl.TEXTURE_2D, textureId)
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // 6 sides, 2 triangles, 3 points each
// end Draw()
/*
*/
gameWindow.SDLWindow.GLSwap()
@ -242,3 +251,105 @@ func handleEvents(running bool, gameWindow *game_window.GameWindow, camera *gl_o
return running
}
var cubeVertices = []float32{
// X, Y, Z, texture U, V
// Bottom
-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
// Top
-1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
// Front
-1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0,
1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
// Back
-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 0.0, 0.0,
-1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 0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1.0, 1.0, 1.0, 0.0,
1.0, -1.0, 1.0, 0.0, 0.0,
-1.0, 1.0, 1.0, 1.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 1.0, 1.0, 1.0,
// Back
-1.0, -1.0, -1.0, 0.0, 0.0,
-1.0, 1.0, -1.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0,
1.0, -1.0, -1.0, 1.0, 0.0,
-1.0, 1.0, -1.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 1.0,
// Left
-1.0, -1.0, 1.0, 0.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0,
-1.0, -1.0, -1.0, 0.0, 0.0,
-1.0, -1.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0,
// Right
1.0, -1.0, 1.0, 1.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0,
1.0, 1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 1.0, 1.0,
1.0, 1.0, -1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 0.0, 1.0,
}

View File

@ -92,10 +92,10 @@ uniform mat4 model;
in vec3 vert;
in vec3 vertColor;
in vec2 vertTexCoord;
out vec3 outColor;
out vec3 fragColor;
out vec2 fragTexCoord;
void main() {
outColor = vertColor;
fragColor = vertColor;
fragTexCoord = vertTexCoord;
gl_Position = projection * camera * model * vec4(vert, 1);
}
@ -104,11 +104,11 @@ void main() {
var fragmentShader = `
#version 330
uniform sampler2D tex;
in vec3 fragColor;
in vec2 fragTexCoord;
in vec3 inColor;
out vec4 outputColor;
void main() {
outputColor = vec4(inColor, 1);
outputColor = texture(tex, fragTexCoord) * vec4(fragColor, 1);
}
` + "\x00"

View File

@ -1,45 +1,51 @@
package gl_objects
import (
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
)
type Box struct {
Size int32
Faces []*Face
Color mgl32.Vec3
Position mgl32.Vec3
TextureOn bool
Size int32
Faces []*Face
Color mgl32.Vec3
Position mgl32.Vec3
TextureOn bool
VertexArray []float32
GLProgram uint32
GLVertexArrayId uint32
GLVertexBufferId uint32
GLTextureId uint32
}
func NewBox(
length, width, depth float32,
position, color mgl32.Vec3, textureOn bool) *Box {
// length = z axis (towards)
depth, width, height float32,
position, color mgl32.Vec3) *Box {
// depth = z axis (towards)
// width = x axis (right)
// depth = y axis (up)
// height = y axis (up)
box := Box{}
box.Color = color
box.Size = 6
topPos := mgl32.Vec3{position.X(), position.Y() + depth/2.0, position.Z()}
top := NewFace(width, length, topPos, mgl32.Vec3{0, 1, 0}, color, textureOn)
topPos := mgl32.Vec3{position.X(), position.Y() + height/2.0, position.Z()}
top := NewFace(width, depth, topPos, mgl32.Vec3{0, 1, 0}, color)
bottomPos := mgl32.Vec3{position.X(), position.Y() - depth/2.0, position.Z()}
bottom := NewFace(width, length, bottomPos, mgl32.Vec3{0, -1, 0}, color, textureOn)
bottomPos := mgl32.Vec3{position.X(), position.Y() - height/2.0, position.Z()}
bottom := NewFace(width, depth, bottomPos, mgl32.Vec3{0, -1, 0}, color)
frontPos := mgl32.Vec3{position.X(), position.Y(), position.Z() + length/2.0}
front := NewFace(width, depth, frontPos, mgl32.Vec3{0, 0, 1}, color, textureOn)
frontPos := mgl32.Vec3{position.X(), position.Y(), position.Z() + depth/2.0}
front := NewFace(width, height, frontPos, mgl32.Vec3{0, 0, 1}, color)
backPos := mgl32.Vec3{position.X(), position.Y(), position.Z() - length/2.0}
back := NewFace(width, depth, backPos, mgl32.Vec3{0, 0, -1}, color, textureOn)
backPos := mgl32.Vec3{position.X(), position.Y(), position.Z() - depth/2.0}
back := NewFace(width, height, backPos, mgl32.Vec3{0, 0, -1}, color)
rightPos := mgl32.Vec3{position.X() + width/2.0, position.Y(), position.Z()}
right := NewFace(length, depth, rightPos, mgl32.Vec3{1, 0, 0}, color, textureOn)
right := NewFace(height, depth, rightPos, mgl32.Vec3{1, 0, 0}, color)
leftPos := mgl32.Vec3{position.X() - width/2.0, position.Y(), position.Z()}
left := NewFace(length, depth, leftPos, mgl32.Vec3{-1, 0, 0}, color, textureOn)
left := NewFace(height, depth, leftPos, mgl32.Vec3{-1, 0, 0}, color)
box.Faces = []*Face{
top,
@ -50,9 +56,53 @@ func NewBox(
left,
}
box.VertexArray = box.GetVertexArray()
return &box
}
func (b *Box) GLInit(glProgram uint32) {
gl.GenVertexArrays(1, &b.GLVertexArrayId)
gl.BindVertexArray(b.GLVertexArrayId)
gl.GenBuffers(1, &b.GLVertexBufferId)
gl.BindBuffer(gl.ARRAY_BUFFER, b.GLVertexBufferId)
gl.BufferData(gl.ARRAY_BUFFER, len(b.VertexArray)*4, gl.Ptr(b.VertexArray), gl.STATIC_DRAW)
var sizeofFloat int32 = 4
stride := VertexSize * sizeofFloat
vertAttrib := uint32(gl.GetAttribLocation(b.GLProgram, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointerWithOffset(vertAttrib, 3, gl.FLOAT, false, stride, uintptr(0))
vertColorAttrib := uint32(gl.GetAttribLocation(b.GLProgram, gl.Str("vertColor\x00")))
gl.EnableVertexAttribArray(vertColorAttrib)
gl.VertexAttribPointerWithOffset(vertColorAttrib, 3, gl.FLOAT, false, stride, uintptr(3*sizeofFloat))
texCoordAttrib := uint32(gl.GetAttribLocation(b.GLProgram, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, stride, uintptr(6*sizeofFloat))
}
func (b *Box) Update() {
// TODO anything?
// maybe collision detection
return
}
func (b *Box) Draw() {
gl.BindVertexArray(b.GLVertexArrayId)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, b.GLTextureId)
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // 6 sides, 2 triangles, 3 points each
}
func (b *Box) SetTexture(textureId uint32) {
b.GLTextureId = textureId
}
func (b *Box) GetVertexArray() []float32 {
// Boxes have 6 faces, 2 triangles per face, 3 vertices per triangle
// (technically 2 vertices overlap in a face, but ignore that for now)

View File

@ -23,9 +23,10 @@ type Camera struct {
RotationVelocity float32
RotateStep float32
MouseSensitivity float32
GLProgram uint32
}
func NewCamera() Camera {
func NewCamera(glProgram uint32) Camera {
camera := Camera{}
camera.Up = mgl32.Vec3{0, 1, 0}
camera.OriginalUp = mgl32.Vec3{0, 1, 0}
@ -34,6 +35,7 @@ func NewCamera() Camera {
camera.MoveStep = 0.1
camera.RotateStep = 0.01
camera.MouseSensitivity = 0.06
camera.GLProgram = glProgram
return camera
}
@ -41,9 +43,9 @@ func (c *Camera) GetCenter() mgl32.Vec3 {
return c.Position.Add(c.Direction)
}
func (c *Camera) Draw(program uint32) {
func (c *Camera) Draw() {
cameraMatrix := mgl32.LookAtV(c.Position, c.GetCenter(), c.Up)
cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00"))
cameraUniform := gl.GetUniformLocation(c.GLProgram, gl.Str("camera\x00"))
gl.UniformMatrix4fv(cameraUniform, 1, false, &cameraMatrix[0])
}

View File

@ -15,46 +15,43 @@ type Face struct {
func NewFace(
width, height float32,
center, normal mgl32.Vec3,
color mgl32.Vec3, textureOn bool) *Face {
// TODO texture
texture := mgl32.Vec2{0, 0}
color mgl32.Vec3) *Face {
// top left
v1 := newFaceVertex(
height/2.0,
-1*width/2.0,
height/2.0,
center,
normal,
color,
texture)
mgl32.Vec2{0, 1})
// bottom left
v2 := newFaceVertex(
-1*height/2.0,
-1*width/2.0,
-1*height/2.0,
center,
normal,
color,
texture)
mgl32.Vec2{0, 0})
// bottom right
v3 := newFaceVertex(
-1*height/2.0,
width/2.0,
-1*height/2.0,
center,
normal,
color,
texture)
mgl32.Vec2{1, 0})
// top right
v4 := newFaceVertex(
height/2.0,
width/2.0,
height/2.0,
center,
normal,
color,
texture)
mgl32.Vec2{1, 1})
t1 := NewTriangle(v1, v2, v3)
t2 := NewTriangle(v3, v4, v1)