C++ 结构和数组限制问题
我一直在使用我的模型加载器,我遇到了一个错误,因为我使用了多个带数组的结构(没有向量),我想在使用它之前初始化数组,因为当我尝试使用来访问带括号[]的向量数组时,程序给了我错误 游戏对象结构:C++ 结构和数组限制问题,c++,arrays,vector,struct,C++,Arrays,Vector,Struct,我一直在使用我的模型加载器,我遇到了一个错误,因为我使用了多个带数组的结构(没有向量),我想在使用它之前初始化数组,因为当我尝试使用来访问带括号[]的向量数组时,程序给了我错误 游戏对象结构: struct GameObject { int ID, parent; string tag; Mesh_t Mesh; Vector3 position; Vector3 scale; Quaternion rotation; Color colo
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;
};
由于Faces向量数组未在任何地方初始化,当程序到达以下代码时,Visual Studio向我抛出运行时错误:这是错误
我试图初始化向量数组,但我不知道如何初始化,因为向量数组中有其他结构,我无法推回结构数据。有什么想法吗?提前谢谢
编辑:
这是.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]
渲染功能:
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);
}
void GLRender()
{
glBindTexture(GL_TEXTURE_2D,0);
RenderSpaceLines();
绘制线(向量3(0,0,0),游戏对象[0]。位置);
glEnableClientState(GL_顶点_数组);
glEnableClientState(GL_纹理_坐标_阵列);
for(int i=0;i0)
{
for(int t=0;t<10;t++)
{
glVertexPointer(3,GL_FLOAT,0,(GLfloat*)游戏对象[i].Mesh.Faces[t].VBO.data();
glTexCoordPointer(2,GL_FLOAT,0,(GLfloat*)游戏对象[i].Mesh.Faces[t].Texture.VBO.data());
glBindTexture(GL_TEXTURE_2D,TextureManager.GetTextureFromName(游戏对象[i].Mesh.Faces[t].TEXTURE.TextureName));
glPushMatrix();
glPushAttrib(GL_颜色_缓冲_位| GL_深度_缓冲_位);
如果(游戏对象[i]。父对象==-1)
{
glTranslatef(GameObjects[i].position.x,GameObjects[i].position.y,GameObjects[i].position.z);
glRotatef(GameObjects[i].rotation.x,1,0,0);
glRotatef(游戏对象[i].rotation.y,0,1,0);
glRotatef(GameObjects[i].rotation.z,0,0,1);
}
else///实体与另一实体的相对性
{
glTranslatef(GameObjects[GameObjects[i].parent].position.x,
GameObjects[GameObjects[i].parent].position.y,
GameObjects[GameObjects[i].parent].position.z);///设置世界相对位置
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(游戏对象[i].position.x,
游戏对象[i].position.y,
游戏对象[i].position.z);
}
glScalef(游戏对象[i].scale.x,
游戏对象[i].scale.y,
游戏对象[i].scale.z);
GLDrawArray(游戏对象[i].Mesh.Faces[t].Geometry,0,游戏对象[i].Mesh.Faces[t].VertexType);
glPopMatrix();
}
}
}
glDisableClientState(GL_顶点_数组);
glDisableClientState(GL_纹理_坐标_数组);
}
Faces
实际上是在Mesh\u 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
对象。您忘记了显示GameObjects
是如何声明的,以及您当前试图如何初始化它。您的声明“我使用多个结构和数组(没有向量)”与您发布的定义相矛盾。使用结构体的push_back
通常没有问题。我用更多信息编辑了这篇文章,我怎样才能推回结构体?我试过了,VS给我的错误是“没有合适的默认构造函数可用”,任何小例子都会很感激:)这个错误可能意味着你正在推回一个错误类型的对象,并且编译器试图为你构造正确的对象VBO
是一个向量
,因此您应该向后推一个GLfloat
。您正在向后推_vertex.x
。vertex.x
aGLfloat
?我猜不是,然后编译器试图找到一个GLfloat
构造函数,该构造函数将vertex.x
类型作为参数。如果它丢失了,它会给你一个错误。你能检查一下是否发生了这种情况吗?这是可行的,但当我尝试加载1450张脸的cilinder时,例如游戏的fps降低到1或2 fps,问题不在渲染函数上,因为我在每个主循环中只测试了10个循环,它仍然滞后,这是我的渲染函数,后期编辑。在代码中只会画10个顶点,因为循环是10次。
GameObjects[GameObjectsCount].Mesh.Faces.resize(k + 1);
GameObjects[GameObjectsCount].Mesh.Faces[k].VBO.push_back(vertex.x);
// ...