Skip to content

Commit 7c3827f

Browse files
author
Tiffany Bennett
committed
Fix teapots demo
1 parent 9522149 commit 7c3827f

File tree

7 files changed

+661
-188
lines changed

7 files changed

+661
-188
lines changed

src/Demo.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
#include <cstdio>
66
#include <cstring>
77
#include <fenv.h>
8+
#include <vector>
9+
#include <string>
810

911
#include "tgl/tgl.h"
1012

1113
extern "C" {
14+
#include "util/logger.h"
1215
#include "util/log.h"
1316
#include "util/version.h"
1417
#include "util/opt.h"
1518
#include "graphics/graphics.h"
1619
}
1720

21+
using namespace std;
22+
1823
enum ArgType {
1924
NO_ARG,
2025
REQUIRED,
@@ -107,6 +112,75 @@ void demoLoad(int argc, char **argv)
107112
il_load_ilgraphics();
108113
}
109114

115+
struct DebugGroupStack {
116+
vector<string> entries;
117+
};
118+
119+
static GLvoid error_cb(GLenum esource, GLenum etype, GLuint id, GLenum eseverity,
120+
GLsizei length, const GLchar* message, const GLvoid* user)
121+
{
122+
auto &stack = *reinterpret_cast<DebugGroupStack*>(const_cast<void*>(user));
123+
const char *ssource;
124+
switch(esource) {
125+
case GL_DEBUG_SOURCE_API_ARB: ssource=" API"; break;
126+
case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB: ssource=" Window System"; break;
127+
case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB: ssource=" Shader Compiler"; break;
128+
case GL_DEBUG_SOURCE_THIRD_PARTY_ARB: ssource=" Third Party"; break;
129+
case GL_DEBUG_SOURCE_APPLICATION_ARB: ssource=" Application"; break;
130+
case GL_DEBUG_SOURCE_OTHER_ARB: ssource=""; break;
131+
default: ssource="???";
132+
}
133+
const char *stype;
134+
switch(etype) {
135+
case GL_DEBUG_TYPE_ERROR_ARB: stype=" error"; break;
136+
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB: stype=" deprecated behaviour"; break;
137+
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB: stype=" undefined behaviour"; break;
138+
case GL_DEBUG_TYPE_PORTABILITY_ARB: stype=" portability issue"; break;
139+
case GL_DEBUG_TYPE_PERFORMANCE_ARB: stype=" performance issue"; break;
140+
case GL_DEBUG_TYPE_OTHER_ARB: stype=""; break;
141+
case GL_DEBUG_TYPE_PUSH_GROUP: {
142+
stack.entries.push_back(message);
143+
return;
144+
}
145+
case GL_DEBUG_TYPE_POP_GROUP: {
146+
stack.entries.pop_back();
147+
return;
148+
}
149+
default: stype="???";
150+
}
151+
const char *sseverity;
152+
switch(eseverity) {
153+
case GL_DEBUG_SEVERITY_HIGH_ARB: sseverity="high"; break;
154+
case GL_DEBUG_SEVERITY_MEDIUM_ARB: sseverity="medium"; break;
155+
case GL_DEBUG_SEVERITY_LOW_ARB: sseverity="low"; break;
156+
default: sseverity="???";
157+
}
158+
string msg(message, length);
159+
if (msg.back() == '\n') {
160+
msg.pop_back();
161+
}
162+
163+
string groups;
164+
if (!stack.entries.empty()) {
165+
groups += " in " + stack.entries.front();
166+
for (auto it = stack.entries.begin()+1; it != stack.entries.end(); it++) {
167+
groups += ".";
168+
groups += *it;
169+
}
170+
}
171+
172+
string buf = string(sseverity) + stype + " #" + to_string(id) + groups + ": " + msg;
173+
string source = string("OpenGL") + ssource;
174+
175+
il_logmsg lmsg;
176+
memset(&lmsg, 0, sizeof(il_logmsg));
177+
lmsg.level = IL_NOTIFY;
178+
lmsg.msg = il_string_bin(const_cast<char*>(buf.data()), buf.size());
179+
lmsg.func = il_string_bin(const_cast<char*>(source.data()), source.size());
180+
181+
il_logger_log(il_logger_cur(), lmsg);
182+
}
183+
110184
Window createWindow(const char *title, unsigned msaa)
111185
{
112186
Window window;
@@ -140,6 +214,17 @@ Window createWindow(const char *title, unsigned msaa)
140214
exit(1);
141215
}
142216

217+
if (TGL_EXTENSION(KHR_debug)) {
218+
glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PUSH_GROUP, GL_DONT_CARE, 0, NULL, true);
219+
glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_POP_GROUP, GL_DONT_CARE, 0, NULL, true);
220+
glDebugMessageCallback(&error_cb, new DebugGroupStack);
221+
glEnable(GL_DEBUG_OUTPUT);
222+
il_log("KHR_debug present, enabling advanced errors");
223+
tgl_check("glDebugMessageCallback()");
224+
} else {
225+
il_log("KHR_debug missing");
226+
}
227+
143228
return window;
144229
}
145230

src/Graphics.cpp

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#include "Graphics.h"
2+
3+
extern "C" {
4+
#include "graphics/transform.h"
5+
#include "util/logger.h"
6+
#include "util/log.h"
7+
}
8+
9+
using namespace std;
10+
11+
void Graphics::free()
12+
{
13+
ilG_shape_free(&box);
14+
ilG_shape_free(&ico);
15+
ilG_skybox_free(&skybox);
16+
ilG_ambient_free(&ambient);
17+
ilG_lighting_free(&sun);
18+
ilG_lighting_free(&point);
19+
ilG_tonemapper_free(&tonemapper);
20+
21+
initialized = false;
22+
}
23+
24+
bool Graphics::init(const Flags &flags)
25+
{
26+
if (initialized) {
27+
free();
28+
} else {
29+
ilG_renderman_init(rm);
30+
ilG_floatspace_init(&space, 2);
31+
}
32+
initialized = true;
33+
34+
ilG_renderman_setup(rm, flags.msaa, flags.hdr);
35+
ilG_renderman_resize(rm, 800, 600);
36+
ilG_floatspace_build(&space, rm);
37+
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
38+
39+
ilG_box(&box);
40+
ilG_icosahedron(&ico);
41+
42+
{
43+
ilG_tex skytex;
44+
ilA_img skyfaces[6];
45+
static const char *const files[6] = {
46+
"north.png",
47+
"south.png",
48+
"up.png",
49+
"down.png",
50+
"west.png",
51+
"east.png"
52+
};
53+
bool cont = true;
54+
for (unsigned i = 0; i < 6; i++) {
55+
ilA_imgerr res = ilA_img_loadfile(skyfaces + i, &demo_fs, files[i]);
56+
if (res) {
57+
il_log("%s: %s", files[i], ilA_img_strerror(res));
58+
cont = false;
59+
break;
60+
}
61+
}
62+
if (!cont) {
63+
return false;
64+
}
65+
ilG_tex_loadcube(&skytex, skyfaces);
66+
char *error;
67+
if (!ilG_skybox_build(&skybox, rm, skytex, &box, &error)) {
68+
il_error("skybox: %s", error);
69+
::free(error);
70+
return false;
71+
}
72+
}
73+
74+
char *error;
75+
if (!ilG_ambient_build(&ambient, rm, &error)) {
76+
il_error("ambient: %s", error);
77+
::free(error);
78+
return false;
79+
}
80+
if (!ilG_lighting_build(&sun, rm, &ico, ILG_SUN, flags.msaa, &error)) {
81+
il_error("sunlighting: %s", error);
82+
::free(error);
83+
return false;
84+
}
85+
if (!ilG_lighting_build(&point, rm, &ico, ILG_POINT, flags.msaa, &error)) {
86+
il_error("lighting: %s", error);
87+
::free(error);
88+
return false;
89+
}
90+
if (!ilG_tonemapper_build(&tonemapper, rm, flags.msaa, &error)) {
91+
il_error("tonemapper: %s", error);
92+
::free(error);
93+
return false;
94+
}
95+
return true;
96+
}
97+
98+
void Graphics::draw(State &state)
99+
{
100+
SDL_SetWindowGrab(window.window, SDL_bool(state.mouse_grab));
101+
SDL_SetRelativeMouseMode(SDL_bool(state.mouse_grab));
102+
SDL_GL_SetSwapInterval(state.vsync);
103+
104+
auto s = window.resize();
105+
unsigned width = s.first, height = s.second;
106+
if (unsigned(width) != rm->width || unsigned(height) != rm->height) {
107+
ilG_renderman_resize(rm, width, height);
108+
ilG_tonemapper_resize(&tonemapper, width, height);
109+
}
110+
space.projection = il_mat_perspective(state.fov, width / float(height), state.zmin, state.zfar);
111+
112+
il_mat skybox_vp = viewmat(ILG_VIEW_R | ILG_PROJECTION);
113+
auto slocs = state.sunlight_locs;
114+
auto plocs = state.point_locs;
115+
std::vector<il_mat>
116+
/* ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION
117+
ILG_MODEL_T | ILG_VIEW_T
118+
ILG_MODEL_T | ILG_VP */
119+
sun_ivp = objmats(slocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.sunlight_count),
120+
sun_mv = objmats(slocs, ILG_MODEL_T | ILG_VIEW_T, state.sunlight_count),
121+
sun_vp = objmats(slocs, ILG_MODEL_T | ILG_VP, state.sunlight_count),
122+
pnt_ivp = objmats(plocs, ILG_INVERSE | ILG_VIEW_R | ILG_PROJECTION, state.point_count),
123+
pnt_mv = objmats(plocs, ILG_MODEL_T | ILG_VIEW_T, state.point_count),
124+
pnt_vp = objmats(plocs, ILG_MODEL_T | ILG_VP, state.point_count);
125+
126+
const float fovsquared = state.fov * state.fov;
127+
ambient.color = state.ambient_col;
128+
ambient.fovsquared = fovsquared;
129+
sun.gbuffer = &rm->gbuffer;
130+
sun.accum = &rm->accum;
131+
sun.width = width;
132+
sun.height = height;
133+
sun.fovsquared = fovsquared;
134+
point.gbuffer = &rm->gbuffer;
135+
point.accum = &rm->accum;
136+
point.width = width;
137+
point.height = height;
138+
point.fovsquared = fovsquared;
139+
tonemapper.exposure = state.exposure;
140+
tonemapper.gamma = state.gamma;
141+
142+
#define push(n) glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, n)
143+
#define pop() glPopDebugGroup()
144+
#define with(n) for (bool cont = (push(n), true); cont; pop(), cont = false)
145+
146+
with("Geometry") {
147+
ilG_geometry_bind(&rm->gbuffer);
148+
}
149+
with("Skybox") {
150+
ilG_skybox_draw(&skybox, skybox_vp);
151+
}
152+
for (auto &d : drawables) {
153+
with(d->name()) {
154+
d->draw(*this);
155+
}
156+
}
157+
with("Ambient Lighting") {
158+
ilG_ambient_draw(&ambient);
159+
}
160+
with("Sunlights") {
161+
ilG_lighting_draw(&sun, sun_ivp.data(), sun_mv.data(), sun_vp.data(),
162+
state.sunlight_lights, state.sunlight_count);
163+
}
164+
with("Point Lights") {
165+
ilG_lighting_draw(&point, pnt_ivp.data(), pnt_mv.data(), pnt_vp.data(),
166+
state.point_lights, state.point_count);
167+
}
168+
with("Tone Mapping") {
169+
ilG_tonemapper_draw(&tonemapper);
170+
}
171+
window.swap();
172+
173+
#undef push
174+
#undef pop
175+
#undef with
176+
}
177+
178+
il_mat Graphics::viewmat(int type)
179+
{
180+
return ilG_floatspace_viewmat(&space, type);
181+
}
182+
183+
std::vector<il_mat> Graphics::objmats(unsigned *objects, int type, unsigned count)
184+
{
185+
std::vector<il_mat> mats;
186+
mats.reserve(count);
187+
ilG_floatspace_objmats(&space, mats.data(), objects, type, count);
188+
return mats;
189+
}

src/Graphics.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef DEMO_GRAPHICS_H
2+
#define DEMO_GRAPHICS_H
3+
4+
#include <SDL.h>
5+
#include <vector>
6+
#include <string>
7+
8+
#include "Demo.h"
9+
10+
extern "C" {
11+
#include "graphics/renderer.h"
12+
#include "graphics/floatspace.h"
13+
}
14+
15+
struct State {
16+
bool mouse_grab = false;
17+
bool vsync = true;
18+
il_mat projection;
19+
float fov = M_PI / 4;
20+
float zmin = 0.5, zfar = 1024.0;
21+
il_vec3 ambient_col = il_vec3_new(.2,.2,.2);
22+
float exposure = 1.0, gamma = 1.0;
23+
24+
unsigned *sunlight_locs = nullptr;
25+
ilG_light *sunlight_lights = nullptr;
26+
size_t sunlight_count = 0;
27+
unsigned *point_locs = nullptr;
28+
ilG_light *point_lights = nullptr;
29+
size_t point_count = 0;
30+
};
31+
32+
class Graphics;
33+
34+
class Drawable {
35+
public:
36+
virtual void draw(Graphics &graphics) = 0;
37+
virtual const char *name() {
38+
return "Untitled";
39+
}
40+
};
41+
42+
class Graphics {
43+
public:
44+
struct Flags {
45+
bool debug = false;
46+
bool srgb = false;
47+
bool hdr = true;
48+
unsigned msaa = 0;
49+
};
50+
51+
Graphics(Window &window)
52+
: window(window) {}
53+
54+
void free();
55+
bool init(const Flags &flags);
56+
void draw(State &state);
57+
il_mat viewmat(int type);
58+
std::vector<il_mat> objmats(unsigned *objects, int type, unsigned count);
59+
60+
Window &window;
61+
ilG_renderman rm[1];
62+
ilG_floatspace space;
63+
ilG_shape box, ico;
64+
ilG_skybox skybox;
65+
std::vector<Drawable*> drawables;
66+
ilG_ambient ambient;
67+
ilG_lighting sun, point;
68+
ilG_tonemapper tonemapper;
69+
bool initialized = false;
70+
};
71+
72+
#endif

0 commit comments

Comments
 (0)