summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-05-09 22:43:20 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-05-09 22:43:20 +0200
commita820c852d14f466f3d9096c7efcd571d3bf006eb (patch)
tree7c0f19da6cb601952f0e8024de0f4e5d00764c83
parent4597c723c8dc1ab83fe23d7b330cd2ce974cba5d (diff)
Fixed chunk border bugginess.
-rw-r--r--scene.cpp4
-rw-r--r--terrain.cpp152
-rw-r--r--terrain.h1
3 files changed, 76 insertions, 81 deletions
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);
diff --git a/terrain.cpp b/terrain.cpp
index f606cc2..e0f635a 100644
--- a/terrain.cpp
+++ b/terrain.cpp
@@ -54,19 +54,19 @@ float Terrain::Node::distance(float px, float pz) {
void Terrain::Node::fill() {
vertex_array[0] = x;
- vertex_array[1] = chunk->heights[(int)floorf((x)*(chunk->height+1)+y)];
+ vertex_array[1] = chunk->heights[(int)floorf((x)*(chunk->h_height)+y)];
vertex_array[2] = y;
vertex_array[3] = x;
- vertex_array[4] = chunk->heights[(int)floorf((x)*(chunk->height+1) + (y + 1))];
+ vertex_array[4] = chunk->heights[(int)floorf((x)*(chunk->h_height) + (y + 1))];
vertex_array[5] = y + 1;
vertex_array[6] = x + 1;
- vertex_array[7] = chunk->heights[(int)floorf((x + 1)*(chunk->height+1) + (y + 1))];
+ vertex_array[7] = chunk->heights[(int)floorf((x + 1)*(chunk->h_height) + (y + 1))];
vertex_array[8] = y + 1;
vertex_array[9] = x + 1;
- vertex_array[10] = chunk->heights[(int)floorf((x + 1)*(chunk->height+1) + y)];
+ vertex_array[10] = chunk->heights[(int)floorf((x + 1)*(chunk->h_height) + y)];
vertex_array[11] = y;
}
@@ -86,7 +86,7 @@ void Terrain::Node::draw_grid() {
void Terrain::Node::draw_normal() {
glNormal3f(0, 1, 0);
glColor3f(1, 0, 0);
- int i = (int)(x*(chunk->height+1) + y);
+ int i = (int)(x*(chunk->h_height) + y);
glBegin(GL_LINES);
Vector3 N = chunk->normals[i];
glVertex3f(chunk->x + x, chunk->heights[i], chunk->y + y);
@@ -119,10 +119,12 @@ Terrain::Chunk::Chunk(Terrain *terrain, float x, float y, float width, float hei
this->y = y;
this->width = width;
this->height = height;
+ this->h_width = width+1;
+ this->h_height = height+1;
this->vbo_object = this->node_count = this->vertices = 0;
this->nodes = NULL;
- heights = terrain->generate_heights(x, y, width+1, height+1);
- normals = new Vector3[(int)((width+1)*(height+1))];
+ heights = terrain->get_chunk(x, y, h_width, h_height);
+ normals = new Vector3[(int)((h_width)*(h_height))];
calc_normals();
@@ -229,10 +231,10 @@ void Terrain::Chunk::make_vbo() {
float *n = buffer + vertices_size + normal_chunk_size*index;
- Vector3 bl = normals[(int)((node->x+1)*(height+1) + node->y)];
- Vector3 br = normals[(int)((node->x)*(height+1) + node->y)];
- Vector3 tr = normals[(int)((node->x)*(height+1) + node->y+1)];
- Vector3 tl = normals[(int)((node->x+1)*(height+1) + node->y+1)];
+ Vector3 bl = normals[(int)((node->x+1)*(h_height) + node->y)];
+ Vector3 br = normals[(int)((node->x)*(h_height) + node->y)];
+ Vector3 tr = normals[(int)((node->x)*(h_height) + node->y+1)];
+ Vector3 tl = normals[(int)((node->x+1)*(h_height) + node->y+1)];
n[0] = n[9] = br.x;
n[1] = n[10] = br.y;
@@ -264,71 +266,65 @@ Terrain::Node* Terrain::Chunk::find(float x, float y) {
void Terrain::Chunk::calc_normals() {
float *right, *left, *up, *down;
right = left = up = down = NULL;
- if(terrain->has_chunk(this->x - chunk_size, this->y))
- right = terrain->get_chunk(this->x - chunk_size, this->y, width, height);
- if(terrain->has_chunk(this->x + chunk_size, this->y))
- left = terrain->get_chunk(this->x + chunk_size, this->y, width, height);
- if(terrain->has_chunk(this->x, this->y + chunk_size))
- up = terrain->get_chunk(this->x, this->y + chunk_size, width, height);
- if(terrain->has_chunk(this->x, this->y - chunk_size))
- down = terrain->get_chunk(this->x, this->y - chunk_size, width, height);
-
- for(int x = 0; x < width; x++) {
- for(int y = 0; y < height; y++) {
- Vector3 p(x, heights[x*(int)(height+1) + y], y);
- Vector3 N;
- Vector3 temp;
-
- // TODO: fix border bugginess
+ right = terrain->get_chunk(this->x - chunk_size, this->y, h_width, h_height);
+ left = terrain->get_chunk(this->x + chunk_size, this->y, h_width, h_height);
+ up = terrain->get_chunk(this->x, this->y + chunk_size, h_width, h_height);
+ down = terrain->get_chunk(this->x, this->y - chunk_size, h_width, h_height);
+
+ for(int x = 0; x < h_width; x++) {
+ for(int y = 0; y < h_height; y++) {
+ Vector3 p(x, heights[x*h_height + y], y);
+ Vector3 N, U, V;
float h;
- if(x > 0 || right) {
- if(x == 0)
- h = right[(chunk_size-1)*(int)(height) + y];
- else
- h = heights[(x-1)*(int)(height+1) + y];
- Vector3 U = Vector3(x-1, h, y) - p;
- if(y > 0 || down) {
- if(y == 0)
- h = down[x*(int)(height) + chunk_size - 1];
- else
- h = heights[x*(int)(height+1) + y - 1];
- Vector3 V = Vector3(x, h, y-1) - p;
- N += V.cross(U);
- }
- if(y < height-1 || up) {
- if(y == height-1)
- h = up[x*(int)(height)]; // y == 0
- else
- h = heights[x*(int)(height+1) + y + 1];
- Vector3 V = Vector3(x, h, y+1) - p;
- N += U.cross(V);
- }
- }
- if(x < width-1 || left) {
- if(x == width-1)
- h = left[y]; // x == 0
- else
- h = heights[(x+1)*(int)(height+1) + y];
- Vector3 U = Vector3(x+1, h, y) - p;
- if(y > 0 || down) {
- if(y == 0)
- h = down[x*(int)(height) + chunk_size - 1];
- else
- h = heights[x*(int)(height+1) + y - 1];
- Vector3 V = Vector3(x, h, y-1) - p;
- N += U.cross(V);
- }
- if(y < height-1 || up) {
- if(y == height-1)
- h = up[x*(int)(height)]; // y == 0
- else
- h = heights[x*(int)(height+1) + y + 1];
- Vector3 V = Vector3(x, h, y+1) - p;
- N += V.cross(U);
- }
- }
+
+ // right
+ if(x == 0)
+ h = right[(chunk_size-1)*(int)(h_height) + y];
+ else
+ h = heights[(x-1)*h_height + y];
+ U = Vector3(x-1, h, y) - p;
+
+ // down
+ if(y == 0)
+ h = down[x*(int)(h_height) + chunk_size - 1];
+ else
+ h = heights[x*h_height + y - 1];
+ V = Vector3(x, h, y-1) - p;
+ N += V.cross(U);
+
+ // up
+ if(y == h_height-1)
+ h = up[x*(int)(h_height) + 1]; // y == 1
+ else
+ h = heights[x*h_height + y + 1];
+ V = Vector3(x, h, y+1) - p;
+ N += U.cross(V);
+
+ // left
+ if(x == h_width-1)
+ h = left[h_height + y]; // x == 1
+ else
+ h = heights[(x+1)*h_height + y];
+ U = Vector3(x+1, h, y) - p;
+
+ //down
+ if(y == 0)
+ h = down[x*(int)(h_height) + chunk_size - 1];
+ else
+ h = heights[x*h_height + y - 1];
+ V = Vector3(x, h, y-1) - p;
+ N += U.cross(V);
+
+ // up
+ if(y == h_height-1)
+ h = up[x*(int)(h_height) + 1]; // y == 1
+ else
+ h = heights[x*h_height + y + 1];
+ V = Vector3(x, h, y+1) - p;
+ N += V.cross(U);
+
N /= N.length();
- normals[x*(int)(height+1) + y] = N;
+ normals[x*h_height + y] = N;
}
}
@@ -402,7 +398,7 @@ void Terrain::raise(float x, float z, float radius, float focus, float strength,
v /= radius * (focus/2);
Node *node = find(i, j);
- int index = (i-node->chunk->x)*(node->chunk->height+1) + j-node->chunk->y;
+ int index = (i-node->chunk->x)*(node->chunk->h_height) + j-node->chunk->y;
if(up)
node->chunk->heights[index] += v;
else
@@ -412,7 +408,7 @@ void Terrain::raise(float x, float z, float radius, float focus, float strength,
if(i % chunk_size == 0) {
node = find(i-chunk_size, j);
if(node) {
- index = (chunk_size)*(node->chunk->height+1) + j-node->chunk->y;
+ index = (chunk_size)*(node->chunk->h_height) + j-node->chunk->y;
if(up)
node->chunk->heights[index] += v;
else
@@ -422,7 +418,7 @@ void Terrain::raise(float x, float z, float radius, float focus, float strength,
if(j % chunk_size == 0) {
node = find(i, j-chunk_size);
if(node) {
- index = (i-node->chunk->x)*(node->chunk->height+1) + chunk_size;
+ index = (i-node->chunk->x)*(node->chunk->h_height) + chunk_size;
if(up)
node->chunk->heights[index] += v;
else
@@ -432,7 +428,7 @@ void Terrain::raise(float x, float z, float radius, float focus, float strength,
if(i % chunk_size == 0 && j % chunk_size == 0) {
node = find(i-chunk_size, j-chunk_size);
if(node) {
- index = (chunk_size)*(node->chunk->height+1) + chunk_size;
+ index = (chunk_size)*(node->chunk->h_height) + chunk_size;
if(up)
node->chunk->heights[index] += v;
else
diff --git a/terrain.h b/terrain.h
index b391f94..b50c5e5 100644
--- a/terrain.h
+++ b/terrain.h
@@ -33,6 +33,7 @@ class Terrain {
Terrain *terrain;
Node **nodes;
float x, y, width, height;
+ int h_width, h_height;
float *heights;
Vector3 *normals;
size_t buf_size;