orientating planes in 3D / billboarding

hi,

just wondering if anyone has done something like this,
trying to draw a flat shape and orientate it in 3d space to face a certain direction.
ive got the Up, Right and Out vectors… and from what i can see when i draw the vectors, they all look correct. although when i do the transformations and draw the shape, it faces the wrong direction. :frowning:

following a billboarding example for this tutorial,
http://www.lighthouse3d.com/opengl/billboarding/index.php3?billCyl

and here is my code,

  
	ofxVec3f upVec;  
	ofxVec3f rightVec;  
	ofxVec3f outVec;  
	ofxVec3f lookAt;  
	ofxVec3f upAux;  
	  
	upVec = ofxVec3f( cd );  
	upVec.normalize();  
	upVec *= -1;  
	  
	rightVec = p21.getNormalized();  
	  
	outVec = upVec.getCrossed( rightVec );  
	outVec.normalize();  
	  
	lookAt = ofxVec3f( 0, 0, 1 );  
  
       // UP TO HERE ALL LOOKS GOOD WHEN I DRAW THE VECTORS.  
  
	ofxVec3f outVecCopy = ofxVec3f( outVec );  
	outVecCopy.y = 0;  
	upAux = lookAt.getCrossed( outVecCopy );  
	float angleCosine = lookAt.dot( outVecCopy );  
	if( ( angleCosine < 0.99990 ) && ( angleCosine > -0.9999 ) )  
	{  
		glRotatef( acos( angleCosine ) * 180 / PI, upAux.x, upAux.y, upAux.z );	  
	}  
	  
	angleCosine = outVec.dot( outVecCopy );  
	  
	if( ( angleCosine < 0.99990 ) && ( angleCosine > -0.9999 ) )  
	{  
		if( outVec.y < 0 )  
		{  
			glRotatef( acos( angleCosine ) * 180 / PI, 1, 0, 0 );	  
		}  
		else  
		{  
			glRotatef( acos( angleCosine ) * 180 / PI, -1, 0, 0 );  
		}  
	}  
  

any help would be super appreciated.

thx.

sweet! just came across a solution on Nehe.
props to matrix transformations!

  
	float *mat = new float[ 16 ];  
	mat[0]	= rightVec.x;  
	mat[1]	= rightVec.y;  
	mat[2]	= rightVec.z;  
	mat[3]	= 0;  
	mat[4]	= upVec.x;  
	mat[5]	= upVec.y;  
	mat[6]	= upVec.z;  
	mat[7]	= 0;  
	mat[8]	= outVec.x;  
	mat[9]	= outVec.y;  
	mat[10]	= outVec.z;  
	mat[11]	= 0;  
	mat[12]	= loc.x;    // location transformation.  
	mat[13]	= loc.y;    // location transformation.  
	mat[14]	= loc.z;    // location transformation.  
	mat[15]	= 1;  
	  
	glMultMatrixf( mat );  
	  
	delete[] mat;  
  

hey, just a tip, maybe you can also use point sprites? they always face the camera

If you want screen-aligned billboarding, the simplest way to calculate it yourself is to take the first 2 rows of the modelview matrix as your 2D axes for the geometry as they are guaranteed to be screen aligned. Here’s some sample code from a GLSL shader I wrote that does this:

  
  
attribute vec3 offset;  
uniform float scale;  
  
varying vec2 texcoord0;  
  
void main()  
{  
	//screen-aligned axes  
	vec3 axis1 = vec3(	gl_ModelViewMatrix[0][0],   
						gl_ModelViewMatrix[1][0],  
						gl_ModelViewMatrix[2][0]);  
						  
	vec3 axis2 = vec3(	gl_ModelViewMatrix[0][1],   
						gl_ModelViewMatrix[1][1],  
						gl_ModelViewMatrix[2][1]);  
  
  
	//offset from center point  
	vec3 corner = (offset.x*axis1 + offset.y*axis2)*scale + gl_Vertex.xyz;  
	  
	// position in eye space  
	gl_Position = gl_ModelViewProjectionMatrix * vec4(corner, 1.);  
  
	texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);  
	gl_FrontColor = gl_Color;  
}  

1 Like