OpenGL2D C++,绘制落墙(执行过程中崩溃),为什么?

问题描述 投票:0回答:1

OpenGL2D C++ 应用程序崩溃,怀疑 VBO 饱和。在drawScene() 中的for 循环中绘制掉落的方块墙(补丁)。关于崩溃问题有什么见解吗?

我的应用程序是一个简单的“游戏”,其中有一个球(主角)可以通过按“a”(左)或“d”(右)来移动。场景中绘制了球弹跳的地形、太阳的天空、风力涡轮机和倒塌的墙。

这是绘制倒塌墙的代码:

//Costruction geometry for the falling wall
    for (int i = 0; i < num_patchesMuro; i++) {

        if (posx_Muro <= width) {
//the walls have the holes, because the ball have to dodge the walls.
            if (arrayMuro[i] == true) {

                
                glBindVertexArray(VAO_Muro);
                Model = mat4(1.0);
                glUniformMatrix4fv(MatModel, 1, GL_FALSE, value_ptr(Model));
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                glDrawArrays(GL_TRIANGLES, 0, vertices_Muro);

        //drawing the single square (single patch): posx_singlequare,posy_singlesquare,dimension x of single square, dimension height of single square,vec4 color,vec4 color, Muro is a Pointxy* structure
                disegna_pianoxy(posx_Muro, posy_muro, dimPatch, dimMuroY, col_murotop, col_murobottom, Muro);


                glGenBuffers(1, &VBO_Mur);
                glBindBuffer(GL_ARRAY_BUFFER, VBO_Mur);
                glBufferData(GL_ARRAY_BUFFER, vertices_Muro * sizeof(Pointxy), &Muro[0], GL_STATIC_DRAW); //the problem I think is here
                glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
                glEnableVertexAttribArray(0);
                glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(2 * sizeof(float)));
                glEnableVertexAttribArray(1);
                glBindVertexArray(0);


                
                
            } 
            posx_Muro += dimPatch;

        }
        else break;

    }

代码方法 disegna_pianoxy():

void disegna_pianoxy(float x, float y, float width, float height, vec4 color_top, vec4 color_bot, Pointxy* piano)
{
    piano[0].x = x; piano[0].y = y; 
    piano[0].r = color_bot.r; piano[0].g = color_bot.g; piano[0].b = color_bot.b; piano[0].a = color_bot.a;
    piano[1].x = x + width; piano[1].y = y;
    piano[1].r = color_top.r; piano[1].g = color_top.g; piano[1].b = color_top.b; piano[1].a = color_top.a;
    piano[2].x = x + width; piano[2].y = y + height; 
    piano[2].r = color_bot.r; piano[2].g = color_bot.g; piano[2].b = color_bot.b; piano[2].a = color_bot.a;

    piano[3].x = x + width; piano[3].y = y + height; 
    piano[3].r = color_bot.r; piano[3].g = color_bot.g; piano[3].b = color_bot.b; piano[3].a = color_bot.a;
    piano[4].x = x; piano[4].y = y + height; 
    piano[4].r = color_top.r; piano[4].g = color_top.g; piano[4].b = color_top.b; piano[4].a = color_top.a;
    piano[5].x = x; piano[5].y = y; 
    piano[5].r = color_bot.r; piano[5].g = color_bot.g; piano[5].b = color_bot.b; piano[5].a = color_bot.a;
}

在很多情况下,应用程序告诉我,崩溃后,glBufferData() 中出现异常。

我还尝试将 VBO 的逻辑移出 for 循环,但在这种情况下绘制的是单个正方形(最后一个补丁),而不是由多个补丁组成的整个墙。

c++ opengl 2d vbo vao
1个回答
0
投票

添加更多信息:

int nTriangles_Muro = 6;
int vertices_Muro = nTriangles_Muro;
Pointxy* Muro = new Pointxy[nTriangles_Muro];

Pointxy 结构如下:

typedef struct { float x, y, r, g, b, a; } Pointxy;

关于绘制对象的逻辑,我遵循了教授给我们的示例代码。我没有任何 OpenGL2D 经验,所以我陷入困境。

© www.soinside.com 2019 - 2024. All rights reserved.