Search code examples
c#opengltexturesopentk

Texture in OpenGL show as single color


I'm fiddling with OpenGL with the help of OpenTK but I can't display a simple texture.

My main problem is that my rectangle don't display the texture but rather a color from it.

Here is my display loop :

// render graphics
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);


GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, textureId);

GL.Begin(PrimitiveType.Quads);
GL.Vertex2(-1.0f, 1.0f);
GL.Vertex2(1.0f, 1.0f);
GL.Vertex2(1.0f, -1.0f);
GL.Vertex2(-1.0f,-1.0f);
GL.End();

GL.Disable(EnableCap.Texture2D);

game.SwapBuffers();

Solution

  • You need some texture coordinates. Right now it's just using the default (0, 0) texcoord.

    Something like this:

    GL.Begin(PrimitiveType.Quads);
    GL.TexCoord2(0.0f, 0.0f);
    GL.Vertex2(-1.0f, 1.0f);
    GL.TexCoord2(1.0f, 0.0f);
    GL.Vertex2(1.0f, 1.0f);
    GL.TexCoord2(1.0f, 1.0f);
    GL.Vertex2(1.0f, -1.0f);
    GL.TexCoord2(0.0f, 1.0f);
    GL.Vertex2(-1.0f,-1.0f);
    GL.End();