Repository navigation
Expand file tree
/
Copy pathGraphics.cpp
More file actions
195 lines (177 loc) · 5.63 KB
/
Copy pathGraphics.cpp
File metadata and controls
195 lines (177 loc) · 5.63 KB
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
#include "Graphics.h"
extern "C" {
#include "graphics/transform.h"
#include "util/logger.h"
#include "util/log.h"
}
using namespace std;
void Graphics::free()
{
ilG_shape_free(&box);
ilG_shape_free(&ico);
ilG_skybox_free(&skybox);
ilG_ambient_free(&ambient);
ilG_lighting_free(&sun);
ilG_lighting_free(&point);
ilG_tonemapper_free(&tonemapper);
initialized = false;
}
bool Graphics::init(const Flags &flags)
{
if (initialized) {
free();
} else {
ilG_renderman_init(rm);
ilG_floatspace_init(&space, 2);
}
initialized = true;
ilG_renderman_setup(rm, flags.msaa != 0, flags.hdr);
ilG_renderman_resize(rm, 800, 600);
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
ilG_box(&box);
ilG_icosahedron(&ico);
{
ilG_tex skytex;
ilA_img skyfaces[6];
static const char *const files[6] = {
"north.png",
"south.png",
"up.png",
"down.png",
"west.png",
"east.png"
};
bool cont = true;
for (unsigned i = 0; i < 6; i++) {
ilA_imgerr res = ilA_img_loadfile(skyfaces + i, &demo_fs, files[i]);
if (res) {
il_log("%s: %s", files[i], ilA_img_strerror(res));
cont = false;
break;
}
}
if (!cont) {
return false;
}
ilG_tex_loadcube(&skytex, skyfaces);
char *error;
if (!ilG_skybox_build(&skybox, rm, skytex, &box, &error)) {
il_error("skybox: %s", error);
::free(error);
return false;
}
}
char *error;
if (!ilG_ambient_build(&ambient, rm, &error)) {
il_error("ambient: %s", error);
::free(error);
return false;
}
if (!ilG_lighting_build(&sun, rm, &ico, ILG_SUN, flags.msaa != 0, &error)) {
il_error("sunlighting: %s", error);
::free(error);
return false;
}
if (!ilG_lighting_build(&point, rm, &ico, ILG_POINT, flags.msaa != 0, &error)) {
il_error("lighting: %s", error);
::free(error);
return false;
}
if (!ilG_tonemapper_build(&tonemapper, rm, flags.msaa != 0, &error)) {
il_error("tonemapper: %s", error);
::free(error);
return false;
}
return true;
}
void Graphics::draw(State &state)
{
SDL_SetWindowGrab(window.window, SDL_bool(state.mouse_grab));
SDL_SetRelativeMouseMode(SDL_bool(state.mouse_grab));
SDL_GL_SetSwapInterval(state.vsync);
auto s = window.resize();
unsigned width = s.first, height = s.second;
if (unsigned(width) != rm->width || unsigned(height) != rm->height) {
ilG_renderman_resize(rm, width, height);
ilG_tonemapper_resize(&tonemapper, width, height);
}
space.projection = il_mat_perspective(state.fov, width / float(height), state.zmin, state.zfar);
il_mat skybox_vp = viewmat(ILG_VIEW_R | ILG_PROJECTION);
auto slocs = state.sunlight_locs;
auto plocs = state.point_locs;
std::vector<il_mat>
/* ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION
ILG_MODEL_T | ILG_VIEW_T
ILG_MODEL_T | ILG_VP */
sun_ivp = objmats(slocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.sunlight_count),
sun_mv = objmats(slocs, ILG_MODEL_T | ILG_VIEW_T, state.sunlight_count),
sun_vp = objmats(slocs, ILG_MODEL_T | ILG_VP, state.sunlight_count),
pnt_ivp = objmats(plocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.point_count),
pnt_mv = objmats(plocs, ILG_MODEL_T | ILG_VIEW_T, state.point_count),
pnt_vp = objmats(plocs, ILG_MODEL_T | ILG_VP, state.point_count);
const float fovsquared = state.fov * state.fov;
ambient.color = state.ambient_col;
ambient.fovsquared = fovsquared;
sun.gbuffer = &rm->gbuffer;
sun.accum = &rm->accum;
sun.width = width;
sun.height = height;
sun.fovsquared = fovsquared;
point.gbuffer = &rm->gbuffer;
point.accum = &rm->accum;
point.width = width;
point.height = height;
point.fovsquared = fovsquared;
tonemapper.exposure = state.exposure;
tonemapper.gamma = state.gamma;
#ifndef __APPLE__
#define push(n) glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, n)
#define pop() glPopDebugGroup()
#define with(n) for (bool cont = (push(n), true); cont; pop(), cont = false)
#else
// OS X does not yet support OpenGL 4.3 (still stuck on 4.1)
#define push(n)
#define pop
#define with(n)
#endif
with("Geometry") {
ilG_geometry_bind(&rm->gbuffer);
}
with("Skybox") {
ilG_skybox_draw(&skybox, skybox_vp);
}
for (auto &d : drawables) {
with(d->name()) {
d->draw(*this);
}
}
with("Ambient Lighting") {
ilG_ambient_draw(&ambient);
}
with("Sunlights") {
ilG_lighting_draw(&sun, sun_ivp.data(), sun_mv.data(), sun_vp.data(),
state.sunlight_lights, state.sunlight_count);
}
with("Point Lights") {
ilG_lighting_draw(&point, pnt_ivp.data(), pnt_mv.data(), pnt_vp.data(),
state.point_lights, state.point_count);
}
with("Tone Mapping") {
ilG_tonemapper_draw(&tonemapper);
}
window.swap();
#undef push
#undef pop
#undef with
}
il_mat Graphics::viewmat(int type)
{
return ilG_floatspace_viewmat(&space, type);
}
std::vector<il_mat> Graphics::objmats(unsigned *objects, int type, unsigned count)
{
std::vector<il_mat> mats;
mats.reserve(count);
ilG_floatspace_objmats(&space, mats.data(), objects, type, count);
return mats;
}