OpenGL纹理中的对角线

时间:2013-07-23 12:04:58

标签: c# opengl textures opentk

我正在使用 OpenTK 1.0 编写自己的渲染引擎,我正在尝试实现纹理,但我遇到了问题。 我正在关注this教程,而不是类似棋盘格的纹理,我得到this

http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads说这些问题可能是由错误的分配和类似的事情引起的,但我检查了所有这些事情,一切似乎都很好。

我还检查了花式forloop-construct实际生成了什么纹理数据,但它一切都很好。

平台:Win7 64位,OpenTK 1.0每晚构建(2013年3月15日起),Nvidia GEFORCE 610M-2GB

到目前为止,这是我的代码。目前我几乎没有做任何事情,然后用纹理和设置相机位置等渲染两个四边形。

纹理加载器:

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;
}

渲染:

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);
    <...>
}

1 个答案:

答案 0 :(得分:2)

我认为问题可能与将字节数组传递给teximage2d函数的方式有关。 openTK文档指定数据应采用IntPtr的形式。你可以使用this post中的代码获得指向一维字节数组的指针,但我并不完全相信这对三维数组有效。 (我想这取决于运行时如何分配内存)。如果它不起作用,那么你可以将你的3D数组转换为一维字节数组并尝试。