i have a peice of code that renders a 3D image, say a cube. I require 3 cubes in in different locations, this is done using instancing and i am able to render the cube in 3 different locations. However, i now need to pick any of the 3 cubes. I am pretty new to DirectX(11) and searches online did not yeild any answers. So how i pick an object when i only have a single set of vertices(triangle) used for the render?


Picking is not generally performed by the GPU so the use of instancing shouldn't matter.

Your CPU-side code presumably knew where the cubes where in order to put their transforms into the instance buffer. Hence it can do a ray-AABB test by transforming the picking ray into the space of the cube in the cube's local space.

Happily, the local space for the cube's AABB is just the unit AABB. You need only transform the picking ray by the inverse of the transform matrix you used for each cube's instance in the instance buffer.

The ray can be generated by creating a ray from the camera position through the point on the near-Z plane corresponding to the mouse location.

| improve this answer | |
  • \$\begingroup\$ ok, i went through the code again and this is how the code looks ` instance_buffer[0].instanceMatrix = vector<float,4>(x,y,z,1.0f); ` This line alone is enough to create an instance of the cube in the new location. So i dont see how i can get any information to perform the picking from this \$\endgroup\$ – introVertice Jan 21 '16 at 0:36
  • \$\begingroup\$ @introVertice: your x, y, and z are the locations of the cube. Assuming that you have no scaling, you know that your cube is a unit cube at that location. Now take your picking ray and do a collision check against the ray and the AABB at that location. \$\endgroup\$ – Sean Middleditch Jan 21 '16 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.