-
Notifications
You must be signed in to change notification settings - Fork 0
/
level-blueprint.go
105 lines (91 loc) · 2.72 KB
/
level-blueprint.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
package main
import (
gl "github.com/chsc/gogl/gl21"
"math"
)
type PerPlayerLevelBlueprint struct {
Orientation Orientation3
PaddleAreaCenter Point3
PaddleAreaWidth float64
PaddleAreaHeight float64
TargetAreaCenter Point3
}
type VertexBlueprint struct {
Position Point3
Color Color
Tu float64
Tv float64
}
type FaceBlueprint struct {
Vertices []VertexBlueprint
TextureIndex int
}
type LevelBlueprint struct {
Players []PerPlayerLevelBlueprint
Walls []FaceBlueprint
}
func SetUpCamera(
screenWidth int32,
screenHeight int32,
lb *LevelBlueprint,
playerIndex int32) {
pb := &lb.Players[playerIndex]
// Calculate the distance between the near plane and the camera. We want
// (PaddleAreaWidth / 2) / cameraDist = tan(CameraHorzFovDegrees / 2)
t := math.Tan(math.Pi * Config.CameraHorzFovDegrees / 360)
cameraDist := pb.PaddleAreaWidth / (2 * t)
cameraPos := pb.PaddleAreaCenter.Minus(
pb.Orientation.Forward.Times(cameraDist))
// Set up the projection matrix.
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Viewport(0, 0, gl.Sizei(screenWidth), gl.Sizei(screenHeight))
gl.Frustum(
gl.Double(-pb.PaddleAreaWidth/2), gl.Double(pb.PaddleAreaWidth/2),
gl.Double(-pb.PaddleAreaHeight/2), gl.Double(pb.PaddleAreaHeight/2),
gl.Double(cameraDist), gl.Double(cameraDist+Config.ViewDepth))
// Set up the modelview matrix.
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
transformation := [16]gl.Double{
gl.Double(pb.Orientation.Right.X),
gl.Double(pb.Orientation.Up.X),
gl.Double(-pb.Orientation.Forward.X),
gl.Double(0.0),
gl.Double(pb.Orientation.Right.Y),
gl.Double(pb.Orientation.Up.Y),
gl.Double(-pb.Orientation.Forward.Y),
gl.Double(0.0),
gl.Double(pb.Orientation.Right.Z),
gl.Double(pb.Orientation.Up.Z),
gl.Double(-pb.Orientation.Forward.Z),
gl.Double(0.0),
gl.Double(0.0), gl.Double(0.0), gl.Double(0.0), gl.Double(1.0)}
gl.MultMatrixd(&transformation[0])
gl.Translated(
gl.Double(-cameraPos.X),
gl.Double(-cameraPos.Y),
gl.Double(-cameraPos.Z))
}
func MakeLevelVertexBuffer(lb *LevelBlueprint) *VertexBuffer {
vb := VertexBuffer{}
points := []Point3{}
colors := []Color{}
texCoords := [][2]float64{}
for wi := range lb.Walls {
indices := []gl.Int{}
for vi := range lb.Walls[wi].Vertices {
points = append(
points, lb.Walls[wi].Vertices[vi].Position)
colors = append(colors, lb.Walls[wi].Vertices[vi].Color)
texCoords = append(
texCoords,
[2]float64{lb.Walls[wi].Vertices[vi].Tu, lb.Walls[wi].Vertices[vi].Tv})
indices = append(indices, gl.Int(len(points)-1))
}
renderStep := VertexBufferRenderStep{gl.TRIANGLE_FAN, indices}
vb.RenderSteps = append(vb.RenderSteps, renderStep)
}
vb.InitVertexData(points, colors, texCoords)
return &vb
}