Skip to content

Painter's Algorithm in 3D Graphics goes CLUNK #1

@AslanRules

Description

@AslanRules

Hello fellow programmers,

I am trying to develop software to create 3D/2D graphics and movies without all of the complicated rendering stuff. Part of this is making sure I'm drawing the 2D shapes that make up the 3D objects represented in the right order, aka following the "painter's algorithm." However, I have ended up in countless ditches while attempting this, and would GREATLY appreciate it if someone could explain to me what I'm doing wrong! While a challenger because of the relatively large amount of code (but not too large), it uses basic Python and Pygame stuff that most familiar programmers will understand. Before explaining my problems, however, I need to explain how my software works:

First, there are "nodes," which are really just X-Y-Z coordinate points. Each node is a list containing an X-coordinate value, Y-coordinate value, and Z-coordinate value. These lists, in turn, are stored in the list nodes.

Next, there are polygons, or "pols," that are essentially just lists containing indices of nodes in the list. This sounds confusing, but it's really simple. To draw a polygon, Pygame only needs a list of points to connect. However, I don't want to make polygons lists of points, because there already is a list of points called nodes, and when I want to edit nodes later, I just want to edit the node in one place, not in multiple lists. So the pols are list of indices telling which node in the nodes list to connect. For instance, suppose A, B, C, D, and E are points. They would be in the nodes list as:

nodes = [A, B, C, D, E]

Suppose I wanted to connect the points C, D, and E to make a triangle appear on the screen. Then, my polygon list would be:

my_pol = [2,3,4]

Because I'm connecting the 3rd, 4th, and 5th items of the nodes list (remember counting in Python starts at 0).

All of these polygons (which are just lists of indices as explained) are stored in lol, not from "laugh out loud," but from "list of lists," which is what it is.

OK. So polygons are covered. Next, what you need to know is that I have a lot of rotating functions in Nodify, half of which aren't even used as they are from old programs and aren't needed as of yet. They use math to rotate the coordinates around the X-axis, Y-axis, and Z-axis in space. I will not explain why the math works because I'm too lazy to figure it out - I'm just glad it works. Otherwise, you can visit Khan Academy's course on 3D shapes.

Lastly, the Painter's Algorithm. This makes sure the 2D shapes are drawn in the right order to see correctly the 3D shapes represented. Each face of a cube has to be drawn in the right order, for example, or you might see the back face in front of the front face! Now, while I know this method will not work for all instances of polygons (in those cases you split the polygon(s) into multiple polygons), my method is:
A) Take each polygon's nodes and calculate the average of their Z-coordinates.
B) Polygons with lower Z-coordinate averagese go in front of polygons with higher Z-coordinate averages, so they get drawn LAST.

Problems with the program:

  1. I'm not sure, but it seems no matter what order the list is in, the program draws the polygons in whatever order it wants anyway. Why is this? Does the list order not matter?
  2. When we rotate the shape on screen, we are not really rotating the nodes in the nodes list but instead are rotating temporary copies of the nodes and then drawing the polygons with the temporary copies. However, that means that our painter's algorithm function, order(), is trying to organize polygons that AREN'T rotated. Therefore, I must rotate temporary copies in my painter's algorithm function also. It is getting different results than the other rotating place, however, and I don't know why. Does anybody see a bug or a problem in my method that may be causing this?
  3. When I try to call my rotating function on the nodes in the painter's algorithm list, it rotates not just the temporary node "rotnot," but also the corresponding node in the not temporary nodes list, making everything rotated each iteration and constantly spinning about! What and why is is it "linking" my rotate function to the nodes list?
    I have tried just writing the rotating code manually instead of calling the function and that worked in not rotating the nodes list, but manually writing the code is more prone to error and no help in trying to fix problem Fixing Typo #2.

Any help at all would be GREATLY appreciated in any one of these problems. Thank you for taking the time to read this, and THANK YOU SO MUCH for your help!

"Mike"

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomershelp wantedExtra attention is neededquestionFurther information is requested

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions