Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尽快渲染立方体?(OpenGL)_C++_C_Opengl - Fatal编程技术网

C++ 尽快渲染立方体?(OpenGL)

C++ 尽快渲染立方体?(OpenGL),c++,c,opengl,C++,C,Opengl,我正在用OpenGL制作一个3D游戏,它的世界基本上完全由AABB立方体组成。我创建这个来制作24个verticie立方体: void CBox::UpdateDimensions( float w, float h, float d, Vertex3f c ) { width = w; height = h; depth = d; center = c; Vertex3f verticies[24]; GLfloat vboverticies[

我正在用OpenGL制作一个3D游戏,它的世界基本上完全由AABB立方体组成。我创建这个来制作24个verticie立方体:

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


}
void CBox::UpdateDimensions(浮点w、浮点h、浮点d、顶点3f c)
{
宽度=w;
高度=h;
深度=d;
中心=c;
顶点3f垂直[24];
GLV建议[72];
GLV标准值[18];
//顶面
垂直[0]=顶点3F(-宽度/2.0f,高度/2.0f,-深度/2.0f);
垂直[1]=顶点3F(-宽度/2.0f,高度/2.0f,深度/2.0f);
垂直[2]=顶点3f(宽度/2.0f,高度/2.0f,深度/2.0f);
垂直[3]=顶点3f(宽度/2.0f,高度/2.0f,-深度/2.0f);
//底面
垂直[4]=顶点3F(-宽度/2.0f,-高度/2.0f,-深度/2.0f);
垂直[5]=顶点3f(宽度/2.0f,-高度/2.0f,-深度/2.0f);
垂直[6]=顶点3f(宽度/2.0f,-高度/2.0f,深度/2.0f);
垂直[7]=顶点3F(-宽度/2.0f,-高度/2.0f,深度/2.0f);
//左脸
垂直[8]=顶点3F(-宽度/2.0f,-高度/2.0f,-深度/2.0f);
垂直[9]=顶点3F(-宽度/2.0f,-高度/2.0f,深度/2.0f);
垂直[10]=顶点3F(-宽度/2.0f,高度/2.0f,深度/2.0f);
垂直[11]=顶点3F(-宽度/2.0f,高度/2.0f,-深度/2.0f);
//右脸
垂直[12]=顶点3f(宽度/2.0f,-高度/2.0f,-深度/2.0f);
垂直[13]=顶点3f(宽度/2.0f,高度/2.0f,-深度/2.0f);
垂直[14]=顶点3f(宽度/2.0f,高度/2.0f,深度/2.0f);
垂直[15]=顶点3f(宽度/2.0f,-高度/2.0f,深度/2.0f);
//正面
垂直[16]=顶点3F(-宽度/2.0f,-高度/2.0f,深度/2.0f);
垂直[17]=顶点3f(宽度/2.0f,-高度/2.0f,深度/2.0f);
垂直[18]=顶点3f(宽度/2.0f,高度/2.0f,深度/2.0f);
垂直[19]=顶点3F(-宽度/2.0f,高度/2.0f,深度/2.0f);
//背面
垂直[20]=顶点3F(-宽度/2.0f,-高度/2.0f,-深度/2.0f);
垂直[21]=顶点3F(-宽度/2.0f,高度/2.0f,-深度/2.0f);
垂直[22]=顶点3f(宽度/2.0f,高度/2.0f,-深度/2.0f);
垂直[23]=顶点3f(宽度/2.0f,-高度/2.0f,-深度/2.0f);
对于(int i=0;i<24;i++)
{
垂直[i].x+=center.x;
垂直[i].y+=中心.y;
垂直[i].z+=center.z;
}
整数计数=0;
对于(int i=0;i<24;++i)
{
vbovertices[count]=vertices[i].x;
计数++;
vbovertices[count]=vertices[i].y;
计数++;
vbovertices[count]=vertices[i].z;
计数++;
}
//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);
重新计算和最大值();
//创建VBO
glDeleteBuffers(1和vboID);
glGenBuffersARB(1,&vboID);
glBindBufferARB(GL_数组_缓冲区,vboID);
glBufferDataARB(GL_数组_缓冲区,(72*sizeof(GLfloat))+
(18*sizeof(GLfloat)),空,总图静态绘制);
glBufferSubDataARB(GL_数组_BUFFER_ARB,0,72*sizeof(GLfloat),vboverticies);
glBufferSubDataARB(GL_数组_BUFFER_ARB,72*sizeof(GLfloat),
18*sizeof(GLfloat),vbonormals);
}
我想知道的是,是否有一种方法可以做到这一点,只上传8个垂直图形到图形卡,而不是24个

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

谢谢

我想知道的是,是否有一种方法可以做到这一点,只上传8个垂直到 图形卡而不是24


看一看这些元素。我看到你使用VBO,EBO也会很有用。如果您的大多数元素具有相同的几何图形(看起来它们具有相同的几何图形),则可以使用GLDrawerElementsInstanced。

步骤1:使用VBOs在一次调用中绘制每个立方体。第二步:如果你有合适的硬件,在一次调用中用实例绘制每个立方体。