c# - 立方体上的 opentk 多个纹理不起作用

标签 c# opentk

所以在我的程序中有一个绘制立方体的函数。我在 linux 上使用 c# 和 monodevelop。这是函数:

private int DrawCube(float x, float y, float z, float ori, int SideTexture, int TopTexture, int BottomTexture)
{
    GL.PushMatrix();

    GL.Translate(x, y, z);
    GL.Rotate(ori, 0, 1, 0);

    GL.BindTexture(TextureTarget.Texture2D, SideTexture);

    GL.Begin(BeginMode.Quads);

    GL.Color3(Color.White);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, 0);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, 0);
    //Top
    //GL.ActiveTexture(TextureUnit.Texture0);
    GL.BindTexture(TextureTarget.Texture2D, TopTexture);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, 0);
    //Bottom
    GL.BindTexture(TextureTarget.Texture2D, BottomTexture);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 0, 0);
    GL.BindTexture(TextureTarget.Texture2D, SideTexture);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, 0);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);

    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 20, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 20, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);

    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);

    GL.End();
    GL.PopMatrix();

return 6;   // Return number of faces drawn
}

正如您可能已经猜到的那样,此函数绘制了一个位置为 x, y, z 的立方体。方向 ori 和侧面的 SideTexture 以及顶部和底部的 TopTexture 和 BottomTexture。 现在的问题是它只用一种纹理绘制立方体!侧面纹理。 我不知道是什么问题。我必须取消绑定(bind)纹理吗? 代码中的其他一切都工作正常,正如我所说的那样,纹理已经存在这种麻烦。感谢您的帮助。

最佳答案

您不能在 begin-end 区域内调用 GL.BindTexture()From the documentation :

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

如果您检查 GL.GetError(),您将看到您收到 InvalidOperation 错误。事实上,GL.GetError() 应该是您在 OpenGL 中未按预期呈现某些内容时的第一 react 。

解决方法:

private int DrawCube(float x, float y, float z, float ori, int SideTexture, int TopTexture, int BottomTexture)
{
    GL.PushMatrix();

    GL.Translate(x, y, z);
    GL.Rotate(ori, 0, 1, 0);

    GL.BindTexture(TextureTarget.Texture2D, SideTexture);
    GL.Begin(BeginMode.Quads);
    GL.Color3(Color.White);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, 0);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, 0);
    GL.End();

    //Top
    GL.BindTexture(TextureTarget.Texture2D, TopTexture);
    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, 0);
    GL.End();

    //Bottom
    GL.BindTexture(TextureTarget.Texture2D, BottomTexture);
    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 0, 0);
    GL.End();

    GL.BindTexture(TextureTarget.Texture2D, SideTexture);
    GL.Begin(BeginMode.Quads);
    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, 0);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);

    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(0, 20, 0);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(20, 20, 0);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);

    GL.TexCoord2(0.0f, 1.0f - 0.0f); GL.Vertex3(20, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 0.0f); GL.Vertex3(0, 0, -20);
    GL.TexCoord2(1.0f, 1.0f - 1.0f); GL.Vertex3(0, 20, -20);
    GL.TexCoord2(0.0f, 1.0f - 1.0f); GL.Vertex3(20, 20, -20);
    GL.End();

    GL.PopMatrix();

    return 6;   // Return number of faces drawn
}

关于c# - 立方体上的 opentk 多个纹理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125271/

相关文章:

opengl - OpenGL 中未使用颜色数据 "Hello world"

c# - 有什么方法可以让端点在带有 C# 的 WebAPI 中使用泛型?

c# - 使用 Linq 和 Lambda 包含默认值

c# - Linq-to-Entities 中的 IDENT_CURRENT

c# - 在 System.Data.Sqlite (.net) 中启用共享缓存模式

c# - 无法在屏幕保护程序的 DLL 'opengl32.dll' 中找到入口点

c# - 如何根据用户选择动态注入(inject)用户控件

c# - 如何将 SDL 视频嵌入现有 OpenGL 项目

c# - vTextCoord 值不起作用 - c# OPENTK

c# - 如何将光标锁定到游戏窗口?