From e39d44e9dc47bb3f8a77955768e3599ec0ed30be Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 29 May 2011 17:31:35 +0200 Subject: Some code cleanup in scene.cpp. --- scene.cpp | 88 ++++++++++++--------------------------------- shaders/water_fragment.glsl | 2 +- 2 files changed, 23 insertions(+), 67 deletions(-) diff --git a/scene.cpp b/scene.cpp index b669664..20692d8 100644 --- a/scene.cpp +++ b/scene.cpp @@ -8,11 +8,7 @@ #include "gl.h" -#include #include -#include - -#define inrange(a, b, c) ((a) >= (b) && (a) <= (c)) Scene::Scene() { running = true; @@ -113,14 +109,8 @@ void Scene::move(float forward, float right, int steps) { dir *= 0.005; dir *= steps; - float x = pos.x + dir.x; - //if(inrange(x, -2, 2)) - pos.x = x; - float z = pos.z + dir.y; - //if(inrange(z, -2, 2)) - pos.z = z; - //pos.x += dir.x; - //pos.z += dir.y; + pos.x += dir.x; + pos.z += dir.y; } bool Scene::select(int x, int y, float& px, float& py, float& pz) { @@ -211,6 +201,7 @@ void Scene::events() { if(tool) delete tool; tool = new RaiseTool(terrain); break; + // TODO: change/make configurable case 124: if(console->showing()) { console->hide(); @@ -334,6 +325,7 @@ void Scene::render() { } } + // handle vertical movement std::string move_str; Terrain::Node *node = terrain->find(pos.x, pos.z); if(node) { @@ -365,14 +357,10 @@ void Scene::render() { lookat(); - //glEnable(GL_LIGHTING); - const float light_pos[4] = {50, 100, 50, 1}; - //const float light_pos[4] = {0, 1, 0, 0}; - //glLightfv(GL_LIGHT0, GL_POSITION, light_pos); - glEnable(GL_CULL_FACE); glCullFace(GL_BACK); + // render terrain if(render_terrain) { const float fog_color[4] = {1, 1, 1, 0}; glFogfv(GL_FOG_COLOR, fog_color); @@ -401,19 +389,14 @@ void Scene::render() { glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, marker_texture); - /*std::queue q; - q.push(qt->root); - while(!q.empty()) { - Quadtree::QuadChunk *chunk = q.front(); - q.pop();*/ + // enable arrays, disabled after water rendering + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + // draw chunk VBOs for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { Terrain::Chunk *chunk = *it; - /*if(!chunk->nodes) { - for(int i = 0; i < 4; i++) - q.push(chunk->children[i]); - continue; - } else if(!chunk->vbo_object) - continue;*/ glPushMatrix(); glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); glUniform2f(chunk_pos, chunk->x, chunk->y); @@ -422,24 +405,19 @@ void Scene::render() { glVertexPointer(3, GL_FLOAT, 0, NULL); glNormalPointer(GL_FLOAT, 0, (GLvoid*)(chunk->vertices*3*sizeof(float))); glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)(chunk->vertices*3*sizeof(float)*2)); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_TRIANGLES, 0, chunk->vertices); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); glPopMatrix(); } glBindBuffer(GL_ARRAY_BUFFER, 0); - glDisable(GL_TEXTURE_2D); - // set active to texture0 to avoid breaking the texture font + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, water_texture); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + water_program.use(); chunk_pos = glGetUniformLocation(water_program.get_program(), "chunk_pos"); player_pos = glGetUniformLocation(water_program.get_program(), "player_pos"); @@ -449,6 +427,7 @@ void Scene::render() { t += 0.00002 * steps; glUniform1f(t_pos, t); + // draw chunk water for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { Terrain::Chunk *chunk = *it; glPushMatrix(); @@ -456,39 +435,30 @@ void Scene::render() { glUniform2f(chunk_pos, chunk->x, chunk->y); float v[4*3]; - /*v[0] = v[2] = v[3] = v[9] = v[11] = v[17] = 0; - v[5] = v[6] = v[8] = v[12] = v[14] = v[15] = terrain->chunk_size; - v[1] = v[4] = v[7] = v[10] = v[13] = v[16] = 30;*/ v[0] = v[2] = v[3] = v[11] = 0; v[5] = v[6] = v[8] = v[9] = terrain->chunk_size; v[1] = v[4] = v[7] = v[10] = 30; float n[4*3]; - /*n[0] = n[2] = n[3] = n[5] = n[6] = n[8] = n[9] = n[11] = n[12] = n[14] = n[15] = n[17]= 0; - n[1] = n[4] = n[7] = n[10] = n[13] = n[16] = 1;*/ n[0] = n[2] = n[3] = n[5] = n[6] = n[8] = n[9] = n[11] = 0; n[1] = n[4] = n[7] = n[10] = 1; float tc[4*2]; - /*tc[0] = tc[1] = tc[2] = tc[6] = tc[7] = tc[11] = 0; - tc[3] = tc[4] = tc[5] = tc[8] = tc[9] = tc[10] = 1;*/ tc[0] = tc[1] = tc[2] = tc[7] = 0; tc[3] = tc[4] = tc[5] = tc[6] = terrain->chunk_size / 2; - glBindTexture(GL_TEXTURE_2D, water_texture); glVertexPointer(3, GL_FLOAT, 0, v); glNormalPointer(GL_FLOAT, 0, n); glTexCoordPointer(2, GL_FLOAT, 0, tc); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_QUADS, 0, 4); - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); glPopMatrix(); } + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisable(GL_BLEND); glUseProgram(0); @@ -499,11 +469,6 @@ void Scene::render() { glColor3f(0, 0, 0); else glColor3f(1, 1, 1); - /*std::queue q; - q.push(qt->root); - while(!q.empty()) { - Quadtree::QuadChunk *chunk = q.front(); - q.pop();*/ for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { Terrain::Chunk *chunk = *it; glPushMatrix(); @@ -524,8 +489,6 @@ void Scene::render() { } } - //glDisable(GL_LIGHTING); - float px, py, pz; if(do_select && select(sx, sy, px, py, pz)) { do_select = false; @@ -535,6 +498,7 @@ void Scene::render() { glDisable(GL_TEXTURE_2D); + // draw sky glPushMatrix(); glTranslatef(700*cosf(yaw), 0, 700*sinf(yaw)); glRotatef(-yaw*180/M_PI+180, 0, 1, 0); @@ -554,16 +518,13 @@ void Scene::render() { glEnd(); glPopMatrix(); + // HUD video::ortho(); float height = font->LineHeight(); glColor3f(1, 1, 1); glTranslatef(0, video::height-height, 0); font->Render((boost::format("chunks: %d cache: %d gravity: %d flying: %d water: %d steps: %d") % terrain->chunks.size() % terrain->tc->get_size() % gravity % flying % underwater % steps).str().c_str()); - //font->Render((boost::format("%dx%d %d levels %d nodes tree creation time: %f steps: %d update: %d") - // % scene.qt->width % scene.qt->height % scene.qt->levels % scene.qt->nodes % scene.qt->init_time % steps % scene.qt->thread_running).str().c_str()); - //glTranslatef(0, height, 0); - //font->Render((boost::format("selected: %x") % selected).str().c_str()); glTranslatef(0, -height, 0); font->Render(move_str.c_str()); glTranslatef(0, -height, 0); @@ -572,11 +533,6 @@ void Scene::render() { glTranslatef(0, -height, 0); font->Render((boost::format("Tool: %s") % tool->get_name()).str().c_str()); } - /*if(selected) { - glTranslatef(0, height, 0); - //font->Render((boost::format("(%s %s %s %s)") % selected->a->str() % selected->b->str() % selected->c->str() % selected->d->str()).str().c_str()); - font->Render(selected->str().c_str()); - }*/ GUI::pre_render(); diff --git a/shaders/water_fragment.glsl b/shaders/water_fragment.glsl index 150f071..7e877e9 100644 --- a/shaders/water_fragment.glsl +++ b/shaders/water_fragment.glsl @@ -6,7 +6,7 @@ uniform sampler2D tex; uniform float t; void main() { - gl_FragColor = texture2D(tex, gl_TexCoord[0].st + vec2(t, t)) - vec4(.2, .2, 0, 0); + gl_FragColor = texture2D(tex, gl_TexCoord[0].st + vec2(t)) - vec4(.2, .2, 0, 0); gl_FragColor = mix(gl_FragColor, gl_Fog.color, pow(length(player_pos - pos)/100, 5)); gl_FragColor.a = 0.7; } -- cgit v1.2.3