InstanceObject* InstanceManager::AddInstanceObject(string modelName, vec3 position, vec3 rotation, float instanceScale, Chunk* pOwningChunk, int voxelX, int voxelY, int voxelZ) { InstanceParent* pNewInstanceParent = GetInstanceParent(modelName); if(pNewInstanceParent == NULL) { // Add new instance parent pNewInstanceParent = new InstanceParent(); pNewInstanceParent->m_modelName = modelName; pNewInstanceParent->m_pQubicleBinary = new QubicleBinary(m_pRenderer); pNewInstanceParent->m_pQubicleBinary->Import(modelName.c_str(), true); SetupGLBuffers(pNewInstanceParent); m_vpInstanceParentList.push_back(pNewInstanceParent); } InstanceObject* pInstanceObject = new InstanceObject(); pInstanceObject->m_erase = false; pInstanceObject->m_render = true; pInstanceObject->UpdateMatrix(position, rotation, instanceScale); pInstanceObject->m_pOwningChunk = pOwningChunk; pInstanceObject->m_voxelX = voxelX; pInstanceObject->m_voxelY = voxelY; pInstanceObject->m_voxelZ = voxelZ; pNewInstanceParent->m_vpInstanceObjectList.push_back(pInstanceObject); return pInstanceObject; }
InstanceObject *InstanceSprite::createInstance() { if(getFlagInstanceBase()) { if(getInstanceCount() < getInstanceCountMax()) { InstanceObject *pNewInstance = (InstanceObject *)clone(); pNewInstance->setFlagInstanceBase(false); pNewInstance->setInstanceIndex(getInstanceCount()); linkVertexBufferDynamic(pNewInstance); addInstance(pNewInstance); return pNewInstance; } } return NULL; }
InstanceObject* Picture::buildInstanceObject(char* fileName, BasicObject* obj) { InstanceObject* io = new InstanceObject(obj); FileIO* inputFile = new FileIO(fileName, 1); //for reading //scale transformation string line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double sx = atof(line.c_str()); line = inputFile->readLine(); double sy = atof(line.c_str()); line = inputFile->readLine(); double sz = atof(line.c_str()); Matrix* scale = AffineTransforms::scale(sx, sy, sz); //rotation transformations line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double rx = atof(line.c_str()); Matrix* rotX = AffineTransforms::rotateX(rx); line = inputFile->readLine(); double ry = atof(line.c_str()); Matrix* rotY = AffineTransforms::rotateY(ry); line = inputFile->readLine(); double rz = atof(line.c_str()); Matrix* rotZ = AffineTransforms::rotateZ(rz); //translation transformation line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double tx = atof(line.c_str()); line = inputFile->readLine(); double ty = atof(line.c_str()); line = inputFile->readLine(); double tz = atof(line.c_str()); Matrix* translate = AffineTransforms::translate(tx, ty, tz); //standard TRS form io->buildTransform(scale); //deletes the matrix when done io->buildTransform(rotX); io->buildTransform(rotY); io->buildTransform(rotZ); io->buildTransform(translate); delete inputFile; return io; }
void Picture::render(int w, int h ) { //while(true){ // Get BasicObjects BasicObject* obj = new BasicObject( OBJ_FILE ); Vertex* eye = new Vertex( 0,0,0 ); Vertex* at = new Vertex(0,0,0); Matrix* camera = buildCamera( CMRA_FILE, eye, at ); // Scene creation Scene* scene = new Scene(); // Set up lighting double shading[8]; getShadingInfo( SHADE_FILE, shading ); Light* light = new Light(); Vertex* light_loc = new Vertex( shading[0], shading[1], shading[2] ); Color* light_clr = new Color( LGHT_CLR ); light->setLocation( light_loc ); light->setColor( light_clr ); Color* amb = new Color( AMB_CLR ); // Add objects to the scene. InstanceObject* sp = buildInstanceObject( TRS_FILE, obj ); sp->setShininess( shading[7] ); scene->addNode( sp ); scene->render( w, h, light, eye, at, amb ); delete eye; delete at; delete amb; delete light; delete scene; //} }
/** * Reads a file to build an InstanceObject object. * Written by Dr. Boshart * Modified by Thomas Shaffer */ InstanceObject* Picture::buildInstanceObject(char* fileName, BasicObject* obj) { InstanceObject* io = new InstanceObject(obj); FileIO* inputFile = new FileIO(fileName, 1); //for reading char* f1 = (char*)malloc(255*sizeof(char)); char* f2 = (char*)malloc(255*sizeof(char)); //scale transformation string line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double sx = atof(line.c_str()); line = inputFile->readLine(); double sy = atof(line.c_str()); line = inputFile->readLine(); double sz = atof(line.c_str()); Matrix* scale = AffineTransforms::scale(sx, sy, sz); //rotation transformations line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double rx = atof(line.c_str()); Matrix* rotX = AffineTransforms::rotateX(rx); line = inputFile->readLine(); double ry = atof(line.c_str()); Matrix* rotY = AffineTransforms::rotateY(ry); line = inputFile->readLine(); double rz = atof(line.c_str()); Matrix* rotZ = AffineTransforms::rotateZ(rz); //translation transformation line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double tx = atof(line.c_str()); line = inputFile->readLine(); double ty = atof(line.c_str()); line = inputFile->readLine(); double tz = atof(line.c_str()); Matrix* translate = AffineTransforms::translate(tx, ty, tz); //standard TRS form io->buildTransform(scale); //deletes the matrix when done io->buildTransform(rotX); io->buildTransform(rotY); io->buildTransform(rotZ); io->buildTransform(translate); // Material line = inputFile->readLine(); line = inputFile->readLine(); double red = atof(line.c_str()); line = inputFile->readLine(); double green = atof(line.c_str()); line = inputFile->readLine(); double blue = atof(line.c_str()); Color* color = new Color( red, green, blue ); io->setColor( color ); // Texture string t_string = string("texture"); line = inputFile->readLine(); //if ( strcmp( line.c_str(), t_string.c_str() ) ) { line = inputFile->readLine(); const char* file = line.c_str(); strcpy( f1, file ); line = inputFile->readLine(); const char* file2 = line.c_str(); strcpy( f2, file2 ); line = inputFile->readLine(); int width = atoi( line.c_str() ); line = inputFile->readLine(); int height = atoi( line.c_str() ); Texture *tex = new Texture( f1, width, height ); Texture *norm = new Texture( f2, width, height ); io->setTexture( tex ); io->setNormal( norm ); //} delete inputFile; return io; }
void game_change_nearest_model(const ScriptArguments& args) { glm::vec3 position(args[0].real, args[1].real, args[2].real); float radius = args[3].real; int newid = 0, oldid = 0; /// @todo fix this being a problem. switch(args[4].type) { case TInt8: oldid = (std::int8_t)args[4].integer; break; case TInt16: oldid = (std::int16_t)args[4].integer; break; default: RW_ERROR("Unhandled integer type"); break; } switch(args[5].type) { case TInt8: newid = (std::int8_t)args[5].integer; break; case TInt16: newid = (std::int16_t)args[5].integer; break; default: RW_ERROR("Unhandled integer type"); break; } if( std::abs(newid) > 178 || std::abs(oldid) > 178 ) { /// @todo implement this path, return; } std::string newmodel; std::string oldmodel; if(newid < 0) newid = -newid; if(oldid < 0) oldid = -oldid; newmodel = args.getVM()->getFile()->getModels()[newid]; oldmodel = args.getVM()->getFile()->getModels()[oldid]; std::transform(newmodel.begin(), newmodel.end(), newmodel.begin(), ::tolower); std::transform(oldmodel.begin(), oldmodel.end(), oldmodel.begin(), ::tolower); auto newobjectid = args.getWorld()->data->findModelObject(newmodel); auto nobj = args.getWorld()->data->findObjectType<ObjectData>(newobjectid); /// @todo Objects need to adopt the new object ID, not just the model. for(auto p : args.getWorld()->instancePool.objects) { auto o = p.second; if( !o->model ) continue; if( o->model->name != oldmodel ) continue; float d = glm::distance(position, o->getPosition()); if( d < radius ) { args.getWorld()->data->loadDFF(newmodel + ".dff", false); InstanceObject* inst = static_cast<InstanceObject*>(o); inst->changeModel(nobj); inst->model = args.getWorld()->data->models[newmodel]; } } }
/** * Reads a file to build an InstanceObject object. * Written by Dr. Boshart * Modified by Thomas Shaffer */ InstanceObject* Picture::buildInstanceObject(char* fileName, BasicObject* obj) { InstanceObject* io = new InstanceObject(obj); FileIO* inputFile = new FileIO(fileName, 1); //for reading //scale transformation string line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double sx = atof(line.c_str()); line = inputFile->readLine(); double sy = atof(line.c_str()); line = inputFile->readLine(); double sz = atof(line.c_str()); Matrix* scale = AffineTransforms::scale(sx, sy, sz); //rotation transformations line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double rx = atof(line.c_str()); Matrix* rotX = AffineTransforms::rotateX(rx); line = inputFile->readLine(); double ry = atof(line.c_str()); Matrix* rotY = AffineTransforms::rotateY(ry); line = inputFile->readLine(); double rz = atof(line.c_str()); Matrix* rotZ = AffineTransforms::rotateZ(rz); //translation transformation line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double tx = atof(line.c_str()); line = inputFile->readLine(); double ty = atof(line.c_str()); line = inputFile->readLine(); double tz = atof(line.c_str()); Matrix* translate = AffineTransforms::translate(tx, ty, tz); //standard TRS form io->buildTransform(scale); //deletes the matrix when done io->buildTransform(rotX); io->buildTransform(rotY); io->buildTransform(rotZ); io->buildTransform(translate); // Material line = inputFile->readLine(); line = inputFile->readLine(); double red = atof(line.c_str()); line = inputFile->readLine(); double green = atof(line.c_str()); line = inputFile->readLine(); double blue = atof(line.c_str()); io->setColor( red, green, blue ); // Texture line = inputFile->readLine(); line = inputFile->readLine(); char* file = (char*)line.c_str(); line = inputFile->readLine(); int width = atoi( line.c_str() ); line = inputFile->readLine(); int height = atoi( line.c_str() ); Texture *tex = new Texture( file, width, height ); io->setTexture( tex ); delete inputFile; return io; }
void Picture::render(Pixel* pix) { //while(true){ // Get BasicObjects BasicObject* obj = readObject( OBJ_FILE ); double z[3]; getZValues( FOV_FILE, z ); //WNACI Matrix* wnd = AffineTransforms::windowingTransform(pix->getWidth(), pix->getHeight()); Matrix* aspct = AffineTransforms::aspectRatio(pix->getWidth(), pix->getHeight(), z[0]); // Matrix for aspect ratio. Vertex* eye = new Vertex( 0,0,0 ); Matrix* camera = buildCamera( CMRA_FILE, eye ); Matrix* normal = AffineTransforms::perspective( z[1], z[2] ); Matrix* tmp = aspct->multiply(camera); delete camera; delete aspct; Matrix* tmp2 = normal->multiply( tmp ); delete tmp; delete normal; Matrix* wnca = wnd->multiply(tmp2); delete tmp2; delete wnd; // END WNACI // Scene creation Scene* scene = new Scene(wnca); // Set up lighting double shading[8]; getShadingInfo( SHADE_FILE, shading ); Light* light = new Light(); Vertex* light_loc = new Vertex( shading[0], shading[1], shading[2] ); Color* light_clr = new Color( LGHT_CLR ); light->setLocation( light_loc ); light->setColor( light_clr ); Color* amb = new Color( AMB_CLR ); // Add objects to the scene. InstanceObject* sp = buildInstanceObject( TRS_FILE, obj ); sp->setShininess( shading[7] ); scene->addNode( sp ); scene->render( pix, light, eye, amb, shading[6] ); delete eye; delete amb; delete light; delete scene; //} }
InstanceObject* Picture::buildInstanceObject(char* fileName, BasicObject* obj) { InstanceObject* io = new InstanceObject(obj); FileIO* inputFile = new FileIO(fileName, 1); //for reading //scale transformation string line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double sx = atof(line.c_str()); line = inputFile->readLine(); double sy = atof(line.c_str()); line = inputFile->readLine(); double sz = atof(line.c_str()); Matrix* scale = AffineTransforms::scale(sx, sy, sz); //rotation transformations line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double rx = atof(line.c_str()); Matrix* rotX = AffineTransforms::rotateX(rx); line = inputFile->readLine(); double ry = atof(line.c_str()); Matrix* rotY = AffineTransforms::rotateY(ry); line = inputFile->readLine(); double rz = atof(line.c_str()); Matrix* rotZ = AffineTransforms::rotateZ(rz); //translation transformation line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double tx = atof(line.c_str()); line = inputFile->readLine(); double ty = atof(line.c_str()); line = inputFile->readLine(); double tz = atof(line.c_str()); Matrix* translate = AffineTransforms::translate(tx, ty, tz); //material line = inputFile->readLine(); //skip this line line = inputFile->readLine(); double mr = atof(line.c_str()); line = inputFile->readLine(); double mg = atof(line.c_str()); line = inputFile->readLine(); double mb = atof(line.c_str()); Color* mat = new Color(mr, mg, mb); io->setDiffuseMaterial(mat); //standard TRS form io->buildTransform(scale); //deletes the matrix when done io->buildTransform(rotX); io->buildTransform(rotY); io->buildTransform(rotZ); io->buildTransform(translate); //Get shininess FileIO* shaderFile = new FileIO("shade.txt", 1); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); line = shaderFile->readLine(); io->setShininess(atof(line.c_str())); delete shaderFile; delete inputFile; return io; }
void Picture::render(Pixel* px) { Color *ambient = new Color(1, 1, 1); Vertex *eye = new Vertex(0,0,0); Light *light = new Light(); double attenuation = 0; //while(true) //{ //char* fileName = "sphere.txt"; //BasicObject* sphere = readObject(fileName); ////delete[] fileName; //mingw appears to delete this automatically //fileName = "trs.txt"; //InstanceObject* sphereInstance = buildInstanceObject(fileName, sphere); ////delete[] fileName; //obtaining the window transform int widthPixels = px->getWidth(); //the dimensions of the panel on which the drawing will occur int heightPixels = px->getHeight(); getShaderInfo(eye, ambient, light, &attenuation); Scene* scene = new Scene(light, ambient); scene->buildTransform(getCameraTransform("camera.txt")); scene->buildTransform(getPerspectiveTransform("fov.txt", widthPixels, heightPixels)); scene->buildTransform(AffineTransforms::window(widthPixels, heightPixels)); BasicObject* cube = readObject("cube.txt"); BasicObject* cylinder = readObject("cylinder.txt"); InstanceObject* upperArm = new InstanceObject(cube); upperArm->setDiffuseMaterial(new Color(1,0,0)); upperArm->buildTransform(AffineTransforms::scale(.2, 1, .2)); InstanceObject* lowerArm = new InstanceObject(cube); lowerArm->setDiffuseMaterial(new Color(0,1,0)); lowerArm->buildTransform(AffineTransforms::scale(.2, 2, .2)); InstanceObject* base = new InstanceObject(cylinder); //no instance transform base->setDiffuseMaterial(new Color(0,0,1)); Matrix *temp; TransformNode *upperArmTransform, *lowerArmTransform, *baseTransform, *robotArm1, *robotArm2; //upper arm transform temp = AffineTransforms::translate(0, 1, 0); temp = AffineTransforms::rotateZ(90)->multiply(temp); temp = AffineTransforms::translate(0, -1, 0)->multiply(temp); temp = AffineTransforms::translate(0, 3, 0)->multiply(temp); upperArmTransform = new TransformNode(); upperArmTransform->buildTransform(temp); //lower arm transform temp = AffineTransforms::translate(0, 2, 0); temp = AffineTransforms::rotateZ(-20)->multiply(temp); temp = AffineTransforms::translate(0, -2, 0)->multiply(temp); temp = AffineTransforms::translate(0, 3, 0)->multiply(temp); lowerArmTransform = new TransformNode(); lowerArmTransform->buildTransform(temp); //base transform temp = AffineTransforms::rotateY(30); temp = AffineTransforms::translate(0, -2, 0)->multiply(temp); baseTransform = new TransformNode(); baseTransform->buildTransform(temp); //robot arm #1 temp = AffineTransforms::scale(.2, .2, .2); temp = AffineTransforms::translate(0, 0, -3)->multiply(temp); robotArm1 = new TransformNode(); robotArm1->buildTransform(temp); //robot arm #2 temp = AffineTransforms::scale(.2, .2, .2); temp = AffineTransforms::rotateX(45)->multiply(temp); temp = AffineTransforms::translate(-3, .5, 0)->multiply(temp); temp = AffineTransforms::scale(.2, .2, .2)->multiply(temp); robotArm2 = new TransformNode(); robotArm2->buildTransform(temp); //build scene upperArmTransform->addChild(upperArm); lowerArmTransform->addChild(upperArmTransform); lowerArmTransform->addChild(lowerArm); baseTransform->addChild(lowerArmTransform); baseTransform->addChild(base); robotArm1->addChild(baseTransform); robotArm2->addChild(baseTransform); scene->addTransformNode(robotArm1); scene->addTransformNode(robotArm2); scene->render(px, eye, attenuation); delete scene; /*TransformNode* tn = new TransformNode(); tn->addChild(sphereInstance); scene->addTransformNode(tn); scene->render(px, eye, attenuation); delete scene;*/ //} }
bool GameWorld::ContactProcessedCallback(btManifoldPoint &mp, void *body0, void *body1) { auto obA = static_cast<btCollisionObject*>(body0); auto obB = static_cast<btCollisionObject*>(body1); if( !( obA->getUserPointer() && obB->getUserPointer() ) ) { return false; } GameObject* a = static_cast<GameObject*>(obA->getUserPointer()); GameObject* b = static_cast<GameObject*>(obB->getUserPointer()); bool valA = a && a->type() == GameObject::Instance; bool valB = b && b->type() == GameObject::Instance; if( ! (valA && valB) && (valB || valA) ) { // Figure out which is the dynamic instance. InstanceObject* dynInst = nullptr; const btRigidBody* instBody = nullptr, * otherBody = nullptr; btVector3 src, dmg; if( valA ) { dynInst = static_cast<InstanceObject*>(a); instBody = static_cast<const btRigidBody*>(obA); otherBody = static_cast<const btRigidBody*>(obB); src = mp.getPositionWorldOnB(); dmg = mp.getPositionWorldOnA(); } else { dynInst = static_cast<InstanceObject*>(b); instBody = static_cast<const btRigidBody*>(obB); otherBody = static_cast<const btRigidBody*>(obA); src = mp.getPositionWorldOnA(); dmg = mp.getPositionWorldOnB(); } if( dynInst->dynamics != nullptr && instBody->isStaticObject() ) { // Attempt to determine relative velocity. auto dV = (otherBody->getLinearVelocity()); auto impulse = dV.length()/ (otherBody->getInvMass()); if( dynInst->dynamics->uprootForce <= impulse ) { dynInst->takeDamage({ {dmg.x(), dmg.y(), dmg.z()}, {src.x(), src.y(), src.z()}, 0.f, GameObject::DamageInfo::Physics, impulse }); } } } // Handle vehicles if(a) handleVehicleResponse(a, mp, true); if(b) handleVehicleResponse(b, mp, false); return true; }