예제 #1
0
        public void Raytracing()
        {
            // prepare for generic OpenGL rendering
            GL.Enable(EnableCap.DepthTest);
            GL.Disable(EnableCap.Texture2D);
            GL.Clear(ClearBufferMask.DepthBufferBit);
            float FOV =  400;
            // creating a scene
            Scene Scene1 = new Scene();

            // Adding axis in the Debug view
            Scene1.toggleaxis(true);
            if (Scene1.Axis == true)
            {
                GL.Color4(1f, 1f, 1f, 1f);
                GL.Begin(PrimitiveType.Lines);
                GL.Vertex2(0.5f, 1); GL.Vertex2(0.5f, -1f);
                GL.Vertex2(0, 0); GL.Vertex2(1, 0);
                GL.End();
            }
            // Adding main objects to the scene
            Sphere Sphere1 = new Sphere(new Vector3(200, 0, 200), 100f, new Vector3(255,0,0), aspectratio);
            Sphere Sphere2 = new Sphere(new Vector3(-200, 0, 200), 100f, new Vector3(0, 0, 255), aspectratio);
            Sphere Sphere3 = new Sphere(new Vector3(-100, 0, 200), 100f, new Vector3(0, 255,0), aspectratio);
            Camera mainCamera = new Camera(new Vector3(0,0,-400), new Vector3(0,0,1), FOV);
            Light LightSource1 = new Light(new Vector3(0, 200, 200), 255, 255, 0);

            // adding light source(s) to the list
            Scene1.LightList.Add(LightSource1);

            //adding primitives to te primitive list 
            Scene1.PrimitivesList.Add(Sphere1);
            Scene1.PrimitivesList.Add(Sphere3);
            Scene1.PrimitivesList.Add(Sphere2);

            // draw every primitive in the list
            foreach (Sphere item in Scene1.PrimitivesList)
            {
                Scene1.DrawCircle(DebugConverter(item.Position), item.Color, item.Radius, screen);
            }

            // start raytracing
            Raytracer raytracer = new Raytracer();
            raytracer.Render(mainCamera, Scene1, screen);

            for (int renderx = 0; renderx < 512; renderx++)
            {
                for (int rendery = 0; rendery < 512; rendery++)
                {
                    int pixel = renderx + rendery * 512;
                    screen.Plot(renderx, rendery, (int)raytracer.Image[pixel]);
                }
            }
            Raytr = raytracer;
        }
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            // called once per frame; render
            raytracer.Render();
            if (terminated)
            {
                Exit();
                return;
            }
            // convert Game.screen to OpenGL texture
            GL.BindTexture(TextureTarget.Texture2D, screenID);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, raytracer.screen.width, raytracer.screen.height, 0,
                          OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, raytracer.screen.pixels);
            // clear window contents
            GL.Clear(ClearBufferMask.ColorBufferBit);
            // setup camera
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            // draw screen filling quad
            GL.Begin(PrimitiveType.Quads);

            GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-1.0f, -1.0f);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0f, -1.0f);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0f, 1.0f);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-1.0f, 1.0f);
            GL.End();


            // convert Game.screen to OpenGL texture
            GL.BindTexture(TextureTarget.Texture2D, screenIDDebug);
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, raytracer.screenDebug.width, raytracer.screenDebug.height, 0,
                          OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, raytracer.screenDebug.pixels);
            // clear window contents
            // setup camera
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            // draw screen filling quad

            GL.Begin(PrimitiveType.Quads);
            GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(0f, -1.0f);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(1.0f, -1.0f);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 1.0f);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(0f, 1.0f);
            GL.End();


            // tell OpenTK we're done rendering
            SwapBuffers();
        }
예제 #3
0
	        // tick: renders one frame
	        public void Tick()
	        {
            //calls the render method within raytracer, debug is also rendered (called from within raytracer.render)
                raytracer.Render();
                handleKeyPresses();

                if (handledPress)
                {
                    screen.Clear(0);
                    handledPress = false;
                }
	        }
예제 #4
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);         
        }
예제 #5
0
        // tick: renders one frame
        public void Tick()
        {
            // Handle keyboard input. Arrow keys move the camera, WASD is used for turning the camera, LShift and Enter move the camera up and down
            var keyboard = OpenTK.Input.Keyboard.GetState();

            if (keyboard[OpenTK.Input.Key.W])
            {
                Tracer.Camera.turnCamera(-0.1f * Tracer.Camera.YDirection);
            }
            if (keyboard[OpenTK.Input.Key.A])
            {
                Tracer.Camera.turnCamera(-0.1f * Tracer.Camera.XDirection);
            }
            if (keyboard[OpenTK.Input.Key.S])
            {
                Tracer.Camera.turnCamera(0.1f * Tracer.Camera.YDirection);
            }
            if (keyboard[OpenTK.Input.Key.D])
            {
                Tracer.Camera.turnCamera(0.1f * Tracer.Camera.XDirection);
            }
            if (keyboard[OpenTK.Input.Key.Enter])
            {
                Tracer.Camera.moveCamera(-0.1f * Tracer.Camera.YDirection);
            }
            if (keyboard[OpenTK.Input.Key.LShift])
            {
                Tracer.Camera.moveCamera(0.1f * Tracer.Camera.YDirection);
            }
            if (keyboard[OpenTK.Input.Key.Left])
            {
                Tracer.Camera.moveCamera(-0.1f * Tracer.Camera.XDirection);
            }
            if (keyboard[OpenTK.Input.Key.Right])
            {
                Tracer.Camera.moveCamera(0.1f * Tracer.Camera.XDirection);
            }
            if (keyboard[OpenTK.Input.Key.Up])
            {
                Tracer.Camera.moveCamera(0.1f * Tracer.Camera.Orientation);
            }
            if (keyboard[OpenTK.Input.Key.Down])
            {
                Tracer.Camera.moveCamera(-0.1f * Tracer.Camera.Orientation);
            }
            Screen.Clear(0);
            Screen.Print("Tracer", 2, 2, 0xffffff);
            Tracer.Render(Debugging);
        }
예제 #6
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            // called once per frame; app logic
            var keyboard = OpenTK.Input.Keyboard.GetState();

            if (keyboard[OpenTK.Input.Key.Escape])
            {
                this.Exit();
            }
            if (keyboard[Key.Left]) //beweeg het scherm naar links
            {
                rayTracer.CameraX -= 1; rayTracer.camera.CameraTransform(-1, 0, 0); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.Right]) //beweeg het scherm naar rechts
            {
                rayTracer.CameraX += 1; rayTracer.camera.CameraTransform(1, 0, 0); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.Up]) //beweeg het scherm naar boven (Z- as)
            {
                rayTracer.CameraZ -= 1; rayTracer.camera.CameraTransform(0, 0, 1); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.Down]) //beweeg het scherm naar beneden (Z- as)
            {
                rayTracer.CameraZ += 1; rayTracer.camera.CameraTransform(0, 0, -1); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.KeypadPlus]) //beweeg het scherm naar boven (Y-as)
            {
                rayTracer.camera.CameraTransform(0, 1, 0); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.KeypadMinus]) //beweeg het scherm naar beneden (Y-as)
            {
                rayTracer.camera.CameraTransform(0, -1, 0); rayTracer.screen.Clear(0);
            }
            if (keyboard[Key.Enter]) //druk op Enter om het beeld te renderen
            {
                rayTracer.Render();
            }
        }
예제 #7
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);
        }
예제 #8
0
 // tick: renders one frame
 public void Tick()
 {
     screen.Clear(CreateColor(0, 0, 0));
     Debug();
     raytracer.Render();
 }
예제 #9
0
파일: game.cs 프로젝트: DvanLaar/Grafisch
 // tick: renders one frame
 public void Tick()
 {
     screen.Clear(0);
     raytracer.Render(screen);
 }