When I do raycasting, I can get the intersected triangle and the hit point, I want to place a bullet hole on the hit point.
How do I calculate the billboard position from the hit point and intersected triangle?
Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 18 April 2013 - 07:34 PM
If you have the surface normal from the triangle, and the location to put the bullet hole, then you can simply construct the four vertices needed for your billboard. The surface normal is always perpendicular to the triangle face, so you just need to find any vector that is 90 degrees away from the normal. Once you have that vector, you can then use it to offset into the four directions around the hit point and place vertices in each corner.
Finding that first vector can be accomplished a number of ways. You can literally rotate the normal vector, or you can assume an up direction (i.e. [0,1,0]) and take the cross product of the normal and the up vector to find another vector that is perpendicular to both of the input vectors.
Jason Zink :: DirectX MVP
Direct3D 11 engine on CodePlex: Hieroglyph 3
Direct3D Books: Practical Rendering and Computation with Direct3D 11, Programming Vertex, Geometry, and Pixel Shaders
Articles: Dual-Paraboloid Mapping Article :: Parallax Occlusion Mapping Article (original):: Fast Silhouettes Article
Games: Lunar Rift
Posted 18 April 2013 - 09:48 PM
I'm not sure how I should start, also I'm not really good in mathematics.
Here is what I got:
D3DXVECTOR3 triangleV1, triangleV2, triangleV3; D3DXVECTOR3 intersectedPoint;
Let me know EXACTLY how do I get:
D3DXVECTOR3 bulletBillboardV1, bulletBillboardV2, bulletBillboardV3, bulletBillboardV4;
Posted 19 April 2013 - 02:19 AM
I am not good at math either, but try this:
D3DXVECTOR3 GetTriNormal(const D3DXVECTOR3& v0, const D3DXVECTOR3& v1, const D3DXVECTOR3& v2) { D3DXVECTOR3 edge1 = v1 - v0; D3DXVECTOR3 edge2 = v2 - v0; D3DXVECTOR3 normal; D3DXVec3Cross(&normal, &edge1, &edge2); return normal; } ... D3DXVECTOR3 up = triangleV1 - intersectedPoint; D3DXVECTOR3 nrm = GetTriNormal(triangleV1, triangleV2, triangleV13); D3DXVECTOR3 right; D3DXVec3Cross(&right, &nrm, &up); D3DXVec3Normalize(&up, &up); D3DXVec3Normalize(&nrm, &nrm); D3DXVec3Normalize(&right, &right); ... float size = 0.3f; D3DXVECTOR3 horizontal = right * size; D3DXVECTOR3 vertical = up * size; bulletBillboardV1 = intersectedPoint - horizontal + vertical; // up-left bulletBillboardV2 = intersectedPoint + horizontal + vertical; // up-right bulletBillboardV3 = intersectedPoint + horizontal - vertical; // down-right bulletBillboardV4 = intersectedPoint - horizontal - vertical; // down-left
no guarantee that it will work. Maybie someone could correct me if i am wrong.
EDIT: It works actually, just tried it with my old "triangle picking" project. Here on the image i just draw lines around "billboards":
If it doesn't show for you or triangles are missing, make sure you got your winding/indices right.
Edited by belfegor, 19 April 2013 - 03:08 AM.
Posted 19 April 2013 - 06:02 AM
For testing, I tried to draw 3 spheres on the triangle points, it works very well with the terrain, but it doesn't work correctly with the box, I get points in a completely different position.
Here is how I get the triangle:
D3DXIntersect(mesh, &rayFrom, &rayDir, &hit, &faceIndex, &pU, &pV, &distance, NULL, NULL); if (hit) { D3DXVECTOR3 V1 = pVertices[pIndices[3 * faceIndex + 0]].p; D3DXVECTOR3 V2 = pVertices[pIndices[3 * faceIndex+ 1]].p; D3DXVECTOR3 V3 = pVertices[pIndices[3 * faceIndex + 2]].p; // Code to draw bullet billboard here... }
Posted 19 April 2013 - 07:34 AM
Let me show you more code, maybe you could spot something wrong:
mesh->LockVertexBuffer(D3DLOCK_READONLY, ( void** )&pVertices); mesh->LockIndexBuffer(D3DLOCK_READONLY, ( void** )&pIndices); D3DXMATRIX matWorld = GetMeshWorldMatrix(); D3DXMATRIX matInverse; D3DXMatrixInverse(&matInverse, NULL, &matWorld); D3DXVECTOR3 rayFrom, rayDir; D3DXVec3TransformCoord(&rayFrom, &bRayFrom, &matInverse); D3DXVec3TransformNormal(&rayDir, &bRayTo, &matInverse); D3DXIntersect(mesh, &rayFrom, &rayDir, &hit, &faceIndex, &pU, &pV, &distance, NULL, NULL); if (hit) { D3DXVECTOR3 V1 = pVertices[pIndices[3 * faceIndex + 0]].p; D3DXVECTOR3 V2 = pVertices[pIndices[3 * faceIndex+ 1]].p; D3DXVECTOR3 V3 = pVertices[pIndices[3 * faceIndex + 2]].p; // Code to draw bullet billboard here... }
Posted 19 April 2013 - 11:17 PM
I notice that the triangles are only valid when the mesh position is XYZ: (0.0f, 0.0f, 0.0f) but when the mesh move to any other position, the results assume that the position is still 0.0f, 0.0f, 0.0f.
Edited by Medo3337, 20 April 2013 - 12:43 AM.
Posted 20 April 2013 - 04:59 PM
When you click on an object you want billboard to be attached to it, right? Then you need to transform your billboard with that objects transformation matrix, usually you set this up just before you draw your billboard:
// from your code above D3DXMATRIX matWorld = GetMeshWorldMatrix();// object matrix you have clicked D3DXMATRIX matInverse; // not this! D3DXMatrixInverse(&matInverse, NULL, &matWorld); ... // billboard is now a "child" of that object d3d9device->SetTransform(D3DTS_WORLD, &matWorld); DrawBillboard();
You need a way to "remember" which billboard is attached to which object.
Posted 20 April 2013 - 07:47 PM
As you know, I'm drawing all bullet hole billboards with one single draw call, so the transformation matrix must be set to identity, I want a way to change this:
// Billboard vertices position D3DXMATRIX matWorld = GetMeshWorldMatrix(); pVertices[j*4+0] = CUSTOM_VERTEX(billboard[i].Q1, 0.0f, 0.0f); pVertices[j*4+1] = CUSTOM_VERTEX(billboard[i].Q2, 0.0f, 1.0f); pVertices[j*4+2] = CUSTOM_VERTEX(billboard[i].Q3, 1.0f, 1.0f); pVertices[j*4+3] = CUSTOM_VERTEX(billboard[i].Q4, 1.0f, 0.0f);
I want to make the calculation of the billboard position with respect to (matWorld).
Posted 21 April 2013 - 02:35 AM
Then you transform billboard vertices at a time you place them on object:
D3DXVec3TransformCoord(&bulletBillboardV1, &bulletBillboardV1, &matWorld); D3DXVec3TransformCoord(&bulletBillboardV2, &bulletBillboardV2, &matWorld); D3DXVec3TransformCoord(&bulletBillboardV3, &bulletBillboardV3, &matWorld); D3DXVec3TransformCoord(&bulletBillboardV4, &bulletBillboardV4, &matWorld);
Posted 21 April 2013 - 07:23 AM
That should attach the billboard to the model, the problem is now with the intersected triangle...
It would be a lot easier to help you if you could attach your whole project for us to examine?
Then you might want to consider different approach like instancing (if you want 1 draw call), but you'll then need to learn how to use shaders (as i know instancing is not possible in "fixed function pipeline"). You could store in vertex declaration quaternion (float4) for rotation and position + uniform scale scalar(float4) to build a transfom matrix in vertex shader. Then you just lock->write-unlock for "billboards" that is attached to objects that might move in the scene. With this approach you might want to consider to specialize 2 classes, one for static (no need to lock->write->unlock) and other for dynamic objects.
Posted 21 April 2013 - 02:02 PM
@belfegor: The current problem is with getting a valid intersected triangle, not the billboards.
The intersected triangle that I get ASSUME that the model is located in XYZ: 0.0f, 0.0f, 0.0f
When I use D3DXIntersect() to get the intersected triangle, the result is only valid if the model position is: 0.0f, 0.0f, 0.0f
I tried:
// Transform intersected triangle v0, v1, v2 according to the model location in the world D3DXVec3TransformCoord(&v0, &v0, &matWorld); D3DXVec3TransformCoord(&v1, &v1, &matWorld); D3DXVec3TransformCoord(&v1, &v1, &matWorld);
But it's not working, I need to find a way to make v0, v1, v2 results according to the model transformation.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.