// initialize public void Init() { scene = new SceneGraph(); scene.Init(); // initialize stopwatch timer = new Stopwatch(); timer.Reset(); timer.Start(); }
// render cubemap public void RenderCubemap(Matrix4 parentMatrix, SceneGraph scene) { Matrix4 finalTransform = localScale * localRotation * localTranslation * parentMatrix; if (cubemap != null) { Vector3 transform = finalTransform.Row3.Xyz; cubemap.Render(programValues.cubemapshader, transform, scene, this); } foreach (ParentMesh p in child_meshes) { p.RenderCubemap(finalTransform, scene); } }
// render depthmap public void Render(Shader shader, SceneGraph scene) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, id); GL.Viewport(0, 0, programValues.depthmapres, programValues.depthmapres); GL.Enable(EnableCap.DepthTest); GL.Clear(ClearBufferMask.DepthBufferBit); GL.CullFace(CullFaceMode.Front); scene.SimpleRender(camera, position, shader); GL.CullFace(CullFaceMode.Back); GL.Disable(EnableCap.DepthTest); GL.Viewport(0, 0, programValues.screenwidth, programValues.screenheight); }
public void Render(Shader shader, Vector3 transform, SceneGraph scene) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.TextureCubeMap, id); Matrix4 cameraPosition = Matrix4.CreateTranslation(-transform); GL.Viewport(0, 0, programValues.cubedepthmapres, programValues.cubedepthmapres); GL.Enable(EnableCap.DepthTest); GL.CullFace(CullFaceMode.Front); for (int i = 0; i < 6; ++i) { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.TextureCubeMapPositiveX + i, id, 0); GL.Clear(ClearBufferMask.DepthBufferBit); scene.SimpleRender(cameraPosition * cameraRotations[i] * cameraFOV, cameraPosition, programValues.depthcubemapshader); } GL.CullFace(CullFaceMode.Back); GL.Viewport(0, 0, programValues.screenwidth, programValues.screenheight); }
// render cubemap public void Render(Shader shader, Vector3 transform, SceneGraph scene, ParentMesh parent) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.TextureCubeMap, id); Matrix4 cameraPosition = Matrix4.CreateTranslation(-transform); GL.Viewport(0, 0, programValues.cubemapres, programValues.cubemapres); GL.Enable(EnableCap.DepthTest); for (int i = 0; i < 6; ++i) { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, id, 0); GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.DepthBufferBit); GL.ClearColor(0, 0, 0, 0); scene.RenderSkyBox(cameraPosition, cameraRotations[i], cameraFOV, programValues.skyboxshader); scene.SimpleRender(cameraPosition * cameraRotations[i] * cameraFOV, cameraPosition, programValues.cubemapshader, parent); } GL.Disable(EnableCap.DepthTest); GL.Viewport(0, 0, programValues.screenwidth, programValues.screenheight); }
// initialize public void Init() { sceneGraph = new SceneGraph(); // load a texture wood = new Texture("../../assets/wood.jpg"); wood1 = new Texture("../../assets/wood1.jpg"); metal = new Texture("../../assets/metal.jpg"); rust = new Texture("../../assets/rust.jpg"); blue = new Texture("../../assets/blue.jpg"); yellow = new Texture("../../assets/yellow.jpg"); // load meshes Tpot = new Mesh("../../assets/teapot.obj", new Vector3(0, 0, 0), new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0, 0, 0), new List <Mesh>(), wood); Tpot2 = new Mesh("../../assets/teapot.obj", new Vector3(0, 0, 0), new Vector3(0.5f, 0.5f, 0.5f), new Vector3(5f, 0, 0), new List <Mesh>(), blue); Tpot3 = new Mesh("../../assets/teapot.obj", new Vector3(0, 0, 0), new Vector3(0.5f, 0.5f, 0.5f), new Vector3(-5f, 6f, 0), new List <Mesh>(), wood1); Tfloor = new Mesh("../../assets/floor.obj", new Vector3(0, 0, 0), new Vector3(4.0f, 4.0f, 4.0f), new Vector3(0, 0, 0), new List <Mesh>(), rust); Tfloor1 = new Mesh("../../assets/floor.obj", new Vector3(1, 0, 0), new Vector3(4.0f, 4.0f, 4.0f), new Vector3(0, 0, -25f), new List <Mesh>(), rust); Tfloor2 = new Mesh("../../assets/floor.obj", new Vector3(0, 1, 0), new Vector3(4.0f, 4.0f, 4.0f), new Vector3(25f, 0, 0), new List <Mesh>(), metal); Tfloor3 = new Mesh("../../assets/floor.obj", new Vector3(1, 1, 0), new Vector3(4.0f, 4.0f, 4.0f), new Vector3(0, -1f, 0), new List <Mesh>(), yellow); //Add created meshes to the hierarchy tree sceneGraph.addPrimaryChild(Tpot); sceneGraph.addPrimaryChild(Tfloor); sceneGraph.addPrimaryChild(Tpot2); //Tpot.addChild(Tpot2); Tpot.addChild(Tpot3); Tfloor.addChild(Tfloor1); Tfloor.addChild(Tfloor2); Tfloor.addChild(Tfloor3); // initialize stopwatch timer = new Stopwatch(); timer.Reset(); timer.Start(); }
// tick for OpenGL rendering code public void RenderGL() { // measure frame duration float frameDuration = timer.ElapsedMilliseconds; timer.Reset(); timer.Start(); // prepare matrix for vertex shader float angle90degrees = PI / 2; Vector3 cameraPos = new Vector3(0, 50f, 0); int cameraID = GL.GetUniformLocation(shader.programID, "cameraPos"); GL.UseProgram(shader.programID); GL.Uniform3(cameraID, cameraPos); Matrix4 Tcar = Matrix4.CreateScale(0.5f) * Matrix4.CreateRotationY(10f) * Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateFromAxisAngle(new Vector3(0, 1, 0), 9 + 0) * Matrix4.CreateTranslation(new Vector3(0, 0, 0)); Matrix4 Tflag = Matrix4.CreateScale(0.08f) * Tcar * Matrix4.CreateTranslation(new Vector3(0, 2, 0)); Matrix4 toWorld = Tflag; Matrix4 Tfloor = Matrix4.CreateScale(4.0f) * Matrix4.CreateFromAxisAngle(new Vector3(0, 1, 0), 0); Matrix4 Tcamera = Matrix4.CreateTranslation(-cameraPos) * Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), angle90degrees); Matrix4 Tview = Matrix4.CreatePerspectiveFieldOfView(1.2f, 1.3f, .1f, 1000); // defining the model view matrix for the mesh object flag.modelViewMatrix = Tflag * Tcamera * Tview; flag.toWorld = toWorld; car.modelViewMatrix = Tcar * Tcamera * Tview; car.toWorld = toWorld; floor.modelViewMatrix = Tfloor * Tcamera * Tview; floor.toWorld = toWorld; SceneGraph floor_sg = new SceneGraph(floor); floor_sg.pos = new Vector3(0, 0, 0); floor_sg.rotation = a; floor_sg.scale = new Vector3(4, 2, 2); // update rotation a += 0.0005f * frameDuration; if (a > 2 * PI) { a -= 2 * PI; } b = a * a; if (a > 6 * PI) { a -= 10 * PI; } if (useRenderTarget) { // enable render target target.Bind(); // render scene via Scenegraph floor_sg.Render(Tcamera * Tview); // render scene to render target /* * flag.Render(shader, Tflag * Tcamera * Tview, toWorld, dark); * car.Render( shader, Tcar*Tcamera*Tview, toWorld, silver); * floor.Render( shader, Tfloor * Tcamera * Tview, toWorld, wood ); */ // render quad target.Unbind(); quad.Render(postproc, target.GetTextureID()); } else { // render scene via SceneGraph floor_sg.Render(Tcamera); // render scene directly to the /* * flag.Render(shader, Tflag * Tcamera * Tview, toWorld, dark); * car.Render(shader, Tcar * Tcamera * Tview, toWorld, silver); * floor.Render(shader, Tfloor * Tcamera * Tview, toWorld, wood); */ } }
// initialize public void Init() { programValues.screenwidth = screen.width; programValues.screenheight = screen.height; timer = new Stopwatch(); timer.Reset(); timer.Start(); // create shaders shader = new Shader("../../shaders/vs.glsl", "../../shaders/fs.glsl"); postproc = new Shader("../../shaders/vs_post.glsl", "../../shaders/fs_post.glsl"); rendershader = new Shader("../../shaders/vs_render.glsl", "../../shaders/fs_render.glsl"); //rendershader = new Shader( "../../shaders/vs_render.glsl", "../../shaders/fs_renderDepthmap.glsl" ); programValues.skyboxshader = new Shader("../../shaders/vs_skybox.glsl", "../../shaders/fs_skybox.glsl"); programValues.cubemapshader = new Shader("../../shaders/vs_cubemap.glsl", "../../shaders/fs_cubemap.glsl"); programValues.depthmapshader = new Shader("../../shaders/vs_depthmap.glsl", "../../shaders/fs_depthmap.glsl"); programValues.depthcubemapshader = new Shader("../../shaders/vs_depthmap.glsl", "../../shaders/fs_depthcubemap.glsl"); cameraPosition = new Matrix4(); cameraRotation = Matrix4.CreateRotationX((float)(0.5 * Math.PI)); cameraFOV = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI * 0.5), screen.width / screen.height, 0.01f, 1000); //create scene scene = new SceneGraph(); // create standard framebuffer framebuffer = GL.GenFramebuffer(); GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); // create standard renderbuffer renderbuffer = GL.GenRenderbuffer(); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent, screen.width, screen.height); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, renderbuffer); // create standard texture colorbuffer = new int[2]; GL.GenTextures(2, colorbuffer); for (int i = 0; i < 2; ++i) { GL.BindTexture(TextureTarget.Texture2D, colorbuffer[i]); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba32f, screen.width, screen.height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, new int[] { (int)All.Nearest }); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, new int[] { (int)All.Nearest }); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, TextureTarget.Texture2D, colorbuffer[i], 0); } DrawBuffersEnum[] attachments = new DrawBuffersEnum[2] { DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1 }; GL.DrawBuffers(2, attachments); if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete) { Console.WriteLine("Framebuffer not set up correctly"); } hdrFramebuffer = new int[2]; hdrColorbuffer = new int[2]; // create post processing GL.GenFramebuffers(2, hdrFramebuffer); GL.GenTextures(2, hdrColorbuffer); for (int i = 0; i < 2; ++i) { GL.BindFramebuffer(FramebufferTarget.Framebuffer, hdrFramebuffer[i]); GL.BindTexture(TextureTarget.Texture2D, hdrColorbuffer[i]); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba32f, programValues.screenwidth, programValues.screenheight, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, hdrColorbuffer[i], 0); } if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete) { Console.WriteLine("HDRFramebuffer not set up correctly"); } // create draw quad float[] vertices = new float[] { -1f, -1f, 1f, -1f, -1f, 1f, -1f, 1f, 1f, -1f, 1f, 1 }; float[] texCoords = new float[] { 0, 0, 1f, 0, 0, 1f, 0, 1f, 1f, 0, 1f, 1f }; //create buffers quadVertexbuffer = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, quadVertexbuffer); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0); quadTexbuffer = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, quadTexbuffer); GL.BufferData(BufferTarget.ArrayBuffer, texCoords.Length * sizeof(float), texCoords, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 0, 0); }