سؤال

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);
    <...>
}
هل كانت مفيدة؟

المحلول

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.

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