Public Group

# How does 3D game get the vertex's pixel-coordinate?

This topic is 3366 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have used D3DX9 to draw a chessboard, now I confuse how can i get pixel-coordinate of the vertex which has been transformed by projective matrix, so that i can compare it with mouse cooodinate ,then put the chessman. Thanks in advance.

##### Share on other sites
To answer your question, you simply apply the graphics transformation yourself, which looks something like this (look up the official version in the d3d docs):

screen pos = viewport * projection * view * world * input;

You're setting those matrices to the d3d pipeline and internally it is concatenating them to transform each vertex. You can just do this yourself.

But, i'll give you a hint. This is not how you do what is referred to as picking. There's no sense in transforming the entire scene in screen space twice, once for rendering and once for picking. Also any spatial partitioning you may (but unlikely) have implemented is going to be in world space. Picking should be done in world space.

This means that you actually transform the mouse coordinates into a ray which points into the scene as if a laser is shot out of the camera. You then compare this ray to the objects in the scene to determine which one was clicked.

It's very easy to do a ray vs plane intersection to determine the XY coordinate of your cheese board that was clicked. You dont even need to reference the vertices if the size of each grid space and a corner point is known.

There other methods. In opengl you can attribute a name to each polygon rendered. Then you can query the names that were drawn to a particular pixel. Some people draw each object or polygon a different color in a special off screen rendering. They then find the color for the clicked pixel and lookup which object that refers to. For your case you'd draw each grid space a different color. You could encode the indices in the R and G components so grid space 2,3 would have a color RGB(2,3,0).

Good luck.

##### Share on other sites
Quote:
 Original post by sobeitI have used D3DX9 to draw a chessboard, now I confuse how can i get pixel-coordinate of the vertex which has been transformed by projective matrix, so that i can compare it with mouse cooodinate ,then put the chessman.Thanks in advance.

Look for the directx tutorial program sample for 'Pick' which demonstrates
how the cursor input can be used to 'pick' a facet triangle in a list of triangles (like ones making up the squares of your 3d chess board).

Basickly the program identifies a line from your camera eyepoint thru a pixel on the screen (a calculated plane) pointed to by the mouse and then does a Line Intersect Triangle test on a list of triangles you provide.

##### Share on other sites

thanks for all of your help.
I'v read "pick" sample, I'm tring to make it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×