0 votes

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?

in Engine by (12 points)

1 Answer

+1 vote

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: http://docs.godotengine.org/en/stable/classes/class_mesh.html?highlight=Mesh#class-mesh-add-surface
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.

by (27,897 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.