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