summaryrefslogtreecommitdiff
path: root/scene.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene.cpp')
-rw-r--r--scene.cpp148
1 files changed, 89 insertions, 59 deletions
diff --git a/scene.cpp b/scene.cpp
index bcec2bf..cc7db04 100644
--- a/scene.cpp
+++ b/scene.cpp
@@ -1,5 +1,6 @@
#include "scene.h"
#include "video.h"
+#include "terrain.h"
#include <SDL_image.h>
#include <boost/format.hpp>
@@ -15,7 +16,8 @@
Scene::Scene() {
running = true;
grid = false;
- terrain = true;
+ normals = false;
+ render_terrain = true;
gravity = true;
last_node = NULL;
@@ -47,27 +49,8 @@ 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");
- std::cout << (boost::format("size: %dx%d") % hm->w % hm->h).str() << std::endl;
- std::cout << "size: " << hm->w << "x" << hm->h << std::endl;
- printf("size: %dx%d\n", hm->w, hm->h);
- 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;
- if(y*hm->w + x >= hm->w * hm->h)
- std::cout << "foo" << std::endl;
- heightmap[y*hm->w + x] = ((float)(*p) / 256) * 20;
- }
- }
- int w = hm->w;
- int h = hm->h;
- SDL_FreeSurface(hm);
- float *hmap = heightmap;
- std::cout << "heightmap: " << heightmap << std::endl;
- qt = new Quadtree(w, h, heightmap);
- std::cout << "qt: " << qt << std::endl;
+ /* init terrain */
+ terrain = new Terrain();
/* load font */
//font = new FTTextureFont("fonts/VeraMono.ttf");
@@ -79,8 +62,8 @@ Scene::Scene() {
Scene::~Scene() {
if(tool)
delete tool;
- if(qt)
- delete qt;
+ if(terrain)
+ delete terrain;
//delete font;
}
@@ -92,11 +75,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);
}
@@ -149,8 +130,7 @@ bool Scene::select(int x, int y, float& px, float& py, float& pz) {
}
void Scene::update() {
- std::cout << "qt: " << qt << std::endl;
- qt->update(pos.x, pos.z);
+ terrain->update(pos.x, pos.z);
}
void Scene::events() {
@@ -175,8 +155,11 @@ void Scene::events() {
case SDLK_g:
grid = !grid;
break;
+ case SDLK_n:
+ normals = !normals;
+ break;
case SDLK_t:
- terrain = !terrain;
+ render_terrain = !render_terrain;
break;
case SDLK_SPACE:
yvel = .05;
@@ -192,7 +175,7 @@ void Scene::events() {
break;
case SDLK_1:
if(tool) delete tool;
- tool = new RaiseTool(qt);
+ tool = new RaiseTool(terrain);
break;
default:
break;
@@ -302,7 +285,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,8 +299,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) {
@@ -333,8 +316,9 @@ 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(terrain) {
+ 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);
@@ -342,6 +326,9 @@ void Scene::render() {
GLint selpos = glGetUniformLocation(terrain_program.get_program(), "selpos");
glUniform3f(selpos, selected.x, selected.y, selected.z);
}
+ 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);
@@ -357,29 +344,36 @@ void Scene::render() {
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, marker_texture);
- std::queue<Quadtree::QuadChunk*> q;
+ /*std::queue<Quadtree::QuadChunk*> q;
q.push(qt->root);
while(!q.empty()) {
Quadtree::QuadChunk *chunk = q.front();
- q.pop();
- if(!chunk->nodes) {
+ q.pop();*/
+ for(std::list<Terrain::Chunk*>::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;
- 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);
+ continue;*/
+ glPushMatrix();
+ glTranslatef(-pos.x + chunk->x, -pos.y, -pos.z + chunk->y);
+ 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);
@@ -392,19 +386,34 @@ void Scene::render() {
glColor3f(0, 0, 0);
else
glColor3f(1, 1, 1);
- std::queue<Quadtree::QuadChunk*> q;
+ /*std::queue<Quadtree::QuadChunk*> q;
q.push(qt->root);
while(!q.empty()) {
Quadtree::QuadChunk *chunk = q.front();
- q.pop();
- if(!chunk->nodes) {
+ q.pop();*/
+ for(std::list<Terrain::Chunk*>::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) {
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<Terrain::Chunk*>::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++)
+ chunk->nodes[i]->draw_normal();
+ glPopMatrix();
}
}
@@ -413,16 +422,37 @@ 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;
}
+ 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);
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")
+ % 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);