• Advertisement
Sign in to follow this  

Billboard Shader

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I know a smiliar thread was started in the GP&T section, but I decided to post here because that one was marked "[RESOLVED]", and I was affraid that no one would bother to look at it. So basically, I have a GLSL shader that works on a quad list to build a bunch of screen facing particles. My problem is that that my rotation transforms don't seem to be right as everything goes to hell when I attempt to render. So far, I've tried the simple stuff like making sure my camPos is being set properly, and flipping the cross products. The thing is that this is the same look at code I use on the CPU, but it breaks on the GPU. Anyone see something I'm missing?
uniform float size;
uniform vec3 camPos;
	
void main()
{	
	//vec4 worldPos;
	
	vec3 vAt = camPos - gl_Vertex.xyz;
	vec3 vRight = cross( vec3( 0.0, 1.0, 0.0 ),  vAt );
	vec3 vUp = cross( vAt, vRight );
	normalize( vAt );
	normalize( vRight );
	normalize( vUp );
	
	//tex coords are used to store how the vert should be transformed
	vec2 pos = gl_MultiTexCoord0.zw * vec2( size/2.0, size );
	mat4 w = mat4(	vec4( vRight, 0.0 ),
			vec4( vUp, 0.0 ),
			vec4( vAt, 0.0 ),
			gl_Vertex );
	
	gl_Position = gl_ModelViewProjectionMatrix * w * vec4( pos, 0.0, 1.0 );;
	
   	gl_TexCoord[0] = gl_MultiTexCoord0;
} 

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by INVERSED
vec3 vRight = cross( vec3( 0.0, 1.0, 0.0 ), vAt );
vec3 vUp = cross( vAt, vRight );

I'm not sure, but the above looks like a candidate for errors -- are you sure that's what you want? Couldn't vUp just be vec3(0.0, 1.0, 0.0) ?

Share this post


Link to post
Share on other sites
Bah, finally figured it out. apparently you actually have to catch a return value from normalize... oops. Anyway, here's the modified code for any who might want it. Again the four vertices of a billboard all have the same poition, and the texcoord z and w store the directions for the right and up of the current vertex.

uniform float size;
uniform vec3 camPos;

void main()
{
vec3 vAt = camPos - gl_Vertex.xyz;
vAt = normalize( vAt );
vec3 vRight = cross( vec3( 0.0, 1.0, 0.0 ), vAt );
vec3 vUp = cross( vAt, vRight );
vRight = normalize( vRight );
vUp = normalize( vUp );


vec2 s = gl_MultiTexCoord0.zw * vec2( size/2.0, size );
vec3 vR = s.xxx * vRight;
vec3 vU = s.yyy * vUp;

vec4 dir = vec4( vR + vU, 0.0 );
gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + dir );

gl_TexCoord[0] = gl_MultiTexCoord0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement