[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Drawing 3D coordinates in 2D screen space using python/pygame?

Posted on 2012-04-06
3
Medium Priority
?
1,305 Views
Last Modified: 2012-04-09
I am drawing 3D coordinates using Python/Pygame.

I KNOW this is meant to be accomplished using OpenGL/DirectX...
and I KNOW these calculations are meant to take place on a GPU...
but I don't care. I'm teaching myself the matrix transformations/projections.

Since I've used XNA/C#, I'm trying to simulate the World*View*Projection Matrix math to calculate screen coordinates for 3D shapes.

Since I have worded the following issue very poorly, I attached my entire source file.

-I'm using numpy, with 4x4 homogenous matrices.
-My vertices all contain a fourth 'w' value of 1

If I have a simple shape, say a diamond, with 6 vertices:

(1, 0, 0), ( -1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)


World Matrix is just an identity matrix.

vP = (0, 0.0, -2.0)
vR = (1.0, 0.0, 0.0)
vU = (0.0, 1.0, 0.0)
look = (0.0, 0.0, 0.0)
vL = look - vP

v1 = dotProduct(-vP, vR.T)
v2 = dotProduct(-vP, vU.T)
v3 = dotProduct(-vP, vL.T)

VIEW = np.matrix([
        [vR[0,0], vU[0,0], vL[0,0], 0.0],
        [vR[0,1], vU[0,1], vL[0,1], 0.0],
        [vR[0,2], vU[0,2], vL[0,2], 0.0],
        [v1, v2, v3, 1.0]
        ])

width, height = 640, 480
fov = math.pi / 2.0
aspect_ratio = float(width) / float(height)
z_far = 100.0
z_near = 0.1

PROJECTION = np.matrix([
        [1 / tan(fov / 2) * aspect_ratio), 0, 0, 0],
        [0, 1 / tan(fov / 2), 0, 0],
        [0, 0, z_far / (z_far - z_near), 1],
        [0, 0, (-z_near * z_far) / (z_far - z_near), 0]
        ])

(x, y, z, w) = world*view*projection*coordinate

and the final X, Y = x/w, y/w

Long story short, my "diamond" should appear well-formed... however this is what I see:
distorted diamond.It SHOULD look like a symmetric diamond on all axes.
draw3d.py
0
Comment
Question by:Justin_Edmands
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
satsumo earned 2000 total points
ID: 37820006
First of all, well done for actually trying the math.  It's very easy to use a ready made 3D system and it saves a lot of time, but you don't learn anything that way.  People who do that inevitably reach a point where they want to do something that exceeds the system they've been using, and then they don't know how to progress.

In this case everything looks OK at first glance.  The first thing that might be wrong is matrix order.  You do world*view*projection to calculate positions, that depends on whether the matrices are pre or post multiplied, which also depends on them being or row or column order correctly.  If either of these factors is wrong you will get the wrong result.  In this case, it appears to have applied the perspective scaling from right to left, which makes me suspect the matrix order.

Ufnortunately I don't know how the python matrix classes work so I can't be more precise than that.  OpenGL and DirectX use different matrix order and that often confuses people.  The majority of other systems seems to use the OpenGL order rather than the DirectX order.

http://en.wikipedia.org/wiki/Row-major_order
0
 

Author Closing Comment

by:Justin_Edmands
ID: 37825128
Essentially, my problem was the order of matrix multiplication. I read up on the row and column ordering.

In the end, I multiplied the transpose of each of those matrices in reverse... i.e. proj.T * view.T * world.T.

Thanks for the help! Now i'm gonna try drawing some complex shapes.
0
 
LVL 12

Expert Comment

by:satsumo
ID: 37825713
Glad to have helped, thanks for the points.

I've actually had to do this math myself for some jobs, including the clipping and rasterising sometimes.  Not every platform has an OpenGL/Direct X type system.  Sometimes you have to understand the math, so doing this is very useful.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question