# Problem creating a TCastleScene by code

**URL:** <https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852>\
**Category:** Creating data (models, textures...)\
**Created:** [April 4, 2025, 12:16pm UTC](https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852 "2025-04-04T12:16:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [April 4, 2025, 12:16pm UTC](https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852/1 "2025-04-04T12:16:41Z")

</div>

I’m playing with the example in [Writing Code To Modify Scenes And Transformations](https://castle-engine.io/viewport_and_scenes_from_code). I 'm trying to create a scene from code using an old OpenGL 1.1 program I wrote time ago.

The problem is that I was using `GL_TRIANGLES` while Castle Engine seems to use `GL_TRIANGLE_STRIP` so when rendering the vertices seems in the correct places but the mesh is a mess (pun not intended) with all triangles overlapping. Am I right?

So is there a way to tell CGE to use `GL_TRIANGLES`?

Later I’ll dig in the sources and see if it’s possible to write my very own `TCastleScene` class. Maybe somebody can guide me in that travel…

---

<div class="post-metadata">

**Author:** ![kagamma](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/kagamma/32/226_2.png) [@kagamma](https://forum.castle-engine.io/u/kagamma)\
**Post date:** [April 4, 2025, 1:59pm UTC](https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852/2 "2025-04-04T13:59:58Z")

</div>

Replace `TIndexedFaceSetNode` (which is basically the equivalent of `GL_QUADS`) in the example with either [TTriangleSetNode](https://castle-engine.io/apidoc/html/X3DNodes.TTriangleSetNode.html) or [TIndexedTriangleSetNode](https://castle-engine.io/apidoc/html/X3DNodes.TIndexedTriangleSetNode.html) should work. Both use `GL_TRIANGLES` underneath.

---

<div class="post-metadata">

**Author:** ![NiunioMartinez](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/niuniomartinez/32/2232_2.png) [@NiunioMartinez](https://forum.castle-engine.io/u/NiunioMartinez)\
**Post date:** [April 4, 2025, 5:12pm UTC](https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852/3 "2025-04-04T17:12:48Z")

</div>

Thankyou kagamma. It works. 🙂

I’ll look for more “SetNode” classes. I didn’t realize that it should be more.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [April 4, 2025, 7:44pm UTC](https://forum.castle-engine.io/t/problem-creating-a-tcastlescene-by-code/1852/4 "2025-04-04T19:44:25Z")

</div>

Adding to @kagamma answer:

- While I also recommend to use `TTriangleSetNode` or `TIndexedTriangleSetNode` if you have only triangles, as it will be most straightforward…

- …note that `TIndexedFaceSetNode` shown in [Writing code to modify scenes and transformations | Manual | Castle Game Engine](https://castle-engine.io/viewport_and_scenes_from_code#_building_a_mesh_using_code) is also possible to use. `TIndexedFaceSetNode` renders a set of polygons, each polygon separated by -1 in the indexes. Polygons can be triangles, quads, or larger n-gons. They can be convex or concave (when `TIndexedFaceSet.Convex = true`, they must be convex; when it’s = false, they can be any kind). The example shows `TIndexedFaceSetNode` exactly because it allows to cover so many cases 🙂

E.g. modify `IndexedFaceSet.SetCoordIndex` from [Writing code to modify scenes and transformations | Manual | Castle Game Engine](https://castle-engine.io/viewport_and_scenes_from_code#_building_a_mesh_using_code) like this to have 3 triangles:

```auto
IndexedFaceSet.SetCoordIndex([
  0, 1, 2, -1, // 1st triangle
  3, 4, 5, -1, // 2nd triangle
  6, 7, 8, -1, // 3rd triangle (note: ending -1 is allowed but optional, doesn't really matter)
]);

```

See docs, in this case X3D spec for `IndexedFaceSet` node is helpful,

- linked from [Geometry3D component | Castle Game Engine](https://castle-engine.io/x3d_implementation_geometry3d.php)
- X3D spec: [Extensible 3D (X3D), ISO/IEC 19775-1:2023, 13 Geometry3D component](https://www.web3d.org/documents/specifications/19775-1/V4.0/Part01/components/geometry3D.html#IndexedFaceSet)
