Things are mostly working, now to make Breakout.

main
Sean Hickey 2021-09-03 16:57:19 -07:00
parent 286efb85c5
commit d38139e7ec
9 changed files with 232 additions and 227 deletions

181
main.go
View File

@ -13,7 +13,6 @@ import (
"github.com/veandco/go-sdl2/sdl"
"gitea.wisellama.rocks/carpy-breakout/pkg/game_window"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_helpers"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_objects"
)
@ -52,7 +51,7 @@ func run() {
sdl.DisableScreenSaver()
//sdl.GLSetAttribute(sdl.GL_MULTISAMPLESAMPLES, 4) // Smooth
sdl.GLSetAttribute(sdl.GL_MULTISAMPLESAMPLES, 2) // Smooth
// Capture the mouse for movement
sdl.SetRelativeMouseMode(true)
@ -69,81 +68,30 @@ func run() {
camera := gl_objects.NewCamera(gameWindow.GLProgram)
camera.Position = mgl32.Vec3{0, 0, 10}
modelUniform := gl.GetUniformLocation(gameWindow.GLProgram, gl.Str("model\x00"))
textureId, err := gl_helpers.NewTexture("square.png")
if err != nil {
log.Fatal(err)
}
// 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, 0, 0})
//box.GLInit(gameWindow.GLProgram)
box.GLInit(gameWindow.GLProgram)
//box.SetTexture(texture)
/*
*/
vertexArray := box.VertexArray
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
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, stride, uintptr(0))
vertColorAttrib := uint32(gl.GetAttribLocation(gameWindow.GLProgram, gl.Str("vertColor\x00")))
gl.EnableVertexAttribArray(vertColorAttrib)
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, 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)
gl.LineWidth(3.0)
running := true
var angle float32 = 0.0
for running {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
model = mgl32.Ident4()
camera.Update()
camera.Draw()
// 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])
//box.Update()
//box.Draw()
/*
*/
gl.BindVertexArray(vao)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, textureId)
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // 6 sides, 2 triangles, 3 points each
/*
*/
box.Update()
box.Draw()
gameWindow.SDLWindow.GLSwap()
@ -172,7 +120,7 @@ func handleEvents(running bool, gameWindow *game_window.GameWindow, camera *gl_o
running = false
case *sdl.MouseMotionEvent:
if t.XRel != 0 || t.YRel != 0 {
camera.RotateInput(t.XRel, t.YRel)
camera.Rotate(t.XRel, t.YRel, 0)
}
case *sdl.MouseButtonEvent:
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
@ -246,110 +194,15 @@ func handleEvents(running bool, gameWindow *game_window.GameWindow, camera *gl_o
camera.SetRotationVelocity(0)
}
}
if t.Keysym.Sym == sdl.K_LSHIFT {
if t.Type == sdl.KEYDOWN {
camera.SetFastMoveVelocity(2)
} else if t.Type == sdl.KEYUP {
camera.SetFastMoveVelocity(0)
}
}
}
}
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.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,
// Left
-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, 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,
-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, -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,
}
var cubeVerticesOrig = []float32{
// X, Y, Z, texture U, V
// Bottom
-1.0, -1.0, -1.0, 0.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, 0.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, 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,
// Front
-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,
-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

@ -8,6 +8,7 @@ import (
"github.com/veandco/go-sdl2/sdl"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_helpers"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_objects"
)
const DefaultWindowWidth int32 = 800
@ -45,7 +46,7 @@ func NewGameWindow(title string) (*GameWindow, error) {
}
gameWindow.GLContext = &context
program, err := gl_helpers.NewProgram(vertexShader, fragmentShader)
program, err := gl_helpers.NewDefaultProgram()
if err != nil {
panic(err)
}
@ -84,32 +85,25 @@ func (w *GameWindow) WindowProjection() {
gl.Viewport(0, 0, width, height)
}
var vertexShader = `
#version 330
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;
in vec3 vert;
in vec3 vertColor;
in vec2 vertTexCoord;
out vec3 fragColor;
out vec2 fragTexCoord;
void main() {
fragColor = vertColor;
fragTexCoord = vertTexCoord;
gl_Position = projection * camera * model * vec4(vert, 1);
}
` + "\x00"
func GLInitVertexAttribs(glProgram uint32, textureOn bool) {
var sizeofFloat int32 = 4
stride := gl_objects.VertexSize * sizeofFloat
vertAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointerWithOffset(vertAttrib, 3, gl.FLOAT, false, stride, uintptr(0))
var fragmentShader = `
#version 330
uniform sampler2D tex;
in vec3 fragColor;
in vec2 fragTexCoord;
out vec4 outputColor;
void main() {
outputColor = texture(tex, fragTexCoord) * vec4(fragColor, 1);
}
` + "\x00"
vertColorAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vertColor\x00")))
gl.EnableVertexAttribArray(vertColorAttrib)
gl.VertexAttribPointerWithOffset(vertColorAttrib, 3, gl.FLOAT, false, stride, uintptr(3*sizeofFloat))
// outputColor = texture(tex, fragTexCoord) * vec4(inColor, 1);
texCoordAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, stride, uintptr(6*sizeofFloat))
textureOnUniform := gl.GetUniformLocation(glProgram, gl.Str("textureOn\x00"))
if textureOn {
gl.Uniform1i(textureOnUniform, 1)
} else {
gl.Uniform1i(textureOnUniform, 0)
}
}

View File

@ -11,8 +11,13 @@ import (
"strings"
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
)
func NewDefaultProgram() (uint32, error) {
return NewProgram(VertexShaderSource, FragmentShaderSource)
}
func NewProgram(vertexShaderSource string, fragmentShaderSource string) (uint32, error) {
vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
@ -111,3 +116,30 @@ func NewTexture(file string) (uint32, error) {
return texture, nil
}
func GLInitVertexAttribs(glProgram uint32, vertexSize int32, textureOn bool) {
var sizeofFloat int32 = 4
stride := vertexSize * sizeofFloat
vertAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointerWithOffset(vertAttrib, 3, gl.FLOAT, false, stride, uintptr(0))
vertColorAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vertColor\x00")))
gl.EnableVertexAttribArray(vertColorAttrib)
gl.VertexAttribPointerWithOffset(vertColorAttrib, 3, gl.FLOAT, false, stride, uintptr(3*sizeofFloat))
texCoordAttrib := uint32(gl.GetAttribLocation(glProgram, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointerWithOffset(texCoordAttrib, 2, gl.FLOAT, false, stride, uintptr(6*sizeofFloat))
textureOnUniform := gl.GetUniformLocation(glProgram, gl.Str("textureOn\x00"))
if textureOn {
gl.Uniform1i(textureOnUniform, 1)
} else {
gl.Uniform1i(textureOnUniform, 0)
}
model := mgl32.Ident4()
modelUniform := gl.GetUniformLocation(glProgram, gl.Str("model\x00"))
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
}

View File

@ -0,0 +1,34 @@
package gl_helpers
var VertexShaderSource = `
#version 330
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;
in vec3 vert;
in vec3 vertColor;
in vec2 vertTexCoord;
out vec3 fragColor;
out vec2 fragTexCoord;
void main() {
fragColor = vertColor;
fragTexCoord = vertTexCoord;
gl_Position = projection * camera * model * vec4(vert, 1);
}
` + "\x00"
var FragmentShaderSource = `
#version 330
uniform sampler2D tex;
uniform int textureOn;
in vec3 fragColor;
in vec2 fragTexCoord;
out vec4 outputColor;
void main() {
vec4 t = vec4(1);
if (textureOn != 0) {
t = texture(tex, fragTexCoord);
}
outputColor = t * vec4(fragColor, 1);
}
` + "\x00"

View File

@ -3,6 +3,8 @@ package gl_objects
import (
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_helpers"
)
type Box struct {
@ -10,6 +12,7 @@ type Box struct {
Faces []*Face
Color mgl32.Vec3
Position mgl32.Vec3
Rotation mgl32.Vec3
TextureOn bool
VertexArray []float32
GLProgram uint32
@ -62,43 +65,58 @@ func NewBox(
}
func (b *Box) GLInit(glProgram uint32) {
b.GLProgram = glProgram
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)
sizeOfFloat := 4
gl.BufferData(gl.ARRAY_BUFFER, len(b.VertexArray)*sizeOfFloat, 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))
for _, face := range b.Faces {
face.GLInit(b.GLProgram)
}
}
func (b *Box) Update() {
// TODO anything?
// maybe collision detection
b.Rotation = mgl32.Vec3{b.Rotation.X(), b.Rotation.Y() + 0.01, b.Rotation.Z()}
return
}
func (b *Box) Draw() {
// Apply rotation
model := mgl32.HomogRotate3D(b.Rotation.Y(), mgl32.Vec3{0, 1, 0})
modelUniform := gl.GetUniformLocation(b.GLProgram, gl.Str("model\x00"))
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
// Apply translation
// TODO
//b.drawTriangles()
b.drawOutline()
}
func (b *Box) drawTriangles() {
gl.BindVertexArray(b.GLVertexArrayId)
gl.BindBuffer(gl.ARRAY_BUFFER, b.GLVertexBufferId)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, b.GLTextureId)
textureOn := b.GLTextureId != 0
if textureOn {
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, b.GLTextureId)
}
gl_helpers.GLInitVertexAttribs(b.GLProgram, VertexSize, textureOn)
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // 6 sides, 2 triangles, 3 points each
}
func (b *Box) drawOutline() {
for _, face := range b.Faces {
face.DrawLineLoop(b.GLProgram)
}
}
func (b *Box) SetTexture(textureId uint32) {
b.GLTextureId = textureId
}

View File

@ -19,6 +19,7 @@ type Camera struct {
Up mgl32.Vec3
OriginalUp mgl32.Vec3
Velocity mgl32.Vec3
FastMoveVelocity float32
MoveStep float32
RotationVelocity float32
RotateStep float32
@ -50,26 +51,23 @@ func (c *Camera) Draw() {
}
func (c *Camera) Update() {
c.Rotate()
c.Rotate(0, 0, c.RotationVelocity)
c.Move(c.Velocity)
}
func (c *Camera) Rotate() {
c.RotateInput(0, 0)
}
func (c *Camera) RotateInput(mouseX, mouseY int32) {
func (c *Camera) Rotate(mouseX, mouseY int32, velocity float32) {
angle := mgl32.Vec3{}
// X Rotation (pitch) - mouse up/down
angle[0] = -1 * float32(mouseY) * c.MouseSensitivity
// Y Rotation (yaw) - mouse left/right
angle[1] = -1 * float32(mouseX) * c.MouseSensitivity
// Z Rotation (roll) - keyboard z/x
angle[2] = c.RotationVelocity
angle[2] = velocity
if angle.LenSqr() == 0 {
return
}
angle = angle.Mul(c.RotateStep)
relativeY := c.Up
@ -97,6 +95,10 @@ func (c *Camera) Move(vec mgl32.Vec3) {
vec = vec.Normalize()
vec = vec.Mul(c.MoveStep)
if c.FastMoveVelocity != 0 {
vec = vec.Mul(c.FastMoveVelocity)
}
perp := c.Direction.Cross(c.Up)
relativeY := c.Up.Mul(vec.Y())
relativeZ := c.Direction.Mul(vec.Z())
@ -125,3 +127,7 @@ func (c *Camera) SetZVelocity(v float32) {
func (c *Camera) SetRotationVelocity(v float32) {
c.RotationVelocity = v
}
func (c *Camera) SetFastMoveVelocity(v float32) {
c.FastMoveVelocity = v
}

View File

@ -3,13 +3,19 @@ package gl_objects
import (
"log"
gl "github.com/go-gl/gl/v3.1/gles2"
"github.com/go-gl/mathgl/mgl32"
"gitea.wisellama.rocks/carpy-breakout/pkg/gl_helpers"
"gitea.wisellama.rocks/carpy-breakout/pkg/math_helpers"
)
type Face struct {
Triangles []*Triangle
Triangles []*Triangle
LineLoopColor mgl32.Vec3
LineLoopVertexArray []float32
GLLineLoopVertexArrayId uint32
GLLineLoopVertexBufferId uint32
}
func NewFace(
@ -17,14 +23,17 @@ func NewFace(
center, normal mgl32.Vec3,
color mgl32.Vec3) *Face {
// top left
faceColor := mgl32.Vec3{1, 1, 1}
// top left, shared
v1 := newFaceVertex(
-1*width/2.0,
height/2.0,
center,
normal,
color,
faceColor,
mgl32.Vec2{0, 1})
v1_2 := *v1
// bottom left
v2 := newFaceVertex(
@ -32,17 +41,18 @@ func NewFace(
-1*height/2.0,
center,
normal,
color,
faceColor,
mgl32.Vec2{0, 0})
// bottom right
// bottom right, shared
v3 := newFaceVertex(
width/2.0,
-1*height/2.0,
center,
normal,
color,
faceColor,
mgl32.Vec2{1, 0})
v3_2 := *v3
// top right
v4 := newFaceVertex(
@ -50,15 +60,18 @@ func NewFace(
height/2.0,
center,
normal,
color,
faceColor,
mgl32.Vec2{1, 1})
t1 := NewTriangle(v1, v2, v3)
t2 := NewTriangle(v3, v4, v1)
t2 := NewTriangle(&v3_2, v4, &v1_2)
face := Face{}
face.Triangles = []*Triangle{t1, t2}
face.LineLoopColor = color
face.LineLoopVertexArray = face.GetLineLoopVertexArray()
return &face
}
@ -90,8 +103,6 @@ func newFaceVertex(
// add back the center so we get the correct absolute position
v = v.Add(center)
log.Println(v)
vertex := NewVertex(
v.X(), v.Y(), v.Z(),
color[0], color[1], color[2],
@ -111,3 +122,48 @@ func (f *Face) GetVertexArray() []float32 {
return a
}
func (f *Face) GetLineLoopVertexArray() []float32 {
a := make([]float32, 4*VertexSize)
i := 0
for _, vertex := range f.Triangles[0].Vertices {
v := *vertex
v.SetColor(f.LineLoopColor)
for _, elem := range v.GetVertexArray() {
a[i] = elem
i++
}
}
fourthV := *f.Triangles[1].Vertices[1]
fourthV.SetColor(f.LineLoopColor)
for _, elem := range fourthV.GetVertexArray() {
a[i] = elem
i++
}
return a
}
func (f *Face) DrawLineLoop(glProgram uint32) {
gl.LineWidth(5.0)
gl.BindVertexArray(f.GLLineLoopVertexArrayId)
gl.BindBuffer(gl.ARRAY_BUFFER, f.GLLineLoopVertexBufferId)
gl_helpers.GLInitVertexAttribs(glProgram, VertexSize, false)
gl.DrawArrays(gl.LINE_LOOP, 0, 4)
}
func (f *Face) GLInit(glProgram uint32) {
textureOn := false
gl_helpers.GLInitVertexAttribs(glProgram, VertexSize, textureOn)
// LineLoop setup
gl.GenVertexArrays(1, &f.GLLineLoopVertexArrayId)
gl.BindVertexArray(f.GLLineLoopVertexArrayId)
gl.GenBuffers(1, &f.GLLineLoopVertexBufferId)
gl.BindBuffer(gl.ARRAY_BUFFER, f.GLLineLoopVertexBufferId)
sizeOfFloat := 4
gl.BufferData(gl.ARRAY_BUFFER, len(f.LineLoopVertexArray)*sizeOfFloat, gl.Ptr(f.LineLoopVertexArray), gl.STATIC_DRAW)
}

View File

@ -1,5 +1,7 @@
package gl_objects
import "github.com/go-gl/mathgl/mgl32"
type Triangle struct {
Vertices []*Vertex
}
@ -11,6 +13,12 @@ func NewTriangle(a, b, c *Vertex) *Triangle {
return &triangle
}
func (t *Triangle) SetColor(color mgl32.Vec3) {
for _, vertex := range t.Vertices {
vertex.SetColor(color)
}
}
func (t *Triangle) GetVertexArray() []float32 {
a := make([]float32, 3*VertexSize)

View File

@ -20,6 +20,10 @@ func NewVertex(x, y, z, r, g, b, u, v float32) *Vertex {
return &vertex
}
func (v *Vertex) SetColor(color mgl32.Vec3) {
v.Color = color
}
func (v *Vertex) GetVertexArray() []float32 {
a := make([]float32, VertexSize)