2014-03-02 61 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