Skip to content

Commit 64207e9

Browse files
author
Tiffany Bennett
committed
Fix lighting demo
1 parent d756392 commit 64207e9

File tree

5 files changed

+156
-168
lines changed

5 files changed

+156
-168
lines changed

src/lighting/Tupfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
include_rules
22

3-
: foreach *.c |> !cc |>
3+
: foreach *.cpp |> !cxx |>
44
: *.o |> !ld |> $(TOP)/lighting$(PROG_SUFFIX)

src/lighting/comp.c renamed to src/lighting/comp.cpp

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include "comp.h"
2+
3+
extern "C" {
14
#include "graphics/context.h"
25
#include "graphics/floatspace.h"
36
#include "graphics/material.h"
@@ -6,6 +9,7 @@
69
#include "graphics/tex.h"
710
#include "math/matrix.h"
811
#include "util/log.h"
12+
}
913

1014
#include "tgl/tgl.h"
1115

@@ -158,51 +162,34 @@ enum {
158162
TEX_EMISSION,
159163
};
160164

161-
typedef struct ilG_computer {
162-
ilG_renderman *rm;
163-
ilG_matid mat;
164-
tgl_vao vao;
165-
GLuint v_vbo, n_vbo, t_vbo;
166-
GLuint mvp_loc, imt_loc;
167-
ilG_tex tex_albedo, tex_normal, tex_refraction, tex_emission;
168-
} ilG_computer;
169-
170-
static void comp_draw(void *obj, ilG_rendid id, il_mat **mats, const unsigned *objects, unsigned num_mats)
165+
void Computer::draw(il_mat mvp, il_mat imt)
171166
{
172-
(void)id, (void)objects;
173-
ilG_computer *self = obj;
174-
ilG_material *mat = ilG_renderman_findMaterial(self->rm, self->mat);
167+
ilG_material *mat = ilG_renderman_findMaterial(rm, this->mat);
175168

176-
ilG_tex_bind(&self->tex_albedo);
177-
ilG_tex_bind(&self->tex_normal);
178-
ilG_tex_bind(&self->tex_refraction);
179-
ilG_tex_bind(&self->tex_emission);
169+
ilG_tex_bind(&tex_albedo);
170+
ilG_tex_bind(&tex_normal);
171+
ilG_tex_bind(&tex_refraction);
172+
ilG_tex_bind(&tex_emission);
180173

181174
ilG_material_bind(mat);
182-
tgl_vao_bind(&self->vao);
183-
for (unsigned i = 0; i < num_mats; i++) {
184-
ilG_material_bindMatrix(mat, self->mvp_loc, mats[0][i]);
185-
ilG_material_bindMatrix(mat, self->imt_loc, mats[1][i]);
186-
glDrawArrays(GL_TRIANGLES, 0, 36);
187-
}
175+
tgl_vao_bind(&vao);
176+
ilG_material_bindMatrix(mat, mvp_loc, mvp);
177+
ilG_material_bindMatrix(mat, imt_loc, imt);
178+
glDrawArrays(GL_TRIANGLES, 0, 36);
188179
}
189180

190-
static void comp_free(void *obj)
181+
void Computer::free()
191182
{
192-
ilG_computer *self = obj;
193-
ilG_renderman_delMaterial(self->rm, self->mat);
194-
tgl_vao_free(&self->vao);
195-
glDeleteBuffers(1, &self->v_vbo);
196-
glDeleteBuffers(1, &self->n_vbo);
197-
glDeleteBuffers(1, &self->t_vbo);
198-
free(self);
183+
ilG_renderman_delMaterial(rm, mat);
184+
tgl_vao_free(&vao);
185+
glDeleteBuffers(1, &v_vbo);
186+
glDeleteBuffers(1, &n_vbo);
187+
glDeleteBuffers(1, &t_vbo);
199188
}
200189

201-
static bool comp_build(void *obj, ilG_rendid id, ilG_renderman *rm, ilG_buildresult *out)
190+
bool Computer::build(ilG_renderman *rm, char **error)
202191
{
203-
(void)id;
204-
ilG_computer *self = obj;
205-
self->rm = rm;
192+
this->rm = rm;
206193

207194
ilG_material m;
208195
ilG_material_init(&m);
@@ -219,36 +206,36 @@ static bool comp_build(void *obj, ilG_rendid id, ilG_renderman *rm, ilG_buildres
219206
ilG_material_textureUnit(&m, TEX_NORMAL, "tex_Normal");
220207
ilG_material_textureUnit(&m, TEX_REFRACTION, "tex_Reflect");
221208
ilG_material_textureUnit(&m, TEX_EMISSION, "tex_Emission");
222-
if (!ilG_renderman_addMaterialFromFile(rm, m, "comp.vert", "comp.frag", &self->mat, &out->error)) {
209+
if (!ilG_renderman_addMaterialFromFile(rm, m, "comp.vert", "comp.frag", &mat, error)) {
223210
return false;
224211
}
225-
ilG_material *mat = ilG_renderman_findMaterial(rm, self->mat);
226-
self->mvp_loc = ilG_material_getLoc(mat, "mvp");
227-
self->imt_loc = ilG_material_getLoc(mat, "imt");
212+
ilG_material *mat = ilG_renderman_findMaterial(rm, this->mat);
213+
mvp_loc = ilG_material_getLoc(mat, "mvp");
214+
imt_loc = ilG_material_getLoc(mat, "imt");
228215

229-
tgl_vao_init(&self->vao);
230-
tgl_vao_bind(&self->vao);
231-
glGenBuffers(1, &self->v_vbo);
232-
glBindBuffer(GL_ARRAY_BUFFER, self->v_vbo);
216+
tgl_vao_init(&vao);
217+
tgl_vao_bind(&vao);
218+
glGenBuffers(1, &v_vbo);
219+
glBindBuffer(GL_ARRAY_BUFFER, v_vbo);
233220
glBufferData(GL_ARRAY_BUFFER, sizeof(cube), cube, GL_STATIC_DRAW);
234221
glVertexAttribPointer(ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
235222
glEnableVertexAttribArray(ATTRIB_POSITION);
236-
glGenBuffers(1, &self->n_vbo);
237-
glBindBuffer(GL_ARRAY_BUFFER, self->n_vbo);
223+
glGenBuffers(1, &n_vbo);
224+
glBindBuffer(GL_ARRAY_BUFFER, n_vbo);
238225
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_n), cube_n, GL_STATIC_DRAW);
239226
glVertexAttribPointer(ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, NULL);
240227
glEnableVertexAttribArray(ATTRIB_NORMAL);
241-
glGenBuffers(1, &self->t_vbo);
242-
glBindBuffer(GL_ARRAY_BUFFER, self->t_vbo);
228+
glGenBuffers(1, &t_vbo);
229+
glBindBuffer(GL_ARRAY_BUFFER, t_vbo);
243230
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_t), cube_t, GL_STATIC_DRAW);
244231
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, NULL);
245232
glEnableVertexAttribArray(ATTRIB_TEXCOORD);
246233

247234
ilG_tex *texes[4] = {
248-
&self->tex_albedo,
249-
&self->tex_normal,
250-
&self->tex_refraction,
251-
&self->tex_emission
235+
&tex_albedo,
236+
&tex_normal,
237+
&tex_refraction,
238+
&tex_emission
252239
};
253240
static const char *const files[] = {
254241
"comp1a.png",
@@ -267,20 +254,5 @@ static bool comp_build(void *obj, ilG_rendid id, ilG_renderman *rm, ilG_buildres
267254
texes[i]->unit = i;
268255
}
269256

270-
int *types = calloc(2, sizeof(int));
271-
types[0] = ILG_MVP;
272-
types[1] = ILG_IMT;
273-
out->free = comp_free;
274-
out->draw = comp_draw;
275-
out->types = types;
276-
out->num_types = 2;
277-
out->obj = obj;
278-
out->name = strdup("Computer");
279257
return true;
280258
}
281-
282-
ilG_builder ilG_computer_builder()
283-
{
284-
ilG_computer *self = calloc(1, sizeof(ilG_computer));
285-
return ilG_builder_wrap(self, comp_build);
286-
}

src/lighting/comp.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef DEMO_COMP_H
2+
#define DEMO_COMP_H
3+
4+
extern "C" {
5+
#include "graphics/renderer.h"
6+
}
7+
8+
class Computer {
9+
ilG_renderman *rm;
10+
ilG_matid mat;
11+
tgl_vao vao;
12+
GLuint v_vbo, n_vbo, t_vbo;
13+
GLuint mvp_loc, imt_loc;
14+
ilG_tex tex_albedo, tex_normal, tex_refraction, tex_emission;
15+
16+
public:
17+
void free();
18+
bool build(ilG_renderman *rm, char **error);
19+
void draw(il_mat mvp, il_mat imt);
20+
};
21+
22+
23+
#endif

src/lighting/lighting.c

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/lighting/lighting.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <SDL.h>
2+
#include <time.h>
3+
#include <sys/time.h>
4+
#include <math.h>
5+
6+
#include "Graphics.h"
7+
#include "comp.h"
8+
9+
extern "C" {
10+
#include "graphics/transform.h"
11+
#include "util/log.h"
12+
}
13+
14+
#ifndef M_PI
15+
#define M_PI 3.1415926535
16+
#endif
17+
18+
class ComputerRenderer : public Drawable {
19+
public:
20+
ComputerRenderer(unsigned object, Computer &comp)
21+
: object(object), comp(comp) {}
22+
23+
void draw(Graphics &graphics) override {
24+
auto mvp = graphics.objmats(&object, ILG_MVP, 1);
25+
auto imt = graphics.objmats(&object, ILG_IMT, 1);
26+
comp.draw(mvp.front(), imt.front());
27+
}
28+
29+
private:
30+
unsigned object;
31+
Computer &comp;
32+
};
33+
34+
int main(int argc, char **argv)
35+
{
36+
demoLoad(argc, argv);
37+
auto window = createWindow("Lighting");
38+
Graphics graphics(window);
39+
Graphics::Flags flags;
40+
if (!graphics.init(flags)) {
41+
return 1;
42+
}
43+
Computer comp;
44+
char *error;
45+
if (!comp.build(graphics.rm, &error)) {
46+
il_error("Computer: %s", error);
47+
free(error);
48+
return 1;
49+
}
50+
il_pos compp = il_pos_new(&graphics.space);
51+
ComputerRenderer compr(compp.id, comp);
52+
graphics.drawables.push_back(&compr);
53+
54+
il_pos_setPosition(&graphics.space.camera, il_vec3_new(0, 0, 20));
55+
56+
il_pos lightp = il_pos_new(&graphics.space);
57+
il_pos_setPosition(&lightp, il_vec3_new(20, 3, 20));
58+
59+
ilG_light lightl;
60+
lightl.color = il_vec3_new(.8*2, .7*2, .2*2);
61+
lightl.radius = 50;
62+
63+
State state;
64+
65+
unsigned lightp_id = lightp.id;
66+
state.sunlight_lights = &lightl;
67+
state.sunlight_locs = &lightp_id;
68+
state.sunlight_count = 1;
69+
70+
while (1) {
71+
SDL_Event ev;
72+
while (SDL_PollEvent(&ev)) {
73+
switch (ev.type) {
74+
case SDL_QUIT:
75+
return 0;
76+
}
77+
}
78+
79+
struct timeval ts;
80+
gettimeofday(&ts, NULL);
81+
int secs = 10;
82+
const float dist = 10.0;
83+
float delta = ((float)(ts.tv_sec%secs) + ts.tv_usec / 1000000.0) / secs;
84+
il_vec3 v;
85+
v.x = sinf(delta * M_PI * 2) * dist;
86+
v.y = 0;
87+
v.z = cosf(delta * M_PI * 2) * dist;
88+
il_quat q = il_quat_fromAxisAngle(0, 1, 0, delta * M_PI * 2);
89+
il_pos_setPosition(&graphics.space.camera, v);
90+
il_pos_setRotation(&graphics.space.camera, q);
91+
92+
graphics.draw(state);
93+
}
94+
}

0 commit comments

Comments
 (0)