neal.s's picture

Basic OpenGL Concept Check + Text Rendering

Hello!

I'm a student working on a silly little side scrolling vector game. I'm abusing OpenTK fairly thoroughly to produce it, and I'm looking for some feedback on my code, plus a little help with getting text rendering.

For the record, I'm using Visual Studio 2008 on Windows 7.

I'm trying to implement the example for OpenGL text rendering found here. I'm very new to OpenGL and consequently a little confused about where to do what? I'm good with all of the non-OpenGL code, but I don't know where to bind the texture or whether I can draw it in the same Matrix mode as the rest of my vectors.

I have a GameWindow subclass that instantiates a Game object.

In the GameWindow's OnResize event I do the following:

        protected override void OnResize(EventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
            double ratio = Width / (double)Height;
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Matrix4d perspectiveMultiplier = Matrix4d.Perspective(45.0, ratio, 1.0, 64.0);
            GL.MultMatrix(ref perspectiveMultiplier);
            Matrix4d lookatMultiplier = Matrix4d.LookAt(0.0, 0.0, 60.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
            GL.MultMatrix(ref lookatMultiplier);
        }

The GameWindow then renders the GameObjects like so:

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            LinkedList<IGameObject> toRender = game.ToRender;
 
            foreach (IGameObject gameObject in toRender)
            {
                Render(gameObject);
            }
 
            SwapBuffers();
        }

The Render function is described as such:

        private void Render(IGameObject objectToRender)
        {
	    GL.Begin(BeginMode.LineStrip);
            foreach (ColorPoint colorPt in objectToRender.Model.Points)
            {
                GL.Color3(colorPt.PointColor);
                VectorShooterLibrary.Utilities.Point toDraw = new VectorShooterLibrary.Utilities.Point();
                toDraw.X = colorPt.X + objectToRender.Coordinate.X;
                toDraw.Y = colorPt.Y + objectToRender.Coordinate.Y;
                GL.Vertex2(toDraw.X, toDraw.Y);
            }
            GL.End();
        }

Comments

Comment viewing options

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

I'm not very experienced with OpenGL myself (just took a 3 credit-hour course a couple years ago, and haven't actually used OTK yet; but I did use Tao to write the final project for my AS degree), but it looks like you're making a common mistake in your OnResize() method; you change to the projection matrix with GL.MatrixMode(MatrixMode.Projection);, but then don't change back to the model view matrix with GL.MatrixMode(MatrixMode.ModelView); when done. You also want to load the identity matrix (GL.LoadIdentity();) at the beginning of your OnRenderFrame() method, although admittedly it looks like your current code never modifies the matrix, so it should currently work without doing so.

Still haven't messed with textures (or text rendering) myself yet, so I can't help you there!

Of course, there are much better (i.e., faster) ways to render than immediate mode (which is deprecated with OpenGL 3.0, anyway); display lists would be the first thing to look at to improve speed, but VBOs (Vertex Buffer Objects) are the best way to go (I haven't used them myself, yet), IIRC.

Do you have an OpenGL reference book? I found the OpenGL Superbible to be very helpful.