c++ - 尽可能快地渲染立方体? (OpenGL)

标签 c++ c opengl

我正在使用 OpenGL 制作一款 3D 游戏,该游戏的世界基本上完全由 AABB 立方体构成。我创建这个是为了制作 24 个垂直立方体:

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


}

我想知道是否有一种方法可以通过只将 8 个顶点而不是 24 个顶点上传到显卡来做到这一点?

渲染这些内容的最快方法是什么?

谢谢

最佳答案

What I'm wondering is if there is a way to do this by only uploaading 8 verticies to the graphics card instead of 24?

看看 glDrawElements。我看到你使用 VBO,EBO 也很有用。如果您的大部分元素具有相同的几何形状(并且看起来它们具有相同的几何形状),您可以使用 glDrawElementsInstanced。

关于c++ - 尽可能快地渲染立方体? (OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3646967/

相关文章:

c++ - wxWidgets - wxMediaCtrl - 视频不播放

c++ - OpenGL - 第一人称相机矩阵的问题

c - 如何获取此字符串的信息?

c++ - 如何在 Visual C++ 2010 Express 中链接 OpenGl?

java - LWJGL/OpenGL 滞后绘制立方体阵列

c++ - 如果多映射中与其关联的 vector 为空,则删除该键

c++ - 指针是否需要指向分配的类数组中的第一个对象才能使用 'delete [] ptrName' ?

c - 使用 getchar 和存储字符时遇到困难

c++ - 如何从 swf 或 abc 文件生成 swc

openGL - 创建弯曲表面