//---------------------------------------- void ofLight::setup() { if(glIndex==-1){ bool bLightFound = false; // search for the first free block for(int i=0; i<OF_MAX_LIGHTS; i++) { if(getActiveLights()[i] == false) { glIndex = i; retain(glIndex); bLightFound = true; break; } } if( !bLightFound ){ ofLog(OF_LOG_ERROR, "ofLight : Trying to create too many lights: " + ofToString(glIndex)); } if(bLightFound) { // run this the first time, since it was not found before // onPositionChanged(); setAmbientColor( getAmbientColor() ); setDiffuseColor( getDiffuseColor() ); setSpecularColor( getSpecularColor() ); setAttenuation( getAttenuationConstant(), getAttenuationLinear(), getAttenuationQuadratic() ); if(getIsSpotlight()) { setSpotlightCutOff(getSpotlightCutOff()); setSpotConcentration(getSpotConcentration()); } if(getIsSpotlight() || getIsDirectional()) { onOrientationChanged(); } } } }
//---------------------------------------- void ofLight::onPositionChanged() { if(data->glIndex==-1) return; // if we are a positional light and not directional, update light position if(getIsSpotlight() || getIsPointLight() || getIsAreaLight()) { data->position = ofVec4f(getGlobalPosition().x,getGlobalPosition().y,getGlobalPosition().z,1); ofGetGLRenderer()->setLightPosition(data->glIndex,data->position); } }
//---------------------------------------- void ofLight::setup() { if(data->glIndex==-1){ bool bLightFound = false; // search for the first free block for(size_t i=0; i<ofLightsData().size(); i++) { if(ofLightsData()[i].expired()) { data->glIndex = i; data->isEnabled = true; ofLightsData()[i] = data; bLightFound = true; break; } } if(!bLightFound && ofIsGLProgrammableRenderer()){ ofLightsData().push_back(data); data->glIndex = ofLightsData().size() - 1; data->isEnabled = true; bLightFound = true; } if( bLightFound ){ // run this the first time, since it was not found before // onPositionChanged(); setAmbientColor( getAmbientColor() ); setDiffuseColor( getDiffuseColor() ); setSpecularColor( getSpecularColor() ); setAttenuation( getAttenuationConstant(), getAttenuationLinear(), getAttenuationQuadratic() ); if(getIsSpotlight()) { setSpotlightCutOff(getSpotlightCutOff()); setSpotConcentration(getSpotConcentration()); } if(getIsSpotlight() || getIsDirectional()) { onOrientationChanged(); } }else{ ofLogError("ofLight") << "setup(): couldn't get active GL light, maximum number of "<< ofLightsData().size() << " reached"; } } }
void LightEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, EntityPropertyFlags& requestedProperties, EntityPropertyFlags& propertyFlags, EntityPropertyFlags& propertiesDidntFit, int& propertyCount, OctreeElement::AppendState& appendState) const { bool successPropertyFits = true; APPEND_ENTITY_PROPERTY(PROP_IS_SPOTLIGHT, getIsSpotlight()); APPEND_ENTITY_PROPERTY(PROP_COLOR, getColor()); APPEND_ENTITY_PROPERTY(PROP_INTENSITY, getIntensity()); APPEND_ENTITY_PROPERTY(PROP_EXPONENT, getExponent()); APPEND_ENTITY_PROPERTY(PROP_CUTOFF, getCutoff()); }
//---------------------------------------- void ofLight::customDraw() { ofPushMatrix(); glMultMatrixf(getGlobalTransformMatrix().getPtr()); if(getIsPointLight()) { ofSphere( 0,0,0, 10); } else if (getIsSpotlight()) { float coneHeight = (sin(spotCutOff*DEG_TO_RAD) * 30.f) + 1; float coneRadius = (cos(spotCutOff*DEG_TO_RAD) * 30.f) + 8; ofCone(0, 0, -(coneHeight*.5), coneHeight, coneRadius); } else { ofBox(10); } ofDrawAxis(20); ofPopMatrix(); }
//---------------------------------------- void ofLight::customDraw(const ofBaseRenderer * renderer) const{; if(getIsPointLight()) { renderer->drawSphere( 0,0,0, 10); } else if (getIsSpotlight()) { float coneHeight = (sin(data->spotCutOff*DEG_TO_RAD) * 30.f) + 1; float coneRadius = (cos(data->spotCutOff*DEG_TO_RAD) * 30.f) + 8; const_cast<ofBaseRenderer*>(renderer)->rotate(-90,1,0,0); renderer->drawCone(0, -(coneHeight*.5), 0, coneHeight, coneRadius); } else if (getIsAreaLight()) { const_cast<ofBaseRenderer*>(renderer)->pushMatrix(); renderer->drawPlane(data->width,data->height); const_cast<ofBaseRenderer*>(renderer)->popMatrix(); }else{ renderer->drawBox(10); } ofDrawAxis(20); }
//---------------------------------------- void ofLight::onOrientationChanged() { if(data->glIndex==-1) return; if(getIsDirectional()) { // if we are a directional light and not positional, update light position (direction) auto lookAtDir = glm::normalize(getGlobalOrientation() * glm::vec4(0,0,-1, 1)).xyz(); data->position = {lookAtDir.x,lookAtDir.y,lookAtDir.z,0.f}; ofGetGLRenderer()->setLightPosition(data->glIndex,data->position); }else if(getIsSpotlight() || getIsAreaLight()) { // determines the axis of the cone light auto lookAtDir = glm::normalize(getGlobalOrientation() * glm::vec4(0,0,-1, 1)).xyz(); data->direction = lookAtDir; ofGetGLRenderer()->setLightSpotDirection(data->glIndex, glm::vec4(data->direction, 0.0f)); } if(getIsAreaLight()){ data->up = getUpDir(); data->right = getXAxis(); } }
//---------------------------------------- void ofLight::onOrientationChanged() { if(data->glIndex==-1) return; // if we are a directional light and not positional, update light position (direction) if(getIsDirectional()) { // (tig) takes into account global orientation should node be parented. ofVec3f lookAtDir = ( getGlobalTransformMatrix().getInverse() * ofVec4f(0,0,-1, 1) ).getNormalized(); data->position = ofVec4f(lookAtDir.x,lookAtDir.y,lookAtDir.z,0); ofGetGLRenderer()->setLightPosition(data->glIndex,data->position); }else if(getIsSpotlight() || getIsAreaLight()) { // determines the axis of the cone light // // (tig) takes into account global orientation should node be parented. ofVec3f lookAtDir = ( getGlobalTransformMatrix().getInverse() * ofVec4f(0,0,-1, 1) ).getNormalized(); data->direction = ofVec3f(lookAtDir.x,lookAtDir.y,lookAtDir.z); ofGetGLRenderer()->setLightSpotDirection(data->glIndex,data->direction); } if(getIsAreaLight()){ data->up = getUpDir(); data->right = getXAxis(); } }
void RenderableLightEntityItem::updateRenderItemFromEntity(LightPayload& lightPayload) { auto entity = this; lightPayload.setVisible(entity->getVisible()); auto light = lightPayload.editLight(); light->setPosition(entity->getPosition()); light->setOrientation(entity->getRotation()); bool success; lightPayload.editBound() = entity->getAABox(success); if (!success) { lightPayload.editBound() = render::Item::Bound(); } glm::vec3 dimensions = entity->getDimensions(); float largestDiameter = glm::compMax(dimensions); light->setMaximumRadius(largestDiameter / 2.0f); light->setColor(toGlm(entity->getXColor())); float intensity = entity->getIntensity();//* entity->getFadingRatio(); light->setIntensity(intensity); light->setFalloffRadius(entity->getFalloffRadius()); float exponent = entity->getExponent(); float cutoff = glm::radians(entity->getCutoff()); if (!entity->getIsSpotlight()) { light->setType(model::Light::POINT); } else { light->setType(model::Light::SPOT); light->setSpotAngle(cutoff); light->setSpotExponent(exponent); } }
//---------------------------------------- float ofLight::getSpotConcentration() const{ if(!getIsSpotlight()) { ofLogWarning("ofLight") << "getSpotConcentration(): light " << data->glIndex << " is not a spot light"; } return data->exponent; }
//---------------------------------------- float ofLight::getSpotlightCutOff() const{ if(!getIsSpotlight()) { ofLogWarning("ofLight") << "getSpotlightCutOff(): light " << data->glIndex << " is not a spot light"; } return data->spotCutOff; }
//---------------------------------------- float ofLight::getSpotConcentration() { if(!getIsSpotlight()) { ofLog(OF_LOG_WARNING, "ofLight :: getSpotConcentration : this light is not a spot light"); } return exponent; }
//---------------------------------------- float ofLight::getSpotlightCutOff() { if(!getIsSpotlight()) { ofLog(OF_LOG_WARNING, "ofLight :: getSpotlightCutOff : this light is not a spot light"); } return spotCutOff; }