Let me start off my saying I know this is a bit off topic but Ive asked every where else and no one has been able to answer me. I am hoping someone here can.
It was recently suggested to me that I have a central rendering engine for all my other engines. I have sucessfully created this engine but while converting Q3BSPFace to my facetype, VertexBuffer I ran into some errors. The conversion of the facetypes themselves goes fine, its just when rendering them that the program crashes. What is happening is that when I call glDrawElements to render my face the count is set to faces[i]->meshvertcount but there are only lfaces[i].vertcount verts in each face. In plain english meshvertcount is bigger then vertcount for each face, so glDrawElements goes out of bounds. I tryed changing the number of verts to meshvertcount but I get the same results.
Here is the code
This is my Facetype VertexBuffer
struct VertexBuffer
{
~VertexBuffer()
{
if(vert)
{
delete vert;
}if(normal) { delete [] normal; } if(texcoord) { delete [] texcoord; } if(lightmapcoord) { delete [] lightmapcoord; }
}
Vect3* vert; //holds all the verts
Vect3* normal; //holds all the normals
Vect2* texcoord; //tolds the texture coords
Vect2* lightmapcoord; //holds the lightmap coords
int count; //the number of items in the top 4 arrays
BYTE colors[4]; //hold the color of the face(RGBA)
UINT* textureID; //the texture ID for the face
UINT* lightmapID; //the lightmap ID for the face
int type; //the type of face it is
int shaderindex; //the index for the shader
int index; //index
int meshvertcount; //the number of mesh verts
};
This is my conversion code
//alloc the memory
facecount = lfacecount;
faces = new VertexBuffer[facecount];//go threw each face
for(int i=0;i<facecount;i++)
{
//put all the indices of each BSPFace into VertexBuffer
faces[i].meshvertcount = lfaces[i].meshvertcount;
faces[i].index = indexarray[lfaces[i].meshvertindex];//copy the lightmap and texture IDs over faces[i].textureID = &texture[lfaces[i].textureID]; faces[i].lightmapID = &lightmaps[lfaces[i].lightmapID]; //the the size of our coords faces[i].count = lfaces[i].vertcount; //alloc the memory for all our coords faces[i].lightmapcoord = new Vect2[faces[i].count]; faces[i].normal = new Vect3[faces[i].count]; faces[i].texcoord = new Vect2[faces[i].count]; faces[i].vert = new Vect3[faces[i].count]; //here is where the problem may be int k = 0; //copy the values over for(int j=lfaces[i].startvertindex;j<(lfaces[i].startvertindex+faces[i].count);j++,k++) { faces[i].lightmapcoord[k] = verts[j].lightmapcoord; faces[i].normal[k] = verts[j].normal; faces[i].texcoord[k] = verts[j].textcoord; faces[i].vert[k] = verts[j].pos; }
}
As you can see above I only copy the values from startvertindex to startvertindex+vertcount over to my face type. I dont understand how this is too few since im getting this number from BSPFace.
This is the code I use to render
//render the faces
void CRender::Render()
{
//go threw all the faces and render them
for(int i=0;i<facecount;i++)
{
//set the texture
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *faces[i]->textureID);//set the lightmaps up glActiveTextureARB(GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, *faces[i]->lightmapID); //set the vertex pointer for each face glVertexPointer(3, GL_FLOAT, sizeof(VertexBuffer), &faces[i]->vert); //assign texture pass to point to normal texture coords glClientActiveTextureARB(GL_TEXTURE0_ARB); glTexCoordPointer(2, GL_FLOAT, sizeof(VertexBuffer), &faces[i]->texcoord); //assign lightmap pass to point glClientActiveTextureARB(GL_TEXTURE1_ARB); glTexCoordPointer(2, GL_FLOAT, sizeof(VertexBuffer), &faces[i]->lightmapcoord); //draw it //the problem is here glDrawElements(GL_TRIANGLES, faces[i]->meshvertcount, GL_UNSIGNED_INT, &faces[i]->index);
}
}
Since meshvertcount is always bigger then vertcount I always go out of bounds.
If it helps here is the complete(linux, should work on any other os though) source.
I really cannt figure this one out, can someone please help me?
Thanks
Nuke
[This message has been edited by nukem (edited 10-19-2003).]