source: trunk/anuga_work/development/anugavis/src/height_quantity.cc @ 7924

Last change on this file since 7924 was 5685, checked in by jack, 17 years ago

Normal calculations for height quantities (fixed).

File size: 3.4 KB
Line 
1#ifdef HAVE_CONFIG_H
2#  include "config.h"
3#endif
4
5#include <string>
6#ifdef HAVE_GL_GL_H
7#  include <GL/gl.h>
8#elif HAVE_OPENGL_GL_H
9#  include <OpenGL/gl.h>
10#endif
11#ifdef HAVE_GL_GLU_H
12#  include <GL/glu.h>
13#elif HAVE_OPENGL_GLU_H
14#  include <OpenGL/glu.h>
15#endif
16#include <netcdf.h>
17#include "height_quantity.hh"
18#include "vector.hh"
19
20using std::string;
21
22HeightQuantity::HeightQuantity(const string &name,
23                               GLdouble offset, GLdouble scale,
24                               GLfloat red, GLfloat green, GLfloat blue):
25  offset(offset), scale(scale), red(red), green(green), blue(blue), name(name),
26  frame_number(-1){
27  GLenum glerror;
28  this->display_list = glGenLists(1);
29  if((glerror = glGetError()) != GL_NO_ERROR) throw gluErrorString(glerror);
30}
31
32HeightQuantity::~HeightQuantity(void){
33  if(glIsList(this->display_list) == GL_TRUE)
34    glDeleteLists(this->display_list, 1);
35}
36
37void HeightQuantity::draw(int frame){
38  if(!this->dynamic) frame = 0;
39  if(this->frame_number != frame) this->compile(frame);
40  this->frame_number = frame;
41  glPushMatrix();
42  glScalef(1, 1, this->scale);
43  glTranslatef(0, 0, this->offset);
44  glColor3f(this->red, this->green, this->blue);
45  glCallList(this->display_list);
46  glPopMatrix();
47}
48
49void HeightQuantity::set_SWWFile(const shared_ptr<SWWFile> &file){
50  int ncstatus;
51  this->sww_file = file;
52  this->points = \
53    shared_array<float>(new float[this->sww_file->number_of_points]);
54  this->dynamic = this->sww_file->nc_inq_varndims_by_name(this->name) == 2;
55  if((ncstatus = nc_inq_varid(this->sww_file->netcdf_id, this->name.c_str(),
56                              &this->varid)) != NC_NOERR)
57    throw nc_strerror(ncstatus);
58}
59#include "output.hh"
60#include <sstream>
61using std::ostringstream;
62void HeightQuantity::compile(int frame){
63  size_t start_1[1] = {0};
64  size_t start_2[2] = {0, 0};
65  size_t span_1[1];
66  size_t span_2[2] = {1, 0};
67  int ncstatus;
68  int vertIndex;
69  if(this->dynamic){
70    start_2[0] = frame;
71    span_2[1] = this->sww_file->number_of_points;
72    ncstatus = nc_get_vara_float(this->sww_file->netcdf_id, this->varid,
73                                 start_2, span_2, this->points.get());
74  }else{
75    span_1[0] = this->sww_file->number_of_points;
76    ncstatus = nc_get_vara_float(this->sww_file->netcdf_id, this->varid,
77                                 start_1, span_1, this->points.get());
78  }
79  if(ncstatus != NC_NOERR) throw nc_strerror(ncstatus);
80  glNewList(this->display_list, GL_COMPILE);
81  glBegin(GL_TRIANGLES);{
82    for(int volume = 0 ; volume < this->sww_file->number_of_volumes ; ++volume){
83      /* Compute normal. */
84      Vector vs[3];
85      for(int vertex = 0 ; vertex < 3 ; ++vertex){
86        int offset = volume * this->sww_file->number_of_vertices + vertex;
87        vertIndex = this->sww_file->volumes[offset];
88        vertIndex = 0;
89        vs[vertex] = Vector(this->sww_file->x[vertIndex],
90                            this->sww_file->y[vertIndex],
91                            this->points     [vertIndex]);
92      }
93      Vector normal = (vs[1] - vs[0]).cross(vs[2] - vs[0]);
94      if(normal.magnitude() < 0) normal = normal.scale(-1.0);
95      glNormal3f(normal.x, normal.y, normal.z);
96
97      /* Insert vertices. */
98      for(int vertex = 0 ; vertex < 3 ; ++vertex){
99        int offset = volume * this->sww_file->number_of_vertices + vertex;
100        vertIndex = this->sww_file->volumes[offset];
101        glVertex3f(this->sww_file->x[vertIndex],
102                   this->sww_file->y[vertIndex],
103                   this->points[vertIndex]);
104       
105      }
106    }
107  }glEnd();
108  glEndList();
109}
Note: See TracBrowser for help on using the repository browser.