2012-03-19 6 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의 내 버전의 IT를 조금 수정 그리고 마지막으로 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이 있어야합니다. 비제라는 버퍼 개체가 GL_ARRAY_BUFFER 표적에 결합되면

OpenGL ES - glVertexAttribPointer documentation

(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이고 두 번째 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 배열에 값을 할당하는 방법에 문제가 있습니까? 더 좋은 그림 그리기 방법을 제안 해 주시면 고맙겠습니다. 위의 방법은 같은 크기의 모든 직사각형을 그리지 않고, 구형에는 삼각형이 더 작은 북극과 남극이 있습니다. 텍스처링에 문제가 발생할 수 있습니다. –

+0

업데이트 된 답변보기 및 – arul

+0

알겠습니다. 그 정도면 충분했습니다. –

관련 문제