Skip to content

Instantly share code, notes, and snippets.

@patriciogonzalezvivo
Last active July 14, 2018 06:43
Show Gist options
  • Save patriciogonzalezvivo/6416736 to your computer and use it in GitHub Desktop.
Save patriciogonzalezvivo/6416736 to your computer and use it in GitHub Desktop.
Billboards on OF
void billBoard(){
	ofVec3f objectLookAt = ofVec3f(0,0,1);
	ofVec3f objToCam = cam.getGlobalPosition();
	objToCam.normalize();
	float theta = objectLookAt.angle(objToCam);
	
	ofVec3f axisOfRotation = objToCam.crossed(objectLookAt);
	axisOfRotation.normalize();
 
	glRotatef(-zRot->getPos(), 0.0, 0.0, 1.0);
	glRotatef(-yRot->getPos(), 0.0, 1.0, 0.0);
	glRotatef(-xRot->getPos(), 1.0, 0.0, 0.0);
	glRotatef(-theta, axisOfRotation.x, axisOfRotation.y, axisOfRotation.z);    
}

or

void billBoard(ofVec3f globalCamPosition, ofVec3f globelObjectPosition){
    ofVec3f objectLookAt = ofVec3f(0,0,1);
    ofVec3f objToCam = globalCamPosition - globelObjectPosition;
    objToCam.normalize();
    float theta = objectLookAt.angle(objToCam);
    ofVec3f axisOfRotation = objToCam.crossed(objectLookAt);
    axisOfRotation.normalize();
    glRotatef(-theta, axisOfRotation.x, axisOfRotation.y, axisOfRotation.z);
}

On draw()

	ofPushMatrix();
	ofSetRectMode(OF_RECTMODE_CENTER);
	billBoard();

	// DRAW

	ofSetRectMode(OF_RECTMODE_CORNER);
	ofPopMatrix();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment