Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411480 Posts in 69369 Topics- by 58426 Members - Latest Member: shelton786

April 23, 2024, 09:29:04 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to implement mouse picking with ray casting?
Pages: [1]
Print
Author Topic: How to implement mouse picking with ray casting?  (Read 1254 times)
gwartney21
Level 0
**



View Profile WWW
« on: May 06, 2016, 01:21:12 AM »

I have been looking all over for how to implement ray casting. I was wondering if any one out there could help me out with how to implement this or some actual recourse on it that is not outdated. I am using c++ with glfw for my window to rendering things in. I appreciate the help guys.
Logged

My name is Alex Gwartney I am currently a college student attending online classes at University of Phoenix. I am pursuing my associates degree in computer science with a concentration in Software Development.
gwartney21
Level 0
**



View Profile WWW
« Reply #1 on: May 06, 2016, 01:43:58 AM »

I was also wanting to know will ray casting work for a 2d plane?
Logged

My name is Alex Gwartney I am currently a college student attending online classes at University of Phoenix. I am pursuing my associates degree in computer science with a concentration in Software Development.
Polly
Level 6
*



View Profile
« Reply #2 on: May 06, 2016, 02:27:23 AM »

It's actually pretty simple. There are 3 things you need to do ...

- In a vertex shader you generally calculate the homogeneous screen coordinates ( gl_Position ) of a vertex by multiplying it with a modelViewProjectionMatrix. For mouse-picking you do the exact opposite. You already know the homogeneous screen coordinates of the mouse, but you want to know the corresponding coordinate ( ray origin ) in your 3D / 2D world.

- A ray also has a direction, getting this direction is the easiest part .. you already calculated it for the view matrix of your camera Smiley

- Now that you have the origin and direction of the ray, you basically loop over all objects / meshes you want to check for picking, check whether the ray intersects with it and if-so, calculate the distance between the ray origin and the intersection point. The intersection with the lowest distance is your mouse picking result.
Logged
gwartney21
Level 0
**



View Profile WWW
« Reply #3 on: May 06, 2016, 04:24:22 AM »

Ah ok thanks for the reply will have to see what i can get working now.
Logged

My name is Alex Gwartney I am currently a college student attending online classes at University of Phoenix. I am pursuing my associates degree in computer science with a concentration in Software Development.
bdsowers
Level 3
***



View Profile WWW
« Reply #4 on: May 07, 2016, 07:31:26 AM »

Here's a quick article on the raycasting portion; it's just a little bit of math:
http://www.lighthouse3d.com/tutorials/maths/ray-triangle-intersection/

Note that when you start applying this to large scenes, you'll probably want to look into spatial partitioning of some kind. But worry about the base implementation before you start making the problem more complex. Wink
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic