#include "ofApp.h"
void ofApp::setup(){
ofSetVerticalSync(true);
ofSetFrameRate(60);
ofBackground(10, 10, 10);
ofEnableDepthTest();
bSmoothLighting = true;
ofSetSmoothLighting(true);
ofSetSphereResolution(128);
ofSetBoxResolution(30);
radius = 180.f;
center = {ofGetWidth()*.5, ofGetHeight()*.5, 0};
pointLight.setDiffuseColor( ofColor(0.f, 255.f, 0.f));
pointLight.setSpecularColor( ofColor(255.f, 255.f, 0.f));
pointLight.setPointLight();
spotLight.setDiffuseColor( ofColor(255.f, 0.f, 0.f));
spotLight.setSpecularColor( ofColor(255.f, 255.f, 255.f));
spotLight.setSpotlight();
spotLight.setSpotlightCutOff( 50 );
spotLight.setSpotConcentration( 45 );
directionalLight.setDiffuseColor(ofColor(0.f, 0.f, 255.f));
directionalLight.setSpecularColor(ofColor(255.f, 255.f, 255.f));
directionalLight.setDirectional();
directionalLight.setOrientation( {0, 90, 0} );
bShiny = true;
material.setShininess( 120 );
material.setSpecularColor(ofColor(255, 255, 255, 255));
bPointLight = bSpotLight = bDirLight = true;
ofDisableArbTex();
ofLogoImage.load("of.png");
bUseTexture = true;
}
void ofApp::update() {
pointLight.setPosition(cos(ofGetElapsedTimef()*.6f) * radius * 2 + center.x,
sin(ofGetElapsedTimef()*.8f) * radius * 2 + center.y,
-cos(ofGetElapsedTimef()*.8f) * radius * 2 + center.z);
spotLight.setOrientation( { 0, cos(ofGetElapsedTimef()) * RAD_TO_DEG, 0 } );
spotLight.setPosition( mouseX, mouseY, 200);
}
void ofApp::draw(){
ofEnableLighting();
material.begin();
if (bPointLight) pointLight.enable();
if (bSpotLight) spotLight.enable();
if (bDirLight) directionalLight.enable();
if(bUseTexture) ofLogoImage.getTexture().bind();
ofSetColor(255, 255, 255, 255);
ofPushMatrix();
ofTranslate(center.x, center.y, center.z-300);
ofRotateRad(ofGetElapsedTimef() * .8, 0, 1, 0);
ofDrawSphere( 0,0,0, radius);
ofPopMatrix();
ofPushMatrix();
ofTranslate(300, 300, cos(ofGetElapsedTimef()*1.4) * 300.f);
ofRotateRad(ofGetElapsedTimef()*.6, 1, 0, 0);
ofRotateRad(ofGetElapsedTimef()*.8, 0, 1, 0);
ofDrawBox(0, 0, 0, 60);
ofPopMatrix();
ofPushMatrix();
ofTranslate(center.x, center.y, -900);
ofRotateRad(ofGetElapsedTimef() * .2, 0, 1, 0);
ofDrawBox( 0, 0, 0, 850);
ofPopMatrix();
if(bUseTexture) ofLogoImage.getTexture().unbind();
if (!bPointLight) pointLight.disable();
if (!bSpotLight) spotLight.disable();
if (!bDirLight) directionalLight.disable();
material.end();
ofDisableLighting();
ofSetColor( pointLight.getDiffuseColor() );
if(bPointLight) pointLight.draw();
ofSetColor(255, 255, 255);
ofSetColor( spotLight.getDiffuseColor() );
if(bSpotLight) spotLight.draw();
ofSetColor(255, 255, 255);
ofDrawBitmapString("Point Light On (1) : "+ofToString(bPointLight) +"\n"+
"Spot Light On (2) : "+ofToString(bSpotLight) +"\n"+
"Directional Light On (3) : "+ofToString(bDirLight)+"\n"+
"Shiny Objects On (s) : "+ofToString(bShiny)+"\n"+
"Spot Light Cutoff (up/down) : "+ofToString(spotLight.getSpotlightCutOff(),0)+"\n"+
"Spot Light Concentration (right/left) : " + ofToString(spotLight.getSpotConcentration(),0)+"\n"+
"Smooth Lighting enabled (x) : "+ofToString(bSmoothLighting,0)+"\n"+
"Textured (t) : "+ofToString(bUseTexture,0),
20, 20);
}
void ofApp::keyPressed(int key){
switch (key) {
case '1':
bPointLight = !bPointLight;
break;
case '2':
bSpotLight = !bSpotLight;
break;
case '3':
bDirLight = !bDirLight;
break;
case 's':
bShiny = !bShiny;
if (bShiny) material.setShininess( 120 );
else material.setShininess( 30 );
break;
case 'x':
bSmoothLighting = !bSmoothLighting;
ofSetSmoothLighting(bSmoothLighting);
break;
case 't':
bUseTexture = !bUseTexture;
break;
case OF_KEY_UP:
spotLight.setSpotlightCutOff(spotLight.getSpotlightCutOff()+1);
break;
case OF_KEY_DOWN:
spotLight.setSpotlightCutOff(spotLight.getSpotlightCutOff()-1);
break;
case OF_KEY_RIGHT:
spotLight.setSpotConcentration(spotLight.getSpotConcentration()+1);
break;
case OF_KEY_LEFT:
spotLight.setSpotConcentration(spotLight.getSpotConcentration()-1);
break;
default:
break;
}
}
void ofApp::keyReleased(int key){
}
void ofApp::mouseMoved(int x, int y ){
}
void ofApp::mouseDragged(int x, int y, int button){
}
void ofApp::mousePressed(int x, int y, int button){
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments