pygame is
Python
Simple DirectMedia Layer
 
 
pygame.org is
Site Swing
soft3d

soft3d

patrick mullen (saluk)

Tags:

Description

Eventually I need to incorporate some 3d effects into one of my projects, but the 3d I need is fairly low poly and only used sparingly. It doesn't seem worth the effort to convert the entire huge system to opengl, as I will lose compatibility. So I thought, why not just render 3d in software.

So far this is just an experiment, but it almost has everything I need. I have optimized for speed as much as I think I can.

Controls:

* arrow keys - move 4-sided box around the screen
* a/z - zoom in and out - don't zoom in too far it may crash
* r,t,y - rotate the box on it's axis

Features:
* surfarray rendering for fast pixels
* 3d math only done on triangle corners
* texture mapped triangles, triangulated quads
* depth buffer for hidden surface removal
* pixel clipping

Changes

First release

Shows a 3d texture mapped 4-sided box with one of it's faces scrolling the texture coordinates

I'm curious what kind of speeds you guys get, as well as if anyone has any ideas for further optimization. Short of a c module of course, which it might become eventually.

Links

Home Page: http://code.google.com/p/pygame-soft3d/
Source: http://pygame-soft3d.googlecode.com/files/soft3d-v0.zip

Screenshot


click to view original size

Releases

soft3d - 0 - Oct 20, 2010

Pygame.org account Comments

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

October 24, 2010 3:41pm - patrick mullen - nickname: (saluk)
Neat idea RB[0]. I'm not sure how much it would help - for a given resolution, I still have to copy every pixel in the array. If I am using bigger pixels, like 2x2 pixels, I still have to copy over 4 entries in the array. Moving that copy out of the loop might help. Also really like the idea of decreasing resolution as object's are closer. Might not work so well for scenes, but could be viable for a simple object viewer.

Yep, already using psyco.

My new version loads OBJ files, and can handle about 1k-2k polygons at 20fps with full zoom, so it's definitely more fill-rate bottleneck than polygon bottleneck, which tells me I am near the limit. Can't very much get away from putting pixels on the screen :)

I'm also sorting polygons so closer ones are drawn first, which can help prevent it from trying to draw pixels that are covered.

Frustrum clipping will help too, as I take a pretty big hit if you zoom too far in - while offscreen pixels are not blit, viewing the lower right corner of a very large rectangle for instance still results in a lot of iterations.
October 23, 2010 7:51pm - RB[0] - nickname: (roebros)
Are you using psyco?
Also, perhaps also set a pixel size, ie:
If you are zoomed out to the point that it is a 1-1 or less ratio for pixels to box size, pixel size is 1 - however zooming in it is larger, so instead of drawing a single pixel and iterating over the entire thing, and 10x10x10 box zoomed to 100x100x100 will still only have 10x10x10 "box pixels" and just draw ten pixels at once - might not look as nice, but would probably speed it up a bit...
October 21, 2010 4:41pm - patrick mullen - nickname: (saluk)
I used about 50 different references on the subject to get my head around it. I'm not surprised our code is similar. It's pretty cool how after the basic difficult elements are in place, further effects are simple.

I'd like to get the speed better, but I think I am close to exhausting the options for python+numpy. I've tried to think of ways to further utilize numpy, but as I said I'm not very math savvy.

I've added OBJ loading, and am about to add lighting, then I'll probably just leave it. It's good enough for what I need it for.
October 21, 2010 10:33am - Ian Mallett - nickname: (geometrian)
I once wrote a software rasterizer in C++; it emulated the OpenGL pipeline very well and supported texture mapping. Looking at your code, though, it seems very similar in method to a 3D engine I wrote a very long time ago for software rendering.
spotlight

 
our projects
pygame.org welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
 
recent releases
Oct 20, 2014

Oct 16, 2014

Oct 9, 2014

Oct 8, 2014

Oct 7, 2014

Oct 4, 2014

Oct 1, 2014

Sep 19, 2014

Sep 17, 2014

Sep 9, 2014

Sep 8, 2014

Sep 7, 2014

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