<p>Figured it out, I did skip a step, you are supposed to multiply by the homogeneous coordinate.</p>
<p>Basically the actually projected point is (courtesy of GLM):</p>
<pre class="lang-cpp prettyprint-override"><code>        vec&lt;4, T, Q&gt; tmp = vec&lt;4, T, Q&gt;(obj, static_cast&lt;T&gt;(1));
        tmp = model * tmp;
        tmp = proj * tmp;

        tmp /= tmp.w;
</code></pre>
<p>Kinda obvious in hindsight.</p>

CC BY-SA 4.0
2021-01-23T04:57:41.543Z
2021-01-23T04:57:41.543Z
Makogan
<p>There is a way to move a selected point in a mesh with a cursor(assuming a camera that doesn;t change between frames).</p>
<p>The way I remember the algorithm (but seems to be wrong) is:</p>
<ul>
<li>Unproject the cursor position onto the world position</li>
<li>Use this and the camera position to create a ray <span class="math-container">$R$</span></li>
<li>for every vertex in the mesh, see if <span class="math-container">$R$</span> intersects a small sphere centered at the vertex.</li>
<li>If you find a valid vertex, mark it as selected</li>
<li>project the selected vertex onto the screen using the view_projection transformation of the camera</li>
<li>store the resulting depth (z component) into a variable.</li>
<li>Now when you move the mouse, you create a fabricated position consisting of the cursor position and the stored projected depth, then you unproject that.</li>
</ul>
<p>The resulting point should be constrained on a plane in 3D that is parallel to the screen. I have coded the procedure but it seems I am mathematically wrong. So I am not entirely sure if I forgot a step.</p>
<p>To be clear, the objective is to drag the mouse around and have the selected point move in 3D such that it is rendered at the same position as the cursor is on the screen.</p>

<raytracing><mathematics><geometry><projections><ui>
Moving a vertex through the cursor
Makogan
# Moving a vertex through the cursor

(Asked by: username_0 on 2021-01-23)

There is a way to move a selected point in a mesh with a cursor(assuming a camera that doesn;t change between frames).

The way I remember the algorithm (but seems to be wrong) is:

* Unproject the cursor position onto the world position
* Use this and the camera position to create a ray $R$
* for every vertex in the mesh, see if $R$ intersects a small sphere centered at the vertex.
* If you find a valid vertex, mark it as selected
* project the selected vertex onto the screen using the view\_projection transformation of the camera
* store the resulting depth (z component) into a variable.
* Now when you move the mouse, you create a fabricated position consisting of the cursor position and the stored projected depth, then you unproject that.

The resulting point should be constrained on a plane in 3D that is parallel to the screen. I have coded the procedure but it seems I am mathematically wrong. So I am not entirely sure if I forgot a step.

To be clear, the objective is to drag the mouse around and have the selected point move in 3D such that it is rendered at the same position as the cursor is on the screen.

**Tags:** raytracing, mathematics, geometry, projections, ui


---

## 1 answers

### Answer by username_0 on 2021-01-23. Score: 1 | Accepted answer

Figured it out, I did skip a step, you are supposed to multiply by the homogeneous coordinate.

Basically the actually projected point is (courtesy of GLM):

```cpp
        vec<4, T, Q> tmp = vec<4, T, Q>(obj, static_cast<T>(1));
        tmp = model * tmp;
        tmp = proj * tmp;

        tmp /= tmp.w;

```

Kinda obvious in hindsight.


---

