• 11
• 9
• 10
• 9
• 10
• entries
155
100
• views
94118

# Geometry ahoy!

79 views

Ok, so I'm not happy with my geometry package. It's responsible for representing geometric objects, such as boxes, spheres and triangle meshes. Here's how you use geometry at the moment, creating a node rendered as a sphere, and collision geometry of the same shape:

geom = Sphere(1) # 1 is radius of the spheregeomNode = Node()# 20 and 20 are the amount of horizontal and vertical slicesgeomNode.render = geom.createPrimitive(20, 20).rendergeomNode.collisionGeometry = geom.createCollisionGeometry()

This has two problems: you can't change collision geometry after you've created it, and geometry has too many responsibilities (geometry classes should be low-level, not caring about graphics and physics). What I'm thinking now is to:
- Have geometry objects create only vertices; normals and texture coordinates can be created afterwards
- Have a factory function in the physics package for creating collision geometry, which would return instances of collision geometry classes

You would use it somewhat like this (I haven't designed the API yet, just pulling something out of my nonexistent hat):

geom = Sphere(1)vertices = geom.generateMesh(20, 20)normals = generateNormals(vertices, smooth=False)texCoords = generateTexCoords(vertices, type="spherical")primitive = RenderPrimitive(vertices, normals, texCoords)geomNode = Node()geomNode.render = primitive.rendergeomNode.collisionGeometry = physics.createCollisionGeometry(geom)

More complicated? Yes. More flexible? Yes! Will I provide a higher level interface too? Yes! Will you use mostly use model loading anyway, instead of geometry objects? Yes! The smooth parameter to generateNormals() would dictate if we are creating vertex or face normals. The type parameter to generateTexCoords() specifies the type of projection (spherical, cylindrical, planar etc). Or there could be separate functions or classes for different projections. I dunno... as I said, haven't designed the interface yet.

Maybe geometry could also optionally create normals and indices, since it's already doing the necessary calculations when generating vertices. Like this: vertices, normals, indices = geom.generateMesh(20, 20, normals=True, indices=True)

(Hmm... maybe I need to change node.render back to an object with a render method instead of a callable, since later I will need the bounding radius of renderables for frustum culling etc...)

Btw. I'll see how easy it would be to create a HTML/CSS TODO list generator with Python. :) I'll post the results or report of my failure later.

EDIT: Ok, I was truck with a Revelation about this, and I think it would be the cleanest way. I was just thinking... how often do you need to render geometry primitives, and does it make any sense to actually keep render geometry and collision geometry in a single class? I have the answer for you: no it doesn't. So, here's aboutish how it will work:

# Alternative 1vertices, normals, texCoords = graphics.geometry.createSphere(1, normals=True, texCoords=True)# Alternative 2vertices = graphics.geometry.createSphere(1)normals = graphics.geometry.generateNormals(vertices, smooth=True)texCoords = graphics.geometry.generateTexCoords(vertices, "spherical")primitive = RenderPrimitive(vertices, normals, texCoords)geomNode = Node()geomNode.render = primitivegeomNode.collisionGeometry = physics.geometry.Sphere(1)

Note that in this example, node.render is an object with a render() method again, not a callable.

There are no comments to display.