-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
233 lines (196 loc) · 6.64 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"log"
"os"
"path"
"runtime"
"github.com/go-gl/gl/v4.3-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
"github.com/kpelelis/go-engine/objloader"
"github.com/linosgian/goph3d/renderer"
"github.com/linosgian/goph3d/scene"
"github.com/linosgian/goph3d/window"
)
const (
metalPath = "res/textures/wood.png"
marblePath = "res/textures/marble.jpg"
FOV = 55.0
)
// This should be given temporarily because of vim-go
var rootPath = os.Getenv("PROJ_PATH") // e.g. /home/lgian/go/src/github.com/linosgian/goph3d
func init() {
runtime.LockOSThread() // This is needed to arrange that main() runs on main thread.
}
func main() {
//Initialize camera object at a certain position
cam := scene.NewCamera(mgl32.Vec3{0, 2, 17}, window.WIDTH/2.0, window.HEIGHT/2.0)
// Initializes Graphics API and GLFW
// NOTE: A pointer to a camera is needed in order for the mouse callback to control it
w, err := window.Init(cam, MouseCallback)
if err != nil {
log.Fatalf("Window initialization failed: %q\n", err)
}
// Temp: Light position
lightPos := mgl32.Vec3{1.2, 2.3, 2}
aspectRatio := float32(window.WIDTH) / window.HEIGHT
// Create all Point lights
lights := make([]*scene.PointLight, 0)
for _, lpos := range PointLightPositions {
l := &scene.PointLight{
Position: lpos,
Ambient: mgl32.Vec3{0.05, 0.05, 0.05},
Diffuse: mgl32.Vec3{0.8, 0.8, 0.8},
Specular: mgl32.Vec3{1, 1, 1},
Constant: 1,
Linear: 0.09,
Quadratic: 0.032,
}
lights = append(lights, l)
}
sc := scene.NewScene(aspectRatio, cam, lights)
r, err := renderer.NewRenderer()
if err != nil {
log.Fatalf("could not create renderer: %q\n", err)
}
// Instantiate all scene nodes and set their model matrices
// -----------------------------
reader, err := objloader.New("/home/lgian/Desktop/obj.obj")
if err != nil {
log.Fatalf("Could not read file: %q\n", err)
}
reader.Read()
reader.Close()
cube := reader.ExportToFloat32Array()[0]
if err := sc.NewNodes(r, "crate", true, cube, path.Join(rootPath, marblePath), "phong", cubePositions); err != nil {
log.Fatalf("Could not create node: %q\n", err)
}
if err := sc.NewNode(r, "lamp", true, cube, path.Join(rootPath, marblePath), "lamp", lightPos); err != nil {
log.Fatalf("Could not create node: %q\n", err)
}
if err := sc.NewNode(r, "plane", true, planeVertices, path.Join(rootPath, metalPath), "phong", mgl32.Vec3{0, 0, 0}); err != nil {
log.Fatalf("Could not create node: %q\n", err)
}
// ----------------------------
gl.Enable(gl.DEPTH_TEST)
sc.InitLights(r)
for !w.ShouldClose() {
// Per-frame time. Used for speed normalization
currentFrame := glfw.GetTime()
sc.DeltaTime = currentFrame - sc.LastFrame
sc.LastFrame = currentFrame
w.Clear()
processInput(w, sc)
// Update everything per-frame
sc.Update(r)
// Rotate all crates according to current time
view := sc.Cam.GetViewMatrix()
rot := mgl32.HomogRotate3D(float32(glfw.GetTime()), mgl32.Vec3{0, 1, 0})
for _, n := range sc.Nodes {
if n.Name == "crate" {
translate := mgl32.Translate3D(n.Position.X(), n.Position.Y(), n.Position.Z())
n.SetModelMatrix(translate.Mul4(rot))
}
r.DrawRaw(n.VaoID, n.ProgramID, n.TexID, view, sc.Perspective, n.ModelMatrix)
}
w.SwapBuffers()
glfw.PollEvents()
}
w.Destroy()
}
func processInput(w *window.GlWindow, sc *scene.Scene) {
if w.GetKey(glfw.KeyEscape) == glfw.Press {
w.SetShouldClose(true)
}
if w.GetKey(glfw.KeyW) == glfw.Press {
sc.Cam.ProcessKeyboard(scene.FORWARD, sc.DeltaTime)
}
if w.GetKey(glfw.KeyS) == glfw.Press {
sc.Cam.ProcessKeyboard(scene.BACKWARD, sc.DeltaTime)
}
if w.GetKey(glfw.KeyA) == glfw.Press {
sc.Cam.ProcessKeyboard(scene.LEFT, sc.DeltaTime)
}
if w.GetKey(glfw.KeyD) == glfw.Press {
sc.Cam.ProcessKeyboard(scene.RIGHT, sc.DeltaTime)
}
if w.GetKey(glfw.KeySpace) == glfw.Press {
w.SetShouldClose(true)
}
}
func MouseCallback(w *glfw.Window, xpos, ypos float64) {
// This is needed so we don't have a global camera variable
cam := (*scene.Camera)(w.GetUserPointer())
// This solves the issue when the mouse enters the scene
// and the camera immediately turns to that point instantly
if cam.FirstMouse {
cam.LastX = xpos
cam.LastY = ypos
cam.FirstMouse = false
}
xoffset := float32(xpos - cam.LastX)
yoffset := float32(ypos - cam.LastY)
cam.LastX = xpos
cam.LastY = ypos
cam.ProcessMouseMovement(xoffset, yoffset)
}
var cubePositions = []mgl32.Vec3{
mgl32.Vec3{1, 1, 1},
mgl32.Vec3{-4, 2, 3},
mgl32.Vec3{-3, 3, 7},
mgl32.Vec3{4, 7, 2},
mgl32.Vec3{9, 4, 1},
}
var PointLightPositions = []mgl32.Vec3{
mgl32.Vec3{0.7, 0.2, 2},
mgl32.Vec3{2.3, 10, 4},
mgl32.Vec3{4, 3, 5},
mgl32.Vec3{1, 2, 12},
}
// var cube = []float32{
// // X,Y,Z / U,V / Nx, Ny, Nz
// -0.5, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, -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