2010-09-08 92 views
3

有沒有辦法像這樣繪製一個立方體,結果只能將8個頂點和24個標記上傳到顯卡?如果是這樣怎麼辦?用GlDrawElements()繪製一個立方體?

感謝

我現在做這樣的:

boxdims.x = w; 
    boxdims.y = h; 
    boxdims.z = d; 
    center = c; 

    GLfloat vboverticies[72]; 
    GLfloat vbonormals[18]; 
    Vertex3f verticies[24]; 

    //Top face 
    verticies[0] = Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[1] = Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[2] = Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[3] = Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 


    //Bottom face 
    verticies[4] = Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[5] = Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[6] = Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[7] = Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 


    //Left face 
    verticies[8] = Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[9] = Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[10] =Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[11] =Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 


    //Right face 
    verticies[12] =Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[13] =Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[14] =Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[15] =Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 

    //Front face 
    verticies[16] =Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[17] =Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[18] =Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 
    verticies[19] =Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, boxdims.z/2.0f); 

    //Back face 
    verticies[20] =Vertex3f(-boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[21] =Vertex3f(-boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[22] =Vertex3f(boxdims.x/2.0f,boxdims.y/2.0f, -boxdims.z/2.0f); 
    verticies[23] =Vertex3f(boxdims.x/2.0f,-boxdims.y/2.0f, -boxdims.z/2.0f); 




    for(int i = 0; i < 24; i++) 
    { 
     verticies[i].x += center.x; 
     verticies[i].y += center.y; 
     verticies[i].z += center.z; 
    } 


    int count = 0; 
    for(int i = 0; i < 24; ++i) 
    { 
     vboverticies[count] = verticies[i].x; 
     count++; 
     vboverticies[count] = verticies[i].y; 
     count++; 
     vboverticies[count] = verticies[i].z; 
     count++; 
    } 

    //glNormal3f(0.0, 1.0f, 0.0f); 
    //glNormal3f(0.0, -1.0f, 0.0f); 
    //glNormal3f(-1.0, 0.0f, 0.0f); 
    //glNormal3f(1.0, 0.0f, 0.0f); 
    //glNormal3f(0.0, 0.0f, 1.0f); 
    //glNormal3f(0.0, 0.0f, -1.0f); 
    vbonormals[0] = (0.0); 
    vbonormals[1] = (1.0); 
    vbonormals[2] = (0.0); 

    vbonormals[3] = (0.0); 
    vbonormals[4] = (-1.0); 
    vbonormals[5] = (0.0); 

    vbonormals[6] = (-1.0); 
    vbonormals[7] = (0.0); 
    vbonormals[8] = (0.0); 

    vbonormals[9] = (1.0); 
    vbonormals[10]= (0.0); 
    vbonormals[11]= (0.0); 

    vbonormals[12]= (0.0); 
    vbonormals[13]= (0.0); 
    vbonormals[14]= (1.0); 

    vbonormals[15]= (0.0); 
    vbonormals[16]= (0.0); 
    vbonormals[17]= (-1.0); 



    //Create the VBO 
    glDeleteBuffers(1, &vboID); 
    glGenBuffersARB(1, &vboID); 
    glBindBufferARB(GL_ARRAY_BUFFER, vboID); 
    glBufferDataARB(GL_ARRAY_BUFFER, (72 * sizeof(GLfloat)) + 
     (18 * sizeof(GLfloat)) , NULL, GL_STATIC_COPY); 

    glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, 72 * sizeof(GLfloat), vboverticies); 
    glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 72 * sizeof(GLfloat), 
     18 * sizeof(GLfloat), vbonormals); 


} 

回答

1

答案是否定的。

如果你需要法線,你必須有24個頂點,因爲沒有臉部分享法線。請注意,shk提供的示例不處理法線。

+0

好吧,如果我不使用法線,那我該怎麼做? – jmasterx 2010-09-08 16:42:43

+0

@Milo:就像鏈接shk給你。使用8個頂點和24個索引,使用glDrawElements() – Bahbar 2010-09-08 18:02:54

0

你可以,但你必須使用幾何着色器,並且在你真的知道你在做什麼之前你不應該惹這個東西。更重要的是,對於一個立方體來說,這並不是什麼大不了的事情。