2012-12-04 86 views
0

我正在爲一個項目製作統一的顏色球體,我遇到了一個問題。球體運行良好,但是當我嘗試用glColorPointer給它們着色時,它們停止出現。當我調用glGetError時,OpenGL沒有顯示任何錯誤,所以我不知道爲什麼會發生這種情況。使用glColorPointer與glDrawElements結果沒有任何東西被繪製

的代碼來生成頂點,顏色等:

void SphereObject::setupVertices() 
{ 
    //determine the array sizes 
    //vertices per row (+1 for the repeated one at the end) * three for each coordinate 
    //times the number of rows 
    int arraySize = myNumVertices * 3; 
    myNumIndices = (myVerticesPerRow + 1) * myRows * 2; 
    myVertices = new GLdouble[arraySize]; 
    myIndices = new GLuint[myNumIndices]; 
    myNormals = new GLdouble[arraySize]; 
    myColors = new GLint[myNumVertices * 4]; 

    //use spherical coordinates to calculate the vertices 
    double phiIncrement = 360/myVerticesPerRow; 
    double thetaIncrement = 180/(double)myRows; 
    int arrayIndex = 0; 
    int colorArrayIndex = 0; 
    int indicesIndex = 0; 
    double x, y, z = 0; 

    for(double theta = 0; theta <= 180; theta += thetaIncrement) 
    { 
     //loop including the repeat for the last vertex 
     for(double phi = 0; phi <= 360; phi += phiIncrement) 
     { 
      //make sure that the last vertex is repeated 
      if(360 - phi < phiIncrement) 
      { 
       x = myRadius * sin(radians(theta)) * cos(radians(0)); 
       y = myRadius * sin(radians(theta)) * sin(radians(0)); 
       z = myRadius * cos(radians(theta)); 
      } 
      else 
      { 
       x = myRadius * sin(radians(theta)) * cos(radians(phi)); 
       y = myRadius * sin(radians(theta)) * sin(radians(phi)); 
       z = myRadius * cos(radians(theta)); 
      } 

      myColors[colorArrayIndex] = myColor.getX(); 
      myColors[colorArrayIndex + 1] = myColor.getY(); 
      myColors[colorArrayIndex + 2] = myColor.getZ(); 
      myColors[colorArrayIndex + 3] = 1; 

      myVertices[arrayIndex] = x; 
      myVertices[arrayIndex + 1] = y; 
      myVertices[arrayIndex + 2] = z; 

      if(theta <= 180 - thetaIncrement) 
      { 
       myIndices[indicesIndex] = arrayIndex/3; 

       myIndices[indicesIndex + 1] = (arrayIndex/3) + myVerticesPerRow + 1; 

       indicesIndex += 2; 
      } 

      arrayIndex += 3; 
      colorArrayIndex += 4; 
     } 
    } 
} 

和代碼實際呈現的東西

void SphereObject::render() 
{ 
    glPushMatrix(); 
    glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); 
    glEnableClientState(GL_COLOR_ARRAY); 
    glColorPointer(4, GL_INT, 0, myColors); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glVertexPointer(3, GL_DOUBLE, 0, myVertices); 
    glDrawElements(GL_QUAD_STRIP, myNumIndices, GL_UNSIGNED_INT, myIndices); 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_COLOR_ARRAY); 
    glPopClientAttrib(); 
    glPopMatrix(); 
} 

任何和所有幫助將不勝感激。出於某種原因我真的很難過。

+0

你的其他上下文狀態是什麼?你如何處理紋理環境邏輯中的頂點顏色? –

+0

上下文狀態是什麼意思?此外紋理邏輯不涉及,因爲沒有紋理進行。 – Pat

回答

1

對顏色指針使用GL_INT(或任何整數類型)時,它將最大可能的整數值線性映射到1.0f(最大顏色),並將0到0.0f(最小顏色)線性映射。

因此,除非您的RGB和A值在十億以上,否則它們可能會顯示爲全黑(或者如果啓用了透明)。我看到你的alpha = 1,在轉換爲float之後基本上爲零。

+0

啊所以對於標準的0 - 255我需要使用一個字節?我很驚訝,即使在紅皮書中,我也沒有看到這一點。無論哪種方式非常感謝你。 – Pat