pygame is
Simple DirectMedia Layer is
Site Swing
An attempt at 3D

An attempt at 3D - 1.5

David (animatinator)



Just a test of the 3D maths I've been attempting to learn recently, which uses only pygame.draw.aaline() - and circles for drawing the points, but that doesn't really count ;-). I cheated slightly by making it simply project the 3D points onto the XY plane so that they're easily mapped to screen co-ordinates, which means that the camera is fixed, but I'll probably change that when I work out how to :P



This is my attempt at writing a 3D program using only basic drawing functions (i.e. pygame.draw.aaline(), pygame.draw.polygon()). As mentioned above, it has a fixed camera but the object in view can be rotated by dragging it with the mouse.

New in this release

  • Solid face rendering: Faces are also drawn in the right order, thanks to a hackish method I devised which takes the average distance from the camera of each of the vertices in a face and then orders them by depth. You can switch between solid and wireframe rendering with the Z-key.
  • Multiple objects: Are also drawn in the right order now, using a similar method (but using an average of all the vertices in the object). This could probably be done much more quickly with a better algorithm, but as this is only a test I'll just leave it in.
  • Interactivity: Now you can rotate the shape in the view by dragging it with the mouse!
  • An attempt at lighting: I also included a dodgy algorithm which lights faces based on their distance from the camera. At least, it was supposed to do that; it seems more likely that it's based on whatever mood the Python interpreter's in at the time of running. But it looks okay, so I'll just leave it be for now.
  • Now supports pyramids as well as cubes: A friend of mine added in a pyramid primitive, which can be used in conjuntion with the cubes to make more nifty shapes.
  • A more nifty default shape: The same friend of mine also made a funky robot shape using several cubes and pyramids.


Home Page:


click to view original size


An attempt at 3D - 1.5 - Jan 18, 2009
An attempt at 3D - 1.0 - Jan 8, 2009 account Comments

If you wish to leave a comment with your account, please sign in first.

August 15, 2009 2:37pm - David - nickname: (animatinator)
A lot less [fiddly] to install, I mean.
*should check to make certain that he hasn't missed entire words out before posting a comment*
August 15, 2009 2:36pm - David - nickname: (animatinator)
effeeme: Aah, that's clever, thanks :) I didn't think of that at the time.

saluk: Yup, that's the wonky algorithm's fault - it decides on one value for how far away an object is based on the average distance to each of its points, and so any objects that intersect will be drawn incorrectly.

raubana: I've actually been trying OpenGL in C++ recently, and I'd agree that it's definitely faster and quite a bit easier. It's still fun to try writing the rendering stuff yourself though, even if it does end up ridiculously slow and glitchy :P

pywiz32: Thanks :) That idea for the images is pretty much what I would do, although I think it'd be stupidly slow for a high-poly model with several textures. I have a rough idea of how it's done in real 3D engines, but I've never really tried doing it myself - mainly because I probably lack the mathematical calibre ;-).

yanom: I would if I knew how to do things like shadows, texture mapping, rotations about arbitrary points and suchlike; in fact I intend to one of these days. However it would actually be a *lot* slower, mainly because whilst OpenGL is heavily optimised and makes full use of graphics hardware for speedy rendering, my approach is software-based, and written in a scripting language :D
It would be a lot less to install without all the messy OpenGL stuff though.
August 1, 2009 2:57pm - yanom - nickname: (yanom)
are you planning on making a 3d library out of this? it seems like it'd be more efficient in terms of computer rescources because it doesnt require opengl
January 23, 2009 6:46pm - Anonymous - nickname: (pywiz32) - 4/5
WOW! This is really good. Especially for the second release. My guess at doing images: rotate + unfiltered scale.
January 20, 2009 9:09pm - Dylan J. Raub - nickname: (dylanjraub)
Yeah, 3d is hard for me, too. I tried OpenGl for python a long time ago without success, but recently I got help from a couple of executables. It's supper fast and much MUCH easier!
January 19, 2009 1:47pm - patrick mullen - nickname: (saluk)
Sorting doesn't work in all cases, if you rotate in a certain way, the neck is drawn in front of the head.
January 18, 2009 1:05pm - Francesco Mastellone - nickname: (effeemme)
Face lightning, at its simplest, is calculated by multiplying each channel of the color of the face by the result of the dot product between the so called normal vector of the face and a vector of length 1 which defines the direction of the light. You might want to try that out.
January 18, 2009 9:38am - Nathan Woodward - nickname: (thedalailama)
Heh, it looks like the tin man.
January 17, 2009 10:37am - David - nickname: (animatinator)
Thanks for the comments :)
I've now added solid rendering of faces, with a lighting algorithm that doesn't actually work as it's supposed to (but looks okay, so I'm keeping it for now :P ). I'll upload the new .py later, once I've fixed a few bugs :)

@magicaxis: I'm not sure what you mean by missing a word, class Cube() is the class used to create and represent the vertices and edges of the cube.
January 16, 2009 4:07am - Adam Halley-Prinable - nickname: (magicaxis)
(or im an idiot and missed something)
January 16, 2009 4:06am - Adam Halley-Prinable - nickname: (magicaxis)
whats with this?

class Cube():

ur missing a word
January 13, 2009 11:59pm - patrick mullen - nickname: (saluk)
These kinds of experiments sure are fun! Code looks pretty clean.
January 9, 2009 7:19am - Nathan Woodward - nickname: (thedalailama)
Heh, I made one of these about a year ago, yours is much clearer and less dodgy.
January 9, 2009 2:20am - Ian Mallett - nickname: (geometrian)
Pretty neat--reminds me of some code I wrote a while back.

our projects welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
recent releases
Feb 21, 2017

Jan 31, 2017

Jan 24, 2017

Jan 18, 2017

Jan 7, 2017

Dec 30, 2016

Dec 8, 2016

Nov 28, 2016

Nov 27, 2016

... more!
for pygame related questions, comments, and suggestions, please see help (lists, irc)