# HG changeset patch # User John Tsiombikas # Date 1345669810 -10800 # Node ID fa8f89d06f6fe6f064871b1f76de14aac8a0522f # Parent 7b65bba15553f46ea2a8672b47ff3b33a4f21351 progress with light rendering diff -r 7b65bba15553 -r fa8f89d06f6f prototype/Makefile --- a/prototype/Makefile Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/Makefile Thu Aug 23 00:10:10 2012 +0300 @@ -10,7 +10,7 @@ CFLAGS = -pedantic $(warn) $(dbg) $(opt) -Ivmath CXXFLAGS = $(CFLAGS) -std=c++11 -LDFLAGS = $(libgl) -lm -lassimpD -limago +LDFLAGS = $(libgl) -lm -lassimp -limago ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lglew diff -r 7b65bba15553 -r fa8f89d06f6f prototype/data/test.level --- a/prototype/data/test.level Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/data/test.level Thu Aug 23 00:10:10 2012 +0300 @@ -30,7 +30,6 @@ ################################################################ ################################################################ ################################################################ -############################### ############################### ################################ ############################### ################################################################ ################################################################ @@ -63,3 +62,4 @@ ################################################################ ################################################################ ################################################################ +################################################################ diff -r 7b65bba15553 -r fa8f89d06f6f prototype/src/level.cc --- a/prototype/src/level.cc Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/src/level.cc Thu Aug 23 00:10:10 2012 +0300 @@ -99,6 +99,24 @@ return Vector3(posx, 0, posy); } +unsigned int Level::get_cell_dirmask(int x, int y) const +{ + unsigned int dmask = TILE_ALL; + if(y > 0 && get_cell(x, y - 1)) { + dmask &= ~TILE_NORTH; + } + if(y < ysz - 1 && get_cell(x, y + 1)) { + dmask &= ~TILE_SOUTH; + } + if(x > 0 && get_cell(x - 1, y)) { + dmask &= ~TILE_WEST; + } + if(x < xsz - 1 && get_cell(x + 1, y)) { + dmask &= ~TILE_EAST; + } + return dmask; +} + void Level::draw() const { glMatrixMode(GL_MODELVIEW); @@ -114,19 +132,7 @@ glTranslatef(pos.x, pos.y, pos.z); glScalef(cell_size, cell_size, cell_size); - unsigned int dmask = TILE_ALL; - if(i > 0 && get_cell(j, i - 1)) { - dmask &= ~TILE_NORTH; - } - if(i < ysz - 1 && get_cell(j, i + 1)) { - dmask &= ~TILE_SOUTH; - } - if(j > 0 && get_cell(j - 1, i)) { - dmask &= ~TILE_WEST; - } - if(j < xsz - 1 && get_cell(j + 1, i)) { - dmask &= ~TILE_EAST; - } + unsigned int dmask = get_cell_dirmask(j, i); cell->draw(dmask); glPopMatrix(); @@ -135,6 +141,29 @@ } } +void Level::draw_lights() const +{ + glMatrixMode(GL_MODELVIEW); + + for(int i=0; idraw_lights(dmask); + + glPopMatrix(); + } + } + } +} + void Level::draw_grid() const { float xlen = xsz * cell_size; @@ -179,7 +208,14 @@ void GridCell::draw(unsigned int draw_mask) const { - for(size_t i=0; idraw(draw_mask); + for(auto tile : tiles) { + tile->draw(draw_mask); } } + +void GridCell::draw_lights(unsigned int draw_mask) const +{ + for(auto tile : tiles) { + tile->draw_lights(draw_mask); + } +} diff -r 7b65bba15553 -r fa8f89d06f6f prototype/src/level.h --- a/prototype/src/level.h Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/src/level.h Thu Aug 23 00:10:10 2012 +0300 @@ -26,8 +26,10 @@ const GridCell *get_cell(int x, int y) const; Vector3 get_cell_pos(int x, int y) const; + unsigned int get_cell_dirmask(int x, int y) const; void draw() const; + void draw_lights() const; }; class GridCell { @@ -41,6 +43,7 @@ void add_tile(const Tile *tile); void draw(unsigned int draw_mask) const; + void draw_lights(unsigned int draw_mask) const; }; #endif // LEVEL_H_ diff -r 7b65bba15553 -r fa8f89d06f6f prototype/src/light.cc --- a/prototype/src/light.cc Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/src/light.cc Thu Aug 23 00:10:10 2012 +0300 @@ -5,6 +5,8 @@ : color(col) { intensity = 1.0; + vbo = 0; + num_faces = 0; } Light::~Light() {} @@ -30,6 +32,29 @@ glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x); } +void Light::draw() const +{ + if(!vbo) { + if(!((Light*)this)->create_mesh()) { + return; + } + } + + glEnableClientState(GL_VERTEX_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexPointer(3, GL_FLOAT, 0, 0); + + glDrawArrays(GL_TRIANGLES, 0, num_faces * 3); + + glDisableClientState(GL_VERTEX_ARRAY); +} + +bool Light::create_mesh() +{ + fprintf(stderr, "%s: undefined\n", __FUNCTION__); + return false; +} + PointLight::PointLight() { @@ -80,6 +105,67 @@ glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]); } +void PointLight::draw() const +{ + //glMatrixMode(GL_MODELVIEW); + //glPushMatrix(); + //glScalef(radius, radius, radius); + + Light::draw(); + + //glPopMatrix(); +} + + + +Vector3 sphvertex(float u, float v) +{ + float theta = u * M_PI * 2.0; + float phi = v * M_PI; + + Vector3 res; + res.x = sin(theta) * cos(phi); + res.y = sin(theta); + res.z = cos(theta) * cos(phi); + return res; +} + + +bool PointLight::create_mesh() +{ + const static int udiv = 8; + const static int vdiv = 4; + + int nquads = udiv * vdiv; + num_faces = nquads * 2; + int nverts = num_faces * 3; + + float du = 1.0 / (float)udiv; + float dv = 1.0 / (float)vdiv; + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, nverts * sizeof(Vector3), 0, GL_STATIC_DRAW); + + Vector3 *vptr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); + + for(int i=0; idraw(); -} - void view_matrix(int eye) { float offs = stereo_eye_sep * eye * 0.5; diff -r 7b65bba15553 -r fa8f89d06f6f prototype/src/renderer.cc --- a/prototype/src/renderer.cc Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/src/renderer.cc Thu Aug 23 00:10:10 2012 +0300 @@ -5,6 +5,7 @@ #include #include "opengl.h" #include "renderer.h" +#include "level.h" #include "sdr.h" #include "datapath.h" @@ -60,9 +61,26 @@ if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) { return false; } + set_uniform_int(mrt_prog, "tex_dif", 0); + set_uniform_int(mrt_prog, "tex_norm", 1); + if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) { return false; } + for(int i=0; i tex_xsz || ysz > tex_ysz) { + tex_xsz = round_pow2(xsz); + tex_ysz = round_pow2(ysz); + + for(int i=0; idraw_lights(); + glPopAttrib(); +#if 0 // render into the MRT buffers + glUseProgram(mrt_prog); glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); - glUseProgram(mrt_prog); - if((loc = glGetUniformLocation(mrt_prog, "tex_dif")) != -1) { - glUniform1i(loc, 0); - } - if((loc = glGetUniformLocation(mrt_prog, "tex_norm")) != -1) { - glUniform1i(loc, 1); - } - draw_func(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + level->draw(); - glUseProgram(0); glBindFramebufferEXT(GL_FRAMEBUFFER, 0); @@ -101,33 +147,16 @@ glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); - glUseProgram(deferred_debug); + glUseProgram(deferred_omni); for(int i=0; idraw_lights(); + glDepthMask(1); for(int i=0; idraw(); + } + } +} + /* int Tile::load_lights(const aiScene *scn) { diff -r 7b65bba15553 -r fa8f89d06f6f prototype/src/tile.h --- a/prototype/src/tile.h Tue Aug 21 06:34:14 2012 +0300 +++ b/prototype/src/tile.h Thu Aug 23 00:10:10 2012 +0300 @@ -34,6 +34,7 @@ bool load(const char *fname); void draw(unsigned int drawmask) const; + void draw_lights(unsigned int drawmask) const; };