2012-03-19 24 views
0

私はhereと呼ばれる手続き球を描こうとしています。 SetupGLでOpenGL ES 2.0でglDrawElementsで三角形を描くことができません

GLfloat sphereVerticies[10000]={0.0}; 
GLubyte triangleIndices[15000]={0}; 

int createSphere (GLfloat spherePoints[], GLubyte triangleIndices[], GLfloat fRadius, GLfloat step) 
{ 
    int points = 0; 

    GLfloat uStep = DEGREES_TO_RADIANS (step); 
    GLfloat vStep = uStep; 

    unsigned long index=0; 

    for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep) 
    { 
     for (GLfloat v = -M_PI_2; v <= M_PI_2; v += vStep) 
     {    
      triangleIndices[index++]=points; 
      triangleIndices[index++]=points+1; 
      triangleIndices[index++]=points+2; 
      triangleIndices[index++]=points+2; 
      triangleIndices[index++]=points+3; 
      triangleIndices[index++]=points; 

      points++; 
      spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u);    // x 
      spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u);  // y 
      spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);     // z 

      points++; 
      spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u + uStep);    // x 
      spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u + uStep);  // y 
      spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);       // z    

      points++; 
      spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u);     // x 
      spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u);   // y 
      spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);      // z 

      points++; 
      spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u + uStep);   // x 
      spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u + uStep);  // y 
      spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);      // z 

     } 
    }   
    return points; 
} 

私が持っている:

.. 
glEnable(GL_CULL_FACE); 
.. 
.. 
glGenVertexArraysOES(1, &_vertexArray); 
glBindVertexArrayOES(_vertexArray); 

numPoints=createSphere(sphereVerticies, triangleIndices, 30.0f, 20.0f); 

    glGenBuffers(1, &_vertexBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(sphereVerticies), sphereVerticies, GL_STATIC_DRAW); 

    glGenBuffers(1, &_indexBuffer); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW); 

    // New lines (were previously in draw) 
    glEnableVertexAttribArray(GLKVertexAttribPosition);   
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies); 

glBindVertexArrayOES(0); 

私は、OpenGL ESのglDrawElementsメソッドを使用できるように

は私が2.0

ここcreateSphereの私のバージョンです、それを少し修正しました最後にdrawInRect:

glBindVertexArrayOES(_vertexArray); 
    glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0); 

ここで私は何が間違っていますか?私は視覚的な出力を見ません。どんな助けでも大歓迎です。ありがとう。

答えて

6

私は二つのことを見ることができます:頂点はすでにバッファリングとVBOにバインドされているので、

glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies);

最後の引数は、0があるはずです。

OpenGL ES - glVertexAttribPointer documentation

非ゼロという名前のバッファ・オブジェクトがGL_ARRAY_BUFFERの標的に結合している場合(glBindBufferを参照)、一般的な頂点属性配列が指定されている間、ポインタがバッファ・オブジェクトのデータへのオフセットバイトとして扱われます店...

そして、

glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0);

頂点数が255を超えるため、GL_UNSIGNED_SHORTを使用する必要があります。インデックスのよう


、これはどのようにインデックスにそれらである。この図から、我々は二つの問題見ることができるように

    (0,1,2)  (2,3,0) 
2  3  2    2  3 
o-------o  o    o-------o 
|  |  | \   | /
|  | => | \   | /
|  |  |  \  |/
o-------o  o-------o  o 
0  1  0  1  0 

、:)

1の三角形はクローズアップしていませんポリゴン

2)巻線に注意してください。最初の三角はCCW、2番目のCWです。

は、このコードを試してみてください:二つの問題を指摘して

 triangleIndices[index++]=points; 
     triangleIndices[index++]=points+1; 
     triangleIndices[index++]=points+2; 
     triangleIndices[index++]=points+3; 
     triangleIndices[index++]=points+2; 
     triangleIndices[index++]=points+1; 
+0

おかげで、今では、いくつかの出力を示していますが、それはまだ正しくありません。各交互三角形の法線は反対である。だから私は図面の断片化を見る。 triangleIndices配列に値を代入する方法に何か問題はありますか? また、より良い描画方法を提案できれば幸いです。上記の方法は、同じサイズのすべての矩形を描画するのではなく、球体が三角形が小さいN極とS極を持っています。テクスチャリングに問題が生じる可能性があります。 –

+0

更新された回答を参照してください。 – arul

+0

よろしくお願いします。それで十分だった。 –

関連する問題