public void Tick() { // start timer timer.Restart(); // run the simulation, 1 step inBuffer.CopyToDevice(); kernel.Execute(workSize); outBuffer.CopyFromDevice(); for (int i = 0; i < pw * ph; i++) { _in[i] = 0; } // visualize current state, DRAW FUNCTION -> GPU BONUS. screen.Clear(0); for (uint y = 0; y < screen.height / scale; y++) { for (uint x = 0; x < screen.width / scale; x++) { if (GetBit(x + xoffset, y + yoffset) == 1) { if (scale > 1) { for (uint j = 0; ((j + 1) % scale) != 0; j++) { for (uint i = 0; ((i + 1) % scale) != 0; i++) { screen.Plot((x * scale + i), (y * scale + j), 0xffffff); } } } else { screen.Plot(x, y, 0xffffff); } } } } for (uint y = 0; y < ph; y++) { for (uint x = 0; x < pw * 32; x++) { if (GetBit(x, y) == 1) { BitSet(x, y); } } } string text = "Scale: " + scale + "x"; screen.Print(text, 5, 5, 0xffffff); // report performance Console.WriteLine("generation " + generation++ + ": " + timer.ElapsedMilliseconds + "ms"); }
// TICK // Main application entry point: the template calls this function once per frame. public void Tick() { // start timer timer.Restart(); // run the simulation, 1 step //Simulate(); //patternBuffer = new OpenCLBuffer<uint>(ocl, pattern); //secondBuffer = new OpenCLBuffer<uint>(ocl, second); // visualize current state screen.Clear(0); //for( uint y = 0; y < screen.height; y++ ) for( uint x = 0; x < screen.width; x++ ) // if (GetBit( x + xoffset, y + yoffset ) == 1) screen.Plot( x, y, 0xffffff ); // report performance Console.WriteLine("generation " + generation++ + ": " + timer.ElapsedMilliseconds + "ms"); // do random OCL stuff GL.Finish(); clearKernel.SetArgument(0, image); kernel.SetArgument(0, image); kernel.SetArgument(1, pw); kernel.SetArgument(2, ph); kernel.SetArgument(3, patternBuffer); kernel.SetArgument(4, secondBuffer); // execute kernel long[] workSize = { 512, 512 }; long[] localSize = { 32, 4 }; kernel.LockOpenGLObject(image.texBuffer); clearKernel.Execute(workSize, null); kernel.Execute(workSize, localSize); kernel.UnlockOpenGLObject(image.texBuffer); }
// 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); }
// 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); }
// tick for background surface public void Tick() { screen.Clear(0); KeyboardState state = OpenTK.Input.Keyboard.GetState(); sceneGraph.HandleInput(state); }
public void Tick() { screen.Clear(0); RenderRaycastScene(); RenderDebugProps(); }
// 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(); }
// tick: renders one frame public void Tick() { screen.Clear(0x000000); KeyboardInput(); DrawGridLines(0x008800, 0x004400, 1f, 1f); PlotFunction(0.1f, 0xff0000); PlotPolarFunction(0.01f, 0x0000ff,0,(float)Math.PI*10f); screen.Print(labelFunction(),5,5, 0xff0000); screen.Print(labelPolarFunction(), 5, 25, 0x0000ff); }
// 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; } }
public void Process() { GL.Finish(); screen.Clear(0); timer.Start(); generatie++; // if (generatie % 2 == 1) { // k_sim.SetArgument(0, patroon); // k_sim.SetArgument(1, volgende); // @Todo: dit ook voor de halo kernels // k_copy.SetArgument(0, patroon); // k_copy.SetArgument(1, volgende); // } // else { // k_sim.SetArgument(0, volgende); // k_sim.SetArgument(1, patroon); // k_copy.SetArgument(0, volgende); // k_copy.SetArgument(1, patroon); // } if (GLInterop) { if (resolution != lastResolution) { image = new OpenCLImage <int>(ocl, resolution.x, resolution.y); lastResolution = resolution; k_sim.SetArgument(3, resolution); } k_sim.SetArgument(5, image); k_sim.LockOpenGLObject(image.texBuffer); k_sim.Execute(werk); k_sim.UnlockOpenGLObject(image.texBuffer); } else { k_sim.SetArgument(5, buffer); k_sim.Execute(werk); buffer.CopyFromDevice(); for (int i = 0; i < buffer.Length; ++i) { screen.pixels[i] = buffer[i]; } } k_copy.Execute(werk_klein); // Kopieer wat zojuist in patroon is geschreven naar de tweede buffer timer.Stop(); Console.Write("\r{0}ms", timer.ElapsedMilliseconds); timer.Reset(); }
// 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); }
// Draws pixel on the screen with ability to zoom public void DrawScreenZoom() { // clear the screen screen.Clear(0); // you essentially draw a smaller segment of the field for (uint y = 0; y < screen.height / zoom; y++) { for (uint x = 0; x < screen.width / zoom; x++) { if (GetBit(x + xoffset, y + yoffset) == 1) { // draw a white square per white bit for (uint i = 0; i < zoom; i++) { for (uint j = 0; j < zoom; j++) { screen.Plot(x * zoom + i, y * zoom + j, 0xffffff); } } } } } }
// 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); }
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);
public void Tick() { GL.Finish(); // clear the screen screen.Clear( 0 ); // do opencl stuff if (GLInterop) kernel.SetArgument( 0, image ); else kernel.SetArgument( 0, buffer ); kernel.SetArgument( 1, t ); t += 0.1f; // execute kernel long [] workSize = { 512, 512 }; long [] localSize = { 32, 4 }; if (GLInterop) { // INTEROP PATH: // Use OpenCL to fill an OpenGL texture; this will be used in the // Render method to draw a screen filling quad. This is the fastest // option, but interop may not be available on older systems. // lock the OpenGL texture for use by OpenCL kernel.LockOpenGLObject( image.texBuffer ); // execute the kernel kernel.Execute( workSize, localSize ); // unlock the OpenGL texture so it can be used for drawing a quad kernel.UnlockOpenGLObject( image.texBuffer ); } else { // NO INTEROP PATH: // Use OpenCL to fill a C# pixel array, encapsulated in an // OpenCLBuffer<int> object (buffer). After filling the buffer, it // is copied to the screen surface, so the template code can show // it in the window. // execute the kernel kernel.Execute( workSize, localSize ); // get the data from the device to the host buffer.CopyFromDevice(); // plot pixels using the data on the host for( int y = 0; y < 512; y++ ) for( int x = 0; x < 512; x++ ) { screen.pixels[x + y * screen.width] = buffer[x + y * 512]; } } }
// 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(); }
// TICK // Main application entry point: the template calls this function once per frame. public void Tick() { // start timer timer.Restart(); // run the simulation, 1 step Simulate(); // visualize current state screen.Clear(0); for (uint y = 0; y < screen.height; y++) { for (uint x = 0; x < screen.width; x++) { if (GetBit(x + xoffset, y + yoffset) == 1) { screen.Plot(x, y, 0xffffff); } } } // report performance Console.WriteLine("generation " + generation++ + ": " + timer.ElapsedMilliseconds + "ms"); }
// tick: renders one frame public void Tick() { screen.Clear(0); //Parallel.For is used for Multi- threading //Loop over all pixels in the screen Parallel.For(0, screen.height, (y) => { Parallel.For(0, screen.width, (x) => { var color = new Vector3(); //Black to begin with Vector2 ray_position = new Vector2(TransX(x), TransY(y)); //position on the screen //for each pixel loop over the light sources foreach (Light light in LightList) { //shoot a ray from each pixel to each existing light source var ray = new Ray(ray_position, light.position - ray_position); bool occluded = false; //loop over all primitives that are in the world foreach (IPrimitive p in PrimitiveList) { if (p.Intersect(ray)) { occluded = true; } } if (!occluded) { color += light.color * lightAttenuation(ray.t) * light.brightness; } } screen.Plot(x, y, ToRGB32(color)); }); }); }
public void RenderGL() { screen.Clear(0); application.HandleInput(); application.Update(); }
// tick for background surface public void Tick() { screen.Clear(0); }
// tick: renders one frame public void Tick() { screen.Clear(CreateColor(0, 0, 0)); Debug(); raytracer.Render(); }
public void Tick() { screen.Clear(0); Render(cam, scene, displaySurf); }
// tick for background surface public void Tick() { screen.Clear(0); screen.Print("hello world", 2, 2, 0xffff00); readKey(); }
// 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); }
// tick: renders one frame public void Tick() { screen.Clear(0); a += .01; }
// tick: renders one frame public void Tick() { screen.Clear(0); raytracer.Render(screen); }
// TICK // Main application entry point: the template calls this function once per frame. public void Tick() { GL.Finish(); // start timer timer.Restart(); //Initiate work sizes long[] workSize = { pw, ph }; long[] workSize2 = { pw *ph }; //Set kernel arguments kernel.SetArgument(5, xoffset); kernel.SetArgument(6, yoffset); resolution = (int)(zoom * 512); long[] workSize3 = { resolution / 32, resolution }; kernel.SetArgument(7, resolution); // run the simulation, 1 step screen.Clear(0); if (GLinterop) { if (resolution != oldResolution) { image = new OpenCLImage <int>(ocl, resolution, resolution); oldResolution = resolution; } //set image as argument imageClearKernel.SetArgument(0, image); imageClearKernel.LockOpenGLObject(image.texBuffer); imageClearKernel.Execute(workSize3); imageClearKernel.UnlockOpenGLObject(image.texBuffer); //lock image object kernel.SetArgument(0, image); kernel.LockOpenGLObject(image.texBuffer); //run kernel kernel.Execute(workSize); //unlock image object kernel.UnlockOpenGLObject(image.texBuffer); secondKernel.Execute(workSize2); } else { kernel.SetArgument(0, buffer); for (int i = 0; i < buffer.Length; i++) { buffer[i] = 0; } buffer.CopyToDevice(); kernel.Execute(workSize); secondKernel.Execute(workSize2); buffer.CopyFromDevice(); for (uint y = 0; y < screen.height; y++) { for (uint x = 0; x < screen.width; x++) { screen.Plot(x, y, buffer[x + y * 512]); } } } // visualize current state // report performance Console.WriteLine("generation " + generation++ + ": " + timer.ElapsedMilliseconds + "ms"); //Console.ReadLine(); }
// tick: renders one frame public void Tick() { screen.Clear(0); //move the lights if (dir % 2 == 0) { lightbuffer[0] += TW(0.1f); lightbuffer[5] -= TW(0.1f); lightbuffer[11] += TW(0.1f); lightbuffer[16] -= TW(0.1f); } else { lightbuffer[0] -= TW(0.1f); lightbuffer[5] += TW(0.1f); lightbuffer[11] -= TW(0.1f); lightbuffer[16] += TW(0.1f); } if (TW(worldsize) < lightbuffer[0] || lightbuffer[0] < TX(-worldsize)) { dir++; } Thread t1 = new Thread(() => { plot(TX((-worldsize + (0f / 8f * worldsize * 2))), TX((-worldsize + (1f / 8f * worldsize * 2)))); }); Thread t2 = new Thread(() => { plot(TX((-worldsize + (1f / 8f * worldsize * 2))), TX((-worldsize + (2f / 8f * worldsize * 2)))); }); Thread t3 = new Thread(() => { plot(TX((-worldsize + (2f / 8f * worldsize * 2))), TX((-worldsize + (3f / 8f * worldsize * 2)))); }); Thread t4 = new Thread(() => { plot(TX((-worldsize + (3f / 8f * worldsize * 2))), TX((-worldsize + (4f / 8f * worldsize * 2)))); }); Thread t5 = new Thread(() => { plot(TX((-worldsize + (4f / 8f * worldsize * 2))), TX((-worldsize + (5f / 8f * worldsize * 2)))); }); Thread t6 = new Thread(() => { plot(TX((-worldsize + (5f / 8f * worldsize * 2))), TX((-worldsize + (6f / 8f * worldsize * 2)))); }); Thread t7 = new Thread(() => { plot(TX((-worldsize + (6f / 8f * worldsize * 2))), TX((-worldsize + (7f / 8f * worldsize * 2)))); }); Thread t8 = new Thread(() => { plot(TX((-worldsize + (7f / 8f * worldsize * 2))), (TX((-worldsize + (8f / 8f * worldsize * 2)))) - 1); }); tarr[0] = t1; tarr[1] = t2; tarr[2] = t3; tarr[3] = t4; tarr[4] = t5; tarr[5] = t6; tarr[6] = t7; tarr[7] = t8; for (int td = 0; td < tarr.Length; td++) { tarr[td].Start(); } for (int td = 0; td < tarr.Length; td++) { tarr[td].Join(); } for (int i = 0; i < (screen.width - 1); i++) { for (int j = 0; j < (screen.height - 1); j++) { screen.Plot(i, j, MixColor(MathHelper.Clamp(floatbuffer[i, j][0], 0, 1), MathHelper.Clamp(floatbuffer[i, j][1], 0, 1), MathHelper.Clamp(floatbuffer[i, j][2], 0, 1))); } } primitivesDraw(); }
// TICK // Main application entry point: the template calls this function once per frame. public void Tick() { // start timer timer.Restart(); // run the simulation, 1 step GL.Finish(); // clear the screen screen.Clear(0); // do opencl stuff if (!GLInterop) { kernel.SetArgument(0, buffer); // if we have an even generation, we set the "pattern" variable in opencl // to the patternBuffer and "second" to the secondBuffer // if the generation is odd, we set "pattern" to secondBuffer and // "second" to patternBuffer // So every generation these buffers get swapped, so we don't have to // manually swap them every time if (generation % 2 == 0) { kernel.SetArgument(4, patternBuffer); kernel.SetArgument(5, secondBuffer); } else { kernel.SetArgument(4, secondBuffer); kernel.SetArgument(5, patternBuffer); } } kernel.SetArgument(6, xoffset); kernel.SetArgument(7, yoffset); // execute kernel long[] workSize = { amountOfCells }; if (GLInterop) { // INTEROP PATH: // Use OpenCL to fill an OpenGL texture; this will be used in the // Render method to draw a screen filling quad. This is the fastest // option, but interop may not be available on older systems. // lock the OpenGL texture for use by OpenCL kernel.LockOpenGLObject( image.texBuffer ); // execute the kernel kernel.Execute( workSize, null ); // Wait for the kernel to finish kernel.Finish(); // execute the copy kernel copyKernel.Execute(workSize, null); // wait for the copykernel to finish copyKernel.Finish(); // unlock the OpenGL texture so it can be used for drawing a quad kernel.UnlockOpenGLObject( image.texBuffer ); } else { // NO INTEROP PATH: // Use OpenCL to fill a C# pixel array, encapsulated in an // OpenCLBuffer<int> object (buffer). After filling the buffer, it // is copied to the screen surface, so the template code can show // it in the window. // execute the kernel kernel.Execute( workSize, null ); // get the data from the device to the host buffer.CopyFromDevice(); // plot pixels using the data on the host for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < screenWidth; x++) { int index = x + y * screenWidth; screen.pixels[index] = buffer[index]; } } } // report performance Console.WriteLine("generation " + generation++ + ": " + timer.ElapsedMilliseconds + "ms"); }
// tick: renders one frame public void Tick() { //float t = 21.5f; // initialize timer if (firstFrame) { timer.Reset(); timer.Start(); firstFrame = false; } // handle keys, only when running time set to -1 (infinite) if (runningTime == -1) if (camera.HandleInput()) { // camera moved; restart ClearAccumulator(); } // render if (useGPU) { // add your CPU + OpenCL path here // mind the gpuPlatform parameter! This allows us to specify the platform on our // test system. // note: it is possible that the automated test tool provides you with a different // platform number than required by your hardware. In that case, you can hardcode // the platform during testing (ignoring gpuPlatform); do not forget to put back // gpuPlatform before submitting! GL.Finish(); // clear the screen screen.Clear(0); // do opencl stuff if (GLInterop) { kernel.SetMemoryArgument(0, texBuffer); } else { kernel.SetMemoryArgument(0, buffer); } // execute kernel //long[] workSize = { screen.width, screen.height }; long[] localSize = { 8, 12 }; long [] workSize = { screen.width , screen.height }; if (GLInterop) { List<ComputeMemory> c = new List<ComputeMemory>() { texBuffer }; queue.AcquireGLObjects(c, null); queue.Execute(kernel, null, workSize, localSize, null); queue.Finish(); queue.ReleaseGLObjects(c, null); } else { camera.Update(); gpuCamera = new GPUCamera(camera.pos, camera.p1, camera.p2, camera.p3, camera.up, camera.right, screen.width, screen.height, camera.lensSize); float scale = 1.0f / (float)++spp; kernel.SetValueArgument(2, gpuCamera); kernel.SetValueArgument(3, scale); queue.Execute(kernel, null, workSize, localSize, null); queue.Finish(); // fetch results if (!GLInterop) { queue.ReadFromBuffer(buffer, ref data, true, null); // visualize result for (int y = 0; y < screen.height; y++) for (int x = 0; x < screen.width; x++) { int temp = x + y * screen.width; screen.pixels[temp] = data[temp]; } } } } else { // this is your CPU only path float scale = 1.0f / (float)++spp; Parallel.For(0, dataArray.Length, parallelOptions, (id) => { for (int j = dataArray[id].y1; j < dataArray[id].y2 && j < screen.height; j++) { for (int i = dataArray[id].x1; i < dataArray[id].x2 && i < screen.width; i++) { // generate primary ray Ray ray = camera.Generate(RTTools.GetRNG(), i, j); // trace path int pixelIdx = i + j * screen.width; accumulator[pixelIdx] += Sample(ray, 0); // plot final color screen.pixels[pixelIdx] = RTTools.Vector3ToIntegerRGB(scale * accumulator[pixelIdx]); } } }); } // stop and report when max render time elapsed int elapsedSeconds = (int)(timer.ElapsedMilliseconds / 1000); if (runningTime != -1) if (elapsedSeconds >= runningTime) { OpenTKApp.Report((int)timer.ElapsedMilliseconds, spp, screen); } }