c++ - 使用 glDrawRangeElements() 渲染无法正常工作

标签 c++ opengl render

这是我之前问题的后续问题。我的所有问题都在我的上一个线程中得到了解答,但这是我遇到的一个新错误。在中间模式下渲染时,一切看起来都很棒。

事实上: http://i.imgur.com/OFV6i.png

现在,我正在使用 glDrawRangeElements() 进行渲染,看看会发生什么:

http://i.imgur.com/mEmH5.png

有没有人见过这样的东西?看起来好像有些索引只是在中间,这根本没有意义。

这是我用来渲染关卡的函数。

void WLD::renderIntermediate(GLuint* textures, long curRegion, CFrustum cfrustum)
{
    // Iterate through all of the regions in the PVS
    for(int i = 0; i < regions[curRegion].visibility.size(); i++)
    {
        // Grab a visible region
        int vis = regions[curRegion].visibility[i];

        // Make sure it points to a mesh
        if(regions[vis].meshptr == NULL)
            continue;

        // Make sure it is in our PVS
        if(!cfrustum.BoxInFrustum(regions[vis].meshptr->minX, regions[vis].meshptr->minY, regions[vis].meshptr->minZ, regions[vis].meshptr->maxX, regions[vis].meshptr->maxY, regions[vis].meshptr->maxZ))
            continue;

        // Optional: Only render the region we are in (for testing)
        //if(vis != curRegion)
        //  continue;

        // Now find the ID of the zone mesh in the array
        int id = regions[vis].meshptr->id;

        // Figure out how many calls we will have to do to render it (different textures)
        int calls = zmeshes[id].numPolyTex;

        int count = 0;

        // Render each call in batches
        for(int j = 0; j < calls; j++)
        {

            // Bind the correct texture
            glBindTexture(GL_TEXTURE_2D, textures[zmeshes[id].polyTexs[j].texIndex]);

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

            errorLog.writeSuccess("Drawing debug: ID: %i - Min: %i - Max: %i - Polys in this call %i - Count: %i - Location: %i", id, zmeshes[id].minmax[j].min, zmeshes[id].minmax[j].max, zmeshes[id].polyTexs[j].polyCount, zmeshes[id].polyTexs[j].polyCount * 3, count);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &zmeshes[id].vertices[0].x);
            glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &zmeshes[id].vertices[0].u);

            // Draw
            glDrawRangeElements(GL_TRIANGLES, zmeshes[id].minmax[j].min, zmeshes[id].minmax[j].max, zmeshes[id].polyTexs[j].polyCount * 3, GL_UNSIGNED_SHORT, zmeshes[id].indices + count);

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

            // Add the number of indices rendered
            count += zmeshes[id].polyTexs[j].polyCount * 3;

        }
    }
}

我已经完成了大量的调试信息,表明我的最小值/最大值设置正确。在这一点上,我认为索引可能有错误,所以我将继续查看/重写该函数。

最佳答案

设法让它发挥作用。非常感谢另一个论坛上有人准确地标记了他的问题,即在原点处绘制了第三个顶点。

http://www.gamedev.net/topic/583558-gldrawelements-is-drawing-all-of-my-vertices-with-one-vertex-at-the-origin-solved/page_gopid_4867052#entry4867052

看来我需要将我的索引数据类型从 int 更改为 short。效果非常好,我的 FPS 提高了 %1500。

关于c++ - 使用 glDrawRangeElements() 渲染无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591609/

相关文章:

c++ - 将存储在 std::string 中的二进制数据转换为 wxString

java - OpenGL + LWJGL - glColor4d() 没有正确着色形状

reactjs - 当一项更改时,React 会重新渲染整个列表

c++ - 如何在 C 或 C++ 中复制文本文件?

c++ - 最小值和最大值之间的随机双倍

opengl - 我的有限 FPS : 60

reactjs - 为 React Native SectionList 的每个部分渲染不同的组件

javascript - 条件渲染与 TabNavigator

c++ - 如何使用带有 strcmp 样式函数的 spaceship <=> 运算符?

c++ - 使用来自不同线程的 opengl-command