protected Vector CalculateW(Vector eye, Vector target) { // -(t - e) Vector gazeDirection = eye - target; gazeDirection.Normalize3(); return gazeDirection; }
public Ray(Vector eye, Vector rayDirection) { this.start = eye; this.direction = rayDirection; useBounds = false; maximumTravelDistance = float.MaxValue; }
public Fragment(Fragment v1, Fragment v2, float t) { this.RasterizedPosition = v1.RasterizedPosition + t * (v2.RasterizedPosition - v1.RasterizedPosition); this.Normal = v1.Normal + t * (v2.Normal - v1.Normal); this.U = v1.U + t * (v2.U - v1.U); this.V = v1.V + t * (v2.V - v1.V); }
public RenderingParameters() { MaxTime = 3.0f; Height = 500; Width = 500; MinTextureMode = SceneTextureMode.Nearest; MagTextureMode = SceneTextureMode.Nearest; RenderMode = SceneRenderMode.Normal; ShadeMode = ShadingMode.PixelPhong; BackgroundColor = new Vector(0,0,0); EnablePersepctiveCorrected = true; EnableShading = true; EnableReflections = true; EnableRefractions = true; EnableShadows = true; EnableAttenuation = true; EnableAntialias = true; EnableParallelization = false; EnableDepthOfField = false; EnableTestDepth = true; EnableFustrumCulling = true; EnableMultipleLights = true; EnableSoftShadows = false; EnableMotionBlur = true; EnableTextureMapping = true; PixelSize = 1; }
public HitRecord() { ObjectName = ""; Distance = float.MaxValue; Material = new SceneMaterial(); TextureColor = new Vector(); ShadedColors = new List<Vector>(); }
public static float[] GetVector3Array(Vector v) { float[] array = new float[3]; array[0] = v.x; array[1] = v.y; array[2] = v.z; return array; }
public Vector ShowColor(Vector position) { Vector color = buffer[(int)position.x, (int)position.y]; Console.WriteLine(color); //buffer[(int)position.x, (int)position.y] = new Vector(0, 0, 0); Glut.glutPostRedisplay(); return color; }
public Ray(Vector eye, Vector rayDirection, Vector cameraLookDirection, float near, float far) { this.start = eye; this.cameraLookDirection = cameraLookDirection; this.direction = rayDirection; useBounds = true; maximumTravelDistance = float.MaxValue; }
public bool IsCap(Vector capPoint, Vector intersection) { Vector diff = capPoint - intersection; float radius = Vector.Dot3(diff, diff); if (radius > this.Radius*this.Radius) return false; else return true; }
public static Vector operator *(Matrix M, Vector v) { float x = M[0,0] * v.x + M[0,1] * v.y + M[0,2] * v.z + M[0,3] * v.w; float y = M[1, 0] * v.x + M[1, 1] * v.y + M[1, 2] * v.z + M[1, 3] * v.w; float z = M[2, 0] * v.x + M[2, 1] * v.y + M[2, 2] * v.z + M[2, 3] * v.w; float w = M[3, 0] * v.x + M[3, 1] * v.y + M[3, 2] * v.z + M[3, 3] * v.w; Vector transformedVector = new Vector(x, y, z, w); return transformedVector; }
public Matrix(Vector[] colVectors) { data = new float[MATRIX_SIZE * MATRIX_SIZE]; for (int row = 0; row < MATRIX_SIZE; ++row) { for (int col = 0; col < MATRIX_SIZE; ++col) { data[row * MATRIX_SIZE + col] = colVectors[col][row]; } } }
protected void DrawSquare(Vector topLeft, Vector color, int width) { for (int x = (int)topLeft.x; x < topLeft.x + width; ++x) { if (x < 0 || x >= rendParams.Width) continue; for (int y = (int)topLeft.y; y < topLeft.y + width; ++y) { if (x < 0 || x >= rendParams.Height) continue; buffer[x, y] = color; } } }
public void Initialize(List<Vector> vertex) { triangles = new SceneTriangle[2]; Vector center = new Vector(); foreach (Vector v in vertex) center += v; center = center / 4.0f; this.Center = center; this.PlaneNormal = Vector.Cross3(vertex[1] - vertex[0], vertex[2] - vertex[0]); this.PlaneNormal.Normalize3(); SceneTriangle t1 = new SceneTriangle(); SceneTriangle t2 = new SceneTriangle(); triangles[0] = t1; triangles[1] = t2; t1.Vertex = new List<Vector>(); t2.Vertex = new List<Vector>(); t1.Vertex.Add(vertex[0]); t1.Vertex.Add(vertex[1]); t1.Vertex.Add(vertex[2]); t2.Vertex.Add(vertex[2]); t2.Vertex.Add(vertex[3]); t2.Vertex.Add(vertex[0]); t1.Materials = new List<SceneMaterial>(); t2.Materials = new List<SceneMaterial>(); t1.U = new List<float>(); t2.U = new List<float>(); t1.V = new List<float>(); t2.V = new List<float>(); for (int i = 0; i < 3; i++) { t1.Materials.Add(this.material); t2.Materials.Add(this.material); t1.U.Add(0); t1.V.Add(0); t2.U.Add(0); t2.V.Add(0); } this.Vertex = vertex; }
protected Vector CalculateV(Vector w, Vector u) { Vector v = Vector.Cross3(w, u); return v; }
protected Vector CalculateU(Vector up, Vector w) { Vector u = Vector.Cross3(up, w); u.Normalize3(); return u; }
public bool IsInside(Vector point) { float insideBase = Vector.Dot3(HeightDirection, point - this.BasePoint); float insideEnd = Vector.Dot3(HeightDirection, point - this.EndPoint); if (insideBase < 0 || insideEnd > 0) return false; else return true; }
public Vector PlaneNormal(Vector point, Vector cameraDirection) { Vector normal = this.HeightDirection; float similarity = Vector.Dot3(normal, -cameraDirection); if (similarity < 0) { normal = -normal; } return normal; }
public override Vector SurfaceNormal(Vector point, Vector cameraDirection) { Vector center = Vector.Dot3(point - this.BasePoint, this.HeightDirection) * this.HeightDirection + this.BasePoint; Vector normal = point - center; normal.Normalize3(); float similarity = Vector.Dot3(normal, -cameraDirection); if (similarity < 0) { normal = -normal; } return normal; }
public override Vector SurfaceNormal(Vector point, Vector eyeDirection) { throw new NotImplementedException(); }
public static void DrawPixel(Vector position, Vector color) { Gl.glColor4f(color.x, color.y, color.z, color.w); Gl.glVertex2i((int)position.x, (int)position.y); }
public Vector GetTexturePixelColor(int x, int y) { if (TextureImage == null) return null; Vector color; lock (lockObject) { Color c = TextureImage.GetPixel(x, y); color = new Vector(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f, c.A / 255.0f); } return color; }
public SceneMaterial() { Specular = new Vector(0, 0, 0); Diffuse = new Vector(0, 0, 0); Reflective = new Vector(0, 0, 0); RefractionIndex = new Vector(0, 0, 0); Refractiveness = new Vector(); }
public override Vector SurfaceNormal(Vector point, Vector eyeDirection) { return -1.0f*base.SurfaceNormal(point, eyeDirection); }
public static Vector LightAdd(Vector v1, Vector v2) { Vector result = new Vector(); result.x = Math.Min(1.0f, v1.x + v2.x); result.y = Math.Min(1.0f, v1.y + v2.y); result.z = Math.Min(1.0f, v1.z + v2.z); result.w = 1.0f; return result; }
/// <summary> /// Dot product of a 3-dimensional vector /// </summary> /// <param name="v1"></param> /// <param name="v2"></param> /// <returns></returns> public static float Dot3(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; }
/// <summary> /// Element by element multiplication of vectors. Useful for light calculations. /// </summary> /// <param name="v1"></param> /// <param name="v2"></param> /// <returns></returns> public static Vector ColorMultiplication(Vector v1, Vector v2) { Vector result = new Vector(); result.x = v1.x * v2.x; result.y = v1.y * v2.y; result.z = v1.z * v2.z; result.w = 1.0f; return result; }
public Vector CalculatePixel(int screenX, int screenY) { //Console.WriteLine("Drawing pixel " + screenX + ", " + screenY); //Naturally, we would have to trace rays in order to find collisions. Vector eye = scene.Camera.Position; // Console.WriteLine("Eye: " + eye); Vector target = scene.Camera.Target; // Console.WriteLine("Target: " + target); Vector up = scene.Camera.Up; //Console.WriteLine("up: " + up); //Use radians float fov = (float)Math.PI * (scene.Camera.FieldOfView / 180.0f); float near = scene.Camera.NearClip; //float far = scene.Camera.FarClip; float far = float.MaxValue; Vector w = CalculateW(eye, target); Vector u = CalculateU(up, w); Vector v = CalculateV(w, u); float tanAngle = (float)Math.Tan(fov / 2.0); float top = tanAngle * near; float bottom = -top; float aspectRatio = width / height; float left = aspectRatio * bottom; float right = -left; int subPixelLevel = 2; int cellsPerRow = 2 << (subPixelLevel - 1); double cellWidth = 1.0 / (double)cellsPerRow; Vector averageColor = new Vector(); float totalSamples = cellsPerRow * cellsPerRow; if (renderingParameters.EnableAntialias) { for (int i = 0; i < cellsPerRow; i++) { double startX = screenX + i * cellWidth; for (int j = 0; j < cellsPerRow; j++) { double startY = screenY + j * cellWidth; float sampleX = (float)(startX + GenerateRandom() * cellWidth); float sampleY = (float)(startY + GenerateRandom() * cellWidth); float uCoord = (sampleX + 0.5f) * (right - left) / width + left; float vCoord = (sampleY + 0.5f) * (top - bottom) / height + bottom; float wCoord = -near; //(x,y) -> (u, v, w) //Sij es con respecto a la posición de la cámara Vector Sij = uCoord * u + vCoord * v + wCoord * w; Vector rayStart = eye; Vector rayDirection = Sij; List<SceneLight> lights = scene.Lights; rayDirection.Normalize3(); Ray ray = new Ray(rayStart, rayDirection, w, near, far); float currentTime = (float)GenerateRandom() * renderingParameters.MaxTime; ray.Time = currentTime; averageColor = averageColor + CalculateColor(ray, near, far, 0, 0, 1.0f); } } } else { float uCoord = (screenX + 0.5f) * (right - left) / width + left; float vCoord = (screenY + 0.5f) * (top - bottom) / height + bottom; float wCoord = -near; //(x,y) -> (u, v, w) //Sij es con respecto a la posición de la cámara Vector Sij = uCoord * u + vCoord * v + wCoord * w; Vector rayStart = eye; Vector rayDirection = Sij; List<SceneLight> lights = scene.Lights; rayDirection.Normalize3(); float currentTime = (float)GenerateRandom() * renderingParameters.MaxTime; Ray ray = new Ray(rayStart, rayDirection, w, near, far); ray.Time = currentTime; Vector finalColor = CalculateColor(ray, near, far, 0, 0, 1.0f); return finalColor; } averageColor = averageColor / totalSamples; return averageColor; }
//Interpolation public Vector GetTexturePixelColor(float u, float v) { if (TextureImage == null) return null; Vector color = new Vector(); lock (lockObject) { float x = u * (TextureImage.Width - 1); float y = v * (TextureImage.Height - 1); float left = (float)Math.Floor(x); float right = (float)Math.Ceiling(x); float top = (float)Math.Floor(y); float bottom = (float)Math.Ceiling(y); float deltaX = x - left; float deltaY = y - top; Color c1, c2, c3, c4; c1 = TextureImage.GetPixel((int)left, (int)top); c2 = TextureImage.GetPixel((int)right, (int)top); c3 = TextureImage.GetPixel((int)left, (int)bottom); c4 = TextureImage.GetPixel((int)right, (int)bottom); Vector v1 = new Vector(c1); Vector v2 = new Vector(c2); Vector v3 = new Vector(c3); Vector v4 = new Vector(c4); Vector topColor = v1 * (1 - deltaX) + v2 * deltaX; Vector bottomColor = v3 * (1 - deltaX) + v4 * deltaX; color = topColor * (1 - deltaY) + bottomColor * deltaY; //color = new Vector(c.R / 255.0f, c.G / 255.0f, c.B / 255.0f, c.A / 255.0f); } return color; }
public static Vector Cross3(Vector v1, Vector v2) { Vector crossVec = new Vector(); crossVec.x = v1.y * v2.z - v2.y * v1.z; crossVec.y = v2.x * v1.z - v1.x * v2.z; crossVec.z = v1.x * v2.y - v2.x * v1.y; crossVec.w = 0.0f; return crossVec; }
public override Vector SurfaceNormal(Vector point, Vector eyeDirection) { Vector surfaceNormal = point - this.Center; surfaceNormal.Normalize3(); float similarity = Vector.Dot3(surfaceNormal, -eyeDirection); if (similarity < 0) { surfaceNormal = -surfaceNormal; } return surfaceNormal; }