【问题标题】:Struct and Arrays limit problems结构和数组限制问题
【发布时间】:2015-08-31 15:26:40
【问题描述】:

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

游戏对象结构:

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 的游戏对象数组

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] 中使用的

渲染函数:

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);
}

【问题讨论】:

  • 您忘记显示GameObjects 是如何声明的以及您当前如何尝试对其进行初始化。
  • 您的声明“我使用多个带有数组的结构(无向量)”与您发布的定义相矛盾。并且在结构体中使用push_back 通常没有问题。
  • 我用更多信息编辑了帖子,我怎么能 push_back 一个结构?我试过了,VS 给了我错误“没有可用的合适的默认构造函数”,任何迷你示例都将不胜感激:)
  • 该错误可能意味着您正在 push_back 一个错误类型的对象,并且编译器试图为您构造正确的对象。 VBOvector&lt;GLfloat&gt;,所以你应该 push_back 一个 GLfloat。你正在 push_back vertex.xvertex.xGLfloat 吗?我猜不是这样,然后编译器试图找到一个GLfloat 构造函数,该构造函数将vertex.x 的类型作为参数。如果它丢失,它会给你一个错误。你能检查一下这是否正在发生吗?

标签: c++ arrays vector struct


【解决方案1】:

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 对象。

【讨论】:

  • 这可行,但是当我尝试加载具有 1450 个面的 cilinder 时,例如游戏 fps 降低到 1 或 2 FPS,问题不在于渲染功能,因为我在每个测试中只测试了 10 个循环主循环,它仍然滞后,这是我的渲染功能,后期编辑。在代码中只会绘制 10 个顶点,因为循环是 10 次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多