2014-03-02 27 views
0

所以在我的程序中有一个绘制多维数据集的功能。我正在使用c#与monodevelop在Linux上。下面是函数:opentk多维数据集上的纹理不起作用

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。 现在的问题是,它只用一个纹理绘制立方体!侧面纹理。 我不知道是什么问题。我必须解除纹理绑定吗? 代码中的其他所有内容都可以正常工作,正如我所说已经存在这种对纹理造成的妨碍。任何帮助表示赞赏。

+0

哦,请吗?没有人? –

回答

2

您无法在开始区域内拨打GL.BindTexture()From the documentation

只有GL命令的子集可在glBegin和glEnd之间使用。这些命令是glVertex,glColor,glSecondaryColor,glIndex, glNormal,glFogCoord,glTexCoord,glMultiTexCoord,glVertexAttrib, glEvalCoord,glEvalPoint,glArrayElement,glMaterial和glEdgeFlag。 另外,可以使用glCallList或glCallLists来执行 只包含上述命令的显示列表。 如果在glBegin和glEnd之间执行任何其他的 GL命令,则错误标志为 set,并且该命令被忽略。

如果检查GL.GetError(),你会看到你得到一个InvalidOperation错误。事实上,GL.GetError()应该是你的第一反应,当某些东西在OpenGL中没有像预期的那样渲染时。

解决办法:

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 
} 
+0

+1谢谢! :-) –

1

哦,是一个对于那些谁,这可能是有帮助的我竟与纹理使用的反面。 topTexture真的附在底部,e.t.c.这是工作代码。 代码从路径上传质地:

static public int UploadTexture(string pathname) 
{ 
    // Create a new OpenGL texture object 
    int id = GL.GenTexture(); 

    // Select the new texture 
    GL.BindTexture(TextureTarget.Texture2D, id); 

    // Load the image 
    Bitmap bmp = new Bitmap(pathname); 

    // Lock image data to allow direct access 
    BitmapData bmp_data = bmp.LockBits(
      new Rectangle(0, 0, bmp.Width, bmp.Height), 
      System.Drawing.Imaging.ImageLockMode.ReadOnly, 
      System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

    // Import the image data into the OpenGL texture 
    GL.TexImage2D(TextureTarget.Texture2D, 
        0, 
        PixelInternalFormat.Rgba, 
        bmp_data.Width, 
        bmp_data.Height, 
        0, 
        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, 
        OpenTK.Graphics.OpenGL.PixelType.UnsignedByte, 
        bmp_data.Scan0); 

    // Unlock the image data 
    bmp.UnlockBits(bmp_data); 

    // Configure minification and magnification filters 
    GL.TexParameter(TextureTarget.Texture2D, 
      TextureParameterName.TextureMinFilter, 
      (int)TextureMinFilter.Linear); 
    GL.TexParameter(TextureTarget.Texture2D, 
      TextureParameterName.TextureMagFilter, 
      (int)TextureMagFilter.Linear); 

    // Return the OpenGL object ID for use 
    return id; 
} 

和代码与纹理

private int DrawCube(float x, float y, float z, float ori, int TopTexture, int BottomTexture, params int[] SideTextures) 
     { 
      GL.PushMatrix(); 

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

      GL.BindTexture(TextureTarget.Texture2D, SideTextures[0]); 
      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, SideTextures[1]); 
      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, SideTextures[2]); 
      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.End(); 
      GL.BindTexture(TextureTarget.Texture2D, TopTexture); 
      GL.Begin(BeginMode.Quads); 
      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.End(); 
      GL.BindTexture(TextureTarget.Texture2D, SideTextures[3]); 
      GL.Begin(BeginMode.Quads); 
      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 
     } 
     protected override void OnResize(EventArgs e) 
     { 
      int w = Width; 
      int h = Height; 
      float aspect = 1; 

      // Calculate aspect ratio, checking for divide by zero 
      if (h > 0) 
      { 
       aspect = (float)w/(float)h; 
      } 

      // Initialise the projection view matrix 
      GL.MatrixMode(MatrixMode.Projection); 
      GL.LoadIdentity(); 

      // Setup a perspective view 
      float FOVradians = MathHelper.DegreesToRadians(45); 
      Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(FOVradians, aspect, 1, 4000); 
      GL.MultMatrix(ref perspective); 

      // Set the viewport to the whole window 
      GL.Viewport(0, 0, w, h); 
     } 

和refrences画立方体,你将不得不使用:

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using OpenTK; 
using OpenTK.Graphics.OpenGL; 

编辑: 在新版本的OpenTK我相信你将不得不使用PrimitiveType.Quad我nstead BeginMode.Quad