Vertex Array and indices

Started by
6 comments, last by venomelektro 19 years, 3 months ago
hi I try to build an index array from a geometry stored in a float* array my array is :

float *vertex_data;
ind Size_Vertex; // number of floats in array
I think my algorythm to build the indices in good but i didnt manage to display the mesh corectly


void VM2_Mesh::BuildIndices() {

	std::vector <unsigned int> ind;
	std::vector <Vector3> ReferencedVertices;
	Vector3 Cvertex;
	bool blisted;

	FILE *d = 0x0;
	int i,j,index;
	d = fopen("debug.txt","w");

	for ( i=0;i<Size_Vertex;i+=3) {

		// vertex_data is a float* array 

		blisted = false;
		Cvertex = Vector3(vertex_data,vertex_data[i+1],vertex_data[i+2]);

		for ( j=0;j<ReferencedVertices.size();j++) {

			if (ReferencedVertices[j] == Cvertex){
					blisted = true; // already listed 
					fprintf(d,"already listed @ j = %d (%.2f,%.2f,%.2f) === (%.2f,%.2f,%.2f)\n",
						j,ReferencedVertices[j].x,ReferencedVertices[j].y,ReferencedVertices[j].z,
						Cvertex.x,Cvertex.y,Cvertex.z

						);
					break;
			}
		}

				if (!blisted){

					ReferencedVertices.push_back(Cvertex);
					index = (ReferencedVertices.size() - 1); // we added at the end of the std::vector 
					ind.push_back( index ); // had this index to our index list 
					fprintf(d,"ADDING INDICE  index = %d (%.2f,%.2f,%.2f)\n",
						index,
						Cvertex.x,Cvertex.y,Cvertex.z);
				
				}

				else {
					ind.push_back(j);

					fprintf(d,"ADDING INDICE  j : %d\n",j);
				}
				

		} // end of for ..

	Size_Indices = ind.size();	
	indices = new unsigned int[Size_Indices];

	if (!indices)
		throw("failed to allocate memory for indices");



	for (int n=0;n<ind.size();n++) {
		indices[n] = ind[n];
		fprintf(d,"i,index no %d : %d\n",n,indices[n] );

	}

	fclose(d);

}


void VM2_Mesh::Render2() {

	        glEnableClientState( GL_VERTEX_ARRAY );
		glEnableClientState( GL_TEXTURE_COORD_ARRAY );
		glEnableClientState(GL_INDEX_ARRAY);
		glBindTexture(GL_TEXTURE_2D,texid); 
 
	        glVertexPointer  ( 3, GL_FLOAT, 0, vertex_data );
	        glTexCoordPointer( 2, GL_FLOAT, 0, text_data   );
		glIndexPointer(GL_UNSIGNED_INT , 0, indices ); 

		glDrawElements(GL_TRIANGLES, Size_Indices, GL_UNSIGNED_INT,indices);

		glDisableClientState( GL_VERTEX_ARRAY );
		glDisableClientState( GL_TEXTURE_COORD_ARRAY );
		glDisableClientState(GL_INDEX_ARRAY);

}

any idea ? [Edited by - venomelektro on January 17, 2005 10:10:01 AM]
Advertisement
glIndexPointer doesn't have anything to do with indices for the vertex array. It's the paletted color mode equivalent to glColorPointer which is used for RGB color mode. I really doubt you're running 8-bit paletted color modes or something, so remove glIndexPointer from your code.

And what do you mean by "didnt manage to display the mesh corectly"? Does it appear completely corrupted, or is the mesh itself correct but the texture is applied incorrectly? I would assume the problem is with the texture coordinates, because the code itself to merge indices looks correct to me. However, you only merge indices based on if the vertices spatial coordinates are equal, ignoring texture coordinates. If the two vertices you merge have the same spatial coordinates but different texture coordinates, then the merged vertex will have correct spatial coordinates, but wrong texture coordinates.

You should merge the index if, and only if, ALL attributes of the vertex are the same, not just the spatial coordinates.
Quote:Original post by Brother Bob
And what do you mean by "didnt manage to display the mesh corectly"? Does it appear completely corrupted, or is the mesh itself correct but the texture is applied incorrectly? I would assume the problem is with the texture coordinates, because the code itself to merge indices looks correct to me. However, you only merge indices based on if the vertices spatial coordinates are equal, ignoring texture coordinates. If the two vertices you merge have the same spatial coordinates but different texture coordinates, then the merged vertex will have correct spatial coordinates, but wrong texture coordinates.

You should merge the index if, and only if, ALL attributes of the vertex are the same, not just the spatial coordinates.



Thanx for replying,

I removed

	//	glEnableClientState(GL_INDEX_ARRAY);//etc..


and I get the same result

look at this picture

http://schafik.free.fr/img/vind.jpg

at left mesh with standard vertex array render method, no problem :

                glEnableClientState( GL_VERTEX_ARRAY );		glEnableClientState( GL_TEXTURE_COORD_ARRAY );		glBindTexture(GL_TEXTURE_2D,texid);  	        glVertexPointer  ( 3, GL_FLOAT, 0, vertex_data );	        glTexCoordPointer( 2, GL_FLOAT, 0, text_data   );                glDrawArrays( GL_TRIANGLES, 0, NumVertices );		glDisableClientState( GL_VERTEX_ARRAY );		glDisableClientState( GL_TEXTURE_COORD_ARRAY );


right , the new indexed method , corrupted

I agree with you , i should also compare texture coordinates but it doesnt seems to do only a texture coord problem

[Edited by - venomelektro on January 17, 2005 10:11:54 AM]
Still the same problem ,

even with texture coord checking ,

Can someone check my renderring code ,

I traced my index array in a text file and my algo seems to be correct

http://schafik.free.fr/img/debug.txt

struct OglVertex {	Vector3 Vertex;	Vector2 TextureCoord;};void VM2_Mesh::BuildIndices() {	std::vector <unsigned int> ind;	std::vector <OglVertex> ReferencedVertices;	Vector3 Vertex;	Vector2 Text;	bool blisted;	OglVertex VertexStruct;	FILE *d = 0x0;	int i,j,t,index;	d = fopen("debug.txt","w");	for ( i=0,t=0; i<Size_Vertex,t<Size_Texture; i+=3,t+=2 ) {		// vertex_data is a float* array 		blisted = false;		Vertex = Vector3(vertex_data,vertex_data[i+1],vertex_data[i+2]);		Text   = Vector2(text_data[t],text_data[t+1] );		VertexStruct.Vertex       = Vertex;		VertexStruct.TextureCoord = Text;		for ( j=0;j<ReferencedVertices.size();j++) {						if ( ( ReferencedVertices[j].Vertex == Vertex ) && ( ReferencedVertices[j].TextureCoord == Text ) ){					blisted = true; // already listed 					break;			}		}				if (!blisted){					ReferencedVertices.push_back(VertexStruct);					index = (ReferencedVertices.size() - 1); // we added at the end of the std::vector 					ind.push_back( index ); // add this index to our index list 													}				else {					ind.push_back(j);					}						} // end of for i ...	Size_Indices = ind.size();		indices = new unsigned int[Size_Indices];	if (!indices)		throw("failed to allocate memory for indices");	for (int n=0;n<ind.size();n++) {		indices[n] = ind[n];		fprintf(d,"index no %d : %d\n",n,indices[n] );	}	fclose(d);}void VM2_Mesh::Render2() {	        glEnableClientState( GL_VERTEX_ARRAY );		glEnableClientState( GL_TEXTURE_COORD_ARRAY );		glBindTexture(GL_TEXTURE_2D,texid);  	        glVertexPointer  ( 3, GL_FLOAT, 0, vertex_data );	        glTexCoordPointer( 2, GL_FLOAT, 0, text_data   );		glDrawElements(GL_TRIANGLES, Size_Indices, GL_UNSIGNED_INT,indices);		glDisableClientState( GL_VERTEX_ARRAY );		glDisableClientState( GL_TEXTURE_COORD_ARRAY );}
Can I please suggest you have a close look at the way you write code. That code you posted isn't consistent in indenting, the use of new lines, or in fact, any form whitespace, and it makes it a nightmare to read.

Anyway, you're not actually preserving the ReferencedVertices array you built inside the BuildIndices function. Maybe you meant to delete the current vertex_data array of vertices, and replace it with a new one filled with the contents of the ReferancedVertices vector at the end of the function, when you filled the new list of indices, but you're not actually doing it. You're probably using the indices you've just generated to index into the old list of vertices, which of course don't match up.
that s ok I fixed the problem

just need to add i/3 insteaf of j

if (!blisted){					ReferencedVertices.push_back(VertexStruct);					index = i/3;					ind.push_back( index ); 								}else   ind.push_back(i/3);
All you've done there is generate the indices from the original array of vertices, not the ReferencedVertices array you've been building. This works because you've forgotten to replace the original array of vertices with the contents of the ReferencedVertices array, as I mentioned above.
Yes, I ve noticed your message thanx

This topic is closed to new replies.

Advertisement