2010-09-05 77 views
2

我正在做一個3D遊戲使用OpenGL,基本上有一個世界完全由AABB立方體。我創造了這個使24 verticie立方體:渲染立方體儘快? (OpenGL的)

void CBox::UpdateDimensions(float w, float h, float d, Vertex3f c) 
{ 
    width = w; 
    height = h; 
    depth = d; 
    center = c; 

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

    //Top face 
    verticies[0] = Vertex3f(-width/2.0f,height/2.0f, -depth/2.0f); 
    verticies[1] = Vertex3f(-width/2.0f,height/2.0f, depth/2.0f); 
    verticies[2] = Vertex3f(width/2.0f,height/2.0f, depth/2.0f); 
    verticies[3] = Vertex3f(width/2.0f,height/2.0f, -depth/2.0f); 


    //Bottom face 
    verticies[4] = Vertex3f(-width/2.0f,-height/2.0f, -depth/2.0f); 
    verticies[5] = Vertex3f(width/2.0f,-height/2.0f, -depth/2.0f); 
    verticies[6] = Vertex3f(width/2.0f,-height/2.0f, depth/2.0f); 
    verticies[7] = Vertex3f(-width/2.0f,-height/2.0f, depth/2.0f); 


    //Left face 
    verticies[8] = Vertex3f(-width/2.0f,-height/2.0f, -depth/2.0f); 
    verticies[9] = Vertex3f(-width/2.0f,-height/2.0f, depth/2.0f); 
    verticies[10] =Vertex3f(-width/2.0f,height/2.0f, depth/2.0f); 
    verticies[11] =Vertex3f(-width/2.0f,height/2.0f, -depth/2.0f); 


    //Right face 
    verticies[12] =Vertex3f(width/2.0f,-height/2.0f, -depth/2.0f); 
    verticies[13] =Vertex3f(width/2.0f,height/2.0f, -depth/2.0f); 
    verticies[14] =Vertex3f(width/2.0f,height/2.0f, depth/2.0f); 
    verticies[15] =Vertex3f(width/2.0f,-height/2.0f, depth/2.0f); 

    //Front face 
    verticies[16] =Vertex3f(-width/2.0f,-height/2.0f, depth/2.0f); 
    verticies[17] =Vertex3f(width/2.0f,-height/2.0f, depth/2.0f); 
    verticies[18] =Vertex3f(width/2.0f,height/2.0f, depth/2.0f); 
    verticies[19] =Vertex3f(-width/2.0f,height/2.0f, depth/2.0f); 

    //Back face 
    verticies[20] =Vertex3f(-width/2.0f,-height/2.0f, -depth/2.0f); 
    verticies[21] =Vertex3f(-width/2.0f,height/2.0f, -depth/2.0f); 
    verticies[22] =Vertex3f(width/2.0f,height/2.0f, -depth/2.0f); 
    verticies[23] =Vertex3f(width/2.0f,-height/2.0f, -depth/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); 

    RecalculateMinAndMax(); 

    //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_DRAW); 

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


} 

什麼我不知道是如果有一種方法只uploaading 8個verticies圖形卡,而不是24來做到這一點?

會是什麼使這些最快的方法是什麼?

感謝

回答

2

什麼我不知道是如果有一種方法只uploaading 8個verticies到 顯卡,而不是24來做到這一點?

看看glDrawElements。我看到你使用VBO,EBO也會有用。如果你的大多數元素具有相同的幾何形狀(而且似乎他們有),可以使用glDrawElementsInstanced。

+0

第1步:繪製每個立方體在1個呼叫與駐國際中心組織。步驟2:如果您有適當的硬件,請在實例化的1次調用中繪製每個立方體。 – Calvin1602 2010-09-09 11:08:55