CheatCat's picture

[Solved] NeHe Lesson 9 - Cannot see the texture!

Well, I have tried to follow NeHe Lesson 9, but the program don't draw the texture! :( (I tried to draw only one "star", heh!) I use Objarni's TexUtil class instead of the InitGL, LoadGLTextures and LoadBMP functions. What is wrong?

using System;
using System.Drawing;
 
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Audio;
using OpenTK.Math;
using OpenTK.Input;
using OpenTK.Platform;
 
namespace StarterKit
{
    class Game : GameWindow
    {
        // Creates a new TextPrinter to draw text on the screen.
        TextPrinter printer = new TextPrinter(TextQuality.Medium);
        Font sans_serif = new Font(FontFamily.GenericSansSerif, 18.0f);
        int texture = 0;
 
        /// <summary>Creates a 800x600 window with the specified title.</summary>
        public Game()
            : base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
        {
            VSync = VSyncMode.On;
        }
 
        /// <summary>Load resources here.</summary>
        /// <param name="e">Not used.</param>
        public override void OnLoad(EventArgs e)
        {
            TexLib.TexUtil.InitTexturing();
            GL.ClearColor(System.Drawing.Color.CornflowerBlue);
            texture = TexLib.TexUtil.CreateTextureFromFile("image.png");
            GL.Enable(EnableCap.DepthTest);
        }
 
        /// <summary>
        /// Called when your window is resized. Set your viewport here. It is also
        /// a good place to set up your projection matrix (which probably changes
        /// along when the aspect ratio of your window).
        /// </summary>
        /// <param name="e">Contains information on the new Width and Size of the GameWindow.</param>
        protected override void OnResize(ResizeEventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Perspective(45.0, Width / (double)Height, 1.0, 64.0);
        }
 
        /// <summary>
        /// Called when it is time to setup the next frame. Add you game logic here.
        /// </summary>
        /// <param name="e">Contains timing information for framerate independent logic.</param>
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
            if (Keyboard[Key.Escape])
                Exit();
        }
 
        /// <summary>
        /// Called when it is time to render the next frame. Add your rendering code here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            Glu.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
 
            GL.BindTexture(TextureTarget.Texture2D, texture);
            GL.LoadIdentity();
            GL.Translate(0, 0, 15);
 
            GL.Begin(BeginMode.Quads);
              GL.TexCoord2(0, 0); GL.Vertex3(-1, -1, 0);
              GL.TexCoord2(1, 0); GL.Vertex3(1, -1, 0);
              GL.TexCoord2(1, 1); GL.Vertex3(1, 1, 0);
              GL.TexCoord2(0, 1); GL.Vertex3(-1, 1, 0);
           GL.End();
 
            SwapBuffers();
        }
 
        [STAThread]
        static void Main()
        {
            // The 'using' idiom guarantees proper resource cleanup.
            // We request 30 UpdateFrame events per second, and unlimited
            // RenderFrame events (as fast as the computer can handle).
            using (Game game = new Game())
            {
                game.Run(30.0, 0.0);
            }
        }
    }
}

Comments

Comment viewing options

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

CheatCat:

One way of thinking about matrices is to just imagine that they are "local coordinate systems" (LCS). Just picture a x-y-z-origo system somewhere in the world. Then the translation/rotation/scale modifies that "object". All drawing operations coordinates (GL.Vertex for example) are specified in the LCS system. So a translation 50 units right moves the LCS-object 50 units right, then a point at Vertex(0,0,0) draws a point at LCS coordinate (0,0,0) which means (50,0,0) in scene coordinates.

Entropy's picture

Nicely explained, objarni.

If you think of it like that, you can add calls to GL.PushMatirix() and GL.PopMatrix() at the beginning and end of the draw method for each separate entity in your "gameworld". If you do that, the LCS reverts back to your scene coordinate system when GL.PopMatrix() is called (more generally, glPopMatrix() reverts the active OpenGL matrix back to what it was when you last called glPushMatrix() ).

Hope that helps...

objarni's picture

Entropy; thanks. Yes that explanation works well also if you think about Robots like in the Robotech project here on opentk.com, and their moving piecing. The torso is one LCS, the upper arm in another, lower arm yet another, but they are connected parent-to-child. And you use Push/Pop inbetween to "remember" previous LCS. Makes those kind of "hierarchies" simple instead of extremely advanced.