From f0eb6a7b101afba52f33c5286e15a862ee68c64e Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 7 May 2011 16:06:08 +0200 Subject: Replace static heightmap with perlin noise using libnoise and noiseutils. --- scene.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index 6cb6cc7..28f4fcc 100644 --- a/scene.cpp +++ b/scene.cpp @@ -1,5 +1,6 @@ #include "scene.h" #include "video.h" +#include "terrain.h" #include #include @@ -46,18 +47,10 @@ Scene::Scene() { soil_texture = load_texture("textures/zooboing-469-sand-modified.jpg"); marker_texture = load_texture("textures/cross.png"); - /* load heightmap */ - SDL_Surface *hm = IMG_Load("heightmap.png"); - float *heightmap = new float[hm->w * hm->h]; - for(int x = 0; x < hm->w; x++) { - for(int y = 0; y < hm->h; y++) { - Uint8 *p = (Uint8*)hm->pixels + y * hm->pitch + x * hm->format->BytesPerPixel; - heightmap[y*hm->w + x] = ((float)(*p) / 256) * 20; - } - } - int w = hm->w; - int h = hm->h; - SDL_FreeSurface(hm); + /* init quadtree */ + int w = 1025; + int h = 1025; + float *heightmap = Terrain::generate_heights(0, 0, w, h); qt = new Quadtree(w, h, heightmap); /* load font */ -- cgit v1.2.3 From 17715403aff8ba1889c9e1ef473fe9319b87d1d2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 7 May 2011 17:53:37 +0200 Subject: Use chunk-relative coordinates in nodes. --- scene.cpp | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index 28f4fcc..b67468b 100644 --- a/scene.cpp +++ b/scene.cpp @@ -76,11 +76,9 @@ void Scene::lookat() { * z = sin Φ sin θ */ Vector3 center(sinf(pitch) * cosf(yaw), cosf(pitch), sinf(pitch) * sinf(yaw)); - center += pos; center.y += cam_height; - //Vector3 up(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch)); Vector3 up(-cosf(pitch) * cosf(yaw), sinf(pitch), -cosf(pitch) * sinf(yaw)); - gluLookAt(pos.x, pos.y+cam_height, pos.z, + gluLookAt(0, cam_height, 0, center.x, center.y, center.z, up.x, up.y, up.z); } @@ -321,9 +319,11 @@ void Scene::render() { terrain_program.use(); GLint show_sel = glGetUniformLocation(terrain_program.get_program(), "show_sel"); glUniform1i(show_sel, show_selection ? 1 : 0); + GLint chunk_pos; if(show_selection) { GLint selpos = glGetUniformLocation(terrain_program.get_program(), "selpos"); glUniform3f(selpos, selected.x, selected.y, selected.z); + chunk_pos = glGetUniformLocation(terrain_program.get_program(), "chunk_pos"); } glEnable(GL_TEXTURE_2D); @@ -352,17 +352,24 @@ void Scene::render() { } else if(!chunk->vbo_object) continue; chunks_rendered++; - glBindBuffer(GL_ARRAY_BUFFER, chunk->vbo_object); - 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); + glPushMatrix(); + glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); + if(show_selection) + glUniform2f(chunk_pos, chunk->x, chunk->y); + + glBindBuffer(GL_ARRAY_BUFFER, chunk->vbo_object); + 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); @@ -396,7 +403,7 @@ void Scene::render() { float px, py, pz; if(do_select && select(sx, sy, px, py, pz)) { do_select = false; - selected = Vector3(px, py, pz); + selected = Vector3(pos.x + px, pos.y + py, pos.z + pz); show_selection = true; } -- cgit v1.2.3 From 7d5c1adf8e581599848b3fec54e0af88eb469046 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 8 May 2011 15:02:30 +0200 Subject: Working dynamic generation of terrain. --- scene.cpp | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index b67468b..8d7301d 100644 --- a/scene.cpp +++ b/scene.cpp @@ -15,6 +15,7 @@ Scene::Scene() { running = true; grid = false; + normals = false; terrain = true; gravity = true; @@ -48,10 +49,7 @@ Scene::Scene() { marker_texture = load_texture("textures/cross.png"); /* init quadtree */ - int w = 1025; - int h = 1025; - float *heightmap = Terrain::generate_heights(0, 0, w, h); - qt = new Quadtree(w, h, heightmap); + qt = new Quadtree(); /* load font */ font = new FTTextureFont("fonts/VeraMono.ttf"); @@ -156,6 +154,9 @@ void Scene::events() { case SDLK_g: grid = !grid; break; + case SDLK_n: + normals = !normals; + break; case SDLK_t: terrain = !terrain; break; @@ -340,17 +341,19 @@ void Scene::render() { glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, marker_texture); - std::queue q; + /*std::queue q; q.push(qt->root); while(!q.empty()) { Quadtree::QuadChunk *chunk = q.front(); - q.pop(); - if(!chunk->nodes) { + q.pop();*/ + for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { + Quadtree::QuadChunk *chunk = *it; + /*if(!chunk->nodes) { for(int i = 0; i < 4; i++) q.push(chunk->children[i]); continue; } else if(!chunk->vbo_object) - continue; + continue;*/ chunks_rendered++; glPushMatrix(); glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); @@ -382,19 +385,34 @@ void Scene::render() { glColor3f(0, 0, 0); else glColor3f(1, 1, 1); - std::queue q; + /*std::queue q; q.push(qt->root); while(!q.empty()) { Quadtree::QuadChunk *chunk = q.front(); - q.pop(); - if(!chunk->nodes) { + q.pop();*/ + for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { + Quadtree::QuadChunk *chunk = *it; + glPushMatrix(); + glTranslatef(-pos.x, -pos.y, -pos.z); + /*if(!chunk->nodes) { for(int i = 0; i < 4; i++) q.push(chunk->children[i]); continue; - } else if(chunk->vbo_object) { + } else*/ if(chunk->vbo_object) { for(unsigned int i = 0; i < chunk->node_count; i++) chunk->nodes[i]->draw_grid(); } + glPopMatrix(); + } + } + if(normals) { + for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { + Quadtree::QuadChunk *chunk = *it; + glPushMatrix(); + glTranslatef(-pos.x, -pos.y, -pos.z); + for(unsigned int i = 0; i < chunk->node_count; i++) + chunk->nodes[i]->draw_normal(); + glPopMatrix(); } } @@ -411,8 +429,8 @@ void Scene::render() { float height = font->LineHeight(); glColor3f(1, 1, 1); glTranslatef(0, video::height-height, 0); - font->Render((boost::format("%dx%d chunks: %d gravity: %d steps: %d") - % qt->width % qt->height % chunks_rendered % gravity % steps).str().c_str()); + font->Render((boost::format("chunks: %d gravity: %d steps: %d") + % chunks_rendered % gravity % 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); -- cgit v1.2.3 From 595ac4744b75688f7ca61993c42ea9eedab3a6b7 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 8 May 2011 15:52:41 +0200 Subject: Merged Quadtree and friends into Terrain. --- scene.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index 8d7301d..e749347 100644 --- a/scene.cpp +++ b/scene.cpp @@ -16,7 +16,7 @@ Scene::Scene() { running = true; grid = false; normals = false; - terrain = true; + render_terrain = true; gravity = true; last_node = NULL; @@ -48,8 +48,8 @@ Scene::Scene() { soil_texture = load_texture("textures/zooboing-469-sand-modified.jpg"); marker_texture = load_texture("textures/cross.png"); - /* init quadtree */ - qt = new Quadtree(); + /* init terrain */ + terrain = new Terrain(); /* load font */ font = new FTTextureFont("fonts/VeraMono.ttf"); @@ -61,8 +61,8 @@ Scene::Scene() { Scene::~Scene() { if(tool) delete tool; - if(qt) - delete qt; + if(terrain) + delete terrain; delete font; } @@ -129,7 +129,7 @@ bool Scene::select(int x, int y, float& px, float& py, float& pz) { } void Scene::update() { - qt->update(pos.x, pos.z); + terrain->update(pos.x, pos.z); } void Scene::events() { @@ -158,7 +158,7 @@ void Scene::events() { normals = !normals; break; case SDLK_t: - terrain = !terrain; + render_terrain = !render_terrain; break; case SDLK_SPACE: yvel = .05; @@ -174,7 +174,7 @@ void Scene::events() { break; case SDLK_1: if(tool) delete tool; - tool = new RaiseTool(qt); + tool = new RaiseTool(terrain); break; default: break; @@ -284,7 +284,7 @@ void Scene::render() { } std::string move_str; - Quadtree::QuadNode *node = qt->find(pos.x, pos.z); + Terrain::Node *node = terrain->find(pos.x, pos.z); if(node) { if(gravity) { float y = node->get_height(pos.x, pos.z); @@ -316,7 +316,7 @@ void Scene::render() { //glLightfv(GL_LIGHT0, GL_POSITION, light_pos); unsigned int chunks_rendered = 0; - if(terrain) { + if(render_terrain) { terrain_program.use(); GLint show_sel = glGetUniformLocation(terrain_program.get_program(), "show_sel"); glUniform1i(show_sel, show_selection ? 1 : 0); @@ -346,8 +346,8 @@ void Scene::render() { while(!q.empty()) { Quadtree::QuadChunk *chunk = q.front(); q.pop();*/ - for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { - Quadtree::QuadChunk *chunk = *it; + 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]); @@ -390,8 +390,8 @@ void Scene::render() { while(!q.empty()) { Quadtree::QuadChunk *chunk = q.front(); q.pop();*/ - for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { - Quadtree::QuadChunk *chunk = *it; + for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { + Terrain::Chunk *chunk = *it; glPushMatrix(); glTranslatef(-pos.x, -pos.y, -pos.z); /*if(!chunk->nodes) { @@ -406,8 +406,8 @@ void Scene::render() { } } if(normals) { - for(std::list::iterator it = qt->chunks.begin(); it != qt->chunks.end(); it++) { - Quadtree::QuadChunk *chunk = *it; + for(std::list::iterator it = terrain->chunks.begin(); it != terrain->chunks.end(); it++) { + Terrain::Chunk *chunk = *it; glPushMatrix(); glTranslatef(-pos.x, -pos.y, -pos.z); for(unsigned int i = 0; i < chunk->node_count; i++) -- cgit v1.2.3 From 4597c723c8dc1ab83fe23d7b330cd2ce974cba5d Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 8 May 2011 20:57:24 +0200 Subject: Halve triangles/vertices per node, assume width and height is always 1. --- scene.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index e749347..489f72c 100644 --- a/scene.cpp +++ b/scene.cpp @@ -298,8 +298,8 @@ void Scene::render() { yvel = 0; } } - move_str = (boost::format("%s n: %.2f,%.2f %.2fx%.2f c: %d,%d %dx%d") - % pos.str() % node->x % node->y % node->width % node->height + move_str = (boost::format("%s n: %.2f,%.2f c: %d,%d %dx%d") + % pos.str() % node->x % node->y % node->chunk->x % node->chunk->y % node->chunk->width % node->chunk->height).str(); if(last_node != node) { -- cgit v1.2.3 From a820c852d14f466f3d9096c7efcd571d3bf006eb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 9 May 2011 22:43:20 +0200 Subject: Fixed chunk border bugginess. --- scene.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index 489f72c..b8f7268 100644 --- a/scene.cpp +++ b/scene.cpp @@ -315,7 +315,6 @@ void Scene::render() { //const float light_pos[4] = {0, 1, 0, 0}; //glLightfv(GL_LIGHT0, GL_POSITION, light_pos); - unsigned int chunks_rendered = 0; if(render_terrain) { terrain_program.use(); GLint show_sel = glGetUniformLocation(terrain_program.get_program(), "show_sel"); @@ -354,7 +353,6 @@ void Scene::render() { continue; } else if(!chunk->vbo_object) continue;*/ - chunks_rendered++; glPushMatrix(); glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); if(show_selection) @@ -430,7 +428,7 @@ void Scene::render() { glColor3f(1, 1, 1); glTranslatef(0, video::height-height, 0); font->Render((boost::format("chunks: %d gravity: %d steps: %d") - % chunks_rendered % gravity % steps).str().c_str()); + % terrain->chunks.size() % gravity % 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); -- cgit v1.2.3 From 7d93cab1eb1629b068858a68ac5e5840fe84a83a Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 10 May 2011 15:40:02 +0200 Subject: Added fog and implemented a simple skybox. --- scene.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'scene.cpp') diff --git a/scene.cpp b/scene.cpp index b8f7268..bf49c57 100644 --- a/scene.cpp +++ b/scene.cpp @@ -316,15 +316,18 @@ void Scene::render() { //glLightfv(GL_LIGHT0, GL_POSITION, light_pos); if(render_terrain) { + const float fog_color[4] = {1, 1, 1, 0}; + glFogfv(GL_FOG_COLOR, fog_color); terrain_program.use(); GLint show_sel = glGetUniformLocation(terrain_program.get_program(), "show_sel"); glUniform1i(show_sel, show_selection ? 1 : 0); - GLint chunk_pos; if(show_selection) { GLint selpos = glGetUniformLocation(terrain_program.get_program(), "selpos"); glUniform3f(selpos, selected.x, selected.y, selected.z); - chunk_pos = glGetUniformLocation(terrain_program.get_program(), "chunk_pos"); } + GLint chunk_pos = glGetUniformLocation(terrain_program.get_program(), "chunk_pos"); + GLint player_pos = glGetUniformLocation(terrain_program.get_program(), "player_pos"); + glUniform3f(player_pos, pos.x, pos.y, pos.z); glEnable(GL_TEXTURE_2D); @@ -355,8 +358,7 @@ void Scene::render() { continue;*/ glPushMatrix(); glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y); - if(show_selection) - glUniform2f(chunk_pos, chunk->x, chunk->y); + glUniform2f(chunk_pos, chunk->x, chunk->y); glBindBuffer(GL_ARRAY_BUFFER, chunk->vbo_object); glVertexPointer(3, GL_FLOAT, 0, NULL); @@ -423,6 +425,27 @@ void Scene::render() { show_selection = true; } + glDisable(GL_TEXTURE_2D); + + glPushMatrix(); + glTranslatef(700*cosf(yaw), 0, 700*sinf(yaw)); + glRotatef(-yaw*180/M_PI+180, 0, 1, 0); + glBegin(GL_QUADS); + glColor3f(1, 1, 1); + glVertex3f(0, 0, -700); + glVertex3f(0, 0, 700); + glColor3f(.5, .5, 1); + glVertex3f(0, 550, 700); + glVertex3f(0, 550, -700); + + glVertex3f(0, 550, -700); + glVertex3f(0, 550, 700); + glColor3f(0, 0, .3); + glVertex3f(1000, 550, 700); + glVertex3f(1000, 550, -700); + glEnd(); + glPopMatrix(); + video::ortho(); float height = font->LineHeight(); glColor3f(1, 1, 1); -- cgit v1.2.3