2013-05-14 30 views
-2

我在使用頂點數組創建平面(使用GL_TRIANGLE)時遇到了一些麻煩。我的OpenGL平面有問題

繼承人我的代碼:

float halfW = (getPlaneWidth()/2); 
float halfD = (getPlaneDepth()/2); 

//Generate vertices 
for(int z = halfD; z >= -halfD; z--) 
{ 
    for(int x = -halfW; x <= halfW; x++) 
    { 
     float xV = x * cellWidth; 
     float yV = 0; 
     float zV = z * cellDepth; 

     Vertices.push_back(xV); 
     Vertices.push_back(yV); 
     Vertices.push_back(zV); 

     float xN = xV - getX(); 
     float yN = yV - getY(); 
     float zN = zV - getZ(); 

     setNormals(&xN, &yN, &zN); //Calculate normals 
     Normals.push_back(xN); 
     Normals.push_back(yN); 
     Normals.push_back(zN); 
    } 
} 
//Generate indices 
for(int y = 0; y < getPlaneDepth(); y++) 
{ 
    for(int x = 0; x < getPlaneWidth(); x++) 
    { 
     int curVertex = (x + (y * (getPlaneDepth() + 1))); //Bottom left vertex ID 

     if(curVertex%2 == 0) 
     { 
      Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left 
      Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right 
      Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right 

      Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right 
      Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left 
      Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left 
     } 
     else //reverse triangle 
     { 
      Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right 
      Indices.push_back((x) + (y) * (getPlaneDepth()+1)); //Bottom Left 
      Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left 

      Indices.push_back((x) + (y+1) * (getPlaneDepth()+1)); //Top Left 
      Indices.push_back((x+1) + (y+1) * (getPlaneDepth()+1)); //Top Right 
      Indices.push_back((x+1) + (y) * (getPlaneDepth()+1)); //Bottom Right 
     } 
    } 
} 

的代碼工作正常,如果寬度和深度是相同的,但如果它們是不同的,它搞砸了。

任何人都可以看到問題嗎?

我已經這樣編碼,因此樞軸點位於飛機的中間。

+0

「它擰緊」如何?你試過什麼尺碼?如果寬度或深度是奇數,會發生什麼情況?有助於更具體。 – Grimmy 2013-05-14 00:45:42

回答

1
int curVertex = (x + (y * (getPlaneWidth() + 1))); //Bottom left vertex ID 

    if(curVertex%2 == 0) 
    { 
     Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left 
     Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right 
     Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right 

     Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right 
     Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left 
     Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left 
    } 
    else //reverse triangle 
    { 
     Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right 
     Indices.push_back((x) + (y) * (getPlaneWidth()+1)); //Bottom Left 
     Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left 

     Indices.push_back((x) + (y+1) * (getPlaneWidth()+1)); //Top Left 
     Indices.push_back((x+1) + (y+1) * (getPlaneWidth()+1)); //Top Right 
     Indices.push_back((x+1) + (y) * (getPlaneWidth()+1)); //Bottom Right 
    } 

+0

剛剛看到這個我自己當我在做一些塗鴉時,很高興你得出同樣的結論呵呵 – Split 2013-05-14 01:18:20