void ofxPBR::drawEnvironment(){ glm::mat4 projectionMatrix = ofGetCurrentMatrix(OF_MATRIX_PROJECTION); float m22 = projectionMatrix[2][2]; float m32 = projectionMatrix[3][2]; \ float nearClip = (2.0f*m32) / (2.0f*m22 - 2.0f); float farClip = ((m22 - 1.0f) * nearClip) / (m22 + 1.0); if (enableCubemap && cubeMap != nullptr && cubeMap->isAllocated()) { float scale = (farClip - nearClip) / 2; glm::mat4 invCurrentViewMatrix = glm::inverse(ofGetCurrentViewMatrix()); glm::vec3 translate = glm::vec3(invCurrentViewMatrix[3][0], invCurrentViewMatrix[3][1], invCurrentViewMatrix[3][2]); ofDisableDepthTest(); ofPushMatrix(); ofTranslate(translate); cubeMap->bind(1); envShader->begin(); envShader->setUniform1f("envLevel", cubeMap->getEnvLevel()); envShader->setUniform1i("envMap", 1); envShader->setUniform1i("numMips", cubeMap->getNumMips()); envShader->setUniform1f("cubeMapExposure", cubeMap->getExposure()); envShader->setUniform1f("cubeMapRotation", cubeMap->getRotation()); ofPushMatrix(); ofScale(scale, scale, scale); sphereMesh.draw(); ofPopMatrix(); envShader->end(); cubeMap->unbind(); ofPopMatrix(); ofEnableDepthTest(); } }
void ofMaterial::beginShader(int texType){ initShaders(); switch(texType){ case OF_NO_TEXTURE: currentShader = &shaderNoTexture; break; case GL_TEXTURE_2D: currentShader = &shaderTexture2D; break; default: currentShader = &shaderTextureRect; break; } const ofMatrix4x4 & normalMatrix = ofGetCurrentNormalMatrix(); currentShader->begin(); currentShader->setUniformMatrix4f("normalMatrix",normalMatrix); currentShader->setUniform4fv("mat_ambient", &data.ambient.r); currentShader->setUniform4fv("mat_diffuse", &data.diffuse.r); currentShader->setUniform4fv("mat_specular", &data.specular.r); currentShader->setUniform4fv("mat_emissive", &data.emissive.r); currentShader->setUniform4fv("global_ambient", &ofGetGlobalAmbientColor().r); currentShader->setUniform1f("mat_shininess",data.shininess); for(size_t i=0;i<ofLightsData().size();i++){ string idx = ofToString(i); if(ofLightsData()[i].expired() || !ofLightsData()[i].lock()->isEnabled){ currentShader->setUniform1f("lights["+idx+"].enabled",0); continue; } shared_ptr<ofLight::Data> light = ofLightsData()[i].lock(); ofVec4f lightEyePosition = light->position * ofGetCurrentViewMatrix(); currentShader->setUniform1f("lights["+idx+"].enabled",1); currentShader->setUniform1f("lights["+idx+"].type", light->lightType); currentShader->setUniform4fv("lights["+idx+"].position", &lightEyePosition.x); currentShader->setUniform4fv("lights["+idx+"].ambient", &light->ambientColor.r); currentShader->setUniform4fv("lights["+idx+"].specular", &light->specularColor.r); currentShader->setUniform4fv("lights["+idx+"].diffuse", &light->diffuseColor.r); if(light->lightType==OF_LIGHT_POINT || light->lightType==OF_LIGHT_AREA){ currentShader->setUniform1f("lights["+idx+"].constantAttenuation", light->attenuation_constant); currentShader->setUniform1f("lights["+idx+"].linearAttenuation", light->attenuation_linear); currentShader->setUniform1f("lights["+idx+"].quadraticAttenuation", light->attenuation_quadratic); } if(light->lightType==OF_LIGHT_SPOT){ ofVec3f direction = light->position + light->direction; direction = direction * ofGetCurrentViewMatrix(); direction = direction - lightEyePosition; currentShader->setUniform3fv("lights["+idx+"].spotDirection", &direction.x); currentShader->setUniform1f("lights["+idx+"].spotExponent", light->exponent); currentShader->setUniform1f("lights["+idx+"].spotCutoff", light->spotCutOff); currentShader->setUniform1f("lights["+idx+"].spotCosCutoff", cos(ofDegToRad(light->spotCutOff))); }else if(light->lightType==OF_LIGHT_DIRECTIONAL){ ofVec3f halfVector = (ofVec3f(0,0,1) + lightEyePosition).getNormalized(); currentShader->setUniform3fv("lights["+idx+"].halfVector", &halfVector.x); }else if(light->lightType==OF_LIGHT_AREA){ currentShader->setUniform1f("lights["+idx+"].width", light->width); currentShader->setUniform1f("lights["+idx+"].height", light->height); ofVec3f direction = light->position + light->direction; direction = direction * ofGetCurrentViewMatrix(); direction = direction - lightEyePosition; ofVec3f right = light->position + light->right; right = right * ofGetCurrentViewMatrix(); right = right - lightEyePosition; ofVec3f up = right.getCrossed(direction); currentShader->setUniform3fv("lights["+idx+"].spotDirection", &direction.x); currentShader->setUniform3fv("lights["+idx+"].right", &right.x); currentShader->setUniform3fv("lights["+idx+"].up", &up.x); } } }