FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lightmapper-cpp/example/example.c at master · study-game-engines/lightmapper-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
study-game-engines
/
lightmapper-cpp
Public
forked from
ands/lightmapper
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lightmapper-cpp
/
example
/
example.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
557 lines (487 loc) · 17.6 KB
Breadcrumbs
lightmapper-cpp
/
example
/
example.c
Copy path
File metadata and controls
557 lines (487 loc) · 17.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#define
_USE_MATH_DEFINES
#include
<stdlib.h>
#include
<stdio.h>
#include
<math.h>
#include
<assert.h>
#include
"glad/glad.h"
#include
"GLFW/glfw3.h"
#define
LIGHTMAPPER_IMPLEMENTATION
#define
LM_DEBUG_INTERPOLATION
#include
"../lightmapper.h"
#ifndef
M_PI
// even with _USE_MATH_DEFINES not always available
#define
M_PI
3.14159265358979323846
#endif
typedef
struct
{
float
p
[
3
];
float
t
[
2
];
}
vertex_t
;
typedef
struct
{
GLuint
program
;
GLint
u_lightmap
;
GLint
u_projection
;
GLint
u_view
;
GLuint
lightmap
;
int
w
,
h
;
GLuint
vao
,
vbo
,
ibo
;
vertex_t
*
vertices
;
unsigned short
*
indices
;
unsigned
int
vertexCount
,
indexCount
;
}
scene_t
;
static
int
initScene
(
scene_t
*
scene
);
static
void
drawScene
(
scene_t
*
scene
,
float
*
view
,
float
*
projection
);
static
void
destroyScene
(
scene_t
*
scene
);
static
int
bake
(
scene_t
*
scene
)
{
lm_context
*
ctx
=
lmCreate
(
64
,
// hemisphere resolution (power of two, max=512)
0.001f
,
100.0f
,
// zNear, zFar of hemisphere cameras
1.0f
,
1.0f
,
1.0f
,
// background color (white for ambient occlusion)
2
,
0.01f
,
// lightmap interpolation threshold (small differences are interpolated rather than sampled)
// check debug_interpolation.tga for an overview of sampled (red) vs interpolated (green) pixels.
0.0f
);
// modifier for camera-to-surface distance for hemisphere rendering.
// tweak this to trade-off between interpolated normals quality and other artifacts (see declaration).
if
(!
ctx
)
{
fprintf
(
stderr
,
"Error: Could not initialize lightmapper.\n"
);
return
0
;
}
int
w
=
scene
->
w
,
h
=
scene
->
h
;
float
*
data
=
calloc
(
w
*
h
*
4
,
sizeof
(
float
));
lmSetTargetLightmap
(
ctx
,
data
,
w
,
h
,
4
);
lmSetGeometry
(
ctx
,
NULL
,
// no transformation in this example
LM_FLOAT
, (
unsigned
char
*
)
scene
->
vertices
+
offsetof(
vertex_t
,
p
),
sizeof
(
vertex_t
),
LM_NONE
,
NULL
,
0
,
// no interpolated normals in this example
LM_FLOAT
, (
unsigned
char
*
)
scene
->
vertices
+
offsetof(
vertex_t
,
t
),
sizeof
(
vertex_t
),
scene
->
indexCount
,
LM_UNSIGNED_SHORT
,
scene
->
indices
);
int
vp
[
4
];
float
view
[
16
],
projection
[
16
];
double
lastUpdateTime
=
0.0
;
while
(
lmBegin
(
ctx
,
vp
,
view
,
projection
))
{
// render to lightmapper framebuffer
glViewport
(
vp
[
0
],
vp
[
1
],
vp
[
2
],
vp
[
3
]);
drawScene
(
scene
,
view
,
projection
);
// display progress every second (printf is expensive)
double
time
=
glfwGetTime
();
if
(
time
-
lastUpdateTime
>
1.0
)
{
lastUpdateTime
=
time
;
printf
(
"\r%6.2f%%"
,
lmProgress
(
ctx
)
*
100.0f
);
fflush
(
stdout
);
}
lmEnd
(
ctx
);
}
printf
(
"\rFinished baking %d triangles.\n"
,
scene
->
indexCount
/
3
);
lmDestroy
(
ctx
);
// postprocess texture
float
*
temp
=
calloc
(
w
*
h
*
4
,
sizeof
(
float
));
for
(
int
i
=
0
;
i
<
16
;
i
++
)
{
lmImageDilate
(
data
,
temp
,
w
,
h
,
4
);
lmImageDilate
(
temp
,
data
,
w
,
h
,
4
);
}
lmImageSmooth
(
data
,
temp
,
w
,
h
,
4
);
lmImageDilate
(
temp
,
data
,
w
,
h
,
4
);
lmImagePower
(
data
,
w
,
h
,
4
,
1.0f
/
2.2f
,
0x7
);
// gamma correct color channels
free
(
temp
);
// save result to a file
if
(
lmImageSaveTGAf
(
"result.tga"
,
data
,
w
,
h
,
4
,
1.0f
))
printf
(
"Saved result.tga\n"
);
// upload result
glBindTexture
(
GL_TEXTURE_2D
,
scene
->
lightmap
);
glTexImage2D
(
GL_TEXTURE_2D
,
0
,
GL_RGBA
,
w
,
h
,
0
,
GL_RGBA
,
GL_FLOAT
,
data
);
free
(
data
);
return
1
;
}
static
void
error_callback
(
int
error
,
const
char
*
description
)
{
fprintf
(
stderr
,
"Error: %s\n"
,
description
);
}
static
void
fpsCameraViewMatrix
(
GLFWwindow
*
window
,
float
*
view
);
static
void
perspectiveMatrix
(
float
*
out
,
float
fovy
,
float
aspect
,
float
zNear
,
float
zFar
);
static
void
mainLoop
(
GLFWwindow
*
window
,
scene_t
*
scene
)
{
glfwPollEvents
();
if
(
glfwGetKey
(
window
,
GLFW_KEY_SPACE
)
==
GLFW_PRESS
)
bake
(
scene
);
int
w
,
h
;
glfwGetFramebufferSize
(
window
,
&
w
,
&
h
);
glViewport
(
0
,
0
,
w
,
h
);
// camera for glfw window
float
view
[
16
],
projection
[
16
];
fpsCameraViewMatrix
(
window
,
view
);
perspectiveMatrix
(
projection
,
45.0f
, (
float
)
w
/ (
float
)
h
,
0.01f
,
100.0f
);
// draw to screen with a blueish sky
glClearColor
(
0.6f
,
0.8f
,
1.0f
,
1.0f
);
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
drawScene
(
scene
,
view
,
projection
);
glfwSwapBuffers
(
window
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
glfwSetErrorCallback
(
error_callback
);
if
(!
glfwInit
())
{
fprintf
(
stderr
,
"Could not initialize GLFW.\n"
);
return
EXIT_FAILURE
;
}
glfwWindowHint
(
GLFW_RED_BITS
,
8
);
glfwWindowHint
(
GLFW_GREEN_BITS
,
8
);
glfwWindowHint
(
GLFW_BLUE_BITS
,
8
);
glfwWindowHint
(
GLFW_ALPHA_BITS
,
8
);
glfwWindowHint
(
GLFW_DEPTH_BITS
,
32
);
glfwWindowHint
(
GLFW_STENCIL_BITS
,
GLFW_DONT_CARE
);
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MAJOR
,
3
);
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MINOR
,
2
);
glfwWindowHint
(
GLFW_OPENGL_FORWARD_COMPAT
,
GL_TRUE
);
glfwWindowHint
(
GLFW_OPENGL_PROFILE
,
GLFW_OPENGL_CORE_PROFILE
);
glfwWindowHint
(
GLFW_OPENGL_DEBUG_CONTEXT
,
GL_TRUE
);
glfwWindowHint
(
GLFW_SAMPLES
,
4
);
GLFWwindow
*
window
=
glfwCreateWindow
(
1024
,
768
,
"Lightmapping Example"
,
NULL
,
NULL
);
if
(!
window
)
{
fprintf
(
stderr
,
"Could not create window.\n"
);
glfwTerminate
();
return
EXIT_FAILURE
;
}
glfwMakeContextCurrent
(
window
);
gladLoadGLLoader
((
GLADloadproc
)
glfwGetProcAddress
);
glfwSwapInterval
(
1
);
scene_t
scene
=
{
0
};
if
(!
initScene
(
&
scene
))
{
fprintf
(
stderr
,
"Could not initialize scene.\n"
);
glfwDestroyWindow
(
window
);
glfwTerminate
();
return
EXIT_FAILURE
;
}
printf
(
"Ambient Occlusion Baking Example.\n"
);
printf
(
"Use your mouse and the W, A, S, D, E, Q keys to navigate.\n"
);
printf
(
"Press SPACE to start baking one light bounce!\n"
);
printf
(
"This will take a few seconds and bake a lightmap illuminated by:\n"
);
printf
(
"1. The mesh itself (initially black)\n"
);
printf
(
"2. A white sky (1.0f, 1.0f, 1.0f)\n"
);
while
(!
glfwWindowShouldClose
(
window
))
{
mainLoop
(
window
,
&
scene
);
}
destroyScene
(
&
scene
);
glfwDestroyWindow
(
window
);
glfwTerminate
();
return
EXIT_SUCCESS
;
}
// helpers ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
int
loadSimpleObjFile
(
const
char
*
filename
,
vertex_t
*
*
vertices
,
unsigned
int
*
vertexCount
,
unsigned short
*
*
indices
,
unsigned
int
*
indexCount
);
static
GLuint
loadProgram
(
const
char
*
vp
,
const
char
*
fp
,
const
char
*
*
attributes
,
int
attributeCount
);
static
int
initScene
(
scene_t
*
scene
)
{
// load mesh
if
(!
loadSimpleObjFile
(
"gazebo.obj"
,
&
scene
->
vertices
,
&
scene
->
vertexCount
,
&
scene
->
indices
,
&
scene
->
indexCount
))
{
fprintf
(
stderr
,
"Error loading obj file\n"
);
return
0
;
}
glGenVertexArrays
(
1
,
&
scene
->
vao
);
glBindVertexArray
(
scene
->
vao
);
glGenBuffers
(
1
,
&
scene
->
vbo
);
glBindBuffer
(
GL_ARRAY_BUFFER
,
scene
->
vbo
);
glBufferData
(
GL_ARRAY_BUFFER
,
scene
->
vertexCount
*
sizeof
(
vertex_t
),
scene
->
vertices
,
GL_STATIC_DRAW
);
glGenBuffers
(
1
,
&
scene
->
ibo
);
glBindBuffer
(
GL_ELEMENT_ARRAY_BUFFER
,
scene
->
ibo
);
glBufferData
(
GL_ELEMENT_ARRAY_BUFFER
,
scene
->
indexCount
*
sizeof
(
unsigned short
),
scene
->
indices
,
GL_STATIC_DRAW
);
glEnableVertexAttribArray
(
0
);
glVertexAttribPointer
(
0
,
3
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
vertex_t
), (
void
*
)offsetof(
vertex_t
,
p
));
glEnableVertexAttribArray
(
1
);
glVertexAttribPointer
(
1
,
2
,
GL_FLOAT
,
GL_FALSE
,
sizeof
(
vertex_t
), (
void
*
)offsetof(
vertex_t
,
t
));
// create lightmap texture
scene
->
w
=
654
;
scene
->
h
=
654
;
glGenTextures
(
1
,
&
scene
->
lightmap
);
glBindTexture
(
GL_TEXTURE_2D
,
scene
->
lightmap
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
GL_LINEAR
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MAG_FILTER
,
GL_LINEAR
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_S
,
GL_CLAMP_TO_EDGE
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_WRAP_T
,
GL_CLAMP_TO_EDGE
);
unsigned
char
emissive
[]
=
{
0
,
0
,
0
,
255
};
glTexImage2D
(
GL_TEXTURE_2D
,
0
,
GL_RGBA
,
1
,
1
,
0
,
GL_RGBA
,
GL_UNSIGNED_BYTE
,
emissive
);
// load shader
const
char
*
vp
=
"#version 150 core\n"
"in vec3 a_position;\n"
"in vec2 a_texcoord;\n"
"uniform mat4 u_view;\n"
"uniform mat4 u_projection;\n"
"out vec2 v_texcoord;\n"
"void main()\n"
"{\n"
"gl_Position = u_projection * (u_view * vec4(a_position, 1.0));\n"
"v_texcoord = a_texcoord;\n"
"}\n"
;
const
char
*
fp
=
"#version 150 core\n"
"in vec2 v_texcoord;\n"
"uniform sampler2D u_lightmap;\n"
"out vec4 o_color;\n"
"void main()\n"
"{\n"
"o_color = vec4(texture(u_lightmap, v_texcoord).rgb, gl_FrontFacing ? 1.0 : 0.0);\n"
"}\n"
;
const
char
*
attribs
[]
=
{
"a_position"
,
"a_texcoord"
};
scene
->
program
=
loadProgram
(
vp
,
fp
,
attribs
,
2
);
if
(!
scene
->
program
)
{
fprintf
(
stderr
,
"Error loading shader\n"
);
return
0
;
}
scene
->
u_view
=
glGetUniformLocation
(
scene
->
program
,
"u_view"
);
scene
->
u_projection
=
glGetUniformLocation
(
scene
->
program
,
"u_projection"
);
scene
->
u_lightmap
=
glGetUniformLocation
(
scene
->
program
,
"u_lightmap"
);
return
1
;
}
static
void
drawScene
(
scene_t
*
scene
,
float
*
view
,
float
*
projection
)
{
glEnable
(
GL_DEPTH_TEST
);
glUseProgram
(
scene
->
program
);
glUniform1i
(
scene
->
u_lightmap
,
0
);
glUniformMatrix4fv
(
scene
->
u_projection
,
1
,
GL_FALSE
,
projection
);
glUniformMatrix4fv
(
scene
->
u_view
,
1
,
GL_FALSE
,
view
);
glBindTexture
(
GL_TEXTURE_2D
,
scene
->
lightmap
);
glBindVertexArray
(
scene
->
vao
);
glDrawElements
(
GL_TRIANGLES
,
scene
->
indexCount
,
GL_UNSIGNED_SHORT
,
0
);
}
static
void
destroyScene
(
scene_t
*
scene
)
{
free
(
scene
->
vertices
);
free
(
scene
->
indices
);
glDeleteVertexArrays
(
1
,
&
scene
->
vao
);
glDeleteBuffers
(
1
,
&
scene
->
vbo
);
glDeleteBuffers
(
1
,
&
scene
->
ibo
);
glDeleteTextures
(
1
,
&
scene
->
lightmap
);
glDeleteProgram
(
scene
->
program
);
}
static
int
loadSimpleObjFile
(
const
char
*
filename
,
vertex_t
*
*
vertices
,
unsigned
int
*
vertexCount
,
unsigned short
*
*
indices
,
unsigned
int
*
indexCount
)
{
FILE
*
file
=
fopen
(
filename
,
"rt"
);
if
(!
file
)
return
0
;
char
line
[
1024
];
// first pass
unsigned
int
np
=
0
,
nn
=
0
,
nt
=
0
,
nf
=
0
;
while
(!
feof
(
file
))
{
fgets
(
line
,
1024
,
file
);
if
(
line
[
0
]
==
'#'
)
continue
;
if
(
line
[
0
]
==
'v'
)
{
if
(
line
[
1
]
==
' '
) {
np
++
;
continue
; }
if
(
line
[
1
]
==
'n'
) {
nn
++
;
continue
; }
if
(
line
[
1
]
==
't'
) {
nt
++
;
continue
; }
assert
(!
"unknown vertex attribute"
);
}
if
(
line
[
0
]
==
'f'
) {
nf
++
;
continue
; }
assert
(!
"unknown identifier"
);
}
assert
(
np
&&
np
==
nn
&&
np
==
nt
&&
nf
);
// only supports obj files without separately indexed vertex attributes
// allocate memory
*
vertexCount
=
np
;
*
vertices
=
calloc
(
np
,
sizeof
(
vertex_t
));
*
indexCount
=
nf
*
3
;
*
indices
=
calloc
(
nf
*
3
,
sizeof
(
unsigned short
));
// second pass
fseek
(
file
,
0
,
SEEK_SET
);
unsigned
int
cp
=
0
,
cn
=
0
,
ct
=
0
,
cf
=
0
;
while
(!
feof
(
file
))
{
fgets
(
line
,
1024
,
file
);
if
(
line
[
0
]
==
'#'
)
continue
;
if
(
line
[
0
]
==
'v'
)
{
if
(
line
[
1
]
==
' '
) {
float
*
p
=
(
*
vertices
)[
cp
++
].
p
;
char
*
e1
,
*
e2
;
p
[
0
]
=
(
float
)
strtod
(
line
+
2
,
&
e1
);
p
[
1
]
=
(
float
)
strtod
(
e1
,
&
e2
);
p
[
2
]
=
(
float
)
strtod
(
e2
,
0
);
continue
; }
if
(
line
[
1
]
==
'n'
) {
/*float *n = (*vertices)[cn++].n; char *e1, *e2; n[0] = (float)strtod(line + 3, &e1); n[1] = (float)strtod(e1, &e2); n[2] = (float)strtod(e2, 0);*/
continue
; }
// no normals needed
if
(
line
[
1
]
==
't'
) {
float
*
t
=
(
*
vertices
)[
ct
++
].
t
;
char
*
e1
;
t
[
0
]
=
(
float
)
strtod
(
line
+
3
,
&
e1
);
t
[
1
]
=
(
float
)
strtod
(
e1
,
0
);
continue
; }
assert
(!
"unknown vertex attribute"
);
}
if
(
line
[
0
]
==
'f'
)
{
unsigned short
*
tri
=
(
*
indices
)
+
cf
;
cf
+=
3
;
char
*
e1
,
*
e2
,
*
e3
=
line
+
1
;
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
unsigned long
pi
=
strtoul
(
e3
+
1
,
&
e1
,
10
);
assert
(
e1
[
0
]
==
'/'
);
unsigned long
ti
=
strtoul
(
e1
+
1
,
&
e2
,
10
);
assert
(
e2
[
0
]
==
'/'
);
unsigned long
ni
=
strtoul
(
e2
+
1
,
&
e3
,
10
);
assert
(
pi
==
ti
&&
pi
==
ni
);
tri
[
i
]
=
(
unsigned short
)(
pi
-
1
);
}
continue
;
}
assert
(!
"unknown identifier"
);
}
fclose
(
file
);
return
1
;
}
static
GLuint
loadShader
(
GLenum
type
,
const
char
*
source
)
{
GLuint
shader
=
glCreateShader
(
type
);
if
(
shader
==
0
)
{
fprintf
(
stderr
,
"Could not create shader!\n"
);
return
0
;
}
glShaderSource
(
shader
,
1
,
&
source
,
NULL
);
glCompileShader
(
shader
);
GLint
compiled
;
glGetShaderiv
(
shader
,
GL_COMPILE_STATUS
,
&
compiled
);
if
(!
compiled
)
{
fprintf
(
stderr
,
"Could not compile shader!\n"
);
GLint
infoLen
=
0
;
glGetShaderiv
(
shader
,
GL_INFO_LOG_LENGTH
,
&
infoLen
);
if
(
infoLen
)
{
char
*
infoLog
=
(
char
*
)
malloc
(
infoLen
);
glGetShaderInfoLog
(
shader
,
infoLen
,
NULL
,
infoLog
);
fprintf
(
stderr
,
"%s\n"
,
infoLog
);
free
(
infoLog
);
}
glDeleteShader
(
shader
);
return
0
;
}
return
shader
;
}
static
GLuint
loadProgram
(
const
char
*
vp
,
const
char
*
fp
,
const
char
*
*
attributes
,
int
attributeCount
)
{
GLuint
vertexShader
=
loadShader
(
GL_VERTEX_SHADER
,
vp
);
if
(!
vertexShader
)
return
0
;
GLuint
fragmentShader
=
loadShader
(
GL_FRAGMENT_SHADER
,
fp
);
if
(!
fragmentShader
)
{
glDeleteShader
(
vertexShader
);
return
0
;
}
GLuint
program
=
glCreateProgram
();
if
(
program
==
0
)
{
fprintf
(
stderr
,
"Could not create program!\n"
);
return
0
;
}
glAttachShader
(
program
,
vertexShader
);
glAttachShader
(
program
,
fragmentShader
);
for
(
int
i
=
0
;
i
<
attributeCount
;
i
++
)
glBindAttribLocation
(
program
,
i
,
attributes
[
i
]);
glLinkProgram
(
program
);
glDeleteShader
(
vertexShader
);
glDeleteShader
(
fragmentShader
);
GLint
linked
;
glGetProgramiv
(
program
,
GL_LINK_STATUS
,
&
linked
);
if
(!
linked
)
{
fprintf
(
stderr
,
"Could not link program!\n"
);
GLint
infoLen
=
0
;
glGetProgramiv
(
program
,
GL_INFO_LOG_LENGTH
,
&
infoLen
);
if
(
infoLen
)
{
char
*
infoLog
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
infoLen
);
glGetProgramInfoLog
(
program
,
infoLen
,
NULL
,
infoLog
);
fprintf
(
stderr
,
"%s\n"
,
infoLog
);
free
(
infoLog
);
}
glDeleteProgram
(
program
);
return
0
;
}
return
program
;
}
static
void
multiplyMatrices
(
float
*
out
,
float
*
a
,
float
*
b
)
{
for
(
int
y
=
0
;
y
<
4
;
y
++
)
for
(
int
x
=
0
;
x
<
4
;
x
++
)
out
[
y
*
4
+
x
]
=
a
[
x
]
*
b
[
y
*
4
]
+
a
[
4
+
x
]
*
b
[
y
*
4
+
1
]
+
a
[
8
+
x
]
*
b
[
y
*
4
+
2
]
+
a
[
12
+
x
]
*
b
[
y
*
4
+
3
];
}
static
void
translationMatrix
(
float
*
out
,
float
x
,
float
y
,
float
z
)
{
out
[
0
]
=
1.0f
;
out
[
1
]
=
0.0f
;
out
[
2
]
=
0.0f
;
out
[
3
]
=
0.0f
;
out
[
4
]
=
0.0f
;
out
[
5
]
=
1.0f
;
out
[
6
]
=
0.0f
;
out
[
7
]
=
0.0f
;
out
[
8
]
=
0.0f
;
out
[
9
]
=
0.0f
;
out
[
10
]
=
1.0f
;
out
[
11
]
=
0.0f
;
out
[
12
]
=
x
;
out
[
13
]
=
y
;
out
[
14
]
=
z
;
out
[
15
]
=
1.0f
;
}
static
void
rotationMatrix
(
float
*
out
,
float
angle
,
float
x
,
float
y
,
float
z
)
{
angle
*= (
float
)
M_PI
/
180.0f
;
float
c
=
cosf
(
angle
),
s
=
sinf
(
angle
),
c2
=
1.0f
-
c
;
out
[
0
]
=
x
*
x
*
c2
+
c
;
out
[
1
]
=
y
*
x
*
c2
+
z
*
s
;
out
[
2
]
=
x
*
z
*
c2
-
y
*
s
;
out
[
3
]
=
0.0f
;
out
[
4
]
=
x
*
y
*
c2
-
z
*
s
;
out
[
5
]
=
y
*
y
*
c2
+
c
;
out
[
6
]
=
y
*
z
*
c2
+
x
*
s
;
out
[
7
]
=
0.0f
;
out
[
8
]
=
x
*
z
*
c2
+
y
*
s
;
out
[
9
]
=
y
*
z
*
c2
-
x
*
s
;
out
[
10
]
=
z
*
z
*
c2
+
c
;
out
[
11
]
=
0.0f
;
out
[
12
]
=
0.0f
;
out
[
13
]
=
0.0f
;
out
[
14
]
=
0.0f
;
out
[
15
]
=
1.0f
;
}
static
void
transformPosition
(
float
*
out
,
float
*
m
,
float
*
p
)
{
float
d
=
1.0f
/ (
m
[
3
]
*
p
[
0
]
+
m
[
7
]
*
p
[
1
]
+
m
[
11
]
*
p
[
2
]
+
m
[
15
]);
out
[
2
]
=
d
*
(
m
[
2
]
*
p
[
0
]
+
m
[
6
]
*
p
[
1
]
+
m
[
10
]
*
p
[
2
]
+
m
[
14
]);
out
[
1
]
=
d
*
(
m
[
1
]
*
p
[
0
]
+
m
[
5
]
*
p
[
1
]
+
m
[
9
]
*
p
[
2
]
+
m
[
13
]);
out
[
0
]
=
d
*
(
m
[
0
]
*
p
[
0
]
+
m
[
4
]
*
p
[
1
]
+
m
[
8
]
*
p
[
2
]
+
m
[
12
]);
}
static
void
transposeMatrix
(
float
*
out
,
float
*
m
)
{
out
[
0
]
=
m
[
0
];
out
[
1
]
=
m
[
4
];
out
[
2
]
=
m
[
8
];
out
[
3
]
=
m
[
12
];
out
[
4
]
=
m
[
1
];
out
[
5
]
=
m
[
5
];
out
[
6
]
=
m
[
9
];
out
[
7
]
=
m
[
13
];
out
[
8
]
=
m
[
2
];
out
[
9
]
=
m
[
6
];
out
[
10
]
=
m
[
10
];
out
[
11
]
=
m
[
14
];
out
[
12
]
=
m
[
3
];
out
[
13
]
=
m
[
7
];
out
[
14
]
=
m
[
11
];
out
[
15
]
=
m
[
15
];
}
static
void
perspectiveMatrix
(
float
*
out
,
float
fovy
,
float
aspect
,
float
zNear
,
float
zFar
)
{
float
f
=
1.0f
/
tanf
(
fovy
*
(
float
)
M_PI
/
360.0f
);
float
izFN
=
1.0f
/ (
zNear
-
zFar
);
out
[
0
]
=
f
/
aspect
;
out
[
1
]
=
0.0f
;
out
[
2
]
=
0.0f
;
out
[
3
]
=
0.0f
;
out
[
4
]
=
0.0f
;
out
[
5
]
=
f
;
out
[
6
]
=
0.0f
;
out
[
7
]
=
0.0f
;
out
[
8
]
=
0.0f
;
out
[
9
]
=
0.0f
;
out
[
10
]
=
(
zFar
+
zNear
)
*
izFN
;
out
[
11
]
=
-1.0f
;
out
[
12
]
=
0.0f
;
out
[
13
]
=
0.0f
;
out
[
14
]
=
2.0f
*
zFar
*
zNear
*
izFN
;
out
[
15
]
=
0.0f
;
}
static
void
fpsCameraViewMatrix
(
GLFWwindow
*
window
,
float
*
view
)
{
// initial camera config
static
float
position
[]
=
{
0.0f
,
0.3f
,
1.5f
};
static
float
rotation
[]
=
{
0.0f
,
0.0f
};
// mouse look
static
double
lastMouse
[]
=
{
0.0
,
0.0
};
double
mouse
[
2
];
glfwGetCursorPos
(
window
,
&
mouse
[
0
],
&
mouse
[
1
]);
if
(
glfwGetMouseButton
(
window
,
GLFW_MOUSE_BUTTON_LEFT
)
==
GLFW_PRESS
)
{
rotation
[
0
]
+=
(
float
)(
mouse
[
1
]
-
lastMouse
[
1
])
*
-0.2f
;
rotation
[
1
]
+=
(
float
)(
mouse
[
0
]
-
lastMouse
[
0
])
*
-0.2f
;
}
lastMouse
[
0
]
=
mouse
[
0
];
lastMouse
[
1
]
=
mouse
[
1
];
float
rotationY
[
16
],
rotationX
[
16
],
rotationYX
[
16
];
rotationMatrix
(
rotationX
,
rotation
[
0
],
1.0f
,
0.0f
,
0.0f
);
rotationMatrix
(
rotationY
,
rotation
[
1
],
0.0f
,
1.0f
,
0.0f
);
multiplyMatrices
(
rotationYX
,
rotationY
,
rotationX
);
// keyboard movement (WSADEQ)
float
speed
=
(
glfwGetKey
(
window
,
GLFW_KEY_LEFT_SHIFT
)
==
GLFW_PRESS
) ?
0.1f
:
0.01f
;
float
movement
[
3
]
=
{
0
};
if
(
glfwGetKey
(
window
,
GLFW_KEY_W
)
==
GLFW_PRESS
)
movement
[
2
]
-=
speed
;
if
(
glfwGetKey
(
window
,
GLFW_KEY_S
)
==
GLFW_PRESS
)
movement
[
2
]
+=
speed
;
if
(
glfwGetKey
(
window
,
GLFW_KEY_A
)
==
GLFW_PRESS
)
movement
[
0
]
-=
speed
;
if
(
glfwGetKey
(
window
,
GLFW_KEY_D
)
==
GLFW_PRESS
)
movement
[
0
]
+=
speed
;
if
(
glfwGetKey
(
window
,
GLFW_KEY_E
)
==
GLFW_PRESS
)
movement
[
1
]
-=
speed
;
if
(
glfwGetKey
(
window
,
GLFW_KEY_Q
)
==
GLFW_PRESS
)
movement
[
1
]
+=
speed
;
float
worldMovement
[
3
];
transformPosition
(
worldMovement
,
rotationYX
,
movement
);
position
[
0
]
+=
worldMovement
[
0
];
position
[
1
]
+=
worldMovement
[
1
];
position
[
2
]
+=
worldMovement
[
2
];
// construct view matrix
float
inverseRotation
[
16
],
inverseTranslation
[
16
];
transposeMatrix
(
inverseRotation
,
rotationYX
);
translationMatrix
(
inverseTranslation
,
-
position
[
0
],
-
position
[
1
],
-
position
[
2
]);
multiplyMatrices
(
view
,
inverseRotation
,
inverseTranslation
);
// = inverse(translation(position) * rotationYX);
}
Back
|
FazBrowse Home
|
New Git URL