summaryrefslogtreecommitdiff
path: root/scene.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-04-02 18:00:55 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-04-02 18:00:55 +0200
commit94c77b14469c9122d945eb1cf337507ff74c2fb6 (patch)
treefae6c6bf0bfc6fb0120559d210907ebf950f450e /scene.cpp
parentfe2df4b280ecd8417cc267b8fc05ec050054ec4c (diff)
Moved select() into Scene, fixed rendering end of terrain.
Diffstat (limited to 'scene.cpp')
-rw-r--r--scene.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/scene.cpp b/scene.cpp
index 47b76bf..c477345 100644
--- a/scene.cpp
+++ b/scene.cpp
@@ -1,11 +1,32 @@
#include "scene.h"
+#include <SDL_image.h>
#include <SDL_opengl.h>
#include <cmath>
#define inrange(a, b, c) ((a) >= (b) && (a) <= (c))
+Scene::Scene() {
+ SDL_Surface *hm = IMG_Load("heightmap10.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);
+ qt = new Quadtree(w, h, heightmap, 3);
+}
+
+Scene::~Scene() {
+ if(qt)
+ delete qt;
+}
+
void Scene::lookat() {
/* calculate cartesian coordinates for the center vector where yaw is Φ and pitch is θ
* x = cos Φ sin θ
@@ -44,3 +65,27 @@ void Scene::move(float forward, float right, int steps) {
//pos.x += dir.x;
//pos.z += dir.y;
}
+
+bool Scene::select(int x, int y, float& px, float& py, float& pz) {
+ GLint view[4] = {0};
+ GLdouble mvmatrix[16] = {0}, projmatrix[16] = {0};
+ glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
+ glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
+ glGetIntegerv(GL_VIEWPORT, view);
+
+ y = view[3] - y - 1;
+
+ float z;
+ glReadBuffer(GL_BACK);
+ glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
+
+ GLdouble _px, _py, _pz;
+ if(gluUnProject(x, y, z, mvmatrix, projmatrix, view, &_px, &_py, &_pz) == GLU_TRUE) {
+ px = _px;
+ py = _py;
+ pz = _pz;
+ return true;
+ }
+
+ return false;
+}