Exemplo n.º 1
0
        private void BasicVis_RenderFrame(object sender, FrameEventArgs e)
        {
            OpenGLUtil.UseFrameBuffer(frameBufferID);                                  // render to our custom framebuffer
            GL.Viewport(0, 0, 1024, 1024);                                             // render on teh entire framebuffer

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // clear the screen
            depthProgram.UseProgram();                                                 // use the depth program

            // camera properties
            Matrix4 proj  = Matrix4.CreateOrthographic(100, 100, 0, 100);
            Matrix4 view  = Matrix4.LookAt(new Vector3(4, 3, 3), Vector3.Zero, new Vector3(0, 1, 0));
            Matrix4 model = Matrix4.Identity;

            depthProgram.SetMVP(model * view * proj);

            depthProgram.EnableAttributes();
            depthProgram.LoadBuffer(vertexBufferID);

            GL.DrawArrays(PrimitiveType.Triangles, 0, vertexBufferData.Length); // here the fragment shader will automatically write the depth to the texture bc of location 0
            depthProgram.DisableAttributes();

            OpenGLUtil.UseFrameBuffer(0);                                              // now render to the screen
            GL.Viewport(0, 0, Width, Height);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // clear the screen

            textureProgram.UseProgram();                                               // use the texture program to display the 2d texture
            textureProgram.SetTexture(textureID);
            textureProgram.EnableAttributes();
            textureProgram.LoadBuffer(vertexBufferID); // load the positions so that it can use them to map to the UV coordinates
            GL.DrawArrays(PrimitiveType.Triangles, 0, vertexBufferData.Length);
            textureProgram.DisableAttributes();

            SwapBuffers();
        }
Exemplo n.º 2
0
        private void RenderLightMapDepths()
        {
            foreach (var light in openGLLights)
            {
                Matrix4 depthProj = default(Matrix4);
                Matrix4 depthView = default(Matrix4);

                if (light.FrameBufferID == -1 || light.ShadowMapID == -1) // no dynamic shadows
                {
                    depthProjs.Add(depthProj);
                    depthViews.Add(depthView);
                    continue;
                }

                if (light.IsDirectional)
                {
                    // ortho view from light (directional)
                    var upDir = Vector3.UnitZ;
                    if (Vector3.Cross(upDir, light.Pos.Xyz).LengthSquared == 0)
                    {
                        upDir = Vector3.UnitY;                                                         // up can be any direction except the look at direction
                    }
                    depthProj = Matrix4.CreateOrthographic(FarClip, FarClip, -FarClip / 2, 2 * FarClip);
                    depthView = Matrix4.LookAt(FarClip * light.Pos.Xyz, Vector3.Zero, upDir);
                }
                else
                {
                    // will look shitty for a point light
                    // projective view from light (spotlight)
                    var upDir = Vector3.UnitZ;
                    if (Vector3.Cross(upDir, light.ConeDir).LengthSquared == 0)
                    {
                        upDir = Vector3.UnitY;                                                                                     // up can be any direction except the look at direction
                    }
                    depthProj = Matrix4.CreatePerspectiveFieldOfView(light.ConeAngle * 2, 1.0f, Camera.MinRange, Camera.MaxRange); // uses camera specs to guide percision
                    depthView = Matrix4.LookAt(light.Pos.Xyz, light.Pos.Xyz + light.ConeDir, upDir);
                }

                OpenGLUtil.UseFrameBuffer(light.FrameBufferID);                            // use our custom framebuffer instead of the screen
                GL.Viewport(0, 0, ShadowMapSize, ShadowMapSize);                           // render on teh entire framebuffer
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // clear the framebuffer

                depthProgram.UseProgram();
                depthProgram.EnableAttributes();
                depthProgram.LoadBuffer(vertexBufferID);

                int startIndex = 0;
                foreach (IGraphicalBody body in Bodies)
                {
                    Matrix4 scale       = Matrix4.Identity;
                    Matrix4 rotation    = Matrix4.CreateFromQuaternion(body.Orientation.ToGLQuaternion());
                    Matrix4 translation = Matrix4.CreateTranslation(body.Translation.ToGLVector3());
                    Matrix4 model       = scale * rotation * translation;
                    depthProgram.SetMVP(model * depthView * depthProj);

                    int numVerts = body.Triangles.Length * 3;
                    GL.DrawArrays(PrimitiveType.Triangles, startIndex, numVerts);  // here the fragment shader will automatically write the depth to the texture bc of location 0
                    startIndex += numVerts;
                }

                depthProgram.DisableAttributes();

                depthProjs.Add(depthProj);
                depthViews.Add(depthView);
                OpenGLUtil.CheckInvalidFrameBuffer();
            }
        }