//-------------------------------------------------------------- void EnvironmentMapping::draw(){ ofBackgroundGradient(ofColor(50), ofColor(255)); ofEnableDepthTest(); cam.begin(); //texture[0].getTexture().bind(); shader.begin(); shader.setUniformMatrix3f("normalMatrix", mat4ToMat3(ofGetCurrentNormalMatrix())); shader.setUniformTexture("EnvMap", texture[0].getTexture(), 0); shader.setUniform3f("BaseColor",baseColor->x/255,baseColor->y/255,baseColor->z/255); shader.setUniform1f("MixRatio", mixRatio); shader.setUniform3f("LightPosition",lightPos->x,lightPos->y,lightPos->z); //cout<<ofGetCurrentNormalMatrix()<<endl; //model.drawFaces(); sphere.draw(); shader.end(); //texture[0].getTexture().unbind(); cam.end(); ofDisableDepthTest(); gui.draw(); }
//-------------------------------------------------------------- void Lighting::drawScene() { static const auto kNumSpheres = 8; static const auto kRadius = 30.0f; static const auto kSpacing = kRadius * 2.0f + 15.0f; static const auto kOffset = -kNumSpheres * kSpacing * 0.5f; #ifdef USE_INSTANCED static const int kTotalSpheres = kNumSpheres * kNumSpheres; if (!this->bufferObject.isAllocated()) { this->vboMesh = this->sphere.getMesh(); glm::mat4 transforms[kTotalSpheres]; for (auto z = 0; z < kNumSpheres; ++z) { for (auto x = 0; x < kNumSpheres; ++x) { auto idx = z * kNumSpheres + x; transforms[idx] = glm::translate(glm::vec3(kOffset + x * kSpacing, kRadius * 2.0f, kOffset + z * kSpacing)) * glm::scale(glm::vec3(kRadius)); } } this->bufferObject.allocate(sizeof(glm::mat4) * kTotalSpheres, transforms, GL_STATIC_DRAW); this->bufferTexture.allocateAsBufferTexture(this->bufferObject, GL_RGBA32F); } this->material.setUniforms(this->shader); this->shader.setUniformTexture("uOffsetTex", this->bufferTexture, 0); this->vboMesh.drawInstanced(OF_MESH_FILL, kTotalSpheres); #else for (auto z = 0; z < kNumSpheres; ++z) { auto zPercent = z / static_cast<float>(kNumSpheres - 1); for (auto x = 0; x < kNumSpheres; ++x) { auto xPercent = x / static_cast<float>(kNumSpheres - 1); this->material.metallic = std::max(zPercent, 0.001f); this->material.roughness = std::max(xPercent * xPercent, 0.001f); this->material.setUniforms(this->shader); ofPushMatrix(); { ofTranslate(kOffset + x * kSpacing, kRadius * 2.0f, kOffset + z * kSpacing); ofScale(kRadius); this->shader.setUniformMatrix4f("uNormalMatrix", ofGetCurrentNormalMatrix()); this->sphere.draw(); } ofPopMatrix(); } } #endif }
//-------------------------------------------------------------- void CubeMapping::draw(){ ofBackgroundGradient(ofColor(50), ofColor(255)); int pos=800; int boundToTextureUnit=pos; glActiveTexture( GL_TEXTURE0 + pos ); glEnable( GL_TEXTURE_CUBE_MAP ); glBindTexture( GL_TEXTURE_CUBE_MAP, textureObjectID ); ofEnableDepthTest(); cam.begin(); //texture[0].getTexture().bind(); shader.begin(); shader.setUniformMatrix3f("normalMatrix", mat4ToMat3(ofGetCurrentNormalMatrix())); shader.setUniform1i("EnvMap", 0); shader.setUniform3f("LightPosition",lightPos->x,lightPos->y,lightPos->z); shader.setUniform3f("BaseColor",baseColor->x/255,baseColor->y/255,baseColor->z/255); shader.setUniform1f("MixRatio", mixRatio); //cout<<ofGetCurrentNormalMatrix()<<endl; cube.draw(); shader.end(); glActiveTexture( GL_TEXTURE0 + boundToTextureUnit ); glBindTexture(GL_TEXTURE_CUBE_MAP, 0 ); glDisable( GL_TEXTURE_CUBE_MAP ); glActiveTexture( GL_TEXTURE0 ); cam.end(); ofDisableDepthTest(); gui.draw(); }
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); } } }