0

I'm writing my own rendering engine using OpenTK 1.0 and I'm trying to implement textures now but I have a problem. I'm following this tutorial and instead of a checkerboard-like texture I get this.

http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads says that such problems are probably caused by faulty allignment and things like that but I've checked all these things and everything seems to be fine.

I also checked what texture-data is actually generated by the fancy forloop-construct but its all fine.

Platform: Win7 64-bit, OpenTK 1.0 Nightly build (from march 15 2013), Nvidia GEFORCE 610M-2GB

This is my code so far. At the moment I'm practically doing nothing else then rendering the two quads with the texture and setting camera position etc.

Texture loader:

private int LoadTexture(string file_name)
{
    int width = 64, height = 64;
    byte[][][] checkImage = new byte[height][][];
    for(int a = 0; a < checkImage.Length; a++) {
        checkImage[a] = new byte[width][];
        for(int b = 0; b < checkImage[a].Length; b++) {
            checkImage[a][b] = new byte[4];
        }
    }

    int i, j, c;
    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++) {
            c = (((i & 0x8) == 0 ? 1 : 0) ^ ((j & 0x8) == 0 ? 1 : 0)) * 255;
            checkImage[i][j][0] = (byte) c;
            checkImage[i][j][1] = (byte) c;
            checkImage[i][j][2] = (byte) c;
            checkImage[i][j][3] = (byte) 255;
        }
    }

    GL.Enable(EnableCap.Texture2D);

    //Create a texture object and specify a texture for that object.
    int texture_handle = GL.GenTexture();
    GL.BindTexture(TextureTarget.Texture2D, texture_handle);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Nearest);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMinFilter.Nearest);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToEdge);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToEdge);

    GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
    GL.TexImage2D(TextureTarget.Texture2D,
        0, PixelInternalFormat.Rgba8,
        width, height, 0,
        OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
        PixelType.UnsignedByte,
        ref checkImage[0][0][0]);

    GL.Disable(EnableCap.Texture2D);

    return texture_handle;
}

Rendering:

OnRenderFrame(FrameEventArgs e) {
    <...>
    GL.Enable(EnableCap.Texture2D);
    GL.BindTexture(TextureTarget.Texture2D, texture_handle);
    GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int) TextureEnvMode.Decal);

    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(0.0, 0.0);
    GL.Vertex3(-2.0, -1.0, 0.0);
    GL.TexCoord2(0.0, 1.0);
    GL.Vertex3(-2.0, 1.0, 0.0);
    GL.TexCoord2(1.0, 1.0);
    GL.Vertex3(0.0, 1.0, 0.0);
    GL.TexCoord2(1.0, 0.0);
    GL.Vertex3(0.0, -1.0, 0.0);

    GL.TexCoord2(0.0, 0.0);
    GL.Vertex3(1.0, -1.0, 0.0);
    GL.TexCoord2(0.0, 1.0);
    GL.Vertex3(1.0, 1.0, 0.0);
    GL.TexCoord2(1.0, 1.0);
    GL.Vertex3(2.41421, 1.0, -1.41421);
    GL.TexCoord2(1.0, 0.0);
    GL.Vertex3(2.41421, -1.0, -1.41421);
    GL.End();

    GL.Disable(EnableCap.Texture2D);
    <...>
}
Jupiter
  • 1,421
  • 2
  • 12
  • 31

1 Answers1

2

I think the problem might be to do with the way you pass the byte array to the teximage2d function. The openTK documentation specifies that the data should be in the form of an IntPtr. You can get a pointer to a one dimensional array of bytes by using the code in this post, but i'm not entirely convinced that will work for a three dimensional array. (I guess it depends on how the runtime allocates memory). If it doesn't work, then you could convert your 3D array into a one dimensional array of bytes and try that.

Community
  • 1
  • 1
Slicedpan
  • 4,995
  • 2
  • 18
  • 33
  • You were right. The problem was the way in which I supply my data to TexImage2D. I checked out your link and tried it. VS gave an error at the lines I used the pointer (as in byte*) saying that it can only be used in an unsafe context. When I changed the fixed keyword to unsafe VS said that my program should 'compile with /unsafe'. I have no idea what that means but then I tried the following: declaring my data array as multidimensional ([,,]) instead of jagged ([][][]). This allowed me to pass the array directly because there is an TexImage2D(.., T8[,,] pixels) overload. This fixed it. – Jupiter Jul 23 '13 at 13:37