openFrameworksのofLight, ofMaterialの機能を調べる
openFrameworksのofLightとofMaterialの機能について調べるために,guiでofLight, ofMaterialのパラメータを操作できるプログラムを書いた.
ambient colorは全体,diffuse colorは反射しているところ,specular colorは強く反射しているところの色を決定する. ofLightとofMaterialのどちらにも,ambient color,diffuse color,specular colorを設定することができる.違いはofLightはライトが直接当たっているところだけに影響,ofMaterialはライトが当たっていないところにも影響するということ. ofMaterialのemissive colorは物体自体の色(発光色?)を決定しており,shinessはdiffuse colorにする部分とspecular colorにする部分の割合を決定している.
以下,コード.
なぜかofLightとofxGuiを組み合わせるときは,draw関数内でofxPanelインスタンスのdrawメソッドを呼び出す前に以下を入れないとguiが上手く表示されないので注意.
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
ofMain.h
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxPanel gui;
ofParameter specular_light, diffuse_light, ambient_light,
specular_material, diffuse_material, ambient_material, emmisive_material;
ofParameter is_box;
ofParameter shiness_material;
ofLight light;
ofMaterial material;
ofEasyCam cam;
};
ofMain.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0);
ofEnableDepthTest();
ofEnableSmoothing();
gui.setup();
gui.add(is_box.set("box", false));
gui.add(specular_light.set("specular", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(diffuse_light.set("diffuse", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(ambient_light.set("ambient", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(specular_material.set("specular", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(diffuse_material.set("diffuse", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(ambient_material.set("ambient", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(emmisive_material.set("emmisive", ofFloatColor(0.5, 0.5, 0.5), ofFloatColor(0, 0, 0), ofFloatColor(1, 1, 1)));
gui.add(shiness_material.set("shiness", 0, -128, 128));
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
material.setShininess(shiness_material);
material.setEmissiveColor(emmisive_material);
material.setAmbientColor(ambient_material);
material.setDiffuseColor(diffuse_material);
material.setSpecularColor(specular_material);
cam.begin();
light.enable();
light.setSpotlight();
light.setPosition(-100, 100, 100);
light.setAmbientColor(ambient_light);
light.setDiffuseColor(diffuse_light);
light.setSpecularColor(specular_light);
if(is_box){
ofBoxPrimitive box;
box.set(150);
box.setPosition(0, 0, 0);
material.begin();
box.draw();
material.end();
} else {
ofSpherePrimitive sphere;
sphere.set(100, 90);
sphere.setPosition(0, 0, 0);
material.begin();
sphere.draw();
material.end();
}
light.disable();
cam.end();
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
gui.draw();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
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){
}
openFrameworks/lightCheck at master · aa-debdeb/openFrameworks