示例#1
0
        private void PopulateVertexBuffer()
        {
            var vertArr = Bodies.SelectMany(gb => gb.Vertices()).ToArray();

            totalNumVerts = vertArr.Length;
            OpenGLUtil.PopulateBuffer(vertexBufferID, vertArr);
        }
示例#2
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();
        }
示例#3
0
        // for debugging
        private void RenderDepthToScreen()
        {
            OpenGLUtil.UseFrameBuffer(0); // now render to the screen
            GL.Viewport(0, 0, 512, 512);  // in the corner
            //GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // clear the screen

            textureProgram.UseProgram(); // use the texture program to display the 2d texture
            textureProgram.SetTexture(openGLLights[1].ShadowMapID);
            textureProgram.EnableAttributes();
            textureProgram.LoadBuffer(vertexBufferID); // load the positions so that it can use them to map to the UV coordinates
            OpenGLUtil.DisableTextureCompare(openGLLights[1].ShadowMapID);
            GL.DrawArrays(PrimitiveType.Triangles, 0, totalNumVerts);
            OpenGLUtil.EnableTextureCompare(openGLLights[1].ShadowMapID);
            textureProgram.DisableAttributes();
        }
示例#4
0
        private void BasicVis_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color.Black);
            GL.Enable(EnableCap.DepthTest);   // enable depth testing
            GL.DepthFunc(DepthFunction.Less); // only accept fragment if it is closer to the camera than whats in there already

            //vertexArrayID = OpenGLUtil.CreateVertexArrayObject();
            //OpenGLUtil.UseVertexArrayObject(vertexArrayID);

            vertexBufferID = OpenGLUtil.CreateBufferObject();
            OpenGLUtil.PopulateBuffer(vertexBufferID, vertexBufferData);

            depthProgram   = new DepthMapProgram();
            textureProgram = new SimpleTextureProgram();

            frameBufferID = OpenGLUtil.CreateFrameBuffer();
            textureID     = OpenGLUtil.CreateDepthTexture(frameBufferID, 2048);
        }
示例#5
0
        private void RenderGraphicsToScreen()
        {
            OpenGLUtil.UseFrameBuffer(0);     // make sure we are rendering to the screen
            GL.Viewport(0, 0, Width, Height); // set view prot to cover full window
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            mainProgram.UseProgram();

            // camera properties
            Matrix4 proj = Matrix4.CreatePerspectiveFieldOfView(Camera.VertFOV, Camera.AspectRatio, Camera.MinRange, Camera.MaxRange); // camera props
            Matrix4 view = Matrix4.LookAt(Camera.Pos.ToGLVector3(), Camera.LookAtPos.ToGLVector3(), Camera.UpDir.ToGLVector3());       // camera state

            mainProgram.SetCameraPosition(Camera.Pos.ToGLVector3());

            // light/shadow properties
            mainProgram.AddLights(openGLLights.ToArray());

            // material properties
            float   shininess     = 50.0f;
            Vector3 specularColor = (Color.White.ToGLVector3());

            mainProgram.SetMaterialProperties(specularColor, shininess);

            mainProgram.EnableAttributes();
            mainProgram.LoadBuffer(vertexBufferID);

            int startIndex = 0;

            foreach (IGraphicalBody body in Bodies)
            {
                // get the model matrix and send it
                Matrix4 scale       = Matrix4.Identity;
                Matrix4 rotation    = Matrix4.CreateFromQuaternion(body.Orientation.ToGLQuaternion());
                Matrix4 translation = Matrix4.CreateTranslation(body.Translation.ToGLVector3());
                Matrix4 model       = scale * rotation * translation;
                mainProgram.SetMVP(model, view, proj);
                mainProgram.SetShadowCasterMVPs(LightSources.Select((l, i) => model * depthViews[i] * depthProjs[i]).ToArray());

                int numVerts = body.Triangles.Length * 3;
                GL.DrawArrays(PrimitiveType.Triangles, startIndex, numVerts);
                startIndex += numVerts;
            }
            mainProgram.DisableAttributes();
        }
示例#6
0
        private void BasicVis_Load(object sender, EventArgs e)
        {
            GL.ClearColor(Color.White);
            GL.Enable(EnableCap.DepthTest);                                                // enable depth testing
            GL.DepthFunc(DepthFunction.Less);                                              // only accept fragment if it is closer to the camera than whats in there already
            GL.Enable(EnableCap.Multisample);                                              // standard AA
            GL.Enable(EnableCap.Blend);                                                    // transparency
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // transparency func

            if (VRFlag)
            {
                WindowState = WindowState.Minimized;
                vrScene.InitGraphics(new GLVRGraphics(vrScene, true)); // always use GL graphics
            }
            else
            {
                WindowBorder    = WindowBorder.Hidden;
                WindowState     = WindowState.Fullscreen;
                CursorVisible   = false;
                Camera.IsLocked = false;
            }

            mainProgram    = new LitMaterialProgram();
            depthProgram   = new DepthMapProgram();
            textureProgram = new SimpleTextureProgram();
            vertexBufferID = OpenGLUtil.CreateBufferObject();

            UpdateBodiesCollection(); // incase any bodies were added/removed before the window loaded
            PopulateVertexBuffer();

            // create and frame buffer and shadow map for each light source if they cast shadows
            // this only occurs once
            foreach (var light in LightSources)
            {
                OpenGLLightSource glLight = light.ToGLLight();
                if (light.CastsDynamicShadows)
                {
                    glLight.FrameBufferID = OpenGLUtil.CreateFrameBuffer();
                    glLight.ShadowMapID   = OpenGLUtil.CreateDepthTexture(glLight.FrameBufferID, ShadowMapSize);
                }
                openGLLights.Add(glLight);
            }
        }
示例#7
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();
            }
        }