00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #ifndef MASK4D_H
00028 #define MASK4D_H
00029
00030 #include <error.h>
00031
00032 namespace Display
00033 {
00066 class Mask4D
00067 {
00068 public:
00069 Mask4D();
00070 ~Mask4D();
00071
00074 template <typename DataType>
00075 void update(const DataType* data,
00076 unsigned int width,
00077 unsigned int height,
00078 unsigned int depth,
00079 unsigned int nb_frames);
00080
00082 void setFrame(unsigned int frame);
00083
00085 unsigned int getFrame() const;
00086
00088 unsigned int getNbFrames() const;
00089
00091 void display();
00092
00095 template <typename DataType>
00096 void display(const DataType* data,
00097 unsigned int width,
00098 unsigned int height,
00099 unsigned int depth);
00100
00102 void clear();
00103
00104 private:
00105 GLuint m_id;
00106 unsigned int m_nb_frames;
00107 unsigned int m_frame;
00108 };
00109
00110
00111 template <typename DataType>
00112 void Mask4D::update(const DataType* data,
00113 unsigned int width,
00114 unsigned int height,
00115 unsigned int depth,
00116 unsigned int nb_frames)
00117 {
00118
00119 if(glIsList(m_id)) glDeleteLists(m_id,m_nb_frames);
00120 m_nb_frames = nb_frames;
00121 m_id = glGenLists(m_nb_frames); glCheckError();
00122
00123 for(unsigned int frame = 0; frame < m_nb_frames; ++frame)
00124 {
00125 glNewList(m_id+frame, GL_COMPILE); glCheckError();
00126
00127
00128 display(&data[frame*width*height*depth], width, height, depth);
00129
00130
00131 glEndList();
00132 }
00133 }
00134
00135
00136 template <typename DataType>
00137 void Mask4D::display(const DataType* data,
00138 unsigned int width,
00139 unsigned int height,
00140 unsigned int depth)
00141 {
00142 int neighbors[6][3] = { {-1,0,0},{+1,0,0},{0,-1,0},{0,+1,0},{0,0,-1},{0,0,+1} };
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 float offset[6][5][3] = {
00162 { {-0.5,+0.5,+0.5},
00163 {-0.5,-0.5,+0.5},
00164 {-0.5,-0.5,-0.5},
00165 {-0.5,+0.5,-0.5},
00166 {-1,0,0} },
00167 { {+0.5,-0.5,+0.5},
00168 {+0.5,+0.5,+0.5},
00169 {+0.5,+0.5,-0.5},
00170 {+0.5,-0.5,-0.5},
00171 {+1,0,0} },
00172 { {-0.5,-0.5,+0.5},
00173 {+0.5,-0.5,+0.5},
00174 {+0.5,-0.5,-0.5},
00175 {-0.5,-0.5,-0.5},
00176 {0,-1,0} },
00177 { {+0.5,+0.5,+0.5},
00178 {-0.5,+0.5,+0.5},
00179 {-0.5,+0.5,-0.5},
00180 {+0.5,+0.5,-0.5},
00181 {0,+1,0} },
00182 { {-0.5,-0.5,-0.5},
00183 {+0.5,-0.5,-0.5},
00184 {+0.5,+0.5,-0.5},
00185 {-0.5,+0.5,-0.5},
00186 {0,0,-1} },
00187 { {-0.5,+0.5,+0.5},
00188 {+0.5,+0.5,+0.5},
00189 {+0.5,-0.5,+0.5},
00190 {-0.5,-0.5,+0.5},
00191 {0,0,+1} } };
00192
00193
00194 glMatrixMode(GL_MODELVIEW);
00195 glPushMatrix();
00196 glTranslatef(-0.5,-0.5,-0.5);
00197 glScalef(1.0f/width, 1.0f/height, 1.0f/depth);
00198 glTranslatef(+0.5,+0.5,+0.5);
00199
00200
00201
00202 glPushAttrib(GL_ENABLE_BIT);
00203 glEnable(GL_COLOR_MATERIAL);
00204 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00205
00206 glEnable(GL_LIGHTING);
00207 glEnable(GL_LIGHT0);
00208
00209
00210 glBegin(GL_QUADS);
00211
00212 for(unsigned int k=0; k<depth; ++k)
00213 for(unsigned int j=0; j<height; ++j)
00214 for(unsigned int i=0; i<width; ++i)
00215 {
00216 if(data[(k*height+j)*width+i] != 0)
00217 {
00218 for(int m=0; m<6; ++m)
00219 {
00220 bool drawFace = false;
00221
00222 int ni = i+neighbors[m][0];
00223 int nj = j+neighbors[m][1];
00224 int nk = k+neighbors[m][2];
00225
00226 if(ni>=0 && ni<(int)width &&
00227 nj>=0 && nj<(int)height &&
00228 nk>=0 && nk<(int)depth)
00229 {
00230 if(data[(nk*height+nj)*width+ni] == 0)
00231 drawFace = true;
00232 }
00233 else
00234 drawFace = true;
00235
00236 if(drawFace)
00237 {
00238
00239 glNormal3f(offset[m][4][0], offset[m][4][1], offset[m][4][2]);
00240 glVertex3f(i+offset[m][0][0], j+offset[m][0][1], k+offset[m][0][2]);
00241 glVertex3f(i+offset[m][1][0], j+offset[m][1][1], k+offset[m][1][2]);
00242 glVertex3f(i+offset[m][2][0], j+offset[m][2][1], k+offset[m][2][2]);
00243 glVertex3f(i+offset[m][3][0], j+offset[m][3][1], k+offset[m][3][2]);
00244 }
00245 }
00246 }
00247 }
00248
00249 glEnd();
00250
00251 glPopMatrix();
00252
00253
00254 glPopAttrib();
00255 }
00256
00257 }
00258
00259 #endif