00001 #include <GL/gl.h>
00002 #include <fstream.h>
00003 #include <math.h>
00004 #include "vuParallelCamera.h"
00005 #include "vuPerspectiveCamera.h"
00006
00007 #include "firsthitraycaster.h"
00008
00009
00010
00011
00012
00013 vu1512122::vu1512122()
00014 {
00015 m_Camera = new vuPerspectiveCamera();
00016 m_Camera->setHeight(240);
00017 m_Camera->setWidth(320);
00018 refresh = false;
00019 }
00020
00021
00022
00023
00024
00025 vu1512122::vu1512122(const vu1512122& inst) : vu151212(inst)
00026 {
00027 }
00028
00029
00030
00031
00032
00033 vu1512122::~vu1512122()
00034 {
00035 }
00036
00037
00038
00039
00040
00041 vu1512122& vu1512122::operator=(const vu1512122& rhs)
00042 {
00043 if (this != &rhs)
00044 {
00045 vu151212::operator=(rhs);
00046
00047 }
00048 return *this;
00049 }
00050
00051 void vu1512122::setImageSize(int sx, int sy)
00052 {
00053 if(m_Camera == NULL) {
00054 cerr << "no camera set" << endl;
00055 return;
00056 }
00057 if (m_Camera->getType() == vuCamera::vuPERSPECTIVE_CAMERA) {
00058 vuPerspectiveCamera *cptr = (vuPerspectiveCamera *)m_Camera;
00059 cptr->setAspect(float(sx)/sy);
00060 }
00061 m_Camera->setWidth(sx);
00062 m_Camera->setHeight(sy);
00063 m_Camera->init();
00064 m_Image.init(m_Camera->getWidth(),m_Camera->getHeight());
00065 }
00066
00067
00068
00069
00070
00071 void vu1512122::setViewVectors(const vuVector& view,const vuVector& up,const vuVector& right)
00072 {
00073
00074
00075
00076
00077
00078
00079 m_Camera->setLookAtVector(view);
00080 m_Camera->setUpVector(up);
00081 m_Camera->setRightVector(right);
00082 m_Camera->init();
00083 }
00084
00085
00086
00087
00088
00089 bool vu1512122::read()
00090 {
00091 bool success = vu151212::read();
00092 if (!success) return false;
00093
00094 vuVector center(getCenter());
00095 m_Camera->setPosition(center);
00096 m_Camera->translateXYZ(0,0,-1.5f*(float)m_Dim1Size);
00097 m_Camera->init();
00098
00099 preprocess();
00100 return true;
00101 }
00102
00103
00104
00105
00106
00107 bool vu1512122::readRaw(void)
00108 {
00109 dword len;
00110 ifstream in;
00111
00112 #ifdef IS_NOCREATE_NEEDED
00113 in.open(m_FileName, ios::in|ios::binary|ios::nocreate);
00114 #else
00115
00116
00117 in.open(m_FileName, ios::in|ios::binary);
00118 #endif
00119
00120 if (!in.is_open()) return false;
00121
00122 in.seekg(0, ios::end);
00123 len = in.tellg();
00124 in.seekg(0, ios::beg);
00125
00126 in >> m_Dim1Size >> m_Dim2Size >> m_Dim3Size;
00127 if (in.fail()) return false;
00128 m_DataSize = m_Dim1Size*m_Dim2Size*m_Dim3Size;
00129
00130 m_Data = new byte[m_DataSize];
00131 in.read((char *) (m_Data), int (m_DataSize));
00132 if (in.fail()) return false;
00133
00134 in.close();
00135
00136 preprocess();
00137
00138 return true;
00139 }
00140
00141
00142
00143
00144
00145 void vu1512122::preprocess(void)
00146 {
00147
00148 cout<<"preprocessing..."<<endl;
00149 }
00150
00151
00152
00153
00154
00155 void vu1512122::render(void)
00156 {
00157 if (refresh)
00158 {
00159 glClearColor(0.0,0.0,0.0,0);
00160 glClear(GL_COLOR_BUFFER_BIT);
00161
00162
00163 renderImage();
00164
00165 refresh = false;
00166 }
00167 displayFromImage();
00168 glFlush();
00169 }
00170
00171
00172
00173
00174
00175 void vu1512122::initOpenGL(void)
00176 {
00177
00178 }
00179
00180
00181
00182
00183
00184 bool vu1512122::intersectRayWithBox(vuVector Ro, vuVector Rd, vuVector *f, vuVector *s, vuVector *llc, vuVector *urc)
00185 {
00186 double tnear=-1.7E308,tfar=1.7E308,t1,t2;
00187
00188 Rd.makeUnit();
00189
00190
00191
00192
00193
00194 if (Rd[0] == 0.0)
00195 {
00196 if ((Ro[0] < (*llc)[0]) || (Ro[0] > (*urc)[0]))
00197 return false;
00198 }
00199 else
00200 {
00201 t1=((*llc)[0]-Ro[0])/Rd[0];
00202 t2=((*urc)[0]-Ro[0])/Rd[0];
00203
00204 if (t1 < t2)
00205 {
00206 tnear=t1;
00207 tfar=t2;
00208 }
00209 else
00210 {
00211 tnear=t2;
00212 tfar=t1;
00213 }
00214 }
00215
00216
00217
00218
00219
00220 if (Rd[1] == 0.0)
00221 {
00222 if ((Ro[1] < (*llc)[1]) || (Ro[1] > (*urc)[1]))
00223 return false;
00224 }
00225 else
00226 {
00227 t1=((*llc)[1]-Ro[1])/Rd[1];
00228 t2=((*urc)[1]-Ro[1])/Rd[1];
00229
00230 if (t1 > t2)
00231 {
00232 double zw=t1;
00233 t1=t2;
00234 t2=zw;
00235 }
00236
00237 if (t1 > tnear) tnear=t1;
00238 if (t2 < tfar) tfar =t2;
00239
00240 if (tfar < tnear)
00241 return false;
00242 }
00243
00244
00245
00246
00247
00248 if (Rd[2] == 0.0)
00249 {
00250 if ((Ro[2] < (*llc)[2]) || (Ro[2] > (*urc)[2]))
00251 return false;
00252 }
00253 else
00254 {
00255 t1=((*llc)[2]-Ro[2])/Rd[2];
00256 t2=((*urc)[2]-Ro[2])/Rd[2];
00257
00258 if (t1 > t2)
00259 {
00260 double zw=t1;
00261 t1=t2;
00262 t2=zw;
00263 }
00264
00265 if (t1 > tnear) tnear=t1;
00266 if (t2 < tfar) tfar =t2;
00267
00268 if (tfar < tnear)
00269 return false;
00270 }
00271
00272 vuVector Rd1(Rd);
00273 Rd *= tnear;
00274 *f = Ro + Rd;
00275 Rd1 *= tnear;
00276 *s = Ro + Rd;
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 return true;
00297 }
00298
00299
00300
00301
00302
00303 void vu1512122::renderImage()
00304 {
00305 if(m_Camera == NULL) {
00306 cerr << "no camera found" << endl;
00307 return;
00308 }
00309 m_Image.init(m_Camera->getWidth(), m_Camera->getHeight());
00310
00311
00312
00313 int XRES=m_Camera->getWidth();
00314 int YRES=m_Camera->getHeight();
00315 unsigned short maxx = m_Dim1Size, maxy = m_Dim2Size, maxz = m_Dim3Size;
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 vuVector l(m_Camera->getLookAtVector());
00346 l.makeUnit();
00347
00348 vuVector dataSetLowerBound(0.0f, 0.0f, 0.0f);
00349 vuVector dataSetUpperBound((float)maxx, (float)maxy, (float)maxz);
00350
00351
00352 vuVector *dslb = &dataSetLowerBound;
00353 vuVector *dsub = &dataSetUpperBound;
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 glBegin(GL_POINTS);
00366 for (int i=0; i<XRES; i++)
00367 {
00368 for (int j=0; j<YRES; j++)
00369 {
00370 vuRay ray(m_Camera->getRay((float)i,(float)j));
00371 vuVector pos,pos2;
00372
00373
00374
00375
00376 double r_out, g_out, b_out;
00377
00378
00379
00380
00381 double th = 100.0;
00382 double val = 0.0;
00383
00384 if (intersectRayWithBox(ray.m_Position, ray.m_Direction, &pos, &pos2, dslb, dsub))
00385 {
00386 ray.m_Position = pos;
00387 ray.advance();
00388
00389 while ( (ray.m_Position[0] <= (*dsub)[0]) && (ray.m_Position[0] >= (*dslb)[0]) &&
00390 (ray.m_Position[1] <= (*dsub)[1]) && (ray.m_Position[1] >= (*dslb)[1]) &&
00391 (ray.m_Position[2] <= (*dsub)[2]) && (ray.m_Position[2] >= (*dslb)[2]) && (val < th) )
00392 {
00393 val =getDataValue(ray.m_Position);
00394
00395 ray.advance();
00396 }
00397 } else ray.m_Position = pos;
00398
00399 if (val >= th)
00400 {
00401 vuVector grad = getGradient(ray.m_Position);
00402 grad.makeUnit();
00403
00404 double prod = fabs(grad.dot(l));
00405 double d_s = 0.8*prod + 0.0*pow(prod,200.0);
00406
00407 r_out = d_s + 0.2;
00408 g_out = d_s + 0.2;
00409 b_out = d_s + 0.2;
00410 }
00411 else
00412 {
00413 r_out = 0.0;
00414 g_out = 0.0;
00415 b_out = 0.0;
00416 }
00417
00418 vuColourRGBa col(r_out, g_out, b_out);
00419 m_Image.set_xy(i,YRES-j, col);
00420
00422
00423
00424
00425 }
00426
00427 cout << "." << flush;
00428
00429
00430
00431 }
00432 glEnd();
00433 }
00434
00435 void vu1512122::displayFromImage ()
00436
00437 {
00438 int mx, my;
00439
00440 m_Image.get_extents (mx, my);
00441
00442 if(mx && my)
00443 m_Image.blit();
00444 else
00445 cout << "bad" << endl;
00446 }