结构和数组限制了问题

时间:2015-08-31 15:26:40

标签: c++ arrays vector struct

我一直在处理我的模型加载器并且我得到一个错误,因为我使用了几个带数组的结构(没有向量),我想在使用之前初始化数组,因为程序在我尝试使用时给出了错误使用方括号[]访问向量数组。

GameObject struct:

struct GameObject
{
    int ID, parent;
    string tag;
    Mesh_t Mesh;
    Vector3 position;
    Vector3 scale;
    Quaternion rotation;
    Color color;
};

网格结构:

struct Meshes_t
{
    vector<GLfloat> VBO;
    vector<GLfloat> VBO_Normal;
    Texture_t Texture;
    DWORD Geometry;
    int VertexType;
};

struct Mesh_t
{
    int VerticesCount;
    int TexturesCount;
    vector<Meshes_t> Faces;
};

问题是当我尝试从“Meshes_t”中将push_back推送到VBO向量数组时:

GameObjects[GameObjectsCount].Mesh.Faces[k].VBO.push_back(vertex.x);
GameObjects[GameObjectsCount].Mesh.Faces[k].VBO.push_back(vertex.y);
GameObjects[GameObjectsCount].Mesh.Faces[k].VBO.push_back(vertex.z);

由于Faces向量数组未在任何地方初始化,当程序到达下面的代码时,Visual Studio会向我抛出运行时错误:这是错误https://gyazo.com/806e3426025ad3885f0d3ed0b7aa1d30

我试图初始化矢量数组,但我不知道怎么做,因为矢量数组里面有其他结构,我不能推回结构数据。有任何想法吗?提前谢谢。

编辑:

这是来自.h

的gameobjects数组
extern struct GameObject GameObjects[1000];

和cpp是struct GameObject GameObjects [1000];

所以我用它来创建游戏对象:

int teste = Entity.CreateEntity("data//models//cube.obj", Vector3(0, 25, 0), Quaternion(0, 0, 0), Vector3(0.51, 0.51, 0.51), -1);

返回的int是我在GameObjects [ReturnedINT]

中使用的

RENDER FUNCTION:

void GLRender ()
{
    glBindTexture(GL_TEXTURE_2D, 0);
    RenderSpaceLines();

    drawLine(Vector3(0, 0, 0), GameObjects[0].position);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        for (int i = 0; i < Entity.GameObjectsCount; i++)
        {
            glBindTexture(GL_TEXTURE_2D, 0);
            if (GameObjects[i].Mesh.TexturesCount > 0)
            {
                for (int t = 0; t < 10; t++)
                {
                    glVertexPointer(3, GL_FLOAT, 0, (GLfloat*)GameObjects[i].Mesh.Faces[t].VBO.data());
                    glTexCoordPointer(2, GL_FLOAT, 0, (GLfloat*)GameObjects[i].Mesh.Faces[t].Texture.VBO.data());

                    glBindTexture(GL_TEXTURE_2D, TextureManager.GetTextureFromName(GameObjects[i].Mesh.Faces[t].Texture.TextureName));

                    glPushMatrix();
                    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                    if (GameObjects[i].parent == -1)
                    {
                        glTranslatef(GameObjects[i].position.x, GameObjects[i].position.y, GameObjects[i].position.z);
                        glRotatef(GameObjects[i].rotation.x, 1, 0, 0);
                        glRotatef(GameObjects[i].rotation.y, 0, 1, 0);
                        glRotatef(GameObjects[i].rotation.z, 0, 0, 1);
                    }
                    else  ///RELATIVITY OF ENTITY TO ANOTHER
                    {
                        glTranslatef(GameObjects[GameObjects[i].parent].position.x,
                            GameObjects[GameObjects[i].parent].position.y,
                            GameObjects[GameObjects[i].parent].position.z);                 ///SET WORLD RELATIVE POSITION

                        glRotatef(GameObjects[GameObjects[i].parent].rotation.x, 1, 0, 0);
                        glRotatef(GameObjects[GameObjects[i].parent].rotation.y, 0, 1, 0);
                        glRotatef(GameObjects[GameObjects[i].parent].rotation.z, 0, 0, 1);

                        glTranslatef(GameObjects[i].position.x,
                            GameObjects[i].position.y,
                            GameObjects[i].position.z);

                    }
                    glScalef(GameObjects[i].scale.x,
                        GameObjects[i].scale.y,
                        GameObjects[i].scale.z);

                    glDrawArrays(GameObjects[i].Mesh.Faces[t].Geometry, 0, GameObjects[i].Mesh.Faces[t].VertexType);
                    glPopMatrix();
                }
            }

        }

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

1 个答案:

答案 0 :(得分:0)

Faces实际上是在Mesh_t的隐式默认构造函数中初始化的。但它将是空的。因此,在访问Faces[k]之前,您必须先将至少k + 1个元素放入向量中。例如:

GameObjects[GameObjectsCount].Mesh.Faces.resize(k + 1);
GameObjects[GameObjectsCount].Mesh.Faces[k].VBO.push_back(vertex.x);
// ...

还要确保GameObjects至少包含GameObjectsCount + 1个对象。