500 Lines or Less A 3D Modeller aosabook.org · 14 points · 0 comments · HN threa...
HN Best 200 @best
500 Lines or Less A 3D Modeller aosabook.org · 14 points · 0 comments · HN thread
TL;DR A chapter from the 500 Lines or Less book series that builds a tiny 3D modeller in under 500 lines of Python, using legacy OpenGL and GLUT. It covers the three basics every CAD tool needs: a scene-graph data structure, rendering to screen, and user interaction such as picking, moving, scaling, coloring and placing shapes.
- CAD tools need three basics: a data structure for the design, a way to display it on a 2D screen, and a way to interact with it.
- The project uses legacy (fixed-function) OpenGL to keep code small, with GLUT for window management and a trackball class from Glumpy for rotation.
- Scene contents form a composite tree of Node objects (primitives Cube and Sphere, plus HierarchicalNode and a SnowFigure made of three spheres), rendered depth-first with a matrix stack.
- Picking uses a ray cast from the mouse against each node's axis-aligned bounding box, a fast approximation that gives wrong hits at sphere corners.
- Interaction is split into an Interaction class that translates GLUT events into application-level callbacks (pick, move, place, rotate_color, scale), so the toolkit could be swapped out.
- The author notes production modellers would need a far more sophisticated event system and full UI, and points to Blender and OpenSCAD as real-world references.
Discussion No comments have been posted on this story yet, so there is no discussion to summarize.
#3DModelling #OpenGL #Python #CAD #ComputerGraphics #Tutorial