pixartist's picture

Switch from orthographic to 3D view ?!

So I have the following code to draw onto a framebuffer object:

internal void Begin(Color background)
        {
 
            GL.UseProgram(0);
            GL.Viewport(0, 0, Width, Height);
            Material.Textures[0].Surface.BindFramebuffer();
 
            GL.ClearColor(background);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, Width, Height, 0.0, 0.0, 1.0);
 
            if(Background != null)
            {
                Background.BindTexture();
                GL.Begin(PrimitiveType.Quads);
                GL.TexCoord2(0.0f, 1.0f);
                GL.Vertex3(0, 0, 0);
                GL.TexCoord2(0.0f, 0.0f);
                GL.Vertex3(0, Height, 0);
                GL.TexCoord2(1.0f, 0.0f);
                GL.Vertex3(Width, Height, 0);
                GL.TexCoord2(1.0f, 1.0f);
                GL.Vertex3(Width, 0, 0);
                GL.End();
            }
        }
        public void Draw(Sprite sprite)
        {
            float left = sprite.position.X - sprite.size.X * sprite.scale * 0.5f;
            float right = sprite.position.X + sprite.size.X * sprite.scale * 0.5f;
            float top = sprite.position.Y - sprite.size.Y * sprite.scale * 0.5f;
            float bottom = sprite.position.Y + sprite.size.Y * sprite.scale * 0.5f;
            float xAdd = Width / 2.0f;
            float yAdd = Height / 2.0f;
            if(left < X + Width - xAdd && right > X-xAdd && top < Y + Height-yAdd && bottom > Y - yAdd)
            {
                spritesToDraw.Add(sprite);
 
            }
        }
 
        internal void End()
        {
            spritesToDraw.Sort((a,b) => {return (a.z >= b.z ? 1 : -1);});
            float xAdd = Width / 2.0f;
            float yAdd = Height / 2.0f;
            foreach(var s in spritesToDraw)
            {
                s.Draw(-(X-xAdd), -(Y-yAdd));
            }
            spritesToDraw.Clear();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Viewport(vpX, vpY, vpW, vpH);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            GL.Ortho(0, 1.0, 1.0, 0.0, 0.0, 4.0);
 
            Material.Use();
            GL.Begin(PrimitiveType.Quads);
            GL.TexCoord2(0.0f, 1.0f);
            GL.Vertex3(0, 0, 0);
            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex3(0, 1, 0);
            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex3(1, 1, 0);
            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex3(1, 0, 0);
 
            GL.End();
            GL.BindTexture(TextureTarget.Texture2D, 0);
        }

Now I would like to draw the stuff onto my framebuffer with perspective distortion (non-orthographic...). I can't seem to find the correct setup in begin() to do this though. I tried setting up a modelview like this:

(instead of Orthographic in Begin())

var viewMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 4.0), AspectRatio, 0.1f, 4.0f);
 
            GL.LoadMatrix(ref viewMatrix);
            GL.Frustum(0, Width, Height, 0.0, 0.0, 1.0);
            GL.MatrixMode(MatrixMode.Modelview);
            var modelviewMatrix = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
            GL.LoadMatrix(ref modelviewMatrix);

But this is obviously not right. I need the frustum base area to be as big as my orthographic projection and a view in the z-direction.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Try using Matrix4.CreatePerspectiveOffCenter to set the base area explicitly.