只有当它们都应该被绘制时,才绘制向量的最后一个对象 [英] Only the last object of a vector is drawn when they should all be drawn

查看:112
本文介绍了只有当它们都应该被绘制时,才绘制向量的最后一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Application对象,它有一个GameChar对象的向量作为其成员变量之一。这个GameChar对象包含一个GLbatch,它在构造函数中初始化,并在调用GameChar对象的draw方法时绘制。当程序启动时,我创建一个应用程序对象和三个GameChar对象,我使用push_back()方法将GameChar推入Application对象的向量。然后,在游戏循环中,我遍历向量并调用所有对象的绘制方法。问题是,只有最后被放在向量中的对象被绘制。我确信向量是正确迭代的,并且draw方法在所有对象上被调用,但无论以什么顺序我称之为这种绘制方法(我已经尝试从结束到开始重复向量,以确保这一点)只有最后插入向量的对象被绘制。

I have an Application object that has a vector of GameChar objects as one of its member variables. This GameChar objects contain a GLbatch that is initialized in the constructor and drawn when you call the draw method of the GameChar object. When the program starts I create an application object and three GameChar objects, and I push the GameChar's into the Application object's vector with the push_back() method. Then, inside a game loop, I iterate through the vector and call the draw method of all I'ts objects. The problem is that only the object that was last put in the vector gets drawn. I have assured that the vector is iterated correctly, and that the draw method is called on all the objects, but no matter in what order I call this draw methods (I have tried to iterate the vector from end to start to assure this), only the object that was last inserted to the vector gets drawn.

这里是创建应用程序和GameChar对象的代码(构造函数按以下顺序获取参数:width ,height,x,y):

Here is the code that creates the application and GameChar objects (the constructor gets the arguments in this order: width,height,x,y):

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);

这是循环:

while (1)
{
    vector<GameChar>::size_type sz=characters.size();
    unsigned int i;
    for (i=0;i<sz;i++)
    {
        characters[i].update();
    }
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    for (i=0;i<sz;i++)
    {

        characters[i].draw();
    }

    glfwSwapBuffers();
}

UPDATE:
下面是GameChar的构造函数:

UPDATE: Here is the constructor of GameChar:

GameChar::GameChar(GLfloat lWidth,GLfloat lHeight,GLfloat lX,GLfloat lY)
{
    xPos=lX;
    yPos=lY;
    height=lHeight;
    width=lWidth;
    if (xPos<-1)
    {
        xPos=-1;
    }
    if (xPos+width>1)
        xPos=1-width;
    if (yPos<-1)
        yPos=-1;
    if (yPos+height>1)
        yPos=1-height;
    verts[0]=xPos;
    verts[1]=yPos;
    verts[2]=0.0f;
    verts[3]=xPos+width;
    verts[4]=yPos;
    verts[5]=0.0f;
    verts[6]=xPos;
    verts[7]=yPos+height;
    verts[8]=0.0f;
    verts[9]=xPos+width;
    verts[10]=yPos+height;
    verts[11]=0.0f;

    batch.Begin(GL_TRIANGLE_STRIP, 4);
    batch.CopyVertexData3f(verts);
    batch.End();
}

绘制方法:

void GameChar::draw()
{
    batch.Draw();
}



我已经用完整的代码更新了游戏循环。

And I've updated the game loop with the complete code.

此外,如果我尝试更改代码:

Also, If i try to change the code:

for (i=0;i<sz;i++)
{
    characters[i].draw();
}

使用此代码:

for (i=0;i<sz;i++)
{
    GLBatch batch;
    batch=characters[i].batch;
    batch.Draw();
}

没有正方形。我真的发现这一切都是一个废话

No square gets drawn. I really find all this a nonsense

另一个更新:

如果我pop_back第三个对象,如下所示:

If I pop_back the third object after pushing it in, like this:

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);
app.characters.push_back(character3);
app.characters.pop_back();

没有绘制任何内容,但如果不推送,像这样:

nothing is drawn, but if don't push it in, like this:

Application app;
GameChar character(0.5f,0.5f,0.0f,0.0f);
GameChar character2(0.5f,0.5f,0.5f,0.5f);
GameChar character3(0.5f,0.5f,-1.0f,-1.0f);
app.characters.push_back(character);
app.characters.push_back(character2);

绘制第二个字符。所以我认为问题是,当一个新的推送,已经在向量中的字符无效,但我不知道为什么这种情况,或如何解决它

The second character is drawn. So I think the problem is that the characters that are already in the vector are invalidated when a new one is pushed, but I don't know why this happens, or how to fix it

推荐答案

我终于设法解决了!我仍然不知道是什么原因的问题,但我改变了GameChar的向量到一个指向GameChar对象的指针的向量,现在它的工作!

I finally managed to fix it! I still don't know what's the cause of the problem, but I've changed the vector of GameChar to a vector of pointers to GameChar objects, and now it works!

这篇关于只有当它们都应该被绘制时,才绘制向量的最后一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆