<p><strong>Edit:</strong> This answer is only helpful in 3D</p>

<h1>If you want to do it geometrically...</h1>

<p>The inverse of the view-projection matrix, $K^{-1}$ is the matrix you want. Where $K = View * Projection$</p>

<p>If $\vec{v}$ is a point in homogeneous screen coordinates, $[x, y, 1]$ where $x$ and $y$ are whatever coordinates your $K$ matrix projects onto. </p>

<p>$$\vec{d} = K^{-1} * \vec{v}$$</p>

<p>Where $\vec{d}$ now represents a <strong>line</strong> in homogeneous coordinates, coming out of your camera origin. Now you'd iterate through the objects in view, and choose the closest one in front of the camera that this line intersects with.</p>

<h1>But there are other ways!</h1>

<h3>Color Picking</h3>

<p>Here, we render (into a buffer) each object in view with a slightly different color, which corresponds to the object ID. We then read it using <code>glReadPixels(mouse_coordinates...)</code> and decide which object we clicked. This is pretty easy to do in the new pipeline - you just write a color passthrough shader. Would not recommend pre OpenGL 3.0 (You'll have to do shenanigans to render the scene without interference)</p>

<h3>Selection Buffer (Old pipeline)</h3>

<p>If you're on the fixed pipeline, you get a nice little toy called the <a href="http://www.glprogramming.com/red/chapter13.html" rel="nofollow">selection buffer</a> to do the work for you.</p>

CC BY-SA 3.0
2015-12-26T18:54:49.437Z
I edited my question so that it is more clear. I am not working in 3d btw.
Andrew Wilson
CC BY-SA 3.0
2015-12-25T12:28:38.090Z
2015-12-26T22:21:54.680Z
2015-12-26T22:21:54.680Z
Jacob Panikulam
<p>I need to be able to click on this polygon in 2d. This polygons set of vertices aren't in the same space as the screen. (Ie: the edge of the screen may be a value of 20 in one space but 1920 in the other) the scale is different. How can I convert the vertices of the polygon to the screen space so that I can use the mouse coordinates to click on it. (This is only 2d)</p>

<transformations><2d><matrix>
How to get polygon coordinates in screen space
Andrew Wilson
# How to get polygon coordinates in screen space

(Asked by: username_0 on 2015-12-23)

I need to be able to click on this polygon in 2d. This polygons set of vertices aren't in the same space as the screen. (Ie: the edge of the screen may be a value of 20 in one space but 1920 in the other) the scale is different. How can I convert the vertices of the polygon to the screen space so that I can use the mouse coordinates to click on it. (This is only 2d)

**Tags:** transformations, 2d, matrix


---

## 1 answers

### Answer by username_1 on 2015-12-25. Score: 3

**Edit:** This answer is only helpful in 3D

# If you want to do it geometrically...

The inverse of the view-projection matrix, $K^{-1}$ is the matrix you want. Where $K = View \* Projection$

If $\vec{v}$ is a point in homogeneous screen coordinates, $[x, y, 1]$ where $x$ and $y$ are whatever coordinates your $K$ matrix projects onto.

$$\vec{d} = K^{-1} \* \vec{v}$$

Where $\vec{d}$ now represents a **line** in homogeneous coordinates, coming out of your camera origin. Now you'd iterate through the objects in view, and choose the closest one in front of the camera that this line intersects with.

# But there are other ways!

### Color Picking

Here, we render (into a buffer) each object in view with a slightly different color, which corresponds to the object ID. We then read it using `glReadPixels(mouse_coordinates...)` and decide which object we clicked. This is pretty easy to do in the new pipeline - you just write a color passthrough shader. Would not recommend pre OpenGL 3.0 (You'll have to do shenanigans to render the scene without interference)

### Selection Buffer (Old pipeline)

If you're on the fixed pipeline, you get a nice little toy called the [selection buffer](http://www.glprogramming.com/red/chapter13.html) to do the work for you.

> **Comments:**
> **username_0**: I edited my question so that it is more clear. I am not working in 3d btw.  

---

