For The Life Of Me I Can't Figure This Ray Selection Out Vb.Net

Started by
3 comments, last by Steve5050 16 years, 8 months ago
I have been trying very hard to get this pick ray working and can' get it done, I have been all over the internet and here and I'm just stuck and ready to put my fist through the screen. Days and Weeks at it. I need some Code to help me with this. I have some c++ code but trying to decifer that is a nightmare at the least. I have this Quad:

 Gl.glBegin(Gl.GL_QUADS)
        Gl.glTexCoord2f(tc1, tc2)
        Gl.glVertex3f(p1.x, p1.y, p1.z)
        Gl.glTexCoord2f(tc3, tc4)
        Gl.glVertex3f(p2.x, p2.y, p2.z)
        Gl.glTexCoord2f(tc5, tc6)
        Gl.glVertex3f(p3.x, p3.y, p3.z)
        Gl.glTexCoord2f(tc7, tc8)
        Gl.glVertex3f(p4.x, p4.y, p4.z)
        Gl.glEnd()

I have this Pick Ray:

 Dim viewport As Integer() = New Integer(3) {}
        Dim modelviewMatrix As Double() = New Double(15) {}
        Dim projectionMatrix As Double() = New Double(15) {}
        Dim realY As Integer
        Dim realXA As Integer
        realXA = Cursor.Position.X
        Dim worldX As Double, worldY As Double, worldZ As Double
        Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport)
        Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, modelviewMatrix)
        Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, projectionMatrix)
        realY = viewport(3) - CInt(Cursor.Position.Y) - 1
        Label1.Text = "Coordinates at cursor X>>" & realXA & "Y>>" & realY
        Glu.gluUnProject(CDbl(realXA), CDbl(realY), 0, modelviewMatrix, projectionMatrix, viewport, worldX, worldY, worldZ)
        Label2.Text = "World Coords at  Z = 0.0 Are>>X>" & worldX & "Y>>" & worldY & "Z>>" & worldZ
        clickRayA = New Vector(CSng(worldX), CSng(worldY), CSng(worldZ))
        Glu.gluUnProject(CDbl(realXA), CDbl(realY), 1.0, modelviewMatrix, projectionMatrix, viewport, worldX, worldY, worldZ)
        Label3.Text = "World Coords at  Z = 0.0 Are>>X>" & worldX & "Y>>" & worldY & "Z>>" & worldZ
        clickRayB = New Vector(CSng(worldX), CSng(worldY), CSng(worldZ))
        clickSlope = Vector.op_Subtraction(clickRayB, clickRayA)
        clickSlope.Normalize()

I do also have the ray visible, so it is there. I just can't get past the end of this code. I know I need something like:

Select = clickRayA,clickSlope,closestHit

I can't figure it out. I need some code to help, C# would do. Thanks If You Can Help! Steve
Advertisement
This seems to be extremely hard to do no matter what
language,I am going to have to figure this out one way
or another.
I have to much time invested in this project to give up
now.

Will check back in later.
Still very much unsolved!

I came back looking for some assistance.

Heeellllllpppppp.
Well, If you can.

Thanks
Steve
Here's how I pick from int x,y in window space

Get the eyepoint from the inverse view matrix (41, 42, 43)

Transform window (int x,y) to clip space (4d vector) (flip axes, linear interpolate, etc, with z = 0, w = 1)

Transform vector4 screen space point (fx, fy, 0, 1) by inverse of viewMatrix * projectionMatrix to get screenWorld (must project back to w=1 as its non-homogeneous)

Ray origin is screenWorld
Ray direction is screenWorld - eyePoint
loop through objects
-- if ray intersects object bounds
---- add object to list
end loop
sort intersecting objects
closest is your picked object

So the tricky part is your ray-bounds intersection
you can look this up for ray/sphere, ray/box, ray/plane, etc.

Assuming I typed it right and explained it right, it works like a charm.
Thanks for trying to help.
I need some help with some code that I can
tack on to the spot where I explained that I
was stuck.

I have been up,down,sideways and all over the
internet looking for a way out of this.

No Luck!

Thanks
Steve

This topic is closed to new replies.

Advertisement