سؤال

I am having issues drawing a texture onto my quad but it remains white. I have looked through a number of guides and I don't seem to be doing anything different from them.

To load the texture:

        Bitmap bitmap = new Bitmap("Textures/Sprite_Can.png");

        GL.GenTextures(1, out textureID);
        GL.BindTexture(TextureTarget.Texture2D, textureID);

        BitmapData data = bitmap.LockBits(new System.Drawing.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.OpenGL.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);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

Setup and apply an orthographic projection:

        GL.MatrixMode(MatrixMode.Projection);

        GL.LoadIdentity();

        GL.Ortho(0, control.Width, 0, control.Height, -1, 1);
        GL.Viewport(0, 0, control.Width, control.Height);  

        GL.MatrixMode(MatrixMode.Modelview);

        GL.LoadIdentity();

        GL.ClearColor(Color4.CornflowerBlue);

And finally the draw:

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);


        GL.LoadIdentity();

        GL.Translate(30, 30, 0);
        GL.BindTexture(TextureTarget.Texture2D, textureID);

        GL.Begin(BeginMode.Quads);
        GL.TexCoord2(0, 0);
        GL.Vertex2(-1 * width / 2, 1 * height / 2);

        GL.TexCoord2(1, 0);
        GL.Vertex2(1 * width / 2, 1 * height / 2);

        GL.TexCoord2(1, 1);
        GL.Vertex2(1 * width / 2, -1 * height / 2);

        GL.TexCoord2(0, 1);
        GL.Vertex2(-1 * width / 2, -1 * height / 2);
        GL.End();

        GL.Flush();
        control.SwapBuffers();

So basically, the quad draws just fine. However, the texture is not rendered. As a result, all I have is just a white square.

هل كانت مفيدة؟

المحلول

In the fixed-function OpenGL pipeline, you must also Enable texture units before a texture bound to one will be applied to anything you draw.

The normal OpenGL API binding for this is glEnable (GL_TEXTURE_2D). The OpenTK equivalent would be: GL.Enable (EnableCap.Texture2D).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top