Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to project a 3D point on to a canvas  (Read 935 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2013-02-19 17:45:41 »

Hi everyone, the current task I've set myself is to take an array of 4 three-dimensional points and project them on to a two-dimensional canvas. I'm having a bit of trouble understanding what I've read and tried to put into practice and was wondering whether anyone could tell me where I've gone wrong.
Say for example I have these 4 three-dimensional points representing the corners of a quadrilateral, with two points at Z coordinate 20 and the other two at Z coordinate 40, making up a square with its faces facing down the X axis. If I then translate this set of points 100 points down the negative X axis, I should now be able to see the back two points, which should also be rendered with perspective projection, meaning that the two furthest points appear closer together. This is the perspective projection I've used: http://msdn.microsoft.com/en-us/library/bb205351(v=vs.85).aspx
In the "Remarks" section.

Now my understanding breaks down when I come to transform the points to their perspective view. From what I've gathered, the point, which can be represented with a 3-dimensional vector is multiplied by the view matrix, which I've initially set to the identity matrix. From here presumably you're meant to have a transformed point (3-dimensional vector), which you then have to multiply by the projection matrix, which in my case is in a perspective view.

From that I now have a new transformed 3-dimensional vector that I can simply render to the screen as I would normal screen pixels. So in a nutshell:

TransformedPoint = Point x ViewMatrix x ProjectionMatrix

Is this right? This appears to just produce the same point as the original. I know I'll have to post some code up here, but could you tell if this logically isn't very sound anyway?
Also, I'm rendering all this into an AWT Canvas.

Thanks in advance for any help!

Paul
Offline relminator
« Reply #1 - Posted 2013-02-22 15:13:03 »

Yes, that should work. 

Or just use gluProject.
Offline Porlus

Junior Member





« Reply #2 - Posted 2013-02-23 16:06:24 »

Hey, thanks for the reply! I've almost got what I need now, but there's a glitch that I don't really understand the reason of. I've taken the 3D point and multiplied if by the view matrix, which I've just set as the identity. I've then multiplied this transformed point by the projection matrix shown above to get the clip space coordinates, which I then multiply by the viewport half width/height to transform it to screen coordinates and then divide both the X and Y components by the Z component for the foreshortening. The problem I've found is that when I move the point forward, it moves into the scene as you'd expect, but when I move it backwards to the point where it's behind the camera and not visible in the scene, it reappears in opposite corner.
So for example, a point that's in the bottom right hand corner, moves out of the window by the bottom right corner as its Z component deceases, but the point then reappears from the top left hand corner and moves towards the centre.
Has anyone ever heard of anything similar to this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #3 - Posted 2013-02-23 17:34:05 »

Hi,

That's how gluProject works as well, there you can grab the 3rd parameter returned to see whether the point is in front ( >1 ) or behind the canvas plane ( <1 ). Maybe you have something similar and otherwise you can just check if the point is behind the canvas plane.

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Porlus

Junior Member





« Reply #4 - Posted 2013-02-23 18:03:18 »

Thanks for the reply. Smiley Here's the code I have for rendering the points:

1  
2  
3  
4  
5  
6  
7  
for(int i = 0; i < vertices.length; i++) {
   Point4f transPos = Point4f.multiply(Point4f.multiply(vertices[i], viewMat), projMat);
   if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue;
   int x = (int) (Main.WIDTH * 0.5 + transPos.getX() * Main.WIDTH * 0.5 / transPos.getZ());
   int y = (int) (Main.HEIGHT * 0.5 + transPos.getY() * Main.HEIGHT * 0.5 / transPos.getZ());
   screen.drawPoint(x, y, 0xffffffff);
}


(btw Main.WIDTH and Main.HEIGHT are the viewport width and height).

I thought that the if statement on the 3rd line checks whether the point is outside of the clip space, which would have eliminated points behind the camera. Could this be because the X and Y components are divided by the Z component after that check? Or is normal to just put in another check to determine whether the Z component of the point is less than the Z component of the camera?
Offline Porlus

Junior Member





« Reply #5 - Posted 2013-02-25 21:51:45 »

Hi all again Smiley I've still got this problem. I've attempted to change a few things, although they didn't really help. The problem remains that when the point moves behind the viewport, it reappears in the opposite corner and moves towards the point to infinity. I tried adding the Z component to this statement:

1  
if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue;


Although it didn't seem to solve my problem, however it did clip points further out in the scene. Does anyone know of any source code where something similar has been achieved and is working (perspective projection of a 3D point on a 2D canvas)?
If anyone could post a source I'd really appreciate it. I'm reading from a few different sources, although from what I'm gathering from them all collectively doesn't mention a glitch of this nature.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!