void Box3::setFrom(ptr<Object3D> obj) { obj->updateMatrixWorld(true); this->makeEmpty(); obj->traverse([&]( ptr<Object3D> node ) { if( instance_of(node, Mesh)) { ptr<Mesh> mesh = downcast(node, Mesh); if( mesh->hasGeometry() ) { ptr<Geometry> geom = mesh->getGeometry(); for( glm::vec3 vec : geom->getVertices() ) { glm::vec3 trans_vec = glm::vec3(geom->matrixWorld * glm::vec4( vec, 1.0 ) ); this->expandByPoint( trans_vec ); } } } }); }
/** * @return false iff all threads are dead. */ void throw_exception (Object *exception) { Thread *auxThread; #ifdef VERIFY assert (exception != null, EXCEPTIONS0); #endif // VERIFY #if DEBUG_EXCEPTIONS printf("Throw exception\n"); #endif if (currentThread == null) { // No threads have started probably return; } else if (exception == interruptedException) { // Throwing an interrupted exception clears the flag currentThread->interruptState = INTERRUPT_CLEARED; } #ifdef VERIFY assert (currentThread->state > DEAD, EXCEPTIONS1); #endif // VERIFY gExceptionPc = pc; gExcepMethodRec = null; #if 0 trace (-1, get_class_index(exception), 3); #endif LABEL_PROPAGATE: tempStackFrame = current_stackframe(); tempMethodRecord = tempStackFrame->methodRecord; if (gExcepMethodRec == null) gExcepMethodRec = tempMethodRecord; gExceptionRecord = (ExceptionRecord *) (get_binary_base() + tempMethodRecord->exceptionTable); tempCurrentOffset = ptr2word(pc) - ptr2word(get_binary_base() + tempMethodRecord->codeOffset); #if 0 trace (-1, tempCurrentOffset, 5); #endif gNumExceptionHandlers = tempMethodRecord->numExceptionHandlers; #if DEBUG_EXCEPTIONS printf("Num exception handlers=%d\n",gNumExceptionHandlers); #endif while (gNumExceptionHandlers--) { if (gExceptionRecord->start <= tempCurrentOffset /* off by one? < ? */ && tempCurrentOffset <= gExceptionRecord->end) { // Check if exception class applies if (instance_of (exception, gExceptionRecord->classIndex)) { // Clear operand stack init_sp (tempStackFrame, tempMethodRecord); // Push the exception object push_ref (ptr2word (exception)); // Jump to handler: pc = get_binary_base() + tempMethodRecord->codeOffset + gExceptionRecord->handler; #if DEBUG_EXCEPTIONS printf("Found exception handler\n"); #endif return; } } gExceptionRecord++; } // No good handlers in current stack frame - go up. auxThread = currentThread; do_return (0); // Note: return takes care of synchronized methods. if (auxThread->state == DEAD) { #if DEBUG_EXCEPTIONS printf("Thread is dead\n"); #endif if (get_class_index(exception) != JAVA_LANG_THREADDEATH) { #if DEBUG_EXCEPTIONS printf("Handle uncaught exception\n"); #endif handle_uncaught_exception (exception, auxThread, gExcepMethodRec, tempMethodRecord, gExceptionPc); } return; } goto LABEL_PROPAGATE; }
void Ex_006_BoundingBoxTest::run() { const string path = "/Users/saburookita/Personal Projects/Three.cpp Rev.2/examples/assets/"; ForwardRenderer renderer; renderer.init( "Ex 006: Bounding Box Tests", 1600 * 2 / 4, 900 * 2 / 4 ); renderer.setCameraControl(Arcball::create(2.0f)); /* Create scene */ auto scene = Scene::create(); scene->setFog(Fog::create( 0x72645b / 2, 2.0, 15.0 )); scene->setViewport( 0.0, 0.0, renderer.getWidth(), renderer.getHeight() ); scene->setShadowMapType( SHADOW_MAP::PCF ); /* Create camera */ auto camera = PerspectiveCamera::create( 50.0, renderer.getAspectRatio(), 0.001, 100.0 ); camera->translate(0.0, 1.5, 5.5); camera->lookAt( 0.0, 1.0, 0.0 ); /* Load our ply models */ vector<string> filenames = { "dragon_vrip_res3.ply", "happy_vrip_res3.ply", }; vector<ptr<Mesh>> statues; float x_offset = -1.0; for( string filename: filenames ) { auto statue = Loader::loadPLY(path + "/ply models/", filename, aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_JoinIdenticalVertices | aiProcess_GenSmoothNormals | aiProcess_FlipWindingOrder ); statue->setMaterial(PhongMaterial::create(0xcccccc, 0x0, 0x000000, 0x999999, 10, true)); statue->getGeometry()->setScale(10.0f); statue->castShadow = true; statue->receiveShadow = true; auto bbox = statue->computeBoundingBox(); glm::vec3 center = bbox->center(); glm::vec3 size = bbox->size(); statue->translate(x_offset, -(center.y - size.y * 0.5), 0.0); cout << *bbox << endl; x_offset += 2.0f; scene->add( statue ); statues.push_back( statue ); } auto box = Mesh::create( CubeGeometry::create(1.0f, 3), PhongMaterial::create(0x777777, 0x777777, 0x0, 0x0, 0.0, true) ); box->setTexture( TextureUtils::loadAsTexture( path, "crate.tga") ); box->getGeometry()->rotateX(45.0f); box->getGeometry()->setScale(1.5f); box->translate(3.0f, 0.5, 0.0); box->castShadow = true; box->receiveShadow = true; scene->add( box ); auto cylinder = Mesh::create( CylinderGeometry::create(0.5, 0.5, 1.0, 30, 5, false), PhongMaterial::create( 0xDDDDDD, 0x0, 0x0, 0x111111, 150.0, true ) ); cylinder->translate(-3.0f, 0.5f, 0.0f); cylinder->castShadow = true; cylinder->receiveShadow = true; scene->add( cylinder ); /* And the ground plane */ auto plane = Mesh::create( PlaneGeometry::create(50.0f), PhongMaterial::create() ); plane->rotateX(-90.0f); plane->receiveShadow = true; scene->add( plane ); /* Cubemap */ // auto env = Mesh::create( CubeGeometry::create(50.0f), MeshCubeMapMaterial::create() ); // env->setTexture( TextureUtils::loadAsEnvMap( path + "cube/pisa", // "nx.png", "ny.png", "nz.png", // "px.png", "py.png", "pz.png")); // // cylinder->setEnvMap( env->getTexture() ); // scene->add( env ); /* Create a (rotating) directional light */ auto dir_light = DirectionalLight::create(0x99CCFF, 1.35, glm::vec3( 3.0, 1.0, 3.0 ) ); dir_light->castShadow = true; dir_light->shadowBias = -0.0001; dir_light->shadowCameraNear = -10.0; dir_light->shadowCameraFar = 10.0; dir_light->shadowMapSize = glm::vec2(512); scene->add( dir_light ); /* Create a spotlight, the shadow should be casted no the left hand side */ auto spot_light = SpotLight::create(0x99CCFF, 1.0, 20.0, 50.0, 1.0 ); spot_light->position = glm::vec3(3.0, 2.0, 3.0); spot_light->castShadow = true; scene->add( spot_light ); /* Create an ambient light */ scene->add( AmbientLight::create(0xCCCCCC)); /* Create a post render callback for objects rotation */ bool rotate_objects = false; float light_rotation_1 = 0.0; renderer.setPostRenderCallbackHandler( [&](){ dir_light->position.x = ( 3.0 * cosf( light_rotation_1 ) ); dir_light->position.z = ( 3.0 * sinf( light_rotation_1 ) ); light_rotation_1 += 0.01; if( rotate_objects ) { box->rotateY(-1.0f); statues[0]->rotateY(-0.5f); } }); renderer.setMouseButtonCallbackHandler([&] (GLFWwindow *, int button, int action, int mod){ auto descendants = scene->getDescendants(); /* Reset the color first */ for( auto obj: descendants ) { if( instance_of(obj, Mesh)) { auto mesh = downcast( obj, Mesh ); if( instance_of(mesh->getMaterial(), PhongMaterial)){ auto phong = downcast(mesh->getMaterial(), PhongMaterial); phong->setDiffuseColor( 0xDDDDDD ); } } } if( action == GLFW_PRESS ) { auto raycaster = Projector::pickingRay(renderer.getCursorPosition(), camera); /* Upon selected / ray picked, change the diffuse color to red */ for( auto obj: descendants ) { if( instance_of(obj, Geometry)) continue; auto bound = obj->getBoundingBox(); if(raycaster->ray->intersects(bound)) { if( instance_of(obj, Mesh)) { auto mesh = downcast( obj, Mesh ); if( instance_of(mesh->getMaterial(), PhongMaterial)){ auto phong = downcast(mesh->getMaterial(), PhongMaterial); phong->setDiffuseColor( 0xDD0000 ); } } } } } }); /* Override key callback handler */ renderer.setKeyCallbackHandler([&](GLFWwindow *, int key, int scancode, int action, int mod) { if( action == GLFW_PRESS ) { switch ( key) { case GLFW_KEY_R: /* Toggle rotation */ rotate_objects = !rotate_objects; break; default: break; } } }); renderer.setGamma( true, true ); renderer.setClearColor( scene->getFog()->getColor() ); renderer.render(scene, camera ); }
void type_node::activate( fact_handle & fh) { if ( instance_of( fh) ) _children( fh); }