Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Raytracer.utils;
  8.  
  9. namespace Raytracer.shapes
  10. {
  11.     class Cube : Shape
  12.     {
  13.         private Vector3 pos_;
  14.         private float size_;
  15.         private CubeFace[] faces_;
  16.         private CubeFace inter_face_;
  17.  
  18.         public Cube(Material mat, Vector3 pos, float s) : base(mat)
  19.         {
  20.             pos_ = pos;
  21.             size_ = s;
  22.  
  23.             //faces_ = new CubeFace[6];
  24.  
  25.             Vector3 a = new Vector3(pos.X - s / 2, pos.Y + s / 2, pos.Z - s / 2);
  26.             Vector3 b = new Vector3(pos.X + s / 2, pos.Y + s / 2, pos.Z - s / 2);
  27.             Vector3 c = new Vector3(pos.X + s / 2, pos.Y + s / 2, pos.Z + s / 2);
  28.             Vector3 d = new Vector3(pos.X - s / 2, pos.Y + s / 2, pos.Z + s / 2);
  29.  
  30.             Vector3 ah = new Vector3(pos.X - s / 2, pos.Y - s / 2, pos.Z - s / 2);
  31.             Vector3 bh = new Vector3(pos.X + s / 2, pos.Y - s / 2, pos.Z - s / 2);
  32.             Vector3 ch = new Vector3(pos.X + s / 2, pos.Y - s / 2, pos.Z + s / 2);
  33.             Vector3 dh = new Vector3(pos.X - s / 2, pos.Y - s / 2, pos.Z + s / 2);
  34.  
  35.  
  36.             CubeFace c0 = new CubeFace(mat, ah, bh, a);
  37.  
  38.             CubeFace c1 = new CubeFace(mat, bh, ch, b);
  39.  
  40.             CubeFace c2 = new CubeFace(mat, ch, dh, c);
  41.  
  42.             CubeFace c3 = new CubeFace(mat, dh, ah, d);
  43.  
  44.             CubeFace sol = new CubeFace(mat, a, b, d);
  45.  
  46.             CubeFace toit = new CubeFace(mat, bh, ah, bh);
  47.  
  48.  
  49.             faces_ = new CubeFace[] { toit, sol, c0, c1,c2,c3 };
  50.  
  51.  
  52.             // First face of the cube.
  53.             //faces_[0] = new CubeFace(mat, new Vector3(pos.X + s / 2, pos.Y + s / 2, pos.Z - s / 2), new Vector3(pos.X + s / 2, pos.Y - s / 2, pos.Z - s / 2), new Vector3(pos.X - s / 2, pos.Y + s / 2, pos.Z - s / 2));
  54.  
  55.  
  56.         }
  57.  
  58.         public override Vector3 intersect(Ray ray)
  59.         {
  60.             inter_face_ = null;
  61.             foreach (CubeFace cf in faces_)
  62.             {
  63.                 if (cf.intersect(ray) != null)
  64.                 {
  65.                     if (inter_face_ != null)
  66.                     {
  67.                         if ((inter_face_.intersect(ray).norm()) > (cf.intersect(ray)).norm())
  68.                         {
  69.                             inter_face_ = cf;
  70.                         }
  71.                     }
  72.                     else
  73.                     {
  74.                         inter_face_ = cf;
  75.                     }
  76.                 }
  77.  
  78.             }
  79.  
  80.             if (inter_face_ != null)
  81.             {
  82.                 return inter_face_.intersect(ray);
  83.             }
  84.             else
  85.             {
  86.                 return null;
  87.             }
  88.  
  89.         }
  90.  
  91.         public override Vector3 normal_at_point(Vector3 point)
  92.         {
  93.             return inter_face_.normal_at_point(point);
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement