
Problem drawing 2D on 3D
Posted Friday, 6 January, 2012 - 11:19 by Random inI'm trying to draw something (just for testing, a square right now) after drawing 3D objects. I've also followed some tutorials around the web, but something probably gone wrong. Here's my code, I hope you can help me! :)
using System; using System.Drawing; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; namespace Julia3D { class JuliaWindow : GameWindow { public Camera camera; private float x = 0.0f, z = 0.0f; public JuliaWindow () : base (800, 600, GraphicsMode.Default) { Title = "Julia Window Test - " + GL.GetString (StringName.Version); VSync = VSyncMode.On; Keyboard.KeyRepeat = true; camera = new Camera (); } protected override void OnLoad (EventArgs e) { base.OnLoad (e); GL.ClearColor (Color.Black); GL.Enable (EnableCap.DepthTest); } protected override void OnResize (EventArgs e) { base.OnResize (e); GL.Viewport (ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView (MathHelper.PiOver4, Width / (float) Height, 1.0f, 64.0f); GL.MatrixMode (MatrixMode.Projection); GL.LoadMatrix (ref projection); } protected override void OnRenderFrame (FrameEventArgs e) { base.OnRenderFrame (e); GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView (MathHelper.PiOver4, Width / (float) Height, 1.0f, 64.0f); GL.MatrixMode (MatrixMode.Projection); GL.LoadMatrix (ref projection); camera.Render (); GL.Translate (0.0f, -0.5f, -6.0f); GL.Scale (3.0f, 1.0f, 3.0f); float size = 5.0f; int linesX = 30; int linesZ = 30; float halfsize = size / 2.0f; GL.Color3 (Color.White); GL.PushMatrix (); { GL.Translate (0.0f, -halfsize, 0.0f); Utils.DrawNet (size, linesX, linesZ); GL.Translate (0.0f, size, 0.0f); Utils.DrawNet (size, linesX, linesZ); } GL.PopMatrix (); GL.Color3 (Color.Blue); GL.PushMatrix (); { GL.Translate (-halfsize, 0.0f, 0.0f); GL.Rotate (90.0f, 0.0f, 0.0f, halfsize); Utils.DrawNet (size, linesX, linesZ); GL.Translate (0.0f, -size, 0.0f); Utils.DrawNet (size, linesX, linesZ); } GL.PopMatrix (); GL.Color3 (Color.Red); GL.PushMatrix (); { GL.Translate (0.0f, 0.0f, -halfsize); GL.Rotate (90.0f, halfsize, 0.0f, 0.0f); Utils.DrawNet (size, linesX, linesZ); GL.Translate (0.0f, size, 0.0f); Utils.DrawNet (size, linesX, linesZ); } GL.PopMatrix (); GL.Scale (1.0f / 3.0f, 1.0f, 1.0f / 3.0f); GL.Translate (x, 0.0f, z); GL.Color3 (Color.Navy); Utils.DrawSphere (1.0, 100, 100); Utils.DrawCube (1.0f); // Finish rendering GL.Flush (); SetupOrtho (); SwapBuffers (); } private void SetupOrtho () { GL.Disable (EnableCap.DepthTest); GL.MatrixMode (MatrixMode.Projection); GL.PushMatrix (); GL.LoadIdentity (); GL.Ortho (0, Width, Height, 0, -1, 1); GL.MatrixMode (MatrixMode.Modelview); GL.PushMatrix (); GL.LoadIdentity (); Draw2D (); GL.MatrixMode (MatrixMode.Projection); GL.PopMatrix (); GL.MatrixMode (MatrixMode.Modelview); GL.PopMatrix (); GL.LoadIdentity (); GL.Enable (EnableCap.DepthTest); } private void Draw2D () { GL.Translate (10, 10, 0); GL.Color3 (Color.Olive); GL.Begin (BeginMode.Quads); { GL.Vertex3 (0, 0, 0); GL.Vertex3 (0, 100, 0); GL.Vertex3 (100, 100, 0); GL.Vertex3 (100, 0, 0); } GL.End (); } [STAThread] static void Main (string[] args) { using (JuliaWindow game = new JuliaWindow ()) { game.Run (30.0); } } } }
The 3D part is displayed correctly, but I can't see the square being drawn.
Thanks in advance,
Random.

