OpenGL 纹理中的对角线

Posted

技术标签:

【中文标题】OpenGL 纹理中的对角线【英文标题】:Diagonal lines in OpenGL texture 【发布时间】:2013-07-23 12:04:58 【问题描述】:

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

http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads 说这些问题可能是由于对齐错误等引起的,但我已经检查了所有这些东西,一切似乎都很好。

我还检查了花哨的 forloop-construct 实际生成了哪些纹理数据,但一切都很好。

平台:Win7 64 位,OpenTK 1.0 Nightly build(自 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】:

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

【讨论】:

你是对的。问题是我将数据提供给 TexImage2D 的方式。我查看了您的链接并尝试了它。 VS 在我使用指针(如字节*)的行中给出了一个错误,说它只能在不安全的上下文中使用。当我将 fixed 关键字更改为 unsafe 时,VS 说我的程序应该“使用 /unsafe 编译”。我不知道这意味着什么,但后来我尝试了以下操作:将我的数据数组声明为多维 ([,,]) 而不是锯齿状 ([][][])。这允许我直接传递数组,因为存在 TexImage2D(.., T8[,,] 像素) 过载。这解决了它。

以上是关于OpenGL 纹理中的对角线的主要内容,如果未能解决你的问题,请参考以下文章

全屏模糊 GLSL 显示对角线(OpenGL 核心)

Android中涉及OpenGL坐标知识

OpenGL,第一人称相机翻译

方阵对角线中的最小元素?

如何避免比较相关矩阵中的对角线

用对角线索引矩阵中的上三角形或下三角形