PDA

View Full Version : Billboard geometry shader



Vexator
09-16-2011, 10:07 AM
I'm writing my first geometry shader in glsl. I want to extrude points uploaded to the gpu to two triangles facing the camera (spherical billboards). The idea is to calculate the three axes of the billboard (typical lookat() function) and then position the vertices along these axes. What i've got somehow works but the billboards start rotating in the wrong direction when the camera moves close and/or spin around their z axis. Any ideas what i'm missing? What bothers me is that the camera's rotation is not considered; shouldn't the billboards' rotation have to be influenced by that?

Thanks!


#version 330 core

layout (points) in;
layout (triangle_strip) out;
layout (max_vertices = 4) out;

layout(std140) uniform;

uniform Transform
{
mat4 view;
mat4 model;

vec4 animate;
mat4 bones[64];
} transform;

uniform Projection
{
mat4 perspective;
mat4 orthographic;
} projection;

uniform vec3 cameraPosition;

out vec2 textureCoords;

void main()
{
mat4 mvp = projection.perspective*transform.view*transform.mo del;

vec3 center = gl_in[0].gl_Position.xyz;

vec3 zAxis = normalize( cameraPosition-center );
vec3 yAxis = vec3( 0.0, 1.0, 0.0 );
vec3 xAxis = normalize( cross(zAxis, yAxis) );

yAxis = normalize( cross(xAxis, zAxis) );

vec3 x = xAxis*0.5;
vec3 y = yAxis*0.5;

gl_Position = mvp*vec4( center-x-y, 1.0 );
textureCoords = vec2( 0.0, 0.0 );
EmitVertex();

gl_Position = mvp*vec4( center-x+y, 1.0 );
textureCoords = vec2( 0.0, 1.0 );
EmitVertex();

gl_Position = mvp*vec4( center+x-y, 1.0 );
textureCoords = vec2( 1.0, 0.0 );
EmitVertex();

gl_Position = mvp*vec4( center+x+y, 1.0 );
textureCoords = vec2( 1.0, 1.0 );
EmitVertex();

EndPrimitive();
}

This is what it looks like atm:

http://img404.imageshack.us/img404/7319/billboards.jpg

Ilian Dinev
09-16-2011, 03:44 PM
Try ideas from:
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=301806#Post3018 06

Vexator
09-17-2011, 03:30 PM
Thank you, that does the trick!