Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Raytracer.utils;
- namespace Raytracer.shapes
- {
- class Cube : Shape
- {
- private Vector3 pos_;
- private float size_;
- private CubeFace[] faces_;
- private CubeFace inter_face_;
- public Cube(Material mat, Vector3 pos, float s) : base(mat)
- {
- pos_ = pos;
- size_ = s;
- //faces_ = new CubeFace[6];
- Vector3 a = new Vector3(pos.X - s / 2, pos.Y + s / 2, pos.Z - s / 2);
- Vector3 b = new Vector3(pos.X + s / 2, pos.Y + s / 2, pos.Z - s / 2);
- Vector3 c = new Vector3(pos.X + s / 2, pos.Y + s / 2, pos.Z + s / 2);
- Vector3 d = new Vector3(pos.X - s / 2, pos.Y + s / 2, pos.Z + s / 2);
- Vector3 ah = new Vector3(pos.X - s / 2, pos.Y - s / 2, pos.Z - s / 2);
- Vector3 bh = new Vector3(pos.X + s / 2, pos.Y - s / 2, pos.Z - s / 2);
- Vector3 ch = new Vector3(pos.X + s / 2, pos.Y - s / 2, pos.Z + s / 2);
- Vector3 dh = new Vector3(pos.X - s / 2, pos.Y - s / 2, pos.Z + s / 2);
- CubeFace c0 = new CubeFace(mat, ah, bh, a);
- CubeFace c1 = new CubeFace(mat, bh, ch, b);
- CubeFace c2 = new CubeFace(mat, ch, dh, c);
- CubeFace c3 = new CubeFace(mat, dh, ah, d);
- CubeFace sol = new CubeFace(mat, a, b, d);
- CubeFace toit = new CubeFace(mat, bh, ah, bh);
- faces_ = new CubeFace[] { toit, sol, c0, c1,c2,c3 };
- // First face of the cube.
- //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));
- }
- public override Vector3 intersect(Ray ray)
- {
- inter_face_ = null;
- foreach (CubeFace cf in faces_)
- {
- if (cf.intersect(ray) != null)
- {
- if (inter_face_ != null)
- {
- if ((inter_face_.intersect(ray).norm()) > (cf.intersect(ray)).norm())
- {
- inter_face_ = cf;
- }
- }
- else
- {
- inter_face_ = cf;
- }
- }
- }
- if (inter_face_ != null)
- {
- return inter_face_.intersect(ray);
- }
- else
- {
- return null;
- }
- }
- public override Vector3 normal_at_point(Vector3 point)
- {
- return inter_face_.normal_at_point(point);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement