[5487] | 1 | #ifdef HAVE_CONFIG_H |
---|
| 2 | # include "config.h" |
---|
| 3 | #endif |
---|
| 4 | |
---|
[5636] | 5 | #include <algorithm> |
---|
[5487] | 6 | #include <string> |
---|
[5684] | 7 | #include <boost/bind.hpp> |
---|
[5600] | 8 | #include <boost/shared_ptr.hpp> |
---|
[5487] | 9 | #ifdef HAVE_GL_GL_H |
---|
| 10 | # include <GL/gl.h> |
---|
| 11 | #elif HAVE_OPENGL_GL_H |
---|
| 12 | # include <OpenGL/gl.h> |
---|
| 13 | #endif |
---|
| 14 | #ifdef HAVE_GL_GLU_H |
---|
| 15 | # include <GL/glu.h> |
---|
| 16 | #elif HAVE_OPENGL_GLU_H |
---|
| 17 | # include <OpenGL/glu.h> |
---|
| 18 | #endif |
---|
| 19 | #include <SDL.h> |
---|
| 20 | #include "anugavis.hh" |
---|
[5598] | 21 | #include "height_quantity.hh" |
---|
[5600] | 22 | #include "sww_file.hh" |
---|
[5487] | 23 | |
---|
[5684] | 24 | using boost::bind; |
---|
[5600] | 25 | using boost::shared_ptr; |
---|
[5636] | 26 | using std::for_each; |
---|
[5487] | 27 | using std::string; |
---|
| 28 | |
---|
[5488] | 29 | AnugaVis::AnugaVis(const string &file_name, int width, int height): |
---|
[5636] | 30 | frame(0), paused(false), screen(NULL), sww_file(new SWWFile(file_name)){ |
---|
| 31 | init_camera(); |
---|
[5487] | 32 | init_SDL(width, height); |
---|
| 33 | init_OpenGL(width, height); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | AnugaVis::~AnugaVis(void){ |
---|
| 37 | if(this->screen != NULL) SDL_Quit(); |
---|
| 38 | } |
---|
| 39 | |
---|
[5600] | 40 | void AnugaVis::add_HeightQuantity(shared_ptr<HeightQuantity> &height){ |
---|
| 41 | height->set_SWWFile(this->sww_file); |
---|
[5598] | 42 | this->heights.push_back(height); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | void AnugaVis::run(void){ |
---|
[5636] | 46 | while(this->step()); |
---|
| 47 | } |
---|
[5598] | 48 | |
---|
[5636] | 49 | void AnugaVis::init_camera(void){ |
---|
| 50 | this->eye = Vector(this->sww_file->minX, this->sww_file->minY, 0); |
---|
| 51 | this->focus = Vector(this->sww_file->minX + |
---|
| 52 | (this->sww_file->maxX - this->sww_file->minX) / 2, |
---|
| 53 | this->sww_file->minY + |
---|
| 54 | (this->sww_file->maxY - this->sww_file->minY) / 2, |
---|
| 55 | 0); |
---|
| 56 | this->eye.z = (eye - focus).magnitude(); |
---|
[5598] | 57 | } |
---|
| 58 | |
---|
[5487] | 59 | void AnugaVis::init_SDL(int width, int height){ |
---|
| 60 | if((SDL_Init(SDL_INIT_VIDEO) == -1) || |
---|
| 61 | (SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) == -1) || |
---|
| 62 | (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) == -1) || |
---|
| 63 | (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) == -1) || |
---|
| 64 | (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) == -1) || |
---|
| 65 | (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) == -1) || |
---|
| 66 | ((this->screen = |
---|
| 67 | SDL_SetVideoMode(width, height, 16, SDL_OPENGL)) == NULL)) |
---|
| 68 | throw SDL_GetError(); |
---|
| 69 | SDL_WM_SetCaption("ANUGA Visualiser", "ANUGA Visualiser"); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void AnugaVis::init_OpenGL(int width, int height){ |
---|
| 73 | GLfloat lightAmbient[] = {0.0, 0.0, 0.0, 1.0}; |
---|
| 74 | GLfloat lightDiffuse[] = {1.0, 1.0, 1.0, 1.0}; |
---|
| 75 | glEnable(GL_DEPTH_TEST); |
---|
| 76 | glShadeModel(GL_FLAT); |
---|
| 77 | glEnable(GL_NORMALIZE); |
---|
| 78 | glMatrixMode(GL_PROJECTION); |
---|
| 79 | gluPerspective(45.0, ((GLdouble)width)/((GLdouble)height), 0.1, 1000); |
---|
| 80 | glMatrixMode(GL_MODELVIEW); |
---|
| 81 | glLoadIdentity(); |
---|
| 82 | glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); |
---|
| 83 | glEnable(GL_COLOR_MATERIAL); |
---|
| 84 | glEnable(GL_LIGHTING); |
---|
| 85 | glEnable(GL_LIGHT0); |
---|
| 86 | glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); |
---|
| 87 | glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); |
---|
| 88 | } |
---|
[5636] | 89 | |
---|
| 90 | static const Uint32 TARGET_TICK_RATE = 50; |
---|
| 91 | bool AnugaVis::step(void){ |
---|
| 92 | static Uint32 ticks = 0; |
---|
| 93 | static Uint32 lastframe; |
---|
| 94 | bool more = true; |
---|
[5684] | 95 | float light[4]; |
---|
[5636] | 96 | |
---|
| 97 | if(ticks == 0) lastframe = ticks = SDL_GetTicks(); |
---|
| 98 | else ticks = SDL_GetTicks(); |
---|
| 99 | if(ticks - lastframe < TARGET_TICK_RATE) return more; |
---|
| 100 | |
---|
| 101 | lastframe = ticks; |
---|
| 102 | if(this->frame < this->sww_file->number_of_timesteps - 1){ |
---|
| 103 | if(!this->paused) this->frame++; |
---|
| 104 | }else |
---|
| 105 | this->paused = 1; |
---|
| 106 | |
---|
[5684] | 107 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 108 | glMatrixMode(GL_MODELVIEW); |
---|
| 109 | glLoadIdentity(); |
---|
| 110 | gluLookAt(this->eye .x, this->eye .y, this->eye .z, |
---|
| 111 | this->focus.x, this->focus.y, this->focus.z, |
---|
| 112 | 0, 0, 1); |
---|
| 113 | light[0] = this->eye.x; |
---|
| 114 | light[1] = this->eye.y; |
---|
| 115 | light[2] = this->eye.z; |
---|
| 116 | light[3] = 1.0; |
---|
| 117 | glLightfv(GL_LIGHT0, GL_POSITION, light); |
---|
| 118 | for_each(this->heights.begin(), |
---|
| 119 | this->heights.end (), |
---|
| 120 | bind(&HeightQuantity::draw, _1, this->frame)); |
---|
[5636] | 121 | return false; |
---|
| 122 | } |
---|