Mouse pick a triangle. How?

Blitz3D Forums/Blitz3D Programming/Mouse pick a triangle. How?

CyberHeater(Posted 2004) [#1]
I'm trying to work out out the best way to select a triangle on my mesh with a mouse such that when I move my mouse over a mesh and press the mouse button, the nearest triangle under the mouse gets selected and highlighted.

Thanks.


Shambler(Posted 2004) [#2]
See the PickedTriangle() function.


AntonyWells(Posted 2004) [#3]
try this,
global resetSrf,resetTri,resetNext
global oldR[3],oldG[3],oldB[3],lastTri
function highLight(camera)
local vert[3]
  if resetNext or mousedown(1)=false
       for j=0 to 2
           vert=triangleVertex(resetSrf,resetTri,j)
           vertexColor(vert,oldR[j],oldG[j],oldB[j])
        next
        resetNext=false
  endif

  if mousedown(1)
      hit=camerapick camera,mousex(),mousey()
      if hit
           tri=pickedTriangle()
           if tri=lastTri return
           srf=pickedSurface()
           for j=0 to 2
              vert=triangleVertex(srf,tri,j)
              oldR[j]=vertexRed(srf,vert)
              oldG[j]=vertexGreen(srf,vert)
              oldB[j]=vertexBlue(srf,vert)
              if j<2
              vertexColor(srf,vert,128+(oldR[j]/2),128+(oldG[j]/2),128+(oldB[j]/2))
              else
              vertexColor(srf,vert,0,0,0)
              endif
           next   
           resetSurf=surf:resetTri=tri:resetNext=true
       endif
  endif
end function


Not tested, but basically call it once every frame passing your camera to it. Set any entities you want to pick as poly pickable. I.e entityPickMode myMesh,2,true.

It'll highlight the current poly, and reset it once the mouse is no longer over it. (Make sure you have the entity using vertex colors. See entityFX)


wmaass(Posted 2004) [#4]
Neato. How might you select a group of triangles? For instance if you wanted to select all the triangles within a certain radius of one triangle?


Picklesworth(Posted 2004) [#5]
The slowest but possibly easiest way to do that is:
you would find and save the click spot (x and y), and keep watching current mouse location. Then do something like
For x = ClickX to MouseX()
 For y = ClickY To mouseY()
  CameraPick camera,x,y
 Next
Next

If possible, you could put a Step value in there to speed it up a bit.

Note that there is probably an infinietly better box selection script in the code archives


Rob(Posted 2004) [#6]
tform verts to camera space and check.


AntonyWells(Posted 2004) [#7]
Personally I'd do a normal camera pick, and then just do a spherical distance (I.e squared, otherwise it'll be a diamond formation) of each tri centre to the pick point. That way you'll get true 3d space closeness picking, not just the tris near the mouse on a 2d flat plane(I.e the screen)


CyberHeater(Posted 2004) [#8]
Thanks guys. With your help, i've managed to find a solution.