Polygon class c++

Started by
13 comments, last by haegarr 15 years, 8 months ago
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
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
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]
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.
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.
If you say "pls", because it is shorter than "please", I will say "no", because it is shorter than "yes"
http://nightlight2d.de/
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
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?
up
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.
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.

This topic is closed to new replies.

Advertisement