Jump to content
  • Advertisement
Sign in to follow this  
smally

OpenGL Read data from file, to display

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im having a problem with how to read in vertex and index data from an XML file and display via OpenGL. Previously I hade been doing this (not reading data from xml) // A cube // { posX, posY, posZ, normX, normY, normZ } static const float vertices[24][6] = { { ... }, { ... }, { etc... } }; const int nVertices = 24; static const int indices[36] = { 0, 1, 2, ... }; const int nIndices = 36; // draw cube using Begin, End, Normal3*, Vertex3* glBegin( GL_TRIANGLES ); for ( int i=0; i<nIndices; i++ ) { glNormal3fv( vertices[ indices ] ); glVertex3fv( vertices[ indices ] ); } glEnd(); However I am now reading in from an XML file which reads (this is a small one): <mesh name="tri"> <vertexArray count="3"> -1.0 0.0 -1.0 0.0 -1.0 0.0 1.0 0.0 -1.0 0.0 -1.0 0.0 0.0 0.0 1.0 0.0 -1.0 0.0 </vertexArray> <indexArray count="3" primitive="triangles"> 0 1 2 </indexArray> </mesh> I can read XML no problem. It's just this is done in a class function and I'm trying to read the data into: float vertices , which is a member of the class (it wasn't previously). But i dont know how to put the data into vertices because I can't preset the array with how many vertices there are (vertices[nVertices][6] a no-no) Can anyone help me ? (I hope I've described this well enough)

Share this post


Link to post
Share on other sites
Advertisement
std::vector allows you to "push" onto it's own list. This means you don't need to know the number of vertices beforehand as you can just keep dropping them onto the vector.


typedef std::vector<float> Vertex;

Vertex VertexList;
// You can create one for each axis if you like.




Then as you're reading them in, it helps to be able to differentiate between the different dimensions you're using. (So if it reads x, y, x, y, x, y.. or x, x, x, y, y, y:

If I read your correctly it's going x, y, z, x, y, z..
So Vertex[0] = x, Vertex[1] = y, Vertex[2] = z and then back to x.



// Obviously not exactly what you should put, this is just the general idea.
while(!EndOfXMLData)
{
VertexData = XMLReader.GetVertex();
VertexList.push_back(VertexData);
}

// To Recall your data is the exact same way as an array.
glBegin( GL_TRIANGLES );
for ( int i=0; i<nIndices; i++ )
{
glNormal3fv( VertexList );
glVertex3fv( VertexList );
}
glEnd();




Like I said, this isn't exact, but should give you the general idea of where to go with it :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!