Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

firsthitraycaster.cpp

Go to the documentation of this file.
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 //------------------------- The default constructor --------------------------
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 //------------------------- The copy constructor -----------------------------
00023 //----------------------------------------------------------------------------
00024 
00025 vu1512122::vu1512122(const vu1512122& inst) : vu151212(inst)
00026 {
00027 }
00028 
00029 //----------------------------------------------------------------------------
00030 //------------------------- The destructor -----------------------------------
00031 //----------------------------------------------------------------------------
00032 
00033 vu1512122::~vu1512122()
00034 {
00035 }
00036 
00037 //----------------------------------------------------------------------------
00038 //------------------------- The assignment operator --------------------------
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 //------------------------- public setViewVectors() --------------------------
00069 //----------------------------------------------------------------------------
00070 
00071 void vu1512122::setViewVectors(const vuVector& view,const vuVector& up,const vuVector& right)
00072 {
00073 //     m_View = view;
00074 //     m_Shift1 = right*3.2f;
00075 //     m_Shift2 = up*3.2f;
00076 //     m_Shift0 = (m_Shift1+m_Shift2)*(-0.5f);
00077 
00078     // maybe we use the camera at some point (so far it's not used)
00079     m_Camera->setLookAtVector(view);
00080     m_Camera->setUpVector(up);
00081     m_Camera->setRightVector(right);
00082     m_Camera->init();
00083 }
00084 
00085 //----------------------------------------------------------------------------
00086 //------------------------- public read() ------------------------------------
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 //------------------------- public readRaw() ---------------------------------
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         // The nocreate is not available on the IRIX boxes that we were compiling
00116         // this code on, so we had to remove this part from
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 //------------------------- private preprocess() -----------------------------
00143 //----------------------------------------------------------------------------
00144 
00145 void vu1512122::preprocess(void)
00146 {
00147   // compute normals
00148   cout<<"preprocessing..."<<endl;
00149 }
00150 
00151 //----------------------------------------------------------------------------
00152 //------------------------- public render() ----------------------------------
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 //------------------------- public initOpenGL() ------------------------------
00173 //----------------------------------------------------------------------------
00174 
00175 void vu1512122::initOpenGL(void)
00176 {
00177   //glEnable(GL_SECRET_FEATURE);
00178 }
00179 
00180 //----------------------------------------------------------------------------
00181 //------------------------- private intersectRayWithBox() --------------------
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   // intersect ray with X slab
00192   //
00193   
00194   if (Rd[0] == 0.0)     // ray is parallel to YZ-plane
00195     {
00196       if ((Ro[0] < (*llc)[0]) || (Ro[0] > (*urc)[0]))
00197         return false;  //ray does not intersect data set
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   // intersect ray with Y slab
00218   //
00219   
00220   if (Rd[1] == 0.0)     // ray is parallel to XZ-plane
00221     {
00222       if ((Ro[1] < (*llc)[1]) || (Ro[1] > (*urc)[1]))
00223         return false;   // ray does not intersect data set
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; // ray misses box
00242     }
00243   
00244   //
00245   // intersect ray with Z slab
00246   //
00247   
00248   if (Rd[2] == 0.0)     // ray is parallel to XY-plane
00249     {
00250       if ((Ro[2] < (*llc)[2]) || (Ro[2] > (*urc)[2]))
00251         return false; // ray does not intersect data set
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; // ray misses box
00270     }
00271 
00272   vuVector Rd1(Rd);
00273   Rd *= tnear;
00274   *f = Ro + Rd;
00275   Rd1 *= tnear;
00276   *s = Ro + Rd;
00277 
00278   //PNT3 f1, s1;
00279   //VEC3 Rd1;
00280   //CopyVEC3(&Rd1,&Rd);
00281   //
00282   //MulVEC3Scal(&Rd,tnear);
00283   //AddPNT3VEC3(&f1,&Ro,&Rd);
00284   //
00285   //MulVEC3Scal(&Rd1,tfar);
00286   //AddPNT3VEC3(&s1,&Ro,&Rd1);
00287   //
00288   //(*f)[0] = f1.x;
00289   //(*f)[1] = f1.y;
00290   //(*f)[2] = f1.z;
00291   //
00292   //(*s)[0] = s1.x;
00293   //(*s)[1] = s1.y;
00294   //(*s)[2] = s1.z;
00295 
00296   return true;
00297 }
00298 
00299 //----------------------------------------------------------------------------
00300 //------------------------- private renderImage() ----------------------------
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 //  double pz = 1.3;  // pseudo-zoom
00312 //  float PI=3.14159265359;
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   vuVector p(-pz*double(maxx >> 1)        , -pz*double(maxy >> 1)         , -double(maxz >> 1)),
00318          dir( 0.0                         ,  0.0                          ,  0.42             ),
00319         down( 0.0                         ,  pz*double(maxy)/double(YRES) ,  0.0              ),
00320        right( pz*double(maxx)/double(XRES),  0.0                          ,  0.0              ),
00321       offset(-pz*double(maxx)             ,  0.0                          ,  0.0              );
00322 
00323   double alpha = PI/2.0, beta = 0.0;
00324   //double alpha = -PI/4.0, beta = PI/4.0;
00325   //double alpha = 0.0, beta = 0.0;
00326 
00327   float rot_values[16] = { cos(alpha)*cos(beta), sin(beta), -sin(alpha)*cos(beta), 0.0,
00328                           -cos(alpha)*sin(beta), cos(beta),  sin(alpha)*sin(beta), 0.0,
00329                            sin(alpha)          , 0        ,  cos(alpha)          , 0.0,
00330                            0.0                 , 0.0      , 0.0                  , 1.0};
00331             
00332   vuMatrix rot(rot_values);
00333 
00334        p *= rot;
00335      dir *= rot;
00336     down *= rot;
00337    right *= rot;
00338   offset *= rot;
00339 
00340   vuVector to_center(double(maxx >> 1), double(maxy >> 1), double(maxz >> 1));
00341   p += to_center;
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   //vuVector dataSetUpperBound(64.0, 64.0, 64.0);
00351 
00352   vuVector *dslb = &dataSetLowerBound;
00353   vuVector *dsub = &dataSetUpperBound;
00354 
00355   //
00356   // shoot ray through each pixel
00357   //
00358 /*
00359   glMatrixMode(GL_PROJECTION);
00360   glLoadIdentity();
00361   glMatrixMode(GL_MODELVIEW);
00362   glLoadIdentity();
00363   glOrtho(0,XRES,0,YRES,-1,1);
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           //double r_in = 0.0, g_in = 0.0, b_in = 0.0, a_in = 0.0,
00374           // a_out, r, g, b, a;
00375         
00376           double  r_out, g_out, b_out;
00377  
00378           // traverse ray
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           //col.glColor();
00422           //glVertex2f(i,YRES-j);
00423           
00424           //p += right;
00425         }
00426       //printf("row %d\n",i);
00427       cout << "." << flush;
00428 
00429       //p += offset;
00430       //p += down;
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 }

Generated on Wed Dec 15 21:20:28 2004 for vuVolume by  doxygen 1.3.9.1