dhukke's picture

Loading Texture Problem

So, here is my problem, I'm trying to load a tex, but it's not working, please somebody help me, here is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
 
namespace NeHeLessons
{
    class Lesson11 : GameWindow
    {
        public float[][][] points;				// The Array For The Points On The Grid Of Our "Wave"
        public int wiggle_count = 0;			// Counter Used To Control How Fast Flag Waves
 
        public float xrot = 0.0f;				// X Rotation ( NEW )
        public float yrot = 0.0f;				// Y Rotation ( NEW )
        public float zrot = 0.0f;				// Z Rotation ( NEW )
 
        public uint[] texture = new uint[1];	// Texture array
 
        public bool finished;
 
        protected bool LoadTextures()
        {
            Bitmap image = null;
            string file = "data/lesson06/NeHe.bmp";
            try
            {
                // If the file doesn't exist or can't be found, an ArgumentException is thrown instead of
                // just returning null
                image = new Bitmap(file);
            }
            catch (System.ArgumentException)
            {
                this.finished = true;
            }
            if (image != null)
            {
                image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                System.Drawing.Imaging.BitmapData bitmapdata;
                Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
 
                bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, 
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
 
                GL.GenTextures(1, this.texture);
 
                // Create Linear Filtered Texture
 
                //GL.BindTexture(GL.GL_TEXTURE_2D, this.texture[0]);
                GL.BindTexture(TextureTarget.Texture2D, this.texture[0]);
                //GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Linear);
                //GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
                //GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB, image.Width, image.Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
                GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, image.Width, image.Height, 0, 
                    PixelFormat.Bgr, PixelType.UnsignedByte, bitmapdata.Scan0);
 
 
 
                image.UnlockBits(bitmapdata);
                image.Dispose();
                return true;
            }
            return false;
        }
 
        /// <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">Not used.</param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
 
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
            GL.MatrixMode(MatrixMode.Projection);
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 100.0f);
            GL.LoadMatrix(ref projection);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
        }
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
 
            base.OnRenderFrame(e);
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
 
            float float_x, float_y, float_xb, float_yb;
 
            GL.Translate(0.0f, 0.0f, -12.0f);
 
            GL.Rotate(this.xrot, 1.0f, 0.0f, 0.0f);
            GL.Rotate(this.yrot, 0.0f, 1.0f, 0.0f);
            GL.Rotate(this.zrot, 0.0f, 0.0f, 1.0f);
 
            GL.BindTexture(TextureTarget.Texture2D, this.texture[0]);
 
            GL.Begin(BeginMode.Quads);
            for (int i = 0; i < 44; i++)
            {
                for (int j = 0; j < 44; j++)
                {
                    float_x = (float)i / 44.0f;
                    float_y = (float)j / 44.0f;
                    float_xb = (float)(i + 1) / 44.0f;
                    float_yb = (float)(j + 1) / 44.0f;
 
                    GL.TexCoord2(float_x, float_y);
                    GL.Vertex3(this.points[i][j][0], this.points[i][j][1], this.points[i][j][2]);
 
                    GL.TexCoord2(float_x, float_yb);
                    GL.Vertex3(this.points[i][j + 1][0], this.points[i][j + 1][1], this.points[i][j + 1][2]);
 
                    GL.TexCoord2(float_xb, float_yb);
                    GL.Vertex3(this.points[i + 1][j + 1][0], this.points[i + 1][j + 1][1], this.points[i + 1][j + 1][2]);
 
                    GL.TexCoord2(float_xb, float_y);
                    GL.Vertex3(this.points[i + 1][j][0], this.points[i + 1][j][1], this.points[i + 1][j][2]);
                }
            }
            GL.End();
 
            float hold = 0.0f;
            if (this.wiggle_count == 2)
            {
                for (int j = 0; j < this.points[0].Length; j++)
                {
                    hold = this.points[0][j][2];
                    for (int i = 0; i < this.points.Length - 1; i++)
                    {
                        this.points[i][j][2] = this.points[i + 1][j][2];
                    }
                    this.points[this.points.Length - 1][j][2] = hold;
                }
                this.wiggle_count = 0;
            }
 
            this.wiggle_count++;
 
            this.xrot += 0.3f;
            this.yrot += 0.2f;
            this.zrot += 0.4f;
 
            SwapBuffers();
        }
 
        /// <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>
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
 
            if (Keyboard[Key.Escape])
                Exit();
        }
        public Lesson11() : base(640, 480, GraphicsMode.Default, "Jeff Molofee's GL Code Tutorial ... NeHe '99")
        {
 
    			VSync = VSyncMode.On;
        }
 
        /// <summary>Load resources here.
        /// A general OpenGL initialization function.  Sets all of the initial parameters.
        /// </summary>
        /// <param name="e">Not used.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            LoadTextures();
 
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);		// This Will Clear The Background Color To Black
            GL.ClearDepth(1.0); // Enables Clearing Of The Depth Buffer
            GL.DepthFunc(DepthFunction.Less);      // The Type Of Depth Test To Do
            GL.Enable(EnableCap.DepthTest);        // Enables Depth Testing
            GL.Enable(EnableCap.AlphaTest);       // Enables Alpha Testing
            GL.ShadeModel(ShadingModel.Smooth);    // Enables Smooth Color Shading
 
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 100.0f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
 
            GL.MatrixMode(MatrixMode.Modelview);
 
            this.points = new float[45][][];
            for (int i = 0; i < this.points.Length; i++)
            {
                this.points[i] = new float[45][];
                for (int j = 0; j < this.points[i].Length; j++)
                {
                    this.points[i][j] = new float[3];
                    this.points[i][j][0] = (float)((i / 5.0f) - 4.5f);
                    this.points[i][j][1] = (float)((j / 5.0f) - 4.5f);
                    this.points[i][j][2] = (float)(Math.Sin((((i / 5.0f) * 40.0f) / 360.0f) * Math.PI * 2.0f));
                }
            }
        }
    }
}
Inline Images
the texture did not appears on the grid

Comments

Comment viewing options

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

What was the expected output and what did you actually get? Are you getting an exception? Have you tried calling GL.GetError()?

Even a screenshot might help understand what is wrong.

dhukke's picture

the texture didn't appears on the grid, what am I doing wrong?

the Fiddler's picture

Looks like you are missing a call to GL.Enable(EnableCap.Texture2d). Also, make sure the texture has width and height that is a power of two and change your PixelInternalFormat to Rgba (which is more compatible and likely faster than Rgb).

If it still doesn't work, try running your project with the debug version of OpenTK.dll which will notify you of any OpenGL errors. You can find the debug dll in opentk-1.0/Binaries/OpenTK/Debug.

dhukke's picture

thanks man, I was forgetting the gl.enable method, now everything is working, and thanks for the rgba tip.