triton's picture

Quad texture mapping is not working...

So I am doing a simple game with OpenGL for my school project. I have been checking out OpenTK for some time now and this seemed like the perfect excuse to play with it. :D

I tried adapting the code I found in a thread here in the forum and the example from OpenTK but I can't get it to work. I am just getting a colored quad on the screen. I am using the image from the OpenTK example, because my image is not a power-of-2 and I wanted to be sure that was not the issue. The OpenTK texture example works fine.

My card is Intel X3100. Vista SP1 with .NET 3.5 SP1. OpenTK 0.9.7.

using System;
using System.Collections.Generic;
 
using System.Drawing;
using System.Drawing.Imaging;
 
using cg;
 
using OpenTK.Graphics;
using OpenTK.Math;
 
namespace Tyrian
{
    class Background : cg.Entity, IDrawListener
    {
        const string bgImage = "data/logo.jpg";
 
        Window _window;
        Bitmap _bitmap;
        int texture;
 
        public Background() : base("bg") { }
 
        public override void Init()
        {
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.Blend);
 
            _window = Application.GetApp().GetWindow();
 
            if (LoadTexture())            
                UploadTexture();            
            else
                throw new System.IO.FileNotFoundException(bgImage);
        }
 
        private bool LoadTexture()
        {
            if (!System.IO.File.Exists(bgImage))
                return false;
 
            _bitmap = new Bitmap(bgImage);
            return true;
        }
 
        private void UploadTexture()
        {
            texture = GL.GenTexture();
 
            BitmapData data = _bitmap.LockBits(new Rectangle(0, 0, _bitmap.Width, _bitmap.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
 
            _bitmap.UnlockBits(data);
 
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        }
 
        public void Draw()
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.BindTexture(TextureTarget.Texture2D, texture);
 
            GL.Begin(BeginMode.Quads);
            {
                GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(0.0f, 0.0f);
                GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(0.0f, 1.0f);
                GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 1.0f); 
                GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 0.0f);
            }
            GL.End();
        }
    }
}

Comments

Comment viewing options

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

Can you provide a screenshot?

Also, try adding a call to GraphicsContext.Assert() in your Init() function, before calling any OpenGL methods.

triton's picture

I've tried adding the assert, but it's the same. The output of the window is just a quad that takes the entire region of the window (this part is how it's supossed to be), but with no texture.

If it's helpful, I could zip the project. I'm really out of options, as I've been trying to debug this all day. :/

puklaus's picture

Hi.

Add

 GL.BindTexture(TextureTarget.Texture2D, texture);

after texture = GL.GenTexture(); line.

And

GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(0.0f, 0.0f);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(0.0f, 1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 1.0f); 
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 0.0f);

check uv coordinates on line 3, maybe it should be GL.TexCoord2(1.0f, 1.0f);

Inertia's picture

There's no projection matrix setup in your code?! The culprit is most likely that you are enabling blending, but the .jpg image has no alpha channel. http://www.opentk.com/doc/chapter/2/opengl/fragment-ops/blending

triton's picture

It's working now. I was missing what puklaus said.

Inertia, I'm gonna check that out. I'm still learning OpenGL basics, so I might be doing something stupid. :)

Thanks everyone for all help.