Polycode : create a code generated mesh

I’ve been working a lot with Polycode during the last few days, just to satisfy my curiosity regarding this easy to use C++/LUA framework. Despite the few problems on new Visual Studio editions, I got it running pretty easily and got to do a lot of fun stuff pretty fast.

Let’s be clear, Polycode is still young and is clearly oriented towards fast prototyping and intuitive realtime output, so I might just be pushing it in the opposite direction it is meant for. I also decided to disregard the LUA part, because I needed to get back into C++, and I like doing stuff the hard way. So that leaves me without any player, helper, or custom compiler of any sort. Despite that, Polycode is a real pleasure to use.

One thing I like to try on 3D frameworks is code generated meshes : define your vertices in code and create objects on the fly. What’s the point? Well for instance, take things like quadtrees that tend to evolve a lot during runtime, you can’t just load a model and expect it to change by itself. Polycode doesn’t really seem ready for this kind of stuff, as a lot of operations are done CPU side, and vertex creation is freaking slow. Anyways, to show the technique I used, we’ll create a simple face with a texture. It might not be the best way to do this, but since documentation is pretty sparse (not to say totally absent) on the matter, I interpreted the generated doc as best as I could.

I’m going to assume you already know how to setup a Polycode project with C++. There are 3 important objects we’ll be using to create our entity :

1
2
3
4
5
Polycode::Polygon *p = NULL; // the polygon that will hold our vertices
Polycode::Mesh *baseMesh = NULL; // the mesh that will hold the polygon
Polycode::SceneMesh *sceneMesh = NULL; // the object that will render the mesh to the scene

Polycode::Scene* scene = new Scene(); // the scene, duh

You’ll notice I explicited the namespace. This is because it conflicts with another Polygon class that Visual Studio forces on C++ projects. Now an important thing : when declaring a Polycode::Mesh instance, you get to choose the mesh type, and everything depends on that. For the most part, the type defines how the vertex list is interpreted and how it is drawn. There’s an awesome type called Polycode::Mesh::QUAD_MESH that takes vertices by groups of 4 and creates a neat quad. But in reality, wireframe is pretty horrible, and I wouldn’t recommend it for deployment. I tend to use the classic Polycode::Mesh::TRI_MESH that takes vertices by groups of 3 to create triangles. It requires more vertex definitions, but it’s more natural for the GPU, because remember kids, your GPU draws triangles in 3D, and nothing else. There are many more types, including dot and line renders, triangle strips and so on, so don’t hesitate to check them out depending on your scenario.

So, back to our vertices, because we still don’t know how to create them. It’s pretty straightforward, the Polygon class is neat :

1
2
3
4
5
6
7
8
p = new Polycode::Polygon(); // instanciate our uber polygon, no parameters
// add our vertices (x,y,z,u,v)
p->addVertex(-1, 0, -1, 0, 1); // there's also a Polycode::Vertex type, and this method returns a Vertex*
p->addVertex(-1, 0, 1, 0, 0);
p->addVertex(1, 0, -1, 1, 1);
p->addVertex(-1, 0, 1, 0, 0);
p->addVertex(1, 0, 1, 1, 0);
p->addVertex(1, 0, -1, 1, 1);

I used a counter clockwise vertex declaration, as this is the default culling option in most situations in 3D development. There is a backfaceCulling property on Polycode entities if you wish to decull the other side, but by default, it is set to false. So these 6 vertices define a quad object that I can assign to my baseMesh and render via my sceneMesh. Easy, right?

1
2
3
4
5
baseMesh = new Polycode::Mesh(Polycode::Mesh::TRI_MESH); // instanciate our mesh with our triangle type
baseMesh->addPolygon(p); // append the polygon to its structure
sceneMesh = new Polycode::SceneMesh(baseMesh); // instanciate our scene mesh with our mesh
sceneMesh->loadTexture("doge.png"); // we'll just load a texture to see if it's working, no shaders
scene->addChild(sceneMesh); // now the scene will render the scene mesh

That’s about all, if you’ve done everything right (that is, also setup the camera to a position and look at (0,0,0)), you should see a magnificent quad on your screen.

Doge
Doge Wireframe

If you got this far, I guess there’s not much else to say. No, seriously, for instance I haven’t seen any index buffer, which is critical in this case since vertex creation is way too slow. I suspect Polycode::Mesh::QUAD_MESH to make use of indices, but nothing is explicit so I cannot say for sure. The fact remains that this is all too slow and a bit cranky regarding workflow. So this is basically experimenting and definitely not ready for production of any sort. Stay safe, don’t do this at home.

Tuxic