Jump to content
  • Advertisement
Sign in to follow this  
giugio

Polygon class c++

This topic is 3746 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

Hy. I'm tring to create a good and fast c++ class for drawing polygons. The problem is this: Where create the load methods for fill the poly class as sample from a collada file? In the class? Is fast fill the poly class with method for add material,texture or vertex? And .. another question : Is better to inherith from the base class poly and create a texturepoly class ,or a materialpolyclass ad ex? thanks

Share this post


Link to post
Share on other sites
Advertisement
You should probably split your polygon class into multiple different classes. You could have a different class for loading your polygon data and drawing it.

Most file formats (e.g. collada, .obj, etc) give you the polygon data (vertex coordinate, normal vector, texture coordinates, tangent vectors, etc.) in a "planar" format (similar to planar image formats), so that you have a pair of data and indices for each vertex attribute, so that you have (vertexdata:vertex indices, normal data: normal indices, ...).

On the other hand, graphics API's usually want interlaved data, a pair of interleaved vertex data and indices there, eg (v0,n0), (v1,n1), (v2,n2): (i0, i1, i2), where v# are vertex coordinates, n# are normal vectors and i# are indices to these interleaved vertices.

For example, a textured cube has 8 vertices, 6 normal vectors and 4 texture coordinates. When these are interleaved, they will form 24 different vertices.

You can write separate classes for different vertex data representations and write a conversion algorithm from one format to the other.

You can then upload the data to vertex buffers and draw it efficiently. You can delete the original data if you don't need it any more.

Mesh data also varies in vertex formats. If you want to support multiple vertex formats you probably want store that with your mesh data.

Texture, material and other properties are better stored elsewhere.

It's probably not a good idea to write a basic mesh super class and extend as different properties are added. Better try to make small classes and combine them with a has-a relationship.

This is what I currently have:

class PlanarVertexData
{
class Plane
{
vector<float> data;
vector<short> indices;
}
List<Plane> planes;
VertexFormat format;
}

class InterleavedVertexData
{
vector<float> data;
vector<short> indices;
VertexFormat format;
}

class VertexView
{
VertexBuffer data;
IndexBuffer indices;
VertexFormat format;

VertexView(InterleavedVertexData data)
{
// upload data to vertex buffers
}

void draw(int firstIndex, int numIndices);
}


class MeshObject
{
class Element
{
List<Texture*> textures;
Material material;
int firstIndex, numIndices;
}

VertexView vertexView;
List<Element> elements;

void draw()
{
setupVertexFormat(vertexView.format);
for element in elements:
setupTextureUnits(element.textures);
setupMaterial(element.material);
vertexView.draw(element.firstIndex, element.numIndices);
}
}


Hope this gives you an idea how to get going.
-Riku

Share this post


Link to post
Share on other sites
Very Thanks Riku.

But I dont understand what are different vertex data representations ,sorry for my english.
You know a books or tutorial on this argument(c++ in graphics programming)?

[Edited by - giugio on August 1, 2008 5:46:09 AM]

Share this post


Link to post
Share on other sites
and another question:
I must implement a draw function for draw wireframe , one for draw selected and so on.
I need to implement 2 or more draw function in the poly class?
Thanks.

Share this post


Link to post
Share on other sites
What do you use? DirectX or OpenGL?
And Wireframe Mode can be achieved by setting a simple state like
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Don't know how to do this in DirectX :D.
regards.

Share this post


Link to post
Share on other sites
I don't really know a good tutorial on the subject, but once you study different mesh data formats and drawing with vertex and index buffers, I'm sure you can figure out what's the deal. Meshes are usually stored in the planar format because it's more space efficient and easier to edit while graphics API's use interleaved format for most efficient drawing.

For wireframe drawing, you can either use your graphics API's drawing functions (e.g. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) or whatever it was) or do another drawing function that draws line loops or similar. You may need another index buffer for this.

-Riku

Share this post


Link to post
Share on other sites
hy.
a user ask me this...
Quote:

My question was: if you draw using vertex arrays (which is also the default in Direct3D), how can you use more than one index array? As far as I know, this is impossible and you have to use one index buffer for all the vertices, normals, texture coordinates, etc...

Jeroen


this is the post:
http://www.gamedev.net/community/forums/topic.asp?topic_id=501821

"how can you use more than one index array"
what means use more than one index array?
wy?

Share this post


Link to post
Share on other sites
As riku said, when it comes to actually passing your data to the graphics API in the form of a vertex buffer, you can only do it as one big interleaved array (so Vertex Position1, Normal1, TextCoord1; Vertex Position2, Normal2, Texcoord2; and so on...) and then you can only have ONE index buffer.

Many data formats like Collada store the information in multiple separate arrays, like :

-VertexPosition1, VertexPosition2, VertexPosition3,...
-Normal1, Normal2, Normal3,...
-TexCoord1, TexCoord2, TexCoord3,...

and then you have one index buffer per array.

So for drawing collada geometry with DirectX or OpenGL, you need to convert the data from one format to the other.

Share this post


Link to post
Share on other sites
Hy again.
I have to refactoring my code because i would use VBO and lists.
The poligon class must be rewritten.
I could have only an interleaved array in the polygon class.
"Could" because i wite a collada importer , that fill a polygonData class with vertex,normals,colors and texture array.
In the collada importer i do all in sequential mode:
1)import vertex and store in vetexes array
2)import normals and store in normals array
3)import colors and store in colorsArray

4)now i set the indexes:the collada importer return me the data in this mode:
IndexVertexX,IndexVertexY,IndexVertexZ,IndexNormal1,IndexColor1 ecc...
now i must load from the arrays( 1)vetexes array, 2)normals array, 3)colorsArray ) the:
VertexX,VertexY,VertexZ,Normal1,Color1 ecc...
and create only the interleaved array for VBO and lists in the class polygondata
but where i put the vertexData( 1)vetexes array, 2)normals array, 3)colorsArray ) ? in the polygonData?or in the importer?
if i put all in the polygon data i have redundant information(arrays 1 2 and 3 and the Interleaved array for VBO).
Then i suppose that could be put in the importer , and when the importer has do his work can be deleted.
is done?
Thanks.

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!