-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug_shape.go
182 lines (148 loc) · 5.43 KB
/
debug_shape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/binary"
"errors"
"fmt"
"math"
"golang.org/x/mobile/exp/f32"
"github.com/go-gl/mathgl/mgl64"
"github.com/goxjs/gl"
"github.com/goxjs/gl/glutil"
)
const (
RACER_LIFTTHRUST_CONE = 3.0 // Height of cone (base radius 1).
)
var liftThrusterPositions = [...]mgl64.Vec3{
{1, 0, 0},
{math.Sqrt2 / 2, math.Sqrt2 / 2, 0},
{0, 1, 0},
{-math.Sqrt2 / 2, math.Sqrt2 / 2, 0},
{-1, 0, 0},
{-math.Sqrt2 / 2, -math.Sqrt2 / 2, 0},
{0, -1, 0},
{math.Sqrt2 / 2, -math.Sqrt2 / 2, 0},
{0, 0, 0},
}
var liftThrusterDirections,
liftThrusterPitchEffect,
liftThrusterRollEffect,
liftThrusterVelEffect = func() ([]mgl64.Vec3, []float64, []float64, []float64) {
var (
liftThrusterDirections = make([]mgl64.Vec3, len(liftThrusterPositions))
liftThrusterPitchEffect = make([]float64, len(liftThrusterPositions))
liftThrusterRollEffect = make([]float64, len(liftThrusterPositions))
liftThrusterVelEffect = make([]float64, len(liftThrusterPositions))
)
var liftThrusterOrigin = mgl64.Vec3{0, 0, RACER_LIFTTHRUST_CONE}
for i := range liftThrusterPositions {
liftThrusterDirections[i] = liftThrusterPositions[i].Sub(liftThrusterOrigin).Normalize()
liftThrusterPitchEffect[i] = RACER_LIFTTHRUST_MAXPITCHROLLACCEL * liftThrusterPositions[i].Y()
liftThrusterRollEffect[i] = RACER_LIFTTHRUST_MAXPITCHROLLACCEL * -liftThrusterPositions[i].X()
liftThrusterVelEffect[i] = 1.0
}
// TODO: Figure this out, verify.
for i := range liftThrusterPositions {
var vehicleModelRadius = mgl64.Vec3{0.825, 0.9625, 0.4}
liftThrusterPositions[i][0] *= vehicleModelRadius[0]
liftThrusterPositions[i][1] *= vehicleModelRadius[1]
liftThrusterPositions[i][2] *= vehicleModelRadius[2]
}
return liftThrusterDirections, liftThrusterPitchEffect, liftThrusterRollEffect, liftThrusterVelEffect
}()
var program3 gl.Program
var pMatrixUniform3 gl.Uniform
var mvMatrixUniform3 gl.Uniform
var colorUniform3 gl.Uniform
var vertexVbo3 gl.Buffer
func loadDebugShape() error {
const (
vertexSource = `//#version 120 // OpenGL 2.1.
//#version 100 // WebGL.
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
attribute vec3 aVertexPosition;
void main() {
gl_PointSize = 5.0;
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
`
fragmentSource = `//#version 120 // OpenGL 2.1.
//#version 100 // WebGL.
#ifdef GL_ES
precision lowp float;
#endif
uniform vec3 uColor;
void main() {
gl_FragColor = vec4(uColor, 1.0);
}
`
)
var err error
program3, err = glutil.CreateProgram(vertexSource, fragmentSource)
if err != nil {
return err
}
gl.ValidateProgram(program3)
if gl.GetProgrami(program3, gl.VALIDATE_STATUS) != gl.TRUE {
return errors.New("VALIDATE_STATUS: " + gl.GetProgramInfoLog(program3))
}
gl.UseProgram(program3)
pMatrixUniform3 = gl.GetUniformLocation(program3, "uPMatrix")
mvMatrixUniform3 = gl.GetUniformLocation(program3, "uMVMatrix")
colorUniform3 = gl.GetUniformLocation(program3, "uColor")
vertexVbo3 = gl.CreateBuffer()
if glError := gl.GetError(); glError != 0 {
return fmt.Errorf("gl.GetError: %v", glError)
}
return nil
}
func calcThrusterDistances() []float64 {
var dists []float64
for i := range liftThrusterPositions {
mat := mgl64.Ident4()
mat = mat.Mul4(mgl64.HomogRotate3D(player.R, mgl64.Vec3{0, 0, -1}))
mat = mat.Mul4(mgl64.HomogRotate3D(player.Pitch, mgl64.Vec3{1, 0, 0}))
mat = mat.Mul4(mgl64.HomogRotate3D(player.Roll, mgl64.Vec3{0, 1, 0}))
pos := mat.Mul4x1(liftThrusterPositions[i].Vec4(1)).Vec3()
pos = pos.Add(mgl64.Vec3{player.X, player.Y, player.Z})
dir := mat.Mul4x1(liftThrusterDirections[i].Vec4(1)).Vec3()
dists = append(dists, track.distToTerrain(pos, dir, RACER_LIFTTHRUST_MAXDIST))
}
return dists
}
var iterations uint64
func debugShapeRender() {
gl.BindBuffer(gl.ARRAY_BUFFER, vertexVbo3)
vertexPositionAttribute := gl.GetAttribLocation(program3, "aVertexPosition")
gl.EnableVertexAttribArray(vertexPositionAttribute)
gl.VertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0)
//t := time.Now()
iterations = 0
thrusterDistances := calcThrusterDistances()
//fmt.Println("calcThrusterDistances:", iterations, time.Since(t).Seconds()*1000, "ms")
// Lift thrusters visualized as lines.
var vertices []float32
for i, p0 := range liftThrusterPositions {
vertices = append(vertices, float32(p0.X()), float32(p0.Y()), float32(p0.Z()))
p1 := p0.Add(liftThrusterDirections[i].Mul(thrusterDistances[i]))
vertices = append(vertices, float32(p1.X()), float32(p1.Y()), float32(p1.Z()))
}
for i, p0 := range liftThrusterPositions {
p1 := p0.Add(liftThrusterDirections[i].Mul(thrusterDistances[i]))
vertices = append(vertices, float32(p1.X()), float32(p1.Y()), float32(p1.Z()))
p2 := p0.Add(liftThrusterDirections[i].Mul(50))
vertices = append(vertices, float32(p2.X()), float32(p2.Y()), float32(p2.Z()))
}
for i, p0 := range liftThrusterPositions {
p1 := p0.Add(liftThrusterDirections[i].Mul(thrusterDistances[i]))
vertices = append(vertices, float32(p1.X()), float32(p1.Y()), float32(p1.Z()))
}
gl.BufferData(gl.ARRAY_BUFFER, f32.Bytes(binary.LittleEndian, vertices...), gl.STATIC_DRAW)
// Lift thrusters visualized as lines.
gl.Uniform3f(colorUniform3, 0, 1, 0)
gl.DrawArrays(gl.LINES, 0, 2*len(liftThrusterPositions))
gl.Uniform3f(colorUniform3, 1, 0, 0)
gl.DrawArrays(gl.LINES, 2*len(liftThrusterPositions), 2*len(liftThrusterPositions))
gl.Uniform3f(colorUniform3, 0, 0, 1)
gl.DrawArrays(gl.POINTS, 2*2*len(liftThrusterPositions), len(liftThrusterPositions))
}