#ifdef HAVE_CONFIG_H # include "config.h" #endif #include #ifdef HAVE_GL_GL_H # include #elif HAVE_OPENGL_GL_H # include #endif #ifdef HAVE_GL_GLU_H # include #elif HAVE_OPENGL_GLU_H # include #endif #include #include "height_quantity.hh" #include "vector.hh" using std::string; HeightQuantity::HeightQuantity(const string &name, GLdouble offset, GLdouble scale, GLfloat red, GLfloat green, GLfloat blue): offset(offset), scale(scale), red(red), green(green), blue(blue), name(name), frame_number(-1){ GLenum glerror; this->display_list = glGenLists(1); if((glerror = glGetError()) != GL_NO_ERROR) throw gluErrorString(glerror); } HeightQuantity::~HeightQuantity(void){ if(glIsList(this->display_list) == GL_TRUE) glDeleteLists(this->display_list, 1); } void HeightQuantity::draw(int frame){ if(!this->dynamic) frame = 0; if(this->frame_number != frame) this->compile(frame); this->frame_number = frame; glPushMatrix(); glScalef(1, 1, this->scale); glTranslatef(0, 0, this->offset); glColor3f(this->red, this->green, this->blue); glCallList(this->display_list); glPopMatrix(); } void HeightQuantity::set_SWWFile(const shared_ptr &file){ int ncstatus; this->sww_file = file; this->points = \ shared_array(new float[this->sww_file->number_of_points]); this->dynamic = this->sww_file->nc_inq_varndims_by_name(this->name) == 2; if((ncstatus = nc_inq_varid(this->sww_file->netcdf_id, this->name.c_str(), &this->varid)) != NC_NOERR) throw nc_strerror(ncstatus); } #include "output.hh" #include using std::ostringstream; void HeightQuantity::compile(int frame){ size_t start_1[1] = {0}; size_t start_2[2] = {0, 0}; size_t span_1[1]; size_t span_2[2] = {1, 0}; int ncstatus; int vertIndex; if(this->dynamic){ start_2[0] = frame; span_2[1] = this->sww_file->number_of_points; ncstatus = nc_get_vara_float(this->sww_file->netcdf_id, this->varid, start_2, span_2, this->points.get()); }else{ span_1[0] = this->sww_file->number_of_points; ncstatus = nc_get_vara_float(this->sww_file->netcdf_id, this->varid, start_1, span_1, this->points.get()); } if(ncstatus != NC_NOERR) throw nc_strerror(ncstatus); glNewList(this->display_list, GL_COMPILE); glBegin(GL_TRIANGLES);{ for(int volume = 0 ; volume < this->sww_file->number_of_volumes ; ++volume){ /* Compute normal. */ Vector vs[3]; for(int vertex = 0 ; vertex < 3 ; ++vertex){ int offset = volume * this->sww_file->number_of_vertices + vertex; vertIndex = this->sww_file->volumes[offset]; vertIndex = 0; vs[vertex] = Vector(this->sww_file->x[vertIndex], this->sww_file->y[vertIndex], this->points [vertIndex]); } Vector normal = (vs[1] - vs[0]).cross(vs[2] - vs[0]); if(normal.magnitude() < 0) normal = normal.scale(-1.0); glNormal3f(normal.x, normal.y, normal.z); /* Insert vertices. */ for(int vertex = 0 ; vertex < 3 ; ++vertex){ int offset = volume * this->sww_file->number_of_vertices + vertex; vertIndex = this->sww_file->volumes[offset]; glVertex3f(this->sww_file->x[vertIndex], this->sww_file->y[vertIndex], this->points[vertIndex]); } } }glEnd(); glEndList(); }