在 OpenGL 中渲染网格多边形 - 非常慢

发布于 2024-11-28 11:22:28 字数 2323 浏览 0 评论 0原文

我最近从中间模式切换并有了一个新的渲染过程。一定有什么我不明白的地方。我认为这与指数有一定关系。

这是我的图表:区域 -> 网格 -> 多边形阵列 -> 3 个引用顶点主列表的顶点索引。

这是我的渲染代码:

// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{

    int num = 0;

    // Set up rendering states
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // Set up my indices
    GLuint indices[3];

    // Cycle through the PVS
    while(num < regions[curRegion].visibility.size())
    {
        int i = regions[curRegion].visibility[num];

        // Make sure the region is not "dead"
        if(!regions[i].dead && regions[i].meshptr != NULL)
        {
            // Check to see if the mesh is in the frustum
            if(cfrustum.BoxInFrustum(regions[i].meshptr->min[0], regions[i].meshptr->min[2], regions[i].meshptr->min[1], regions[i].meshptr->max[0], regions[i].meshptr->max[2], regions[i].meshptr->max[1]))
            {
                // Cycle through every polygon in the mesh and render it
                for(int j = 0; j < regions[i].meshptr->polygonCount; j++)
                {   
                    // Assign the index for the polygon to the index in the huge vertex array
                    // This I think, is redundant
                    indices[0] = regions[i].meshptr->poly[j].vertIndex[0];
                    indices[1] = regions[i].meshptr->poly[j].vertIndex[1];
                    indices[2] = regions[i].meshptr->poly[j].vertIndex[2];

                    // Enable texturing and bind the appropriate texture
                    glEnable(GL_TEXTURE_2D);
                    glBindTexture(GL_TEXTURE_2D, textures[regions[i].meshptr->poly[j].tex]);

                    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);

                    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].u);

                    // Draw
                    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
                }
            }
        }
    num++;
    }

    // End of rendering - disable states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

抱歉,如果我遗漏了任何内容。我非常感谢反馈和帮助。我什至会考虑付钱给擅长 OpenGL 和优化的人来帮助我解决这个问题。

I recently switched from intermediate mode and have a new rendering process. There must be something I am not understanding. I think it has something to do with the indices.

Here is my diagram: Region->Mesh->Polygon Array->3 vertex indices which references the master list of vertices.

Here my render code:

// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{

    int num = 0;

    // Set up rendering states
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // Set up my indices
    GLuint indices[3];

    // Cycle through the PVS
    while(num < regions[curRegion].visibility.size())
    {
        int i = regions[curRegion].visibility[num];

        // Make sure the region is not "dead"
        if(!regions[i].dead && regions[i].meshptr != NULL)
        {
            // Check to see if the mesh is in the frustum
            if(cfrustum.BoxInFrustum(regions[i].meshptr->min[0], regions[i].meshptr->min[2], regions[i].meshptr->min[1], regions[i].meshptr->max[0], regions[i].meshptr->max[2], regions[i].meshptr->max[1]))
            {
                // Cycle through every polygon in the mesh and render it
                for(int j = 0; j < regions[i].meshptr->polygonCount; j++)
                {   
                    // Assign the index for the polygon to the index in the huge vertex array
                    // This I think, is redundant
                    indices[0] = regions[i].meshptr->poly[j].vertIndex[0];
                    indices[1] = regions[i].meshptr->poly[j].vertIndex[1];
                    indices[2] = regions[i].meshptr->poly[j].vertIndex[2];

                    // Enable texturing and bind the appropriate texture
                    glEnable(GL_TEXTURE_2D);
                    glBindTexture(GL_TEXTURE_2D, textures[regions[i].meshptr->poly[j].tex]);

                    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);

                    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].u);

                    // Draw
                    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
                }
            }
        }
    num++;
    }

    // End of rendering - disable states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

Sorry if I left anything out. And I really appreciate feedback and help with this. I would even consider paying someone who is good with OpenGL and optimization to help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2024-12-05 11:22:28

如果一次只渲染 3 个顶点,那么使用数组渲染就没有意义。这个想法是通过一次呼叫发送数千。也就是说,您只需一次调用即可渲染单个“多边形阵列”或“网格”。

There is no point in using array rendering if you're only rendering 3 vertices at a time. The idea is to send thousands through with a single call. That is, you render a single "Polygon Array" or "Mesh" with one call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文