Exemple #1
0
//void lightCalculation(){
float rayCalculation(float levelDetail){
    
    /* Default closest interection to 3000*/
    float closestIntersection = 3000;
    int currentSphere = -1;
    
    int i;
    for(i = 0; i < numberOfSpheres; i++){
        
        //if Ray and Sphere intersect
        if((intersectRaySphere(&ray, &spheres[i], &closestIntersection)) == 1){
            currentSphere = i;
        }
    }
    //if not intersection found return
    if(currentSphere == -1){
        return levelDetail;
    }
    
    //Finding the scaler value of the closestInterestion and the ray directions
    Vector scaled = vectorScalar(closestIntersection, &ray.direction);
    //Starting at the new location just found
    Vector newStart = vectorAddition(&ray.start, &scaled);
    
    //Find the normal for this new vector at the point of intersection
    Vector normal = vectorSubtraction(&newStart, &spheres[currentSphere].pos);
    
    float temp = vectorDotProduct(&normal, &normal);
    
    //if there's no dot product of the normal vector return
    if(temp == 0){
        return levelDetail;
    }
    
    temp = (1.0 / sqrtf(temp));
    //Setting the noram vector
    normal = vectorScalar(temp, &normal);
    
    //calucting the lights and shadows
    levelDetail = lightCalculation(levelDetail, currentSphere, &newStart, &normal);
    
    // The reflected ray start and direction
    ray.start = newStart;
    float reflect = 2 * vectorDotProduct(&ray.direction, &normal);
    Vector tmp = vectorScalar(reflect, &normal);
    ray.direction = vectorSubtraction(&ray.direction, &tmp);

    return levelDetail;
}
Exemple #2
0
/* Check if the ray and sphere intersect */
int intersectRaySpherePartB(Ray *ray, Sphere *sphere){
    
    float A, B, C, root;
    
    /* The vector dot product of the direction */
    A = vectorDotProduct(&ray->direction, &ray->direction);
    
    /* The vector distance from the start of
       the ray and the position of the circle. */
    Vector distance = vectorSubtraction(&ray->start, &sphere->pos);
    
    /* Postiion - c */
    B = 2 * vectorDotProduct(&ray->direction, &distance);
    
    /* (postion - c)^2 - r^2 */
    C = vectorDotProduct(&distance, &distance) - (sphere->radius * sphere->radius);
    
    /* Solving the discriminant */
    root = B * B - 4 * A * C;
    
    /* If the root is negtive return false/zero */
    if(root < 0){
        return 0;
    }
    return 1;
}
Exemple #3
0
/* Calculating the lights and shadows*/
float lightCalculation(float levelDetail, int currentSphere, Vector *newStart, Vector *normal){

     // Determine  the current material
     int getMaterialIndexValue = (spheres + currentSphere)->material;
     Material currentMaterial = materials[getMaterialIndexValue];
     
     //Find the value of the light at the current position
     int j;
    
     for( j = 0; j < numberOfLights; j++){
     
         Light currentLight = lights[j];
         Vector distance = vectorSubtraction(&currentLight.pos, newStart);
     
         //If the dot product of the normal and the distance is less than or equal to zero
         //coutinue
         if(vectorDotProduct(normal, &distance) <= 0) {
             continue;
         }
     
         float closestIntersection;
         
         //setting the new closesInterection point
         closestIntersection = sqrtf(vectorDotProduct(&distance,&distance));
     
         if(closestIntersection <= 0) {
             continue;
         }
     
         Ray lightRay;
         lightRay.start = *newStart;
         lightRay.direction = vectorScalar((1/closestIntersection), &distance);
     
         // Calculate shadows
         int shadow = 0;
         int k;
     
         for (k = 0; k < numberOfSpheres; ++k){
     
             if (intersectRaySphere(&lightRay, &spheres[k], &closestIntersection) == 1){
                 shadow = 0;
                 break;
             }
         }
         if (shadow != 1){
     
             //Calculating the lambert value
             float lambert = vectorDotProduct(&lightRay.direction, normal) * levelDetail;
             //Processing the Lambert Diffusion
             LambertDiffusion(lambert, currentLight, currentMaterial);
     
         }
        }
    levelDetail *= currentMaterial.reflection;
    
    return levelDetail;

}
Exemple #4
0
Line *createLine(vector camera, png_uint_32 x, png_uint_32 y) {
  Line *retval = malloc(sizeof(Line));

  retval->start = camera;

  vector planePoint = vectorCreate(-8.0 + 16.0 * ((double)x / (double)WIDTH), 0.0, 10.0 - 10.0 * ((double)y / (double)HEIGHT));
  vector dir = vectorSubtraction(planePoint, camera);

  retval->end = vectorAddition(camera, vectorMultiply(dir,50));

  return retval;
}
void SkeletonState::renderLimb(SkeletonPoint& pt1, SkeletonPoint& pt2)
{
    if(pt1.confidence <= 0.5 || pt2.confidence <= 0.5)
        return;

    float a[3] = {pt1.x, pt1.y, pt1.z};
    float b[3] = {pt2.x, pt2.y, pt2.z};

    // vector formed by the two joints
    float c[3];
    vectorSubtraction(a, b, c);

    // glu cylinder vector
    float z[3] = {0,0,1};

    // r is axis of rotation about z
    float r[3];
    vectorCrossProduct(z, c, r);

    // get angle of rotation in degrees
    float angle = 180./M_PI * acos((vectorDotProduct(z, c)/vectorMagnitude(c)));

    glPushMatrix();

    // translate to second joint
    glTranslatef(pt2.x, pt2.y, pt2.z);
    glRotatef(angle, r[0], r[1], r[2]);

    // set up quadric object
    GLUquadricObj* quadObj = gluNewQuadric();

    gluCylinder(quadObj, 10, 10, vectorMagnitude(c), 10, 10);

    glPopMatrix();

    // delete used quadric
    gluDeleteQuadric(quadObj);
}
Exemple #6
0
/* Check if the ray and sphere intersect */
int intersectRaySphere(Ray *ray, Sphere *sphere, float *closestIntersection){
    
    float A,B,C;
    int intersect = 0;
    
    /* A = direction * direction , the vector dot product of the direction */
    A = vectorDotProduct(&ray->direction, &ray->direction);
    
    /* The vector distance from the start of
     the ray and the position of the circle. */
    Vector distance = vectorSubtraction(&ray->start, &sphere->pos);
    
    /* 2*(postion - c) */
    B = 2 * vectorDotProduct(&ray->direction, &distance);
    
    /* (position - c)^2 - r^2 */
    C = vectorDotProduct(&distance, &distance) - (sphere->radius * sphere->radius);
    
    /*Solve the  Quadratic Formula */
    
    intersect = quadraticFormula(A, B, C, closestIntersection);
    
    return intersect;
}
Exemple #7
0
vector rayTrace(Line *ray, Primitive **primitives, Light **lights, float r_idx) {
  Intersection *bestIntersection = NULL;
  vector retval = vectorCreate(0.0, 0.0, 0.0);
  
  for (int p = 0; p < 10; p++) {
    if (!primitives[p])
      break;
    Intersection *thisIntersection = primitiveIntersect(primitives[p], ray);
    if (!bestIntersection) {
      bestIntersection = thisIntersection;
      continue;
    }
    if (thisIntersection && thisIntersection->distance < bestIntersection->distance) {
      free(bestIntersection);
      bestIntersection = thisIntersection;
    }
  }
  
  if (bestIntersection) {
    Primitive *primitive = bestIntersection->primitive;
    Material *material = primitive->material;

    vector mcolor = vectorMultiply(material->color, 0.0);
    
    vector N = primitiveNormal(primitive, bestIntersection);
    vector V = vectorUnit(vectorSubtraction(ray->end, ray->start));
    
    for (int l = 0; l < 2; l++) {
      Light *light = lights[l];
      vector lvec = vectorSubtraction(light->location, bestIntersection->intersectionPoint);
      
      vector pcolor = material->color;
      vector lcolor = light->color;

      Intersection *linter = NULL;
      Line *lightRay = makeLine(bestIntersection->intersectionPoint, light->location);
      
      float shade = 0.0;
      
      for (int p = 0; p < 3; p++) {
        if (primitives[p] == primitive)
          continue;
        if ((linter = primitiveIntersect(primitives[p], lightRay)))
          shade += 1.0 - linter->primitive->material->transparency;
      }
      
      free(lightRay);
      
      if (shade < 1.0) {
        if (material->specular > 0.0) {
          float sintensity = 0.0;
          
          vector L = vectorUnit(lvec);          
          vector R = vectorSubtraction(L, vectorMultiply(N, 2 * vectorDotProduct(L,N)));
          
          float dot = vectorDotProduct(V, R);
          
          if (dot > 0.0) {
            sintensity = pow(dot, 20) * material->specular * light->intensity * (1.0 - shade);
          }
          
          if (sintensity > 0.0) {
            mcolor = vectorAddition(mcolor, vectorMultiply(lcolor, sintensity));
          }
        }

        if (material->diffuse > 0.0) {
          float dintensity = material->diffuse * vectorDotProduct(vectorUnit(lvec), primitive->normal(bestIntersection->primitive, bestIntersection)) * light->intensity * (1.0 - shade);

          if (dintensity > 0.0) {
            mcolor = vectorAddition(mcolor, vectorMultiply(vectorCProduct(pcolor, lcolor), dintensity));
          }
        }
      }
      
      free(linter);
    }
    
    if (material->reflection > 0.0) {
      vector R = vectorUnit(vectorSubtraction(V, vectorMultiply(N, 2 * vectorDotProduct(V,N))));
      Line *rline = makeLine(vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(R, EPS)), vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(R, 30)));

      vector rcolor = rayTrace(rline, primitives, lights, r_idx);

      mcolor = vectorAddition(vectorMultiply(mcolor, 1.0 - material->reflection), vectorMultiply(rcolor, material->reflection));

      free(rline);
    }
    
    if (material->transparency > 0) {
      float refraction = material->refraction;
      float n = r_idx / refraction;
      vector Nr = vectorMultiply(N, bestIntersection->direction);
      float cosI = - vectorDotProduct(Nr, V);
      float cosT2 = 1.0 - n * n * (1.0 - cosI * cosI);
      if (cosT2 > 0.0) {
        vector T = vectorAddition(vectorMultiply(V, n), vectorMultiply(Nr, n * cosI - sqrt(cosT2)));

        Line *rline = makeLine(vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(T, EPS)), vectorAddition(bestIntersection->intersectionPoint, vectorMultiply(T, 30)));
        vector rfcol = rayTrace(rline, primitives, lights, r_idx);

        mcolor = vectorAddition(vectorMultiply(mcolor, 1.0 - material->transparency), vectorMultiply(rfcol, material->transparency));
        
        free(rline);
      }
    }

    retval = mcolor;
    
    free(bestIntersection);
  }  
  return retval;
}