Billboard Shader Issue (HLSL/DX9)

Started by
17 comments, last by DJTN 12 years, 8 months ago
Gsamour - so you build this matrix based off a position you get from averaging 4 vertices (center) that belong to the same quad, once you have that -you transform each vertices that belongs to that quad with the newly built matrix? This gives the vertices a new position in model space right?


I'm getting strange results...


Advertisement
I have a particle position (so, the center of my particles), then build quads around that center...
But I may have a bug in my implementation... because building the billboard matrix requires the camera position and particle position being in the same space (In my program I was thinking world space). But looking at my actual code, I don't think I'm doing that... let me double check my implementation and I'll get back to you.

I have a particle position (so, the center of my particles), then build quads around that center...
But I may have a bug in my implementation... because building the billboard matrix requires the camera position and particle position being in the same space (In my program I was thinking world space). But looking at my actual code, I don't think I'm doing that... let me double check my implementation and I'll get back to you.



Am I even going down the right road here? Let me explain my current method so we're on the same page. I'm updating the verticies position in the vertex buffer to face the camera so I can draw them in one call. I would assume I take the 4 vertices that make up the quad, average them (i.e. add all the vectors into 1 and divide by 4) and then apply a rotation matrix. is this correct?

Am I even going down the right road here? Let me explain my current method so we're on the same page. I'm updating the verticies position in the vertex buffer to face the camera so I can draw them in one call. I would assume I take the 4 vertices that make up the quad, average them (i.e. add all the vectors into 1 and divide by 4) and then apply a rotation matrix. is this correct?


That sounds right to me (obtaining the centroid, calculating the billboard rotation matrix, then applying it). The question is... are your vertices in local space or world space? I assume your camera position is in world space, is that correct?
Yes my camera is in world space and my verticies are in model space. Based off what you're saying, I should move my vertices to world space before the transform. How do I get them back to model space?

Yes my camera is in world space and my verticies are in model space. Based off what you're saying, I should move my vertices to world space before the transform. How do I get them back to model space?


The short answer is "you don't". To get them back to model space, you would have to transform them by the inverse of the billboard matrix (which undoes the "looking at the camera" effect... not what you want). You leave them in world space, then in the vertex shader, instead of multiplying by world * view * projection matrices, you just multiply by view * projection matrices. Unfortunately, I think this means you will have to have a big vertex buffer with all the vertices of all your quads (not just for one tree, but for all the trees in the scene).

I apologize if I haven't been clear and if my solution seems inefficient. I'll keep thinking about it to see if there's a better way to solve your problem.

P.S. I don't have a lot of experience with tree rendering, but I've seen a lot of games with static branches/leaves (non-billboard). Is this not good enough for your program?

[quote name='DJTN' timestamp='1313176858' post='4848376']
Yes my camera is in world space and my verticies are in model space. Based off what you're saying, I should move my vertices to world space before the transform. How do I get them back to model space?


The short answer is "you don't". To get them back to model space, you would have to transform them by the inverse of the billboard matrix (which undoes the "looking at the camera" effect... not what you want). You leave them in world space, then in the vertex shader, instead of multiplying by world * view * projection matrices, you just multiply by view * projection matrices. Unfortunately, I think this means you will have to have a big vertex buffer with all the vertices of all your quads (not just for one tree, but for all the trees in the scene).

I apologize if I haven't been clear and if my solution seems inefficient. I'll keep thinking about it to see if there's a better way to solve your problem.

P.S. I don't have a lot of experience with tree rendering, but I've seen a lot of games with static branches/leaves (non-billboard). Is this not good enough for your program?
[/quote]

It is too expensive to render 100's of 3d tree models. Usually a couple of 3d tree models are rendered and to give depth- 20-30 billboards with tree images are used in the distance. To solve my problem I could easily re-render the same billboard over and over again changing my world matrix using an array of vectors storing their different locations- but this is inefficient. Asking DirectX to render 2 triangles in a draw call and looping through it 30 times is not the answer. My approach was to dynamically load several quads into a mesh object and draw them all in one batch but I ran into the problem with them not facing the camera. I assumed it would be rather trivial to update the vertex buffer with the position but this has turned into a nightmare.

Someone around here has had to billboard before. I've been searching for weeks for a solution but nothing has turned up. Usually they only discuss how to make ONE quad face the camera using the world matrix to rotate before drawing. What if I have several billboards spaced out? How can you make each one of them face the camera without drawing them separately?




It turns out this has already been discussed before:

http://www.gamedev.net/topic/154890-billboarding-with-vertex-shaders/

It turns out this has already been discussed before:

http://www.gamedev.n...vertex-shaders/



Thanks gsamour.

This topic is closed to new replies.

Advertisement