void CglAxis::display(){ int shaderID = initProgram(pcv->fresnelID()); GLuint MatrixID = glGetUniformLocation(shaderID, "MVP"); GLuint colorID = glGetUniformLocation(shaderID, "COL"); GLuint MID = glGetUniformLocation(shaderID, "M"); GLuint VID = glGetUniformLocation(shaderID, "V"); int fill_light_ID = glGetUniformLocation(shaderID, "FILL"); int side_light_ID = glGetUniformLocation(shaderID, "SIDE"); int back_light_ID = glGetUniformLocation(shaderID, "BACK"); int gridID = glGetUniformLocation(shaderID, "GRID"); //GRID if( (pcv->profile.displayBottomGrid) && (pScene->scene_type != CGL_GALERY) ){ enableFog(shaderID); glm::mat4 VIEW = sVIEW(); glm::mat4 MVP = sPROJ() * sVIEW() * sMODEL() * glm::translate( glm::scale(glm::mat4(1), glm::vec3(pScene->getScale(),1,pScene->getScale())), glm::vec3(0) );//glm::vec3(sMODEL()[3]) ); //glm::translate(glm::mat4(1), center) * // glm::scale(glm::translate( sPROJ() * VIEW * MODEL, center), // glm::vec3(pScene->getScale(), 1, pScene->getScale())) * // glm::translate(glm::mat4(1), -center); uniform(MatrixID, MVP); uniform(MID, MODEL); uniform(VID, VIEW); uniform(colorID, glm::vec3(0.,0.,1.0)); std::vector<pCglLight> lights = pcv->getSubWindow()->getScene()->getLights(); uniform( fill_light_ID, *(lights[0]->getLightMatrix(pMaterial))); uniform( side_light_ID, *(lights[1]->getLightMatrix(pMaterial))); uniform( back_light_ID, *(lights[2]->getLightMatrix(pMaterial))); uniform(gridID, 1.0f); //glLineWidth(1.0); //bindBuffer(0, GL_ARRAY_BUFFER, secondaryGridBuffer); //glBindAttribLocation( shaderID, 0, "vertex_position"); //glDrawArrays(GL_LINES, 0, secondaryGrid.size()/3); glEnable(GL_BLEND); //glLineWidth(2.0); bindBuffer(0, GL_ARRAY_BUFFER, mainGridBuffer); glBindAttribLocation( shaderID, 0, "vertex_position"); bindBuffer(1, GL_ARRAY_BUFFER, nBuffer); glBindAttribLocation( shaderID, 1, "vertex_normal"); //glDrawArrays(GL_QUADS, 0, mainGrid.size()/3); //STENCIL WRITING glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 0xFF); // Set any stencil to 1 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glStencilMask(0xFF); // Write to stencil buffer glDepthMask(GL_FALSE); // Don't write to depth buffer glClear(GL_STENCIL_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, mainGrid.size()/3); glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1 glStencilMask(0x00); // Don't write anything to stencil buffer glDepthMask(GL_TRUE); // Write to depth buffer glDisable(GL_STENCIL_TEST); //Draw d'un contour //glLineWidth(10.0f); //uniform(gridID, 0.0f); //glEnable(GL_POLYGON_OFFSET_LINE); //glPolygonOffset(1.0,1.0); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glDrawArrays(GL_QUADS, 0, mainGrid.size()/3); //glDisable(GL_POLYGON_OFFSET_LINE); //glLineWidth(1.0f); glDisable(GL_BLEND); disableFog(shaderID); glPolygonMode(GL_FRONT, GL_FILL); } shaderID = initProgram(pcv->simpleID()); glPolygonMode(GL_FRONT, GL_LINE); MatrixID = glGetUniformLocation(shaderID, "MVP"); colorID = glGetUniformLocation(shaderID, "COL"); MID = glGetUniformLocation(shaderID, "M"); VID = glGetUniformLocation(shaderID, "V"); //Axes if(pcv->profile.displayAxes){ glDisable(GL_DEPTH_TEST); glViewport(0,0,view->width/6,view->width/6); glm::mat4 NEUTRAL = glm::mat4(glm::perspective(70.0, 1.0, view->m_znear, view->m_zfar)) * sVIEW() * glm::mat4(1); glm::mat4 M = glm::scale(NEUTRAL, glm::vec3(view->zoom)); uniform(MatrixID, M); bindBuffer(0, GL_ARRAY_BUFFER, axesBuffer); glBindAttribLocation( shaderID, 0, "vertex_position"); glLineWidth(2.0); uniform(colorID, glm::vec3(1,0,0)); glDrawArrays(GL_LINES, 0, 2); uniform(colorID, glm::vec3(0,1,0)); glDrawArrays(GL_LINES, 2, 4); uniform(colorID, glm::vec3(0,0,1)); glDrawArrays(GL_LINES, 4, 6); view->reshape(view->width,view->height); glEnable(GL_DEPTH_TEST); } glLineWidth(1.0); glPolygonMode(GL_FRONT, GL_FILL); freeBuffer(); }
void MFixedRenderer::drawScene(MScene * scene, MOCamera * camera) { M_PROFILE_SCOPE(MFixedRenderer::drawScene); struct MEntityLight { MBox3d lightBox; MOLight * light; }; struct MSubMeshPass { unsigned int subMeshId; unsigned int lightsNumber; MObject3d * object; MOLight * lights[4]; }; // sub objects #define MAX_TRANSP_SUBOBJ 4096 static int transpList[MAX_TRANSP_SUBOBJ]; static float transpZList[MAX_TRANSP_SUBOBJ]; static MSubMeshPass transpSubObjs[MAX_TRANSP_SUBOBJ]; // lights list #define MAX_ENTITY_LIGHTS 256 static int entityLightsList[MAX_ENTITY_LIGHTS]; static float entityLightsZList[MAX_ENTITY_LIGHTS]; static MEntityLight entityLights[MAX_ENTITY_LIGHTS]; // get render MRenderingContext * render = MEngine::getInstance()->getRenderingContext(); render->enableLighting(); render->enableBlending(); // make frustum MFrustum * frustum = camera->getFrustum(); frustum->makeVolume(camera); // update visibility updateVisibility(scene, camera); // fog enableFog(camera); // camera MVector3 cameraPos = camera->getTransformedPosition(); // transp sub obj number unsigned int transpSubObsNumber = 0; // lights unsigned int l; unsigned int lSize = scene->getLightsNumber(); // entities unsigned int i; unsigned int eSize = scene->getEntitiesNumber(); for(i=0; i<eSize; i++) { // get entity MOEntity * entity = scene->getEntityByIndex(i); MMesh * mesh = entity->getMesh(); if(! entity->isActive()) continue; if(! entity->isVisible()) { if(mesh) { MArmature * armature = mesh->getArmature(); MArmatureAnim * armatureAnim = mesh->getArmatureAnim(); if(armature) { // animate armature if(armatureAnim) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // TODO : optimize and add a tag to desactivate it updateSkinning(mesh, armature); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } } continue; } // draw mesh if(mesh) { MVector3 scale = entity->getTransformedScale(); MBox3d * entityBox = entity->getBoundingBox(); float minScale = scale.x; minScale = MIN(minScale, scale.y); minScale = MIN(minScale, scale.z); minScale = 1.0f / minScale; unsigned int entityLightsNumber = 0; for(l=0; l<lSize; l++) { // get entity MOLight * light = scene->getLightByIndex(l); if(! light->isActive()) continue; if(! light->isVisible()) continue; // light box MVector3 lightPos = light->getTransformedPosition(); MVector3 localPos = entity->getInversePosition(lightPos); float localRadius = light->getRadius() * minScale; MBox3d lightBox( MVector3(localPos - localRadius), MVector3(localPos + localRadius) ); if(! entityBox->isInCollisionWith(&lightBox)) continue; MEntityLight * entityLight = &entityLights[entityLightsNumber]; entityLight->lightBox = lightBox; entityLight->light = light; entityLightsNumber++; if(entityLightsNumber == MAX_ENTITY_LIGHTS) break; } // animate armature if(mesh->getArmature() && mesh->getArmatureAnim()) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // animate textures if(mesh->getTexturesAnim()) animateTextures(mesh, mesh->getTexturesAnim(), entity->getCurrentFrame()); // animate materials if(mesh->getMaterialsAnim()) animateMaterials(mesh, mesh->getMaterialsAnim(), entity->getCurrentFrame()); unsigned int s; unsigned int sSize = mesh->getSubMeshsNumber(); for(s=0; s<sSize; s++) { MSubMesh * subMesh = &mesh->getSubMeshs()[s]; MBox3d * box = subMesh->getBoundingBox(); // check if submesh visible if(sSize > 1) { MVector3 * min = box->getMin(); MVector3 * max = box->getMax(); MVector3 points[8] = { entity->getTransformedVector(MVector3(min->x, min->y, min->z)), entity->getTransformedVector(MVector3(min->x, max->y, min->z)), entity->getTransformedVector(MVector3(max->x, max->y, min->z)), entity->getTransformedVector(MVector3(max->x, min->y, min->z)), entity->getTransformedVector(MVector3(min->x, min->y, max->z)), entity->getTransformedVector(MVector3(min->x, max->y, max->z)), entity->getTransformedVector(MVector3(max->x, max->y, max->z)), entity->getTransformedVector(MVector3(max->x, min->y, max->z)) }; if(! frustum->isVolumePointsVisible(points, 8)) continue; } // subMesh center MVector3 center = (*box->getMin()) + ((*box->getMax()) - (*box->getMin()))*0.5f; center = entity->getTransformedVector(center); // sort entity lights unsigned int lightsNumber = 0; for(l=0; l<entityLightsNumber; l++) { MEntityLight * entityLight = &entityLights[l]; if(! box->isInCollisionWith(&entityLight->lightBox)) continue; MOLight * light = entityLight->light; float z = (center - light->getTransformedPosition()).getLength(); entityLightsList[lightsNumber] = l; entityLightsZList[l] = (1.0f/z)*light->getRadius(); lightsNumber++; } if(lightsNumber > 1) sortFloatList(entityLightsList, entityLightsZList, 0, (int)lightsNumber-1); // local lights if(lightsNumber > 4) lightsNumber = 4; for(l=0; l<lightsNumber; l++) { MEntityLight * entityLight = &entityLights[entityLightsList[l]]; MOLight * light = entityLight->light; // attenuation float quadraticAttenuation = (8.0f / light->getRadius()); quadraticAttenuation = (quadraticAttenuation*quadraticAttenuation)*light->getIntensity(); // color MVector3 color = light->getFinalColor(); // set light render->enableLight(l); render->setLightPosition(l, light->getTransformedPosition()); render->setLightDiffuse(l, MVector4(color)); render->setLightSpecular(l, MVector4(color)); render->setLightAmbient(l, MVector3(0, 0, 0)); render->setLightAttenuation(l, 1, 0, quadraticAttenuation); // spot render->setLightSpotAngle(l, light->getSpotAngle()); if(light->getSpotAngle() < 90){ render->setLightSpotDirection(l, light->getRotatedVector(MVector3(0, 0, -1)).getNormalized()); render->setLightSpotExponent(l, light->getSpotExponent()); } else { render->setLightSpotExponent(l, 0.0f); } } for(l=lightsNumber; l<4; l++){ render->disableLight(l); } render->pushMatrix(); render->multMatrix(entity->getMatrix()); // draw opaques drawOpaques(subMesh, mesh->getArmature()); if(subMesh->hasTransparency()) { if(transpSubObsNumber < MAX_TRANSP_SUBOBJ) { // transparent subMesh pass MSubMeshPass * subMeshPass = &transpSubObjs[transpSubObsNumber]; // lights subMeshPass->lightsNumber = lightsNumber; for(l=0; l<lightsNumber; l++) subMeshPass->lights[l] = entityLights[entityLightsList[l]].light; // z distance to camera float z = getDistanceToCam(camera, center); // set values transpList[transpSubObsNumber] = transpSubObsNumber; transpZList[transpSubObsNumber] = z; subMeshPass->subMeshId = s; subMeshPass->object = entity; transpSubObsNumber++; } } render->popMatrix(); } mesh->updateBoundingBox(); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } } // texts unsigned int tSize = scene->getTextsNumber(); for(i=0; i<tSize; i++) { MOText * text = scene->getTextByIndex(i); if(text->isActive() && text->isVisible()) { // transparent pass MSubMeshPass * subMeshPass = &transpSubObjs[transpSubObsNumber]; // center MBox3d * box = text->getBoundingBox(); MVector3 center = (*box->getMin()) + ((*box->getMax()) - (*box->getMin()))*0.5f; center = text->getTransformedVector(center); // z distance to camera float z = getDistanceToCam(camera, center); // set values transpList[transpSubObsNumber] = transpSubObsNumber; transpZList[transpSubObsNumber] = z; subMeshPass->object = text; transpSubObsNumber++; } } // sort transparent list if(transpSubObsNumber > 1) sortFloatList(transpList, transpZList, 0, (int)transpSubObsNumber-1); // draw transparents for(i=0; i<transpSubObsNumber; i++) { MSubMeshPass * subMeshPass = &transpSubObjs[transpList[i]]; MObject3d * object = subMeshPass->object; // objects switch(object->getType()) { case M_OBJECT3D_ENTITY: { MOEntity * entity = (MOEntity *)object; unsigned int subMeshId = subMeshPass->subMeshId; MMesh * mesh = entity->getMesh(); MSubMesh * subMesh = &mesh->getSubMeshs()[subMeshId]; // animate armature if(mesh->getArmature() && mesh->getArmatureAnim()) animateArmature( mesh->getArmature(), mesh->getArmatureAnim(), entity->getCurrentFrame() ); // animate textures if(mesh->getTexturesAnim()) animateTextures(mesh, mesh->getTexturesAnim(), entity->getCurrentFrame()); // animate materials if(mesh->getMaterialsAnim()) animateMaterials(mesh, mesh->getMaterialsAnim(), entity->getCurrentFrame()); // lights for(l=0; l<subMeshPass->lightsNumber; l++) { MOLight * light = subMeshPass->lights[l]; // attenuation float quadraticAttenuation = (8.0f / light->getRadius()); quadraticAttenuation = (quadraticAttenuation*quadraticAttenuation)*light->getIntensity(); // color MVector3 color = light->getFinalColor(); // set light render->enableLight(l); render->setLightPosition(l, light->getTransformedPosition()); render->setLightDiffuse(l, MVector4(color)); render->setLightSpecular(l, MVector4(color)); render->setLightAmbient(l, MVector3(0, 0, 0)); render->setLightAttenuation(l, 1, 0, quadraticAttenuation); // spot render->setLightSpotAngle(l, light->getSpotAngle()); if(light->getSpotAngle() < 90){ render->setLightSpotDirection(l, light->getRotatedVector(MVector3(0, 0, -1)).getNormalized()); render->setLightSpotExponent(l, light->getSpotExponent()); } else { render->setLightSpotExponent(l, 0.0f); } } for(l=subMeshPass->lightsNumber; l<4; l++){ render->disableLight(l); } render->pushMatrix(); render->multMatrix(entity->getMatrix()); drawTransparents(subMesh, mesh->getArmature()); render->popMatrix(); mesh->updateBoundingBox(); (*entity->getBoundingBox()) = (*mesh->getBoundingBox()); } break; case M_OBJECT3D_TEXT: { MOText * text = (MOText *)object; render->pushMatrix(); render->multMatrix(text->getMatrix()); drawText(text); render->popMatrix(); } break; } } render->disableLighting(); render->disableFog(); }
//-------------------------------------------------------------- void ofApp::draw(){ ofBackground(scaledVol); ofNoFill(); ofSetColor(scaledVol); cam.begin(); ofScale(1, 1, -1); enableFog(fogNear, fogFar); float moveOffset = moveSpeed * ofGetElapsedTimef() / tunnelSeparation; int moveWhole = floor(moveOffset); float moveFraction = fmodf(moveOffset, 1); ofVboMesh tunnelSegment; tunnelSegment.setMode(OF_PRIMITIVE_LINES); ofVec3f segmentOffset(0, 0, tunnelSeparation); for(int i = 0; i < circleResolution; i++) { float theta0 = ofMap(i + 0, 0, circleResolution - 1, 0, 360); float theta1 = ofMap(i + 1, 0, circleResolution - 1, 0, 360); ofVec2f v0(tunnelRadius, 0); ofVec2f v1(tunnelRadius, 0); v0.rotate(theta0); v1.rotate(theta1); tunnelSegment.addVertex(v0); tunnelSegment.addVertex(v1); tunnelSegment.addVertex(v0); tunnelSegment.addVertex(segmentOffset + v0); tunnelSegment.addVertex(segmentOffset + v0); tunnelSegment.addVertex(segmentOffset + v1); } ofPushMatrix(); ofVec2f lerpOrientation = ofVec2f().interpolate(getOrientation(rotationChange * (1 + moveWhole)), moveFraction); ofRotateX(-lerpOrientation.x); ofRotateY(-lerpOrientation.y); ofTranslate(0, 0, -moveFraction * tunnelSeparation); for(int i = 0; i < tunnelLength; i++) { if(i > 0) { ofVec2f orientation = getOrientation(rotationChange * (i + moveWhole)); ofRotateX(orientation.x); ofRotateY(orientation.y); } ofSetColor(255); tunnelSegment.draw(); ofTranslate(0, 0, tunnelSeparation); } ofPopMatrix(); disableFog(); cam.end(); }