Exemple #1
0
 // Tekent een ray op de debug met de line segmenten.
 public void DrawDebugRay(Ray ray, Vector3 color)
 {
     screen.Line(ConverttoDebugX(ray.O.X),
                 ConverttoDebugY(ray.O.Z),
                 ConverttoDebugX((ray.D.X * ray.t) + ray.O.X),
                 ConverttoDebugY((ray.D.Z * ray.t) + ray.O.Z), FixColor(color));
 }
Exemple #2
0
        // tick: renders one frame
        public void Tick()
        {
            keyPress();
            screen.Clear(0);
            // A step
            a= a + 0.01F;

            // These rotate the square
            float x1 = -1, y1 = 1.0f;
            float rx1 = (float)(x1 * Math.Cos(a) - y1 * Math.Sin(a));
            float ry1 = (float)(x1 * Math.Sin(a) + y1 * Math.Cos(a));

            float x2 = 1, y2 = 1.0f;
            float rx2 = (float)(x2 * Math.Cos(a) - y2 * Math.Sin(a));
            float ry2 = (float)(x2 * Math.Sin(a) + y2 * Math.Cos(a));

            float x3 = 1, y3 = -1.0f;
            float rx3 = (float)(x3 * Math.Cos(a) - y3 * Math.Sin(a));
            float ry3 = (float)(x3 * Math.Sin(a) + y3 * Math.Cos(a));

            float x4 = -1, y4 = -1.0f;
            float rx4 = (float)(x4 * Math.Cos(a) - y4 * Math.Sin(a));
            float ry4 = (float)(x4 * Math.Sin(a) + y4 * Math.Cos(a));

            // These translate to screen coordinates and actually draw the square
            screen.Line(TX(rx1), TY(ry1), TX(rx2), TY(ry2), 0xffffff);
            screen.Line(TX(rx2), TY(ry2), TX(rx3), TY(ry3), 0xffffff);
            screen.Line(TX(rx3), TY(ry3), TX(rx4), TY(ry4), 0xffffff);
            screen.Line(TX(rx4), TY(ry4), TX(rx1), TY(ry1), 0xffffff);


        }
Exemple #3
0
	// tick: renders one frame
	public void Tick()
	{
		screen.Clear( 0 );
		screen.Print( "hello world", 2, 2, 0xffffff );
        screen.Line(2, 20, 160, 20, 0xff0000);
            a += 1f / 30;
            Ex3(a);
	}
        public virtual void DrawDebug(Surface screen)
        {
            int x1 = Debug.TX(p.X, screen.width);
            int z1 = Debug.TZ(p.Z, screen.height);
            int x2 = Debug.TX((e1 + p).X, screen.width);
            int z2 = Debug.TZ((e1 + p).Z, screen.height);
            int x3 = Debug.TX((e2 + p).X, screen.width);
            int z3 = Debug.TZ((e2 + p).Z, screen.height);

            screen.Line(x1, z1, x2, z2, Raytracer.VectorToColor(Vector3.One));
            screen.Line(x1, z1, x3, z3, Raytracer.VectorToColor(Vector3.One));
            screen.Line(x2, z2, x3, z3, Raytracer.VectorToColor(Vector3.One));
        }
Exemple #5
0
        // tick: renders one frame
        public void Tick()
        {
			//clear
			screen.Clear(0);
			raytracer.rays.Clear();

			int DEFAULT_COLOR = convertColor(new Vector3(255));

			for (int i = 0; i < SCREEN_SIZE; i++)
			{
				for (int j = 0; j < SCREEN_SIZE; j++)
				{
					//Render Screen
					int location = i + j * SCREEN_SIZE * 2;               
					Vector3 Color = raytracer.Render(i / (SCREEN_SIZE * 1.0f), j / (SCREEN_SIZE * 1.0f));               
					screen.pixels[location] = convertColor(Color); 

					//Debug Screen Default
					screen.pixels[i + SCREEN_SIZE + j * SCREEN_SIZE * 2] = 0;        
				}
			}
            //Debug View
            //Draw Sphere
			foreach(Primitive primitive in raytracer.scene.primitives)
            {
                if(primitive is Sphere)
                {
                    Sphere sphere = (Sphere)primitive;
					for (int degree = 0; degree < 360; degree++)
                    {
						int coordX = (int)(CUBE_SIZE * (sphere.radius * Math.Cos(degree) + sphere.pos.X));
						int coordY = (int)(CUBE_SIZE * (sphere.radius * Math.Sin(degree) - sphere.pos.Z));
                        screen.Plot(coordX+ 768, coordY + 350, convertColor(sphere.Color));                        
                    }
                }                
            }              
			//Draw Camera point                 
			screen.Box((int)raytracer.camera.pos.X + 767, (int)raytracer.camera.pos.Z * -1 + 349, (int)raytracer.camera.pos.X + 769, (int)raytracer.camera.pos.Z * -1 + 351, DEFAULT_COLOR);

            //Draw Rays
			foreach(Ray ray in raytracer.rays)
			{
				float coordX = CUBE_SIZE * (ray.O.X + 40 * ray.D.X);
				float coordY = CUBE_SIZE * (ray.O.Z + 40 * ray.D.Z);            
				screen.Line((int)ray.O.X + 768, (int)ray.O.Z * -1 + 350, (int)coordX + 768, (int)coordY * -1 + 350, DEFAULT_COLOR);
			}
			//Dividing Line
			screen.Line(SCREEN_SIZE, 0, SCREEN_SIZE, SCREEN_SIZE, DEFAULT_COLOR);
            //User Input
			application.UpdateCam(raytracer.camera);         
        }
Exemple #6
0
            GL.CompileShader(ID);
            GL.AttachShader(program, ID);
            Console.WriteLine(GL.GetShaderInfoLog(ID));
        }

        // tick: renders one frame
        public void Tick()
        {
            screen.Clear(0);
            screen.Print("hello world", 2, 2, 0xffffff);
            screen.Line(2, 20, 160, 20, 0xff0000);
            Vierkant();
            DraaiendVierkant();

            Matrix4 M = Matrix4.CreateFromAxisAngle(new Vector3(0, 0, 1), Timer);
            M *= Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), 1.9f);
Exemple #7
0
        public override void debug(Surface screen)
        {
            float  newradius = (float)Math.Sqrt(Radius2 - Location.Y);
            VPoint middle    = Location;

            middle.Y = 0;
            VPoint previousDrawPoint = new VPoint(0, 0, 1);

            previousDrawPoint  = previousDrawPoint.Normalize() * Radius;
            previousDrawPoint += middle;
            for (int i = 1; i < 121; i++)
            {
                VPoint DrawPoint = new VPoint(Game.SinTable[(i * 3) % 360], 0, Game.SinTable[(i * 3 + 90) % 360]);
                DrawPoint  = DrawPoint.Normalize() * Radius;
                DrawPoint += middle;
                int x1, x2;
                x1 = DrawPoint.transform("x");
                x2 = previousDrawPoint.transform("x");
                if (x1 <= 512 && x2 <= 512)
                {
                    screen.Line(x1, DrawPoint.transform("y"), x2, previousDrawPoint.transform("y"), Mat.GetColor(new VPoint(0, 0, 0)).getColor());
                }
                previousDrawPoint = DrawPoint;
            }
        }
Exemple #8
0
        private void RenderRaycastScene()
        {
            int viewportWidth  = screen.width / 2;
            int viewportHeight = screen.height;

            Vector3 origin = camera.GetPosition();
            Vector3 direction;

            for (int x = 0; x < viewportWidth; x++)
            {
                for (int y = 0; y < viewportHeight; y++)
                {
                    direction = camera.GetRayDirection(x / (float)viewportWidth, y / (float)viewportHeight);
                    Ray       r         = new Ray(origin, direction);
                    Intersect intersect = new Intersect();
                    intersect.OriginalRay = r;

                    scene.IntersectWithScene(intersect);

                    screen.Plot(x, y, intersect.Col);
                    if (y == viewportHeight / 2 && x % 32 == 0)
                    {
                        screen.Line(TX(camera.GetPosition().X), TY(camera.GetPosition().Y),
                                    TX(intersect.OriginalRay.distance * direction.X + camera.GetPosition().X),
                                    TY(intersect.OriginalRay.distance * direction.Y + camera.GetPosition().Y), 255 << 8);
                    }
                }
            }
        }
Exemple #9
0
 public void Debug()
 {
     for (int i = 0; i < raytracer.eindpunten.Length; i++)
     {
         screen.Line(((int)Xtrans(raytracer.cameraPosition.X)), ((int)Ytrans(raytracer.cameraPosition.Z)), ((int)Xtrans(raytracer.eindpunten[i].X)), ((int)Ytrans((raytracer.eindpunten[i].Y))), 0xff00ff);// tekent de ray van de camera naar een intersectie
         //screen.Line(((int)Xtrans(raytracer.eindpunten[i].X)), ((int)Ytrans(raytracer.eindpunten[i].Y)), ((int)Xtrans(scene.lightPositie.X)), ((int)Ytrans(scene.lightPositie.Z)),0x00ffff);// tekent de rays van een intersectie naar de lichtbron
     }
     r = scene.radius * (screen.height / 10);
     for (double i = 0.0; i < 360.0; i += 0.1)
     {
         hoek          = i * Math.PI / 180;
         xpositieDebug = (int)(Xtrans((int)scene.sphere1Positie.X) + r * Math.Cos(hoek));
         ypositieDebug = (int)(Ytrans((int)scene.sphere1Positie.Z) + r * Math.Sin(hoek));
         screen.Plot(xpositieDebug, ypositieDebug, CreateColorf(scene.kleur1.X, scene.kleur1.Y, scene.kleur1.Z));
     }
     for (double i = 0.0; i < 360.0; i += 0.1)
     {
         hoek          = i * Math.PI / 180;
         xpositieDebug = (int)(Xtrans((int)scene.sphere2Positie.X) + r * Math.Cos(hoek));
         ypositieDebug = (int)(Ytrans((int)scene.sphere2Positie.Z) + r * Math.Sin(hoek));
         screen.Plot(xpositieDebug, ypositieDebug, CreateColorf(scene.kleur2.X, scene.kleur2.Y, scene.kleur2.Z));
     }
     for (double i = 0.0; i < 360.0; i += 0.1)
     {
         hoek          = i * Math.PI / 180;
         xpositieDebug = (int)(Xtrans((int)scene.sphere3Positie.X) + r * Math.Cos(hoek));
         ypositieDebug = (int)(Ytrans((int)scene.sphere3Positie.Z) + r * Math.Sin(hoek));
         screen.Plot(xpositieDebug, ypositieDebug, CreateColorf(scene.kleur3.X, scene.kleur3.Y, scene.kleur3.Z));
     }
     screen.Plot(((int)Xtrans(raytracer.cameraPosition.X)), ((int)Ytrans(raytracer.cameraPosition.Z)), 0xffffff); // geeft de camera positie
     screen.Plot(((int)Xtrans(scene.lightPositie.X)), ((int)Ytrans(scene.lightPositie.Z)), 0x0fffff);             // geeft de licht positie
 }
Exemple #10
0
        //Draw the ray on the debug.
        public void DrawRay(Surface screen)
        {
            int x1 = Debug.TX(O.X, screen.width);
            int y1 = Debug.TZ(O.Z, screen.height);
            int x2 = Debug.TX((O + D * l).X, screen.width);
            int y2 = Debug.TZ((O + D * l).Z, screen.height);

            screen.Line(x1, y1, x2, y2, 16776960);
        }
Exemple #11
0
 // tick: renders one frame
 public void Tick()
 {
     screen.Clear(0);
     // Drawing the Red Seperation line in the middle   
     screen.Line(screen.width / 2, 0, screen.width / 2, screen.height, 0xff0000);
     screen.Print("X", screen.width - 25, screen.height /2 + 10, 0xffffff);
     screen.Print("Z", screen.width / 4 * 3+ 5, screen.height - 20, 0xffffff);
     // start rendering
     Raytracing();  
 }
Exemple #12
0
        public void debug(Surface screen)
        {
            int x1, x2;

            x1 = Upperleft.transform("x");
            x2 = Upperright.transform("x");
            if (x1 <= 512 && x2 <= 512)
            {
                screen.Line(x1, Upperleft.transform("y"), x2, Upperright.transform("y"), 255255255);
            }
        }
        public override void RenderDebug(Surface surface, T TX, T TY)
        {
            float   angle = (float)Math.PI / 100 * 2;
            Vector2 v1    = new Vector2(radius, 0);
            Vector2 v2    = v1.Rotate(angle);

            v2.Rotate(angle);
            for (float a = 0; a < 99; a += 1)
            {
                surface.Line(TX(v1.X + location.X), TY(v1.Y + location.Y), TX(v2.X + location.X), TY(v2.Y + location.Y), 0xFFFFFF);
                v1 = v2;
                v2 = v2.Rotate(angle);
            }
        }
Exemple #14
0
	// tick: renders one frame
	public void Tick()
	{
        screen.Clear(0);
            // top left corner
            float x1 = -1, y1 = 1.0f;
            float rx1 = (float)(x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha));
            float ry1 = (float)(x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha));

            // top right corner
            float x2 = 1, y2 = 1.0f;
            float rx2 = (float)(x2 * Math.Cos(alpha) - y2 * Math.Sin(alpha));
            float ry2 = (float)(x2 * Math.Sin(alpha) + y2 * Math.Cos(alpha));

            // bottom right corner
            float x3 = 1, y3 = -1.0f;
            float rx3 = (float)(x3 * Math.Cos(alpha) - y3 * Math.Sin(alpha));
            float ry3 = (float)(x3 * Math.Sin(alpha) + y3 * Math.Cos(alpha));

            // bottom left corner
            float x4 = -1, y4 = -1.0f;
            float rx4 = (float)(x4 * Math.Cos(alpha) - y4 * Math.Sin(alpha));
            float ry4 = (float)(x4 * Math.Sin(alpha) + y4 * Math.Cos(alpha));


            int redColor = createColor(255, 50, 50);
            int whiteColor = createColor(255, 255, 255);
            screen.Line(TX(rx1), TY(ry1), TX(rx2), TY(ry2), redColor);
            screen.Line(TX(rx2), TY(ry2), TX(rx3), TY(ry3), redColor);
            screen.Line(TX(rx3), TY(ry3), TX(rx4), TY(ry4), redColor);
            screen.Line(TX(rx4), TY(ry4), TX(rx1), TY(ry1), redColor);

            alpha += .01;

            //draw x and y axes
            screen.Line(TX(minX), TY(0), TX(maxX), TY(0), whiteColor);
            screen.Line(TX(0), TY(minY), TX(0), TY(maxY), whiteColor);

            //draw custom lines here ( y = ax + b, color c)
            int greenColor = createColor(40, 255, 40);
            drawLine(3, 5, greenColor);

            handleKeyPresses();
        }
Exemple #15
0
        public void debug(Surface screen, VPoint endPoint, int type)
        {
            int color = 0;

            switch (type)
            {
            case 0:
                color = 0xFF0000;
                break;

            case 1:
                color = 0x00FF00;
                break;

            case 2:
                color = 0x0000FF;
                break;

            case 3:
                color = 0xFFFFFF;
                break;

            case 4:
                color = 0x888800;
                break;
            }
            // Make sure that the debug output isn't drawn in the scene (in other words, limit the x value to 512, if it exceeds 512 don't draw the line in question)
            int x1, x2;

            x1 = Location.transform("x");
            x2 = endPoint.transform("x");
            if (x1 <= 512 && x2 <= 512)
            {
                screen.Line(x1, Location.transform("y"), x2, endPoint.transform("y"), color);
            }
        }
Exemple #16
0
        Vector3 AugustRay(int x, int y, Vector3 screenpoint, int limit)
        {
            bool      black        = true;
            Vector3   direction    = Vector3.Normalize(screenpoint - cam.position);
            Vector2   screenCam    = returnScreenCoordinates(cam.position);
            Vector3   pixelColor   = Vector3.Zero;
            float     distAtten    = 0;
            Vector3   lightSum     = Vector3.Zero;
            Vector3   baselightSum = Vector3.Zero;
            Vector3   baseSum      = Vector3.Zero;
            Primitive basePrim     = null;
            float     baseDist     = 0;
            Vector3   baseDir      = Vector3.Zero;

            float shortestDistance = 1000;//1000 should be replaced with the length limit of a ray

            //voor deze ray de kortste distance zoeken(zodat dingen achter elkaar niet verschijnen )
            float     distance    = 0;
            Primitive currentPrim = null;

            ClosestPrim(cam.position, direction, 0, out currentPrim, out distance);
            basePrim = currentPrim;
            baseDist = distance;
            baseDir  = direction;


            baselightSum = doehetnou(distance, currentPrim, direction);

            float     recDist = 0;
            Vector3   recDir;
            Primitive recPrim = null;
            Vector3   origin  = cam.position;
            Vector3   recOrg;

            if (currentPrim is Sphere && currentPrim.reflective)
            {
                //bounce and normal debug
                if (y == 0)
                {
                    if (x % (int)(debugMod) == 0)
                    {
                        Vector3 bounce = Bounce(direction, distance, currentPrim as Sphere, cam.position);
                        Vector2 begin  = returnScreenCoordinates((distance * direction) + cam.position);
                        Vector2 eind   = returnScreenCoordinates(bounce + ((distance * direction) + cam.position));

                        Vector2 kamera = returnScreenCoordinates(cam.position);

                        screen.Line((int)begin.X, (int)begin.Y, (int)eind.X, (int)eind.Y, 0xffff00);
                        screen.Line((int)kamera.X, (int)kamera.Y, (int)begin.X, (int)begin.Y, 0xffff00);
                    }
                }

                SecRay(x, y, (distance * direction) + cam.position, Bounce(direction, distance, currentPrim as Sphere, origin), recursionDepth, out recDist, out recPrim, out recDir, out recOrg);

                //if (recDist > 0)
                //    distance = recDist;

                //else
                //{
                //    lightSum += new Vector3(0.1f, 0.1f, 0.1f);
                //}
                currentPrim = recPrim;
                origin      = recOrg;
                direction   = recDir;
            }



            if (distance != 0 && currentPrim != null)
            {
                if (distance < shortestDistance)
                {
                    shortestDistance = distance;
                }


                if (distance * maxDist <= 1)
                {
                    distAtten = 1 - (distance * maxDist);
                }


                Vector3 point     = (distance * direction) - cam.position;
                Vector3 basePoint = (baseDist * baseDir) - cam.position;

                if (y == 0)
                {
                    if (x % (int)(debugMod) == 0)
                    {
                        screenPosition = returnScreenCoordinates(cam.position + direction * shortestDistance);
                        screen.Line((int)screenCam.X, (int)screenCam.Y, (int)screenPosition.X, (int)screenPosition.Y, 0xff0000);
                    }
                }

                //texturing voor de reflectie
                if (currentPrim is Plane)
                {
                    int roundOff = (int)new Vector3((direction * distance) + origin).Z;
                    if (roundOff % 2 == 0)
                    {
                        currentPrim.color = new Vector3(1, 1, 1);
                    }
                    else
                    {
                        currentPrim.color = new Vector3(0.1f, 0.1f, 0.1f);
                    }
                }

                #region main light
                //light and shadows
                foreach (Light l in scene.lights)
                {
                    Vector3 shadowRay = new Vector3(point - l.position);
                    Vector3 baseRay   = new Vector3(basePoint - l.position);

                    Vector3 lightDirection     = Vector3.Normalize(shadowRay);
                    Vector3 baselightDirection = Vector3.Normalize(baseRay);

                    if (currentPrim is Sphere)
                    {
                        Vector3 sphereNormal = Vector3.Normalize(currentPrim.position - ((direction * distance) + cam.position));
                        float   angle        = Vector3.Dot(sphereNormal, lightDirection);
                        if (angle > epsilon)
                        {
                            float distanceAttenuation = 1 - (1 / (shadowRay.Length * shadowRay.Length));
                            // if (!currentPrim.reflective)
                            lightSum += LightSumCalc(l, direction, distance, angle, currentPrim, cam.position);
                            //Console.WriteLine(lightSum);
                            if (y == 0)
                            {
                                if (x % (int)(debugMod) == 0)
                                {
                                    screenPosition = returnScreenCoordinates(cam.position + direction * shortestDistance);
                                    Vector2 shadowRayScreenPosition = returnScreenCoordinates(point + new Vector3(-shadowRay.X, 0, -shadowRay.Z));
                                    //screen.Line((int)screenPosition.X, (int)screenPosition.Y, (int)shadowRayScreenPosition.X, (int)shadowRayScreenPosition.Y, 0x0000ff);
                                }
                            }
                        }
                    }
                    //}

                    //checkt voor plane hoe de schaduwen vallen
                    if (currentPrim is Plane)
                    {
                        float angle = Vector3.Dot((currentPrim as Plane).normal, lightDirection);
                        lightSum += LightSumCalc(l, direction, distance, angle, currentPrim, cam.position);
                    }

                    if (y == 0)
                    {
                        if (x % (int)(debugMod) == 0)
                        {
                            screenPosition = returnScreenCoordinates(cam.position + direction * shortestDistance);
                            Vector2 shadowRayScreenPosition = returnScreenCoordinates(point + new Vector3(-shadowRay.X, 0, -shadowRay.Z));
                            screen.Line((int)screenPosition.X, (int)screenPosition.Y, (int)shadowRayScreenPosition.X, (int)shadowRayScreenPosition.Y, 0x0000ff);
                        }
                    }
                }
                #endregion

                float red = 0, green = 0, blue = 0;

                red   = 255 * (currentPrim.color.X * (lightSum.X * 0.5f + baselightSum.X * 0.5f)) * (basePrim.color.X);
                green = 255 * (currentPrim.color.Y * (lightSum.Y * 0.5f + baselightSum.Y * 0.5f)) * (basePrim.color.Y);
                blue  = 255 * (currentPrim.color.Z * (lightSum.Z * 0.5f + baselightSum.Z * 0.5f)) * (basePrim.color.Z);



                red   = Clamp(red, 0, 255);
                green = Clamp(green, 0, 255);
                blue  = Clamp(blue, 0, 255);


                pixelColor = new Vector3(red, green, blue);
            }


            if (y == 0)
            {
                if (x % (int)(debugMod) == 0)
                {
                    screenPosition = returnScreenCoordinates(cam.position + direction * shortestDistance);

                    Vector2 normalizedPosition = returnScreenCoordinates(cam.position + direction);
                    screen.Line((int)screenCam.X, (int)screenCam.Y, (int)screenPosition.X, (int)screenPosition.Y, 0xffff00);
                    screen.Line((int)screenCam.X, (int)screenCam.Y, (int)normalizedPosition.X, (int)normalizedPosition.Y, 0xff0000);
                }
            }



            for (int i = -2; i < 3; i++)
            {
                screen.Line((int)screenCam.X - 2, (int)screenCam.Y + i, (int)screenCam.X + 2, (int)screenCam.Y + i, 0x00ff00);
            }

            return(pixelColor);
        }
Exemple #17
0
        public void Tick()
        {

            for (int i = 0; i < 512; i++)
            {
                for (int j = 0; j < 512; j++)
                {
                    int location = j + i * 1024;
                    float u = j / 512f;
                    float v = i / 512f;

                    Vector3 floatcolor = tracer.Render(u, v);

                    int intcolor = getIntColor(floatcolor);

                    screen.pixels[location] = intcolor;


                }
            }

            //Debug Screen
            for (int k = 512; k < 1024; k++)
            {
                for (int l = 0; l < 512; l++)
                {
                    int location = k + l * 1024;
                    screen.pixels[location] = 0;
                }
            }

            //Draws every sphere, including its own offset and a bonus offset, so that the spheres don't cling to the edges.
            //Color offset is a cheap solution to give the spheres a different color each time.

            foreach (Sphere sphere in tracer.scene.sphereList)
            {
                int intcolor = getIntColor(sphere.material.color);
                for (int theta = 0; theta < 360; theta++)
                {
                    double xcord = sphere.rad * 5 * Math.Cos(theta);
                    double ycord = sphere.rad * 5 * Math.Sin(theta);
                    int offsetX = 5 * (int)sphere.spherePos.X;
                    int offsetZ = 5 * (int)sphere.spherePos.Z * -1;
                    screen.Plot((int)xcord + offsetX + 750, (int)ycord + offsetZ + 400, intcolor);
                }
            }

            //Draws the camera in the debug view
            screen.Plot((int)tracer.camera.cameraPos.X + 750, (int)tracer.camera.cameraPos.Z * -1 + 400, 0xff0000);

            foreach (Light light in tracer.scene.lightList)
            {
                screen.Plot((int)light.lightPos.X + 750, (int)light.lightPos.Z * -1 + 400, 0xcc66ff);
            }


            //Draws the primary rays in the debug view          
            for (int i = 0; i < tracer.raylist.Count; i++)
            {
                float t = 50;
                foreach (Primitive prim in tracer.scene.sphereList)
                {
                    Intersection intersect = prim.Intersect(tracer.raylist[i]);
                    if (intersect != null)
                    {
                        if (intersect.distance < t)
                        {
                            t = intersect.distance;
                        }
                    }
                }

                float primOx = tracer.raylist[i].Origin.X;
                float primOz = tracer.raylist[i].Origin.Z;

                float primTx = (primOx + t * tracer.raylist[i].Direction.X) * 5;
                float primTz = (primOz + t * tracer.raylist[i].Direction.Z) * 5;

                screen.Line((int)primOx + 750, (int)primOz * -1 + 400, (int)primTx + 750, (int)primTz * -1 + 400, 0xffff00);
            }
            tracer.raylist.Clear();
            

            //Draws a seperation line.
            screen.Line(512, 0, 512, 512, 0xff0000);

            app.MoveCam(tracer.camera);
        }
        // Render: renders one frame
        public void Render()
        {
            screen.Clear(0);
            Vector3 Color;

            //render screens
            for (int y = 0; y < screen.height; y++)
            {
                for (int x = 0; x < screen.width; x++)
                {
                    //Create primary ray
                    Ray ray;
                    ray.t = 30;
                    ray.O = camera.CamPos;
                    ray.D = screen.pos0 + (x * ((screen.pos1 - screen.pos0) / 512.0f)) + (y * ((screen.pos2 - screen.pos0) / 512.0f));
                    //ray normalized direction
                    ray.D = (ray.D - camera.CamPos).Normalized();

                    Intersection nearest    = null;
                    Intersection nearestref = null;

                    foreach (Primitive p in prims)
                    {
                        Intersection overr = p.Intersection(ref ray);

                        if (overr != null)
                        {
                            nearest = overr;
                        }
                    }

                    // if there is an intersection, create a shadow ray
                    if (nearest != null)
                    {
                        //Check if the material is reflective
                        if (nearest.isMirror == false)
                        {
                            Color = CastShadowRay(nearest);
                        }
                        else
                        {
                            Color = CastShadowRay(nearest) * (1 - recursive / 100);
                            //Create reflection ray
                            Ray reflectionRay;
                            reflectionRay.D = ray.D - 2 * nearest.N * (Vector3.Dot(ray.D, nearest.N));
                            reflectionRay.O = nearest.I + reflectionRay.D * 0.0001f;
                            reflectionRay.t = 300;

                            foreach (Primitive p in prims)
                            {
                                Intersection overr = p.Intersection(ref reflectionRay);

                                if (overr != null)
                                {
                                    nearestref = overr;
                                }
                            }

                            Vector3 Color2 = Vector3.Zero;

                            if (nearestref != null)
                            {
                                //check if the nearest reflected object is reflective to
                                if (nearestref.isMirror)
                                {
                                    if (recursive != 100)
                                    {
                                        Color2 = CastShadowRay(nearestref) * (recursive / 100.0f);
                                    }
                                }
                                else
                                {
                                    Color2 = CastShadowRay(nearestref) * (recursive / 100.0f);
                                }
                            }
                            else
                            {
                                Color2 = Vector3.Zero;
                            }

                            Color = Color + Color2;


                            //draw reflectionrays on debug screen.
                            if (x % 20 == 0 && y == screen.height / 2)
                            {
                                screenDebug.Line(CordxTrans(reflectionRay.O.X), CordzTrans(reflectionRay.O.Z), CordxTrans(reflectionRay.D.X * ray.t), CordzTrans(reflectionRay.D.Z * ray.t), 0xff00ff);
                            }
                        }
                    }
                    else
                    {
                        Color = Vector3.Zero;
                    }


                    //plot the correct color on the correct pixel
                    screen.Plot(x, y, Color);

                    //Draw 1 in 10 rays on the debugscreen
                    if (x % 20 == 0 && y == screen.height / 2)
                    {
                        screenDebug.Line(CordxTrans(camera.CamPos.X), CordzTrans(camera.CamPos.Z), CordxTrans(ray.D.X * ray.t + camera.CamPos.X), CordzTrans(ray.D.Z * ray.t + camera.CamPos.Z), 0xffff00);
                    }
                }
            }


            //Draw Debug screen
            //Draw line between screen and debug screen
            screenDebug.Line(0, 0, 0, 1024, 0xffffff);
            //Draw camera as 2 orange lines
            screenDebug.Line(CordxTrans(camera.CamPos.X) - 5, CordzTrans(camera.CamPos.Y) - 1, CordxTrans(camera.CamPos.X) + 5, CordzTrans(camera.CamPos.Y) - 1, 0xffa500);
            screenDebug.Line(CordxTrans(camera.CamPos.X) - 5, CordzTrans(camera.CamPos.Y) + 1, CordxTrans(camera.CamPos.X) + 5, CordzTrans(camera.CamPos.Y) + 1, 0xffa500);
            //Draw screen as a blue line
            screenDebug.Line(CordxTrans(screen.pos1.X), CordzTrans(screen.pos1.Z), CordxTrans(screen.pos2.X), CordzTrans(screen.pos2.Z), 0x00ffff);
            //Draw spheres
            var sphere1 = GetSphere1;

            screenDebug.DrawSphere(sphere1);
            var sphere2 = GetSphere2;

            screenDebug.DrawSphere(sphere2);
            var sphere3 = GetSphere3;

            screenDebug.DrawSphere(sphere3);
        }
Exemple #19
0
 private void DrawLine(float x1, float y1, float x2, float y2, int color)
 {
     screen.Line(TX(x1), TY(y1), TX(x2), TY(y2), color);
 }