Advertisement
bubblesppf

Uniform Spatial Subdivision

Jul 25th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.99 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <sys/stat.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <vector>
  8. #include <GL/freeglut.h>
  9.  
  10. const int IMAGESIZE=128;
  11. const int Rows=IMAGESIZE;
  12. const int Cols=IMAGESIZE;
  13.  
  14. struct Float3
  15. {
  16.     float X;
  17.     float Y;
  18.     float Z;
  19.     Float3(float x=0, float y=0, float z=0)
  20.         :
  21.     X(x), Y(y), Z(z){}
  22.    
  23.     Float3(const Float3& rhs)
  24.         :
  25.     X(rhs.X), Y(rhs.Y), Z(rhs.Z){}
  26.  
  27.    inline Float3& operator+=(const Float3& rhs)
  28.    {    X += rhs.X; Y += rhs.Y; Z += rhs.Z; return *this; }
  29.  
  30.    inline Float3& operator-=(const Float3& rhs)
  31.     {X -= rhs.X; Y -= rhs.Y; Z -= rhs.Z; return *this;}
  32.  
  33.    inline Float3& operator*=(const float& rhs)
  34.     {X *= rhs; Y *= rhs; Z *= rhs; return *this;}
  35.  
  36.    inline Float3& operator/=(const float& rhs)
  37.     {X /= rhs; Y /= rhs; Z /= rhs; return *this;}
  38.  
  39.    inline bool operator!=(const Float3& rhs)
  40.     {return X!=rhs.X || Y!=rhs.Y || Z!=rhs.Z; }
  41.  
  42.    inline Float3 operator*(float rhs) const
  43.     {return Float3(X*rhs, Y*rhs, Z*rhs);   }
  44.  
  45.    inline Float3 operator+(Float3& rhs) const
  46.     {return Float3(X+rhs.X, Y+rhs.Y, Z+rhs.Z);}
  47.  
  48.    inline Float3 operator-(Float3& rhs) const
  49.     {return Float3(X-rhs.X, Y-rhs.Y, Z-rhs.Z);   }
  50.  
  51.    inline Float3 operator/(Float3& rhs) const
  52.     {return Float3(X/rhs.X, Y/rhs.Y, Z/rhs.Z);   }
  53.      
  54.     inline float length()
  55.     {return sqrt(X*X +Y*Y + Z*Z); }
  56.  
  57.    inline float lengthsq()
  58.     {return X*X +Y*Y + Z*Z; }
  59. };
  60.  
  61. typedef struct  
  62. {
  63.     Float3 p1;
  64.     Float3 p2;
  65.     Float3 p3;
  66.     Float3 n1;
  67.     Float3 n2;
  68.     Float3 n3;
  69. }triangle;
  70.  
  71. int nTriangles;
  72. triangle *T;
  73. Float3 *normalArray;
  74.  
  75. struct Bound
  76. {
  77.     Float3 minBound;
  78.     Float3 maxBound;
  79.     Float3 center;
  80.     float dia;
  81. }B;
  82.  
  83. struct uchar4 {
  84.     unsigned char r,g,b,a;
  85. };
  86.  
  87. uchar4*raster;
  88. uchar4 *colorArray;
  89.  
  90. struct Camera {
  91.     Float3 From,At,Up;
  92. };
  93.  
  94. Camera Cam = {Float3(0.0f,1.0f/3.0f,2.0f),
  95.               Float3(0.0f,1.0f/3.0f,0.0f),
  96.               Float3(0.0f,1.0f,0.f)};
  97.  
  98. struct  Ray{
  99.     Float3 ray_d;
  100.     Float3 ray_o;
  101.     float tMin, tMax;
  102. };
  103.  
  104. typedef struct {
  105.     int nx,ny,nz;
  106.     Bound *box;
  107.     int Ghit(Ray ray, int hitrec, Bound *box);
  108.     triangle tri;
  109. }Grid;
  110. Grid G;
  111. std::vector<Grid> cell;
  112.  
  113.  
  114. struct Co_ordinateFrame
  115. {
  116.     Float3 x_axis;
  117.     Float3 y_axis;
  118.     Float3 z_axis;
  119.     Float3 origin;
  120. };
  121. Co_ordinateFrame CF;
  122.  
  123. float txmin, tymin, tzmin;
  124. float txmax, tymax, tzmax;
  125.  
  126. void readModel(char *filename, Bound *B, Camera *cam)//, Grid *G
  127. {
  128.     B->minBound.X = B->minBound.Y = B->minBound.Z = 1e5;
  129.     B->maxBound.X = B->maxBound.Y = B->maxBound.Z = -1e5;
  130.  
  131.     txmin = tymin = tzmin = 1e5;
  132.     txmax = tymax = tzmax = -1e5;
  133.    
  134.     int nVertices;
  135.     FILE *fl=fopen(filename,"r");
  136.     if (fl==NULL)
  137.     {
  138.         printf("Can not read file.\n");
  139.         exit(1);
  140.     }
  141.     fscanf(fl,"%d", &nVertices);
  142.     float *V = (float *) malloc(nVertices*3*sizeof(float));
  143.     float *N = (float *) malloc(nVertices*3*sizeof(float));
  144.     float *tex = (float *) malloc(nVertices*2*sizeof(float));
  145.     for (int i=0; i<nVertices; i++){
  146.         fscanf(fl,"%f%f%f%f%f%f%f%f",V+i*3,V+i*3+1,V+i*3+2,N+i*3,N+i*3+1,N+i*3+2,tex+i*2,tex+i*2+1);
  147.         if (V[i*3] < B->minBound.X) B->minBound.X=V[i*3];
  148.         if (V[i*3] > B->maxBound.X) B->maxBound.X=V[i*3];
  149.         if (V[i*3+1] < B->minBound.Y) B->minBound.Y=V[i*3+1];
  150.         if (V[i*3+1] > B->maxBound.Y) B->maxBound.Y=V[i*3+1];
  151.         if (V[i*3+2] < B->minBound.Z) B->minBound.Z=V[i*3+2];
  152.         if (V[i*3+2] > B->maxBound.Z) B->maxBound.Z=V[i*3+2];
  153.     }
  154.     B->center.X = (B->minBound.X + B->maxBound.X)/2.0f;
  155.     B->center.Y = (B->minBound.Y + B->maxBound.Y)/2.0f;
  156.     B->center.Z = (B->minBound.Z + B->maxBound.Z)/2.0f;
  157.  
  158.     float dX = (B->maxBound.X - B->minBound.X);
  159.     float dY = (B->maxBound.Y - B->minBound.Y);
  160.     float dZ = (B->maxBound.Z - B->minBound.Z);
  161.  
  162.     B->dia = sqrt(dX*dX+dY*dY+dZ*dZ);
  163.     cam->At = B->center;
  164.     if ((dX > dY) && (dX > dZ)){
  165.     cam->Up.X = 1.0f; cam->Up.Y = cam->Up.Z = 0.0f;
  166.     if (dY > dZ){
  167.     cam->From.Z = B->center.Z + B->dia;
  168.     cam->From.X = B->center.X; cam->From.Y = B->center.Y;
  169.     }
  170.     else{
  171.     cam->From.Y = B->center.Y + B->dia;
  172.     cam->From.X = B->center.X; cam->From.Z = B->center.Z;
  173.     }
  174.     }
  175.     else if((dY > dX) && (dY > dZ)){
  176.     cam->Up.Y = 1.0f; cam->Up.X = cam->Up.Z = 0.0f;
  177.     if (dX > dZ){
  178.     cam->From.Z = B->center.Z + B->dia;
  179.     cam->From.Y = B->center.Y; cam->From.X = B->center.X;
  180.     }
  181.     else {
  182.     cam->From.X = B->center.X + B->dia;
  183.     cam->From.Y = B->center.Y; cam->From.Z = B->center.Z;
  184.     }
  185.     }
  186.     else {
  187.     cam->Up.Z = 1.0f; cam->Up.X = cam->Up.Y = 0.0f;
  188.     if (dX > dY){
  189.     cam->From.Y = B->center.Y + B->dia;
  190.     cam->From.X = B->center.X; cam->From.Z = B->center.Z;
  191.     }
  192.     else{
  193.     cam->From.X = B->center.X - B->dia;
  194.     cam->From.Y = B->center.Y; cam->From.Z = B->center.Z;
  195.     }
  196.     }
  197.  
  198.     G.nx = B->maxBound.X/(IMAGESIZE-1);
  199.     G.ny = B->maxBound.Y/(IMAGESIZE-1);
  200.     G.nz = B->maxBound.Z/(IMAGESIZE-1);
  201.  
  202.     fscanf(fl,"%d", &nTriangles);
  203.     char textureFilename[256];
  204.     fscanf(fl,"%s",textureFilename);
  205.     printf("[Vertices: %d], [Triangles: %d], [Texture File: %s]\n",
  206.     nVertices, nTriangles, textureFilename);
  207.     //E = (int *) malloc(nTriangles*3*sizeof(int));
  208.     T = (triangle *)malloc(nTriangles*sizeof(triangle));
  209.     normalArray = (Float3 *)malloc(nTriangles*sizeof(Float3));
  210.     int E1, E2, E3;
  211.     for (int i=0; i<nTriangles; i++){
  212.         fscanf(fl,"%d%d%d",&E1,&E2,&E3);
  213.         T[i].p1.X = V[E1*3];T[i].p1.Y = V[E1*3+1];T[i].p1.Z = V[E1*3+2];
  214.         T[i].p2.X = V[E2*3];T[i].p2.Y = V[E2*3+1];T[i].p2.Z = V[E2*3+2];
  215.         T[i].p3.X = V[E3*3];T[i].p3.Y = V[E3*3+1];T[i].p3.Z = V[E3*3+2];
  216.         //-- Set n1, n2, n3
  217.         T[i].n1.X = N[E1*3];T[i].n1.Y = N[E1*3+1];T[i].n1.Z = N[E1*3+2];
  218.         T[i].n2.X = N[E1*3];T[i].n2.Y = N[E1*3+1];T[i].n2.Z = N[E1*3+2];
  219.         T[i].n3.X = N[E1*3];T[i].n3.Y = N[E1*3+1];T[i].n3.Z = N[E1*3+2];
  220.     }
  221.  
  222.     fclose(fl);
  223.     return;
  224. }
  225.  
  226. Float3 Scale(Float3 vec1,float s)
  227. {
  228.     Float3 res;
  229.     res.X = vec1.X*s;
  230.     res.Y = vec1.Y*s;
  231.     res.Z = vec1.Z*s;
  232.  
  233.     return res;
  234. }
  235.   float Dot(Float3 vec1,Float3 vec2)
  236.  {
  237.     float dot = vec1.X*vec2.X+vec1.Y*vec2.Y +vec1.Z*vec2.Z;
  238.  
  239.     return dot;
  240.  }
  241.  
  242.  Float3 Cross(Float3 vec1,Float3 vec2)
  243.  {
  244.      Float3 cross;
  245.  
  246.      cross.X = (vec1.Y*vec2.Z) - (vec1.Z*vec2.Y);
  247.      cross.Y = (vec1.Z*vec2.X) - (vec1.X*vec2.Z);
  248.      cross.Z = (vec1.X*vec2.Y) - (vec1.Y*vec2.X);
  249.  
  250.      return cross;
  251.  }
  252.  Float3 normalize(Float3 vec)
  253.  {
  254.  
  255.      float mag = sqrt(Dot(vec,vec));
  256.      Float3 result;
  257.      result.X = vec.X /mag;
  258.      result.Y = vec.Y/mag;
  259.      result.Z = vec.Z/mag;
  260.  
  261.      return result;
  262.  }
  263.  
  264.  void  ComputeCameraframe()
  265.  {
  266.     CF.origin = Cam.From;
  267.     CF.z_axis = normalize(Cam.From - Cam.At);
  268.     CF.x_axis = normalize(Cross(Cam.Up,CF.z_axis));
  269.     CF.y_axis = normalize(Cross(CF.z_axis,CF.x_axis));
  270.  }
  271.  
  272.  Ray get_Ray(int row, int col)
  273.  {
  274.     Ray ray;
  275.     ray.ray_o = CF.origin;//eye point
  276.  
  277.     Float3  scale_resx = Scale(CF.x_axis,1.0-(2.0*col)/Cols);
  278.     Float3  scale_resy = Scale(CF.y_axis,1.0-(2.0*row)/Rows);
  279.     ray.ray_d = scale_resy - CF.z_axis - scale_resx;
  280.     ray.tMin = 0.0f;
  281.     ray.tMax = 10000000000.0f;
  282.     return ray;
  283. }
  284.  
  285. bool ray_box_intersection(Ray ray, Bound box)
  286. {
  287.        
  288.        if(ray.ray_d.X >= 0.0f)
  289.        {
  290.          txmin = (box.minBound.X - ray.ray_o.X)/ray.ray_d.X;
  291.          txmax = (box.maxBound.X - ray.ray_o.X)/ray.ray_d.X;
  292.        }
  293.        else
  294.        {
  295.            txmin = (box.maxBound.X - ray.ray_o.X)/ray.ray_d.X;
  296.            txmax = (box.minBound.X - ray.ray_o.X)/ray.ray_d.X;
  297.        }
  298.  
  299.        if(ray.ray_d.Y >= 0.0f)
  300.        {
  301.             tymin = (box.minBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  302.             tymax = (box.maxBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  303.        }
  304.        else
  305.        {
  306.            tymin = (box.maxBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  307.            tymax = (box.minBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  308.        }
  309.        if(txmin > tymax || tymin > txmax)
  310.            return false;
  311.        if(tymin > txmin)
  312.            txmin = tymin;
  313.        if(tymax < txmax)
  314.            txmax = tymax;
  315.  
  316.        if(ray.ray_d.Z >= 0.0f)
  317.        {
  318.              tzmin = (box.minBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  319.              tzmax = (box.maxBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  320.        }
  321.        else
  322.        {
  323.            tzmin = (box.maxBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  324.            tzmax = (box.minBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  325.        }
  326.        if(txmin > tzmax || tzmin > txmax)
  327.            return false;
  328.        if(tzmin > txmin)
  329.            txmin = tzmin;
  330.        if(tzmax < txmax)
  331.            txmax = tzmax;
  332.     if (txmin < ray.tMax && txmax > ray.tMin){
  333.         if (txmin > ray.tMin) ray.tMin = txmin;
  334.         if (txmax < ray.tMax) ray.tMax = txmax;
  335.     }
  336.     else return false;
  337. }
  338.  
  339. int ray_trianglelist_intersection(Ray ray,  triangle *triangles, float *ray_t, float *t_beta, float *t_gamma)
  340. {
  341.     float t,beta,gamma;
  342.     float a,b,c,d,e, f, g, h, i,j,k,l;
  343.  
  344.     float nearest_t=ray.tMax;
  345.     float nearest_beta, nearest_gamma;
  346.     int hitId = -1;
  347.  
  348.     for(int nt = 0;nt<nTriangles;nt++)
  349.     {
  350.         Float3 col1 = triangles[nt].p1 - triangles[nt].p2;
  351.         Float3 col2 = triangles[nt].p1 - triangles[nt].p3;
  352.  
  353.         a = col1.X;   d = col2.X;  g = ray.ray_d.X;
  354.         b = col1.Y;   e = col2.Y;  h = ray.ray_d.Y;
  355.         c = col1.Z;   f = col2.Z;  i = ray.ray_d.Z;
  356.  
  357.         Float3 col3 = triangles[nt].p1 - ray.ray_o;
  358.  
  359.         j = col3.X;
  360.         k = col3.Y;
  361.         l = col3.Z;
  362.  
  363.         float M = a*(e*i-h*f)+b*(g*f-d*i)+c*(d*h-e*g);
  364.         //float M = a*(e*i-h*f)+d*(h*c-b*i)+g*(b*f-e*c);
  365.         //compute t
  366.  
  367.         t = -(f*(a*k-j*b)+e*(j*c-a*l)+d*(b*l-k*c))/M;
  368.         //t = (a*(e*l-f*k) + d*(k*c-b*l) + j*(b*f-c*e))/M;
  369.  
  370.         if(t<ray.tMin || t>nearest_t)
  371.             continue;
  372.         //compute gamma
  373.         gamma = (i*(a*k-j*b)+h*(j*c-a*l)+g*(b*l-k*c))/M;
  374.         //gamma = (a*(k*i-l*h)-j*(b*i-c*h)+g*(b*l-c*k))/M;
  375.         if(gamma<0.0f || gamma>1.0f)
  376.             continue;
  377.         //compute beta
  378.         beta = (j*(e*i-h*f)+k*(g*f-d*i)+l*(d*h-e*g))/M;
  379.         //beta = (j*(e*i-h*f)-d*(k*i-l*h)+g*(k*f-l*e))/M;
  380.  
  381.         if(beta<0.0f || beta > 1.0f-gamma)
  382.             continue;
  383.         nearest_t = t; nearest_beta = beta; nearest_gamma = gamma;
  384.         hitId = nt;
  385.     }
  386.     if (nearest_t < ray.tMax){
  387.         *ray_t = nearest_t; *t_beta = nearest_beta; *t_gamma = nearest_gamma;
  388.         return hitId;
  389.     }
  390.     else
  391.         return -1;
  392. }
  393.  
  394.  
  395. int Grid::Ghit(Ray ray, int hitrec, Bound *box)//,
  396. {  
  397.     int I_inc, I_stop, J_inc, J_stop, K_inc, K_stop;
  398.     I_inc = J_inc = K_inc = 1;
  399.     I_stop = J_stop =  K_stop = 1;
  400.     float dtX,dtY,dtZ;
  401.     dtX = dtY = dtZ = 0.0f;
  402.     float tXnext, tYnext, tZnext , tlast;
  403.    
  404.     Float3 pointOnRay = ray.ray_o + Scale(ray.ray_d, ray.tMax);
  405.  
  406.     if(ray.ray_d.X >= 0.0f )
  407.     {
  408.         txmin = (box->minBound.X - ray.ray_o.X)/ray.ray_d.X;
  409.         I_inc += 1;
  410.         I_stop = nx;
  411.     }
  412.     else
  413.     {
  414.         txmin = (box->maxBound.X - ray.ray_o.X)/ray.ray_d.X;
  415.         txmax = (box->minBound.X - ray.ray_o.X)/ray.ray_d.X;
  416.  
  417.         I_inc -= 1;
  418.         I_stop = -1;
  419.     }
  420.  
  421.     if(ray.ray_d.Y >= 0.0f)
  422.         {
  423.    
  424.           tymin = (box->minBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  425.           tymax = (box->maxBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  426.           J_inc += 1;
  427.           J_stop = ny;
  428.         }
  429.        else
  430.         {
  431.            tymin = (box->maxBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  432.            tymax = (box->minBound.Y - ray.ray_o.Y)/ray.ray_d.Y;
  433.            J_inc -= 1;
  434.            J_stop = -1;
  435.         }
  436.  
  437.     if(txmin > tymax || tymin > txmax) return -1;
  438.  
  439.     if(ray.ray_d.Z >= 0.0f)
  440.     {
  441.         tzmin = (box->minBound.Z - ray.ray_o.Z)/ray.ray_d.Z;   
  442.         tzmax = (box->maxBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  443.         K_inc += 1;
  444.         K_stop = nz;
  445.     }
  446.     else
  447.     {
  448.         tzmin = (box->maxBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  449.         tzmax = (box->minBound.Z - ray.ray_o.Z)/ray.ray_d.Z;
  450.         K_inc -= 1;
  451.         K_stop = -1;
  452.     }
  453.    
  454.     if(tzmin > txmax || txmin > tzmax) return -1;
  455.     if(tzmin > tymax || tymin > tzmax) return -1;
  456.  
  457.     dtX = (txmax - txmin)/nx;
  458.     dtY = (tymax - tymin)/ny;
  459.     dtZ = (tzmax - tzmin)/nz;
  460.     int  I, J, K;
  461.    
  462.     // if a point on the ray is inside the grid
  463.  
  464.     if(ray.tMin >= box->minBound.X)
  465.     {
  466.         I = floor(nx*(pointOnRay.X - box->minBound.X)/(box->maxBound.X - box->minBound.X));
  467.         if(I < 0) I = 0;
  468.         if(I > nx - 1) I = nx - 1;
  469.  
  470.         J = floor(ny*(pointOnRay.Y- box->minBound.Y)/(box->maxBound.Y - box->minBound.Y));
  471.         if(J < 0) J = 0;
  472.         if(J > ny-1) J = ny - 1;
  473.  
  474.         K = floor(nz*(pointOnRay.Z - box->minBound.Z)/(box->maxBound.Z - box->minBound.Z));
  475.         if(K < 0 ) K = 0;
  476.         if(K > nz-1) K = nz - 1;
  477.         tXnext = txmin + (I + (I_inc + 1)/2)*dtX;
  478.         tYnext = tymin + (J + (J_inc + 1)/2)*dtY;
  479.         tZnext = tzmin + (K + (I_inc + 1)/2)*dtZ;
  480.         tlast = ray.tMin;
  481.     }
  482.     else if (txmin > tymin && txmin > tzmin)
  483.     {
  484.         I = I_stop - I_inc;
  485.         pointOnRay.Y = ray.ray_o.Y + ray.tMin;//txminyd;
  486.         J = floor(ny*(pointOnRay.Y - box->minBound.Y)/(box->maxBound.Y- box->minBound.Y));
  487.         if(J < 0) J = 0;
  488.         if(J > ny-1) J = ny-1;
  489.        
  490.         pointOnRay.Z = ray.ray_o.Z + ray.tMin; //tyminZd;
  491.         K = floor(nz*(pointOnRay.Z - box->minBound.Z)/(box->maxBound.Z- box->minBound.Z));
  492.         if(K < 0 ) K = 0;
  493.         if(K > nz-1) K = nz-1;
  494.        
  495.         tXnext = txmin + dtX;
  496.         tYnext = tymin + (J + (J_inc + 1)/2)*dtY;
  497.         tZnext = tzmin + (K + (K_inc + 1)/2)*dtZ;
  498.         tlast = txmin;
  499.     }
  500.     else if(tymin > tzmin)
  501.     {
  502.         J = J_stop - J_inc;
  503.         pointOnRay.X = ray.ray_o.X + ray.tMin; //tyminxd
  504.         I = floor(nx*(pointOnRay.X - box->minBound.X)/(box->maxBound.X - box->minBound.X));
  505.         if(I < 0) I = 0;
  506.         if(I > nx-1) I = nx-1;
  507.        
  508.         pointOnRay.Z = ray.ray_o.Z + ray.tMin; //tyminzd
  509.         K = floor(nz*(pointOnRay.Z - box->minBound.Z)/(box->maxBound.Z- box->minBound.Z));
  510.            
  511.         if(K < 0 ) K = 0;
  512.         if(K > nz -1) K = nz -1;
  513.        
  514.         tYnext = tymin + dtY;
  515.         tXnext = txmin + (I + (I_inc + 1)/2)*dtX;
  516.         tZnext = tzmin + (K + (K_inc + 1)/2)*dtZ;
  517.         tlast = tymin;
  518.     }
  519.     else K = K_stop - K_inc;
  520.  
  521.         pointOnRay.X = ray.ray_o.X + ray.tMin;//tzminxd
  522.         I = floor(nx*(pointOnRay.X - box->minBound.X)/(box->maxBound.X- box->minBound.X));
  523.        
  524.         if (I < 0 ) I = 0;
  525.         if (I > nx - 1) I = nx - 1;
  526.        
  527.         pointOnRay.Y = ray.ray_o.Y + ray.tMin; //tzminyd
  528.         J = floor(nx*(pointOnRay.Y - box->minBound.Y)/(box->maxBound.Y- box->minBound.Y));
  529.        
  530.         if(J < 0) J=0;
  531.         if(J > ny-1) J = ny-1;
  532.                    
  533.         tZnext = tzmin + dtZ;
  534.         tXnext = txmin + (I + (I_inc + 1)/2)*dtX;
  535.         tYnext = tymin + (J + (J_inc + 1)/2)*dtZ;
  536.         tlast = tzmin;
  537.        
  538.     if(tlast > ray.tMax) return -1;
  539.  
  540.     while(true)
  541.     {
  542.         if(tXnext < tYnext && tXnext < tZnext)
  543.         {
  544.             if(cell[I,J].Ghit(ray, hitrec, box) && hitrec < ray.tMax) //box //ray,tlast,tXnext,rec
  545.            
  546.                 return hitrec;
  547.                 tlast = tXnext;
  548.                 tXnext += dtX;
  549.                 I += I_inc;
  550.                 if(I==I_stop) return -1;
  551.         }
  552.         else if(tYnext < tZnext)
  553.         {
  554.             if(cell[I,J].Ghit(ray, hitrec, box) && hitrec < ray.tMax) //box //ray,tlast,tXnext,rec
  555.                     return hitrec;
  556.                 tlast = tYnext;
  557.                 tYnext += dtY;
  558.                 J += J_inc;
  559.                 if(J==J_stop) return -1;
  560.         }
  561.         else
  562.             {
  563.                 if(cell[I,J].Ghit(ray,hitrec,box) && hitrec < ray.tMax) //box //ray,tlast,tXnext,rec
  564.                     return hitrec;
  565.                 tlast = tZnext;
  566.                 tZnext += dtZ;
  567.                 K += K_inc;
  568.                 if(K==K_stop) return -1;
  569.             }
  570.     }
  571. }
  572.  
  573. int ray_uss_intersection(Ray ray, Grid *G, float *ray_t, float *t_beta, float *t_gamma )
  574. {  
  575.    int USS_hitID = 0;
  576.    int  USS_hit = G->Ghit(ray, USS_hitID, &B);
  577.    int M, N;
  578.    M = G->nx;
  579.    N = G->ny;
  580.    if(USS_hit < 0)
  581.    {
  582.        while (&cell!= NULL)
  583.        {
  584.            int Tri_hitID = ray_trianglelist_intersection(ray, &cell[M,N].tri, ray_t, t_beta, t_gamma);
  585.             if (Tri_hitID >= 0 )//&& the point is inside the cell
  586.             {
  587.                 //grid = cell[USS_hit];
  588.                 return Tri_hitID;
  589.             }
  590.        }
  591.    }
  592.    return -1;
  593. }
  594.  
  595. int ray_scene_intersection(Ray ray, Grid *G, Bound *B, float *ray_t, float *t_beta, float *t_gamma)
  596. {
  597.     // Step I: Do bounding box test
  598.     if (ray_box_intersection(ray, *B))
  599.         //return ray_trianglelist_intersection(ray, T, ray_t, t_beta, t_gamma);
  600.         return ray_uss_intersection(ray, G, ray_t, t_beta, t_gamma);
  601.     else return -1;
  602.     //
  603.     // Step II:  Do bounding box test
  604.     // if (ray_box_intersection(ray, *B))
  605.     //    //return ray_uss_intersection(ray, G);
  606.     ////
  607.     //return ray_trianglelist_intersection(ray, T, ray_t, t_beta, t_gamma);
  608. }
  609. uchar4 get_Color(Ray r)
  610.  {
  611.      uchar4 background_color = {0,0,0,255};
  612.      uchar4 foreground_color; //= {100,200,150,255};
  613.      float t,b,g;
  614.      int hitId = ray_scene_intersection(r,&G,&B,&t,&b,&g);
  615.    
  616.     if (hitId < 0)
  617.          return background_color;
  618.      else{
  619.        
  620.          //--compute foreground color
  621.         // find the point of intersection
  622.           Float3 hitvert1 = T[hitId].n1;
  623.           Float3 hitvert2 = T[hitId].n2;
  624.           Float3 hitvert3 = T[hitId].n3;
  625.           Float3 interNorm1 = Scale(hitvert1,(1.0f-b-g))+Scale(hitvert2,b)+Scale(hitvert3,g);
  626.  
  627.          // Float3 normal = normalArray[hitId];
  628.  
  629.          // Assume light at camera from point.
  630.           Float3 light_Pos = Cam.From;
  631.           Float3 pI = r.ray_o + Scale(r.ray_d,t);
  632.         // Compute and normalize the light direction: normalize(minus(from,point_of_intersection))
  633.           Float3 light_dir = normalize(light_Pos -pI);
  634.           float ndotl = Dot(interNorm1,light_dir);
  635.           if (ndotl<0.0f)ndotl = 0.0f;
  636.            foreground_color.r = ndotl*255;
  637.            foreground_color.g = ndotl*255;
  638.            foreground_color.b = ndotl*255;
  639.            foreground_color.a = 255;
  640.         return foreground_color;
  641.      }
  642. }
  643.  
  644.  
  645. unsigned char *updateRasterTexture()
  646.  {
  647.     for (int row=0;row<Rows;row++)
  648.         for(int col=0;col<Cols;col++)
  649.         {
  650.             Ray ray_r = get_Ray(row, col);//get_Ray(Rows/2, Cols/2);//
  651.             int index = row*Cols+col;
  652.             raster[index]= get_Color(ray_r);
  653.         }
  654.     return (unsigned char *)raster;
  655.  
  656.  }
  657.  
  658. GLuint textureId;
  659.  static const int nVertices = 4;
  660.  
  661.  static GLfloat V[nVertices*3] = {
  662.     -1.0f, -1.0f, 0.0f,
  663.     1.0f, -1.0f, 0.0f,
  664.     1.0f,1.0f,0.0f,
  665.     -1.0f,1.0f,0.0f,
  666.  };
  667.  
  668.  static GLfloat Te[nVertices*2] = {
  669.     0.0f,0.0f,
  670.     1.0f,0.0f,
  671.     1.0f,1.0f,
  672.     0.0f,1.0f
  673.  };
  674.  
  675.  void displayHandler()
  676.  {
  677.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  678.     //glActiveTexture(GL_TEXTURE0);
  679.     glMatrixMode(GL_MODELVIEW);
  680.     glLoadIdentity();
  681.     glEnable(GL_TEXTURE_2D);
  682.     glBindTexture(GL_TEXTURE_2D,textureId);
  683.     unsigned char *buffer=NULL;
  684.     buffer = updateRasterTexture();
  685.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Cols, Rows,
  686.                   GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  687.     glBegin(GL_QUADS);
  688.         glTexCoord2f(Te[0],1.0-Te[1]);
  689.         glVertex3f(V[0],V[1],V[2]);
  690.         glTexCoord2f(Te[2],1.0-Te[3]);
  691.         glVertex3f(V[3],V[4],V[5]);
  692.         glTexCoord2f(Te[4],1.0-Te[5]);
  693.         glVertex3f(V[6],V[7],V[8]);
  694.         glTexCoord2f(Te[6],1.0-Te[7]);
  695.         glVertex3f(V[9],V[10],V[11]);
  696.     glEnd();
  697.     glDisable(GL_TEXTURE_2D);
  698.     glBindTexture(GL_TEXTURE_2D,0);
  699.     glutSwapBuffers();
  700.  }
  701.  void kbdHandler( unsigned char key, int x, int y )
  702.  {
  703.     switch( key )
  704.     {
  705.        case 27:
  706.           exit(0);
  707.           break;
  708.       default: return;
  709.     }
  710.     glutPostRedisplay();
  711.  }
  712.  
  713.  void reshapeHandler(int w, int h)
  714.  {
  715.      glViewport(0, 0, w, h);
  716.      glMatrixMode(GL_PROJECTION);
  717.      glLoadIdentity();
  718.  
  719.     glutPostRedisplay();
  720.  }
  721.  
  722.  void resize(int windowWidth, int windowHeight) {
  723.     glViewport(0, 0, windowWidth, windowHeight);
  724.  }
  725.  void inittexture()
  726.  {
  727.     //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  728.     // Initialize texture
  729.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  730.     glGenTextures(1, &textureId);                       // Generate OpenGL texture IDs
  731.     glBindTexture(GL_TEXTURE_2D, textureId);                    // Bind Our Texture
  732.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  733.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  734.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  735.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  736.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  737.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Cols, Rows, 0,
  738.         GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  739.  //build our texture mipmaps
  740.     static int textureSize;
  741.     //gluBuild2DMipmaps( GL_TEXTURE_2D, 4, textureSize, textureSize,
  742.     //         GL_RGBA, GL_UNSIGNED_BYTE, NULL );
  743.     //glGenerateMipmap(GL_TEXTURE_2D);
  744.  }
  745.  int initGraphics(int argc, char *argv[], int imageSize)
  746.  {
  747.     // Initalize OpenGL Context
  748.     glutInit(&argc, argv);
  749.     glutInitWindowPosition(100,150);
  750.     glutInitWindowSize(imageSize,imageSize);
  751.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  752.     glutCreateWindow (argv[0]);
  753.     glClearColor(0,0,0,0);
  754.     glShadeModel(GL_FLAT);
  755.     glEnable(GL_DEPTH_TEST);
  756.     inittexture();
  757.     // Set Callback Handler
  758.     glutDisplayFunc(displayHandler);
  759.     //glutIdleFunc(Idle);
  760.     glutReshapeFunc(reshapeHandler);
  761.     glutKeyboardFunc(kbdHandler);
  762.    return 0;
  763.  }
  764.  
  765.  int main(int argc,char** argv)
  766. {
  767.     raster = (uchar4 *)malloc(Rows*Cols*sizeof(uchar4));
  768.     readModel("legoModel.txt", &B, &Cam );
  769.     ComputeCameraframe();
  770.     if (initGraphics(argc,argv,512))
  771.         {
  772.             printf("Can not initialize graphics.\n");
  773.             return 0;
  774.         }
  775.     glutMainLoop();
  776.     return 0;
  777. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement