2011-10-31 4 views
1

OpenGL에서 GL_TRIANGLES를 사용하여 구를 렌더링하려고합니다. 다음 코드로 무엇을 얻었는지에 대한 이미지입니다. Bad Sphere구형 정점 연결하기 OpenGL

이것은 단위 구로 추정됩니다.

내가 여기 wikipedia.

에서 기본 구 근사치에서 정점을 유도 한 나는 가 알려 주시기 바랍니다 .. 단위 영역을 렌더링하기 위해 만든 코드가 어디

void createGreenSphere(mat4 modelView){ 
    std::vector<Vertex> v; 
    int numSphereSlices = 12; 
    int numSphereSegments = 12; 
    float theta = 0; 
    float phi = 0; 
    float phiDelt = (2*PI)/numSphereSegments; 
    float thetaDelt = PI/numSphereSlices; 
    float* vertices = new float[numSphereSlices*numSphereSegments*4]; 
    float* normals = new float[numSphereSlices*numSphereSegments*4]; 
    float* colors = new float[numSphereSlices*numSphereSegments*3]; 
    int colorCnt = 0; 
    int vertCnt = 0; 
    for(int heightCnt = 0; heightCnt < numSphereSlices; heightCnt++){ 
    theta += thetaDelt; 
    phi = 0; 
    for(int widthCnt = 0; widthCnt < numSphereSegments; widthCnt++){ 
     phi += phiDelt; 
     vertices[vertCnt] = sin(theta)*cos(phi); 
     normals[vertCnt] = vertices[vertCnt]; 
     vertCnt++; 
     vertices[vertCnt] = sin(theta)*sin(phi); 
     normals[vertCnt] = vertices[vertCnt]; 
     vertCnt++; 
     vertices[vertCnt] = cos(theta); 
     normals[vertCnt] = vertices[vertCnt]; 
     vertCnt++; 
     vertices[vertCnt] = 1.0; 
     normals[vertCnt] = vertices[vertCnt]; 
     vertCnt++; 
     colors[colorCnt] = 0.0; 
     colorCnt++; 
     colors[colorCnt] = 1.0; 
     colorCnt++; 
     colors[colorCnt] = 0.0; 
     colorCnt++; 
    } 
    } 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glBufferData(GL_ARRAY_BUFFER, vertCnt-1 * sizeof(float), vertices, GL_STATIC_DRAW);             
    glBindBuffer(GL_ARRAY_BUFFER, cbo); 
    glBufferData(GL_ARRAY_BUFFER, colorCnt-1 * sizeof(float), colors, GL_STREAM_DRAW);  

    glBindBuffer(GL_ARRAY_BUFFER, nbo); 
    glBufferData(GL_ARRAY_BUFFER, vertCnt-1 * sizeof(float), normals, GL_STATIC_DRAW); 

    unsigned short* indices = new unsigned short[numSphereSlices*numSphereSegments*6]; 
    int indexCnt = 0; 
    for (int i=0;i<numSphereSlices;i++){ 
    for(int j=0;j<numSphereSegments;j++){ 
     indices[indexCnt] = j + numSphereSegments*i; 
     indexCnt++; 
     indices[indexCnt] = j+1 + numSphereSegments*i; 
     indexCnt++; 
     indices[indexCnt] = numSphereSegments+j + numSphereSegments*i; 
     indexCnt++; 
     indices[indexCnt] = numSphereSegments+j+1 + numSphereSegments*i; 
     indexCnt++; 
     indices[indexCnt] = numSphereSegments+j + numSphereSegments*i; 
     indexCnt++; 
     indices[indexCnt] = j+1 + numSphereSegments*i; 
     indexCnt++; 
    } 
    } 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);              
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (numSphereSlices*numSphereSegments*6) *  sizeof(unsigned short), indices, GL_STATIC_DRAW); 

    delete [] indices; 

    glUniformMatrix4fv(u_modelMatrixLocation, 1, GL_FALSE, &modelView[0][0]); 

    glDrawElements(GL_TRIANGLES, numSphereSlices*numSphereSegments, GL_UNSIGNED_SHORT, 0); 


    glDisableVertexAttribArray(positionLocation); 
    glDisableVertexAttribArray(colorLocation); 
    glDisableVertexAttribArray(normalLocation); 
} 
잘못 가고있다

문제가 정점 생성 또는 색인 연결과 관련이 있는지 확실하지 않습니다.

+0

모든 것이 정상적으로 보입니다. 당신의 뷰 매트릭스에 문제가 있다고 생각합니다. 시작하려면 지금 사용하고있는 뷰 매트릭스의 조 변경을 사용해보십시오. –

+0

그게 문제가 아니라고 생각합니다. 큐브와 같은 다른 모양을 찾고있었습니다. – Kyle

답변

3

원본으로 복사 한 코드를 복사하여 붙여 넣기 Creating a 3D sphere in Opengl using Visual C++

class SolidSphere 
{ 
protected 
    std::vector<GLfloat> vertices; 
    std::vector<GLfloat> normals; 
    std::vector<GLfloat> texcoords; 
    std::vector<GLushort> indices; 

public: 
    void SolidSphere(float radius, unsigned int rings, unsigned int sectors) 
    { 
     float const R = 1./(float)(rings-1); 
     float const S = 1./(float)(sectors-1); 
     int r, s; 

     sphere_vertices.resize(rings * sectors * 3); 
     sphere_normals.resize(rings * sectors * 3); 
     sphere_texcoords.resize(rings * sectors * 2); 
     std::vector<GLfloat>::iterator v = sphere_vertices.begin(); 
     std::vector<GLfloat>::iterator n = sphere_normals.begin(); 
     std::vector<GLfloat>::iterator t = sphere_texcoords.begin(); 
     for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) { 
       float const y = sin(-M_PI_2 + M_PI * r * R); 
       float const x = cos(2*M_PI * s * S) * sin(M_PI * r * R); 
       float const z = sin(2*M_PI * s * S) * sin(M_PI * r * R); 

       *t++ = s*S; 
       *t++ = r*R; 

       *v++ = x * radius; 
       *v++ = y * radius; 
       *v++ = z * radius; 

       *n++ = x; 
       *n++ = y; 
       *n++ = z; 
     } 

     sphere_indices.resize(rings * sectors * 4); 
     std:vector<GLushort>::iterator i = sphere_indices.begin(); 
     for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) { 
       *i++ = r * sectors + s; 
       *i++ = r * sectors + (s+1); 
       *i++ = (r+1) * sectors + (s+1); 
       *i++ = (r+1) * sectors + s; 
     } 
    } 
} 
+0

흠. 나는 인덱스를 어떻게 만들었는지 두 번 확인했고 그 인덱스는 쿼드가 아닌 삼각형을 사용하기 때문에 약간의 차이점을 제외하고는 자신과 동일하게 보인다. 이 방법으로 구현하면 여전히 동일한 출력을 얻게됩니다. 나는 정점을 만드는 것에 대해 생각해 봤지만 약간의 구문 차이를 제외하면 당신과 똑같은 것처럼 보인다. 다른 제안? – Kyle

관련 문제