Hello,
I’m discovering CGE and I would like to use it to program a viewer for polyhedron. But I don’t want to load a external file. It is possible to build by code a 3D object if I have all the vertices coordinnates and if I also know which vextices connect to build each face ? I would like to control by code the color of each face and be able to draw or not a line for each visible edge ?
I don’t ask detail code just know if it seems possible. I can more or less do it for a test with Lazarus and OpenGL but OpenGL is not always easy to use so I imagine that CGE classes are great to do that much more easily.
Thank you for your answer
Have a good day
Alexandre
Hello!
Yes, everything that can be displayed in Castle Game Engine can be also built by code.
Our manual page Writing code to modify scenes and transformations documents it in details, in particular the section there “11. Building a mesh using code” exactly shows a code that builds a mesh using Pascal code. You define yourself the vertexes, and how they are connected to display polygons.
In the end, you will have TCastleScene instance with the nodes that define the mesh, and you can use it just like any other TCastleScene instance where contents would be loaded from 3D file (like X3D or glTF).
So I advise to follow the manual from start, at least up to this chapter 
The “nodes” that are used to define it, like TIndexedFaceSetNode, are X3D nodes. So for many questions like “how to display X” an answer will be like “use X3D node like Y” 
-
Each X3D node is a Pascal class and is documented (briefly) in our docs and in depth in the X3D specification. For example, TIndexedFaceSetNode is here: in Pascal, in X3D spec. Reading this shows you the full capabilities – all of these X3D things can be created/modified by Pascal code.
-
To display edges, you would create TIndexedLineSetNode.
( To display edges easier, one could also activate TCastleScene.RenderOptions.WireframeEffect, but this makes the wireframe have the same color. From your description, I understand you need more flexibility, so this is not an option for you. )
-
To determine colors per face you would set TIndexedFaceSetNode.Color to an instance of TColorNode and set TIndexedFaceSetNode.ColorPerVertex to false. Similar approach can be used to modify colors for lines.
I hope this is good to get you started 
Thank you very for your detailed answer, it’s very kind.
Have a good day
Alexandre
1 Like
Hello,
I need a small more information 
I saw that the Gtlf file format absolutly need triangle faces and I understood why. Obj file format doesn’t have this" “limitation”. When I load a .obj file in castle-model-view, with for example pentagonal faces, and I choose “solid wireframe” I see that every face is in fact transformed in a set of triangles …and it’s not what I want to be able to do.
So here is my essential question
…. by code is it possible to define a no triangle face and see edge of this face ? If I read the example to gace me to read in the previous message; I guess that the answer is yes ?
IndexedFaceSet.SetCoordIndex([0, 1, 3, 2, -1, 2, 3, 4, 5, -1, 5, 4, 6, 7, -1]);
Here there are 4 points faces between -1 value, that’s it ?
Thank you again for your answer.
Have a good day and hello from France 
Alexandre
For rendering, the polygons (anything with more than 3 vertexes) are transformed into triangles. (Some OpenGL versions supported also natively quads, but not OpenGLES.)
If you choose to display the wireframe using TCastleScene.RenderOptions.WireframeEffect and you look at mesh with polygons (like TIndexedFaceSetNode) then indeed you will see this (somewhat internal) fact. We have a TODO to improve the wireframe look, to not show these “internal edges” effectively, see roadmap item, related news a few months ago. But, long story short, it’s not finished – so by using TCastleScene.RenderOptions.WireframeEffect , you will see these internal edges.
The way to avoid them:
-
Build the TIndexedLineSetNode or TLineSetNode yourself. From your original description, if you want to e.g. selectively change edges colors, I guessed yo will want to do it anyway
So this means you have 2 geometry nodes for one mesh: one with faces, one with edges. If the edges have sufficient thickness (which you can influence e.g. using MyScene.RenderOptions.LineWidth ), then they will be visible “on top” of the mesh.
-
A little easier: duplicate the mesh (like TIndexedFaceSetNode) but set the TAbstractShapeNode.Shading to shWireframe on the mesh’s copy. This internally does something similar to above: it builds an TIndexedLineSetNode or TLineSetNode to display the wireframe, and it’s intelligent to avoid showing the internal edges.
1 Like
Thanks again for your second precious answer. I can now read the coordinnates from a text file, build the mesh and have only the edges I want with the “duplicate the mesh” solution.
It seem’s that CGE will be able to deeply help me to create this little program about polyhedra I want to do since a long time now. Because polyhedra are just beautiful to look at and to build 
Thank you again for your genius work.
Alexandre
1 Like