Set polygon towards camera [Vertex Shader Help]

hi guys!

How to transform polygon towards camera with vertex shader?
Like in Java: spatial.LookAt(cam.pos(), Vector3f.UnitY).

But i would like to transform the polygon mesh with vertex shader.

I guess i need to do something with GL_POSITION?

My vertex shader at preent is:

[java]
vec4 pos = vec4(inPosition,1.0);
gl_Position = g_WorldViewProjectionMatrix * pos;
[/java]

Here is the screenshot what i want:

@pspeed , i guess you can suggest something for this as you did Rope and Fire shaders. :slight_smile: I would appresiate a lot.

Thanks.

Does your quad have texture coordinates?

The vertex shader has no knowledge of “the object” and so each vertex must have enough information to translate it appropriately. So it must have the object coordinate (usually I do this in position) and some value to determine how to offset it (texture coordinates can be used if they are 0,0 - 1,1 or whatever).

To pin to an axis like you have it is a little trickier than having it screen orient, but not too bad.

Also, are your quads going to be the same size or will they be different widths and heights? What kind of size are we talking about?

@pspeed said: Does your quad have texture coordinates?

The vertex shader has no knowledge of “the object” and so each vertex must have enough information to translate it appropriately. So it must have the object coordinate (usually I do this in position) and some value to determine how to offset it (texture coordinates can be used if they are 0,0 - 1,1 or whatever).

To pin to an axis like you have it is a little trickier than having it screen orient, but not too bad.

Also, are your quads going to be the same size or will they be different widths and heights? What kind of size are we talking about?

The mesh can have different proportions of height/width. The size can be different too… from 1 to 300 units.

I saw some examples in Unity. They do it with Matrix some how. But my vector knowledge is not so advanced.
Here is the post i saw: http://forum.unity3d.com/threads/109111-GLSL-Animated-sprite-shader-billboard-questions?p=1189215&viewfull=1#post1189215
They do it in vertex shader.

EDITED: the mesh has texture coordinates.

Well, the presumption is that you are doing this so that you can batch a bunch of quads together. (If not, just use billboard). So if you wanted to use a matrix to do this then you’d need to store the whole matrix with every vertex. But that’s overkill. Actually, I guess if all objects shared a common up vector and would always then you could use one matrix for the whole batch. This would just be the inverse camera rotation (basically) and you’d multiply it by the world view matrix before applying projection. I’ve never done this approach personally but it sounds simple enough.

Otherwise, somehow, each vertex needs to know the model position (model origin) and the vertex position relative to the model position. Usually I would use the texture coordinates for this but it does get a bit trickier if each quad can be a different size.

I had to do this because normal y-axis-locked billboards look like crap when looking down from above. So for my fire billboards I mix the y-axis-locked position with a more sprite-like position depending on the angle of the viewer.

Just depends on which approach you prefer/need.

@pspeed said: ...

Just depends on which approach you prefer/need.

It seems that’s pretty complicated for me. I’ll make the rotation on CPU.

Thank you!