How to create a cube in an opengl style way without VS.PRIMITIVE_QUADS

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By D2191
:warning: Old Version Published before Godot 3 was released.

Hi,

Im currently working on a project and looking for a way to dynamically generate shapes, Im aware there is a testcube object however im looking to generate some shapes using just the api. However there doesent seem to be a simple way to dynamically generate an immediategeometry object and create a cube mesh in the same way one would with opengl by using quads, or polygons…

I have tried the following:

var cube=ImmediateGeometry.new()
cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(1,1,-1))
cube.add_vertex(Vector3(-1,1,-1))
cube.add_vertex(Vector3(-1,1,1))
cube.add_vertex(Vector3(1,1,1))
cube.end()

cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(1,-1,1))
cube.add_vertex(Vector3(-1,-1,1))
cube.add_vertex(Vector3(-1,-1,-1))
cube.add_vertex(Vector3(-1,-1,-1))
cube.end()

cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(1,1,1))
cube.add_vertex(Vector3(-1,1,1))
cube.add_vertex(Vector3(-1,-1,1))
cube.add_vertex(Vector3(1,-1,1))
cube.end()


cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(1,-1,-1))
cube.add_vertex(Vector3(-1,-1,-1))
cube.add_vertex(Vector3(-1,1,-1))
cube.add_vertex(Vector3(1,1,-1))
cube.end()


cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(-1,1,1))
cube.add_vertex(Vector3(-1,1,-1))
cube.add_vertex(Vector3(-1,-1,-1))
cube.add_vertex(Vector3(-1,-1,1))
cube.end()

cube.begin(VS.PRIMITIVE_POINTS ,white)
cube.add_vertex(Vector3(1,1,-1))
cube.add_vertex(Vector3(1,1,1))
cube.add_vertex(Vector3(1,-1,1))
cube.add_vertex(Vector3(1,-1,-1))
cube.end()

and other variations using VS.primitive_triangles etc…however at most it displays only 1/2 of the entire cube due to it being triangles and not polygons/quads…Is there any other way to simply create a cube mesh using a similar style of api or will i have to use triangles and double up the code?

:bust_in_silhouette: Reply From: Zylann

There is no API for that yet (and quads got deprecated in OpenGL 3.x, used in upcoming Godot 3).
ImmediateGeometry and SurfaceTool don’t provide indexed geometry functions, so you have to write 6 vertices per cube face.

You can generate a Mesh resource using indexed vertex arrays though, using this method: Mesh — Godot Engine (stable) documentation in English
Its documentation is confusing though, here is an example:

# I know it works in master, didn't test in 2.x
var mesh = Mesh.new()
var arrays = []
arrays.resize(9) # Mesh.ARRAY_MAX, not available in Godot 2.x
arrays[Mesh.ARRAY_VERTEX] = your_vertices
arrays[Mesh.ARRAY_INDEX] = your_indices
mesh.add_surface(Mesh.PRIMITIVE_TRIANGLES, arrays)

Which allows you to get the vertex count from 36 down to 8.

That’s a bit lower-level than GL1/GL2-style, but in GL3 this meshing style went away. If you want it still, a helper can be made but it’s going to end up as indexed triangles in the end.