Created
February 22, 2011 22:35
-
-
Save onedayitwillmake/839580 to your computer and use it in GitHub Desktop.
Libcinder gl::light vs straight gl calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cinder/app/AppBasic.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/gl/Texture.h" | |
#include "cinder/gl/Light.h" | |
#include "cinder/gl/Material.h" | |
#include "cinder/MayaCamUI.h" | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 }; | |
ci::ColorA cNoMat = ci::ColorA( 0.0f, 0.0f, 0.0f, 1.0f ); | |
GLfloat mat_ambient[] = { 0.6, 0.3, 0.4, 1.0 }; | |
ci::ColorA cAmbient = ci::ColorA( 0.6f, 0.3f, 0.4f, 1.0f ); | |
GLfloat mat_diffuse[] = { 0.3, 0.5, 0.8, 1.0 }; | |
ci::ColorA cDiffuse = ci::ColorA( 0.3f, 0.5f, 0.8f, 1.0f ); | |
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; | |
ci::ColorA cSpecular = ci::ColorA( 1.0f, 1.0f, 1.0f, 1.0f ); | |
GLfloat mat_emission[] = { 0.0, 0.1, 0.3, 0.0 }; | |
ci::ColorA cEmission = ci::ColorA( 0.0f, 0.1f, 0.3f, 0.0f ); | |
GLfloat mat_shininess[] = { 128.0 }; | |
GLfloat no_shininess[] = { 0.0 }; | |
bool useMaterial; | |
class CinderLightApp : public AppBasic { | |
public: | |
void prepareSettings( Settings *settings ); | |
void setup(); | |
void setupCamera(); | |
void setupLights(); | |
// Keyboard | |
bool _isOptionDown; | |
void keyUp ( KeyEvent event); | |
void keyDown( KeyEvent event); | |
// Mouse | |
void mouseMove( MouseEvent event ); | |
void mouseDown( MouseEvent event ); | |
void mouseDrag( MouseEvent event ); | |
void mouseUp ( MouseEvent event ); | |
// Loop | |
void update(); | |
void draw(); | |
MayaCamUI mMayaCam; | |
ci::CameraPersp *_cameraPerspective; | |
ci::Quatf *_sceneRotation; | |
// Light | |
ci::gl::Light *_light; | |
ci::gl::Material *_ribbonMaterial; | |
// animation | |
double mTimePrevious; | |
double mTime; | |
bool bAnimate; | |
// mat | |
bool DIFFUSE; | |
bool AMBIENT; | |
bool SPECULAR; | |
bool EMISSIVE; | |
Vec2f mMousePos; | |
}; | |
#define APP_INITIAL_WIDTH 1024 | |
#define APP_INITIAL_HEIGHT 768 | |
void CinderLightApp::prepareSettings( Settings* settings ) | |
{ | |
settings->setWindowSize( APP_INITIAL_WIDTH, APP_INITIAL_HEIGHT ); | |
settings->setFrameRate( 60.0f ); | |
} | |
void CinderLightApp::setup() | |
{ | |
setupCamera(); | |
setupLights(); | |
DIFFUSE = true; | |
AMBIENT = true; | |
SPECULAR = true; | |
EMISSIVE = true; | |
useMaterial = false; | |
_isOptionDown = false; | |
mTimePrevious = getElapsedSeconds(); | |
mTime = 0.0; | |
bAnimate = false; | |
} | |
void CinderLightApp::setupCamera() | |
{ | |
// Create camera | |
Vec3f p = Vec3f::one() * 10.0f; | |
CameraPersp cam = CameraPersp( getWindowWidth(), getWindowHeight(), 45.0f ); | |
cam.setEyePoint( p ); | |
cam.setCenterOfInterestPoint( Vec3f::zero() ); | |
cam.setPerspective( 45.0f, getWindowAspectRatio(), 0.1f, 500.0f ); | |
// Set mayacamera | |
mMayaCam.setCurrentCam( cam ); | |
// set up our projector (a special type of light) | |
_light = new gl::Light( gl::Light::POINT, 0 ); | |
//mLight = new gl::Light( gl::Light::DIRECTIONAL, 0 ); | |
_light->lookAt( ci::Vec3f::one() * 20, Vec3f( 0, 0, 0 ) ); | |
_light->setAmbient( Color( 1.0f, 1.0f, 1.0f ) ); | |
_light->setDiffuse( Color( 1.0f, 1.0f, 1.0f ) ); | |
_light->setSpecular( Color( 1.0f, 1.0f, 1.0f ) ); | |
_light->setShadowParams( 40.0f, 1.0f, 30.0f ); | |
_light->enable(); | |
// setup our scene (a simple gray cube) | |
_ribbonMaterial = new gl::Material(); | |
_ribbonMaterial->setSpecular( Color::white() ); | |
_ribbonMaterial->setDiffuse( Color(0.9f, 0.1f, 0.6f) ); | |
_ribbonMaterial->setAmbient( Color( 0.6f, 0.2f, 0.2f ) ); | |
_ribbonMaterial->setShininess( 5.0f ); | |
} | |
void CinderLightApp::setupLights() | |
{ | |
} | |
void CinderLightApp::mouseMove( MouseEvent event ) | |
{ | |
mMousePos.x = event.getX() - getWindowWidth() * 0.5f; | |
mMousePos.y = getWindowHeight() * 0.5f - event.getY(); | |
} | |
void CinderLightApp::mouseDown( MouseEvent event ) | |
{ | |
// let the camera handle the interaction | |
mMayaCam.mouseDown( event.getPos() ); | |
} | |
void CinderLightApp::mouseDrag( MouseEvent event ) | |
{ | |
// let the camera handle the interaction | |
mMayaCam.mouseDrag( event.getPos(), event.isLeftDown(), event.isMetaDown(), event.isRightDown() ); | |
} | |
void CinderLightApp::mouseUp( MouseEvent event ) | |
{ | |
// let the camera handle the interaction | |
mMayaCam.mouseDown( event.getPos() ); | |
} | |
void CinderLightApp::keyDown( KeyEvent event ) { | |
// _isOptionDown = event.isMetaDown(); | |
if(event.getCode() == KeyEvent::KEY_b) { | |
bAnimate = !bAnimate; | |
} | |
if(event.getCode() == KeyEvent::KEY_m) { | |
useMaterial = !useMaterial; | |
} | |
if( event.getChar() == 'd' || event.getChar() == 'd' ){ | |
DIFFUSE = ! DIFFUSE; | |
} | |
else if( event.getChar() == 'a' || event.getChar() == 'A' ){ | |
AMBIENT = ! AMBIENT; | |
} | |
else if( event.getChar() == 's' || event.getChar() == 'S' ){ | |
SPECULAR = ! SPECULAR; | |
} | |
else if( event.getChar() == 'e' || event.getChar() == 'E' ){ | |
EMISSIVE = ! EMISSIVE; | |
} | |
else if( event.getChar() == 'f' || event.getChar() == 'F' ){ | |
setFullScreen( ! isFullScreen() ); | |
} | |
else if( event.getChar() == '/' || event.getChar() == '?' ){ | |
// mInfoPanel.toggleState(); | |
} | |
else if( event.getChar() == 'r' || event.getChar() == 'R' ){ | |
// renderInfoPanel = ! renderInfoPanel; | |
} | |
else if( event.getChar() == ',' || event.getChar() == '<' ){ | |
mat_shininess[0] *= 0.5f; | |
if( mat_shininess[0] < 8.0f ) | |
mat_shininess[0] = 8.0f; | |
} | |
else if( event.getChar() == '.' || event.getChar() == '>' ){ | |
mat_shininess[0] *= 2.0f; | |
if( mat_shininess[0] > 128.0f ) | |
mat_shininess[0] = 128.0f; | |
} | |
} | |
void CinderLightApp::keyUp( KeyEvent event ) { | |
} | |
void CinderLightApp::update() | |
{ | |
double elapsed = getElapsedSeconds() - mTimePrevious; | |
mTimePrevious = getElapsedSeconds(); | |
if(bAnimate) mTime += 0.25 * elapsed; | |
/*// animate camera | |
if(bAnimate) { | |
Vec3f eye = Vec3f( cosf(mTime), 1.0f, sinf(mTime) ) * 8.0f; | |
mCamera->lookAt( eye, Vec3f::zero() ); | |
} //*/ | |
// animate light | |
if(bAnimate) { | |
Vec3f p = Vec3f( cosf(mTime), 1.0f - 0.5f * sinf(0.1f * mTime), sinf(mTime) ) * 55.0f; | |
_light->lookAt(p, ci::Vec3f::zero() ); | |
}//*/ | |
} | |
void CinderLightApp::draw() | |
{ | |
// clear out the window with black | |
// gl::clear( Color( 0, 0, 0 ) ); | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
gl::enableDepthWrite(); | |
gl::enableDepthRead(); | |
gl::enableAlphaBlending(); | |
glEnable( GL_LIGHTING ); | |
gl::setMatrices( mMayaCam.getCamera() ); | |
// _light->update( mMayaCam.getCamera() ); | |
// _light->enable(); | |
glEnable( GL_LIGHTING ); | |
glEnable( GL_LIGHT0 ); | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
GLfloat light_position[] = { mMousePos.x, mMousePos.y, 75.0f, 0.0f }; | |
glLightfv( GL_LIGHT0, GL_POSITION, light_position ); | |
_ribbonMaterial->apply(); | |
// DRAW | |
float sphereSpacing = 75.0f; | |
float sphereRadius = 35.0f; | |
int sphereDetail = 32; | |
int spheresPerRow = 9; | |
int spheresPerColumn = 5; | |
for( int x=0; x<spheresPerRow; x++ ){ | |
float xPer = (float)x/(float)(spheresPerRow - 1 ); | |
for( int y=0; y<spheresPerColumn; y++ ){ | |
float yPer = (float)y/(float)(spheresPerColumn - 1 ); | |
float xp = sphereSpacing * ( x - ( spheresPerRow - 1 ) * 0.5f ); | |
float yp = sphereSpacing * ( y - ( spheresPerColumn - 1 ) * 0.5f ); | |
glPushMatrix(); | |
glTranslatef( xp, yp, 0.0); | |
if( DIFFUSE ){ | |
ci::ColorA color( CM_HSV, xPer, yPer, 1.0f, 1.0f ); | |
// glMaterialfv( GL_FRONT, GL_DIFFUSE, color ); | |
_ribbonMaterial->setDiffuse( color ); | |
} else { | |
// glMaterialfv( GL_FRONT, GL_DIFFUSE, no_mat ); | |
_ribbonMaterial->setDiffuse( cNoMat ); | |
} | |
if( AMBIENT ) { | |
// glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient ); | |
_ribbonMaterial->setAmbient( cAmbient ); | |
} else { | |
// glMaterialfv( GL_FRONT, GL_AMBIENT, no_mat ); | |
_ribbonMaterial->setAmbient( cNoMat ); | |
} | |
//#define USE_MATERIAL 1 | |
//#if USE_MATERIAL | |
if(useMaterial) | |
{ | |
if( SPECULAR ) | |
{ | |
_ribbonMaterial->setSpecular( cSpecular ); | |
_ribbonMaterial->setShininess( mat_shininess[0] ); | |
} else { | |
_ribbonMaterial->setSpecular( ci::ColorA( 0.0f, 0.0f, 0.0f, 1.0f ) ); | |
_ribbonMaterial->setShininess( no_shininess[0] ); | |
} | |
} | |
else | |
{ | |
//#else | |
if( SPECULAR ){ | |
glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular ); | |
glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess ); | |
} else { | |
glMaterialfv( GL_FRONT, GL_SPECULAR, no_mat ); | |
glMaterialfv( GL_FRONT, GL_SHININESS, no_shininess ); | |
} | |
} | |
//#endif | |
if( EMISSIVE ) { | |
// glMaterialfv( GL_FRONT, GL_EMISSION, mat_emission ); | |
_ribbonMaterial->setEmission( cEmission ); | |
} else { | |
// glMaterialfv( GL_FRONT, GL_EMISSION, no_mat ); | |
_ribbonMaterial->setEmission( cNoMat ); | |
} | |
gl::drawSphere( Vec3f::zero(), sphereRadius, sphereDetail ); | |
glPopMatrix(); | |
} | |
} | |
// DRAW NO LIGHTING | |
glDisable( GL_LIGHTING ); | |
// Draw the lighting frustum | |
glColor3f( 1.0f, 1.0f, 1.0f ); | |
gl::drawFrustum( _light->getShadowCamera() ); | |
} | |
CINDER_APP_BASIC( CinderLightApp, RendererGl ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment