HelloPolycodeApp::HelloPolycodeApp(PolycodeView *view) { core = new POLYCODE_CORE(view, 640,480,false,true,0,0,90, 0, true); PhysicsScene2D *scene = new PhysicsScene2D(0.1, 50); ScenePrimitive *ceiling = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 2.0, 0.1); ceiling->setColor(0.0, 0.0, 0.0, 1.0); ceiling->setPosition(0, 0.5); scene->addPhysicsChild(ceiling, PhysicsScene2DEntity::ENTITY_RECT, true); // Revolute Joint ScenePrimitive *shape = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.03, 0.2); shape->setAnchorPoint(0.0, 1.0, 0.0); shape->setPosition(-0.3, 0.48); scene->addPhysicsChild(shape, PhysicsScene2DEntity::ENTITY_RECT, false); scene->createRevoluteJoint(shape, ceiling, 0.0, 0.01); scene->applyImpulse(shape, 10, 0); // Distance Joint shape = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.1, 0.02); shape->setPosition(0.0, 0.2); scene->addPhysicsChild(shape, PhysicsScene2DEntity::ENTITY_RECT, false); scene->createDistanceJoint(shape, ceiling, false); scene->applyImpulse(shape, 2, 0); SceneLine *line = new SceneLine(shape, ceiling); line->setColor(1.0, 0.0, 0.0, 1.0); scene->addChild(line); // Prismatic Joint shape = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.05, 0.1); shape->setPosition(0.3, 0.3); scene->addPhysicsChild(shape, PhysicsScene2DEntity::ENTITY_RECT, false); scene->createPrismaticJoint(ceiling, shape, Vector2(0,1), 0,0, false, -0.3, 0, true); SceneEntity *lineAnchor = new SceneEntity(); lineAnchor->setPosition(0.3,0.5); line = new SceneLine(shape, lineAnchor); line->setColor(0.0, 1.0, 0.0, 1.0); scene->addChild(line); }
void Scene::loadScene(const String& fileName) { OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb"); if(!inFile) { Logger::log("Error opening scene file\n"); return; } Number r,g,b,a; OSBasics::read(&r, sizeof(Number), 1, inFile); OSBasics::read(&g, sizeof(Number), 1, inFile); OSBasics::read(&b, sizeof(Number), 1, inFile); clearColor.setColor(r,g,b,1.0f); OSBasics::read(&r, sizeof(Number), 1, inFile); OSBasics::read(&g, sizeof(Number), 1, inFile); OSBasics::read(&b, sizeof(Number), 1, inFile); ambientColor.setColor(r,g,b,1.0f); unsigned int numObjects, objectType,namelen; char buffer[1024]; char flag; Number t[3],rq[4]; SceneEntity *newEntity; unsigned int lightmapIndex = 0; OSBasics::read(&flag, 1, 1, inFile); hasLightmaps = false; if(flag == 1) hasLightmaps = true; OSBasics::read(&numObjects, sizeof(unsigned int), 1, inFile); Logger::log("Loading scene (%d objects)\n", numObjects); for(int i=0; i < numObjects; i++) { OSBasics::read(t, sizeof(Number), 3, inFile); OSBasics::read(rq, sizeof(Number), 4, inFile); newEntity = NULL; OSBasics::read(&objectType, sizeof(unsigned int), 1, inFile); SceneMesh *newSceneMesh; Mesh *newMesh; Material *newMaterial; switch(objectType) { case ENTITY_MESH: if(hasLightmaps) { OSBasics::read(&lightmapIndex, sizeof(unsigned int), 1, inFile); } else { lightmapIndex = 0; } OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile); memset(buffer, 0, 1024); OSBasics::read(buffer, 1, namelen, inFile); Logger::log("adding mesh (texture: %s)\n", buffer); OSBasics::read(&r, sizeof(Number), 1, inFile); OSBasics::read(&g, sizeof(Number), 1, inFile); OSBasics::read(&b, sizeof(Number), 1, inFile); OSBasics::read(&a, sizeof(Number), 1, inFile); newMaterial = (Material*) CoreServices::getInstance()->getResourceManager()->getResource(Resource::RESOURCE_MATERIAL, buffer); newMesh = new Mesh(Mesh::TRI_MESH); newMesh->loadFromFile(inFile); newSceneMesh = new SceneMesh(newMesh); newSceneMesh->setColor(r,g,b,a); newSceneMesh->lightmapIndex = lightmapIndex; addEntity(newSceneMesh); newEntity = (SceneEntity*)newSceneMesh; staticGeometry.push_back(newSceneMesh); if(newMaterial != NULL) newSceneMesh->setMaterial(newMaterial); break; case ENTITY_ENTITY: { Logger::log("loading entity\n"); String entityType = readString(inFile); SceneEntity *newCustomEntity = new SceneEntity(); newCustomEntity->custEntityType = entityType; newEntity = newCustomEntity; customEntities.push_back(newCustomEntity); } break; case ENTITY_COLLMESH: { /* unsigned int collType,numVertices,numFaces; Number co[3]; OSBasics::read(&collType, sizeof(unsigned int), 1, inFile); OSBasics::read(&numVertices, sizeof(unsigned int), 1, inFile); Mesh *mesh = new Mesh(Mesh::TRI_MESH); for(int i=0; i < numVertices; i++) { OSBasics::read(co, sizeof(Number), 3, inFile); Vertex *newVert = new Vertex(co[0], co[1], co[2]); mesh->addVertex(newVert); } OSBasics::read(&numFaces, sizeof(unsigned int), 1, inFile); unsigned int fo[3]; for(int i=0; i < numFaces; i++) { OSBasics::read(fo, sizeof(unsigned int), 3, inFile); Polygon *newPoly = new Polygon(); newPoly->addVertex(mesh->getVertex(fo[0])); newPoly->addVertex(mesh->getVertex(fo[1])); newPoly->addVertex(mesh->getVertex(fo[2])); mesh->addPolygon(newPoly); } SceneMesh *newMesh = new SceneMesh(mesh); newEntity = newMesh; loadCollisionChild(newEntity); collisionGeometry.push_back(newMesh); newMesh->visible = false; // ScenePrimitive *test = new ScenePrimitive(ScenePrimitive::TYPE_BOX, newMesh->bBox.x,newMesh->bBox.y,newMesh->bBox.z); // newMesh->addEntity(test); */ } break; case ENTITY_CAMERA: newEntity = (SceneEntity*)this->getDefaultCamera(); break; case ENTITY_LIGHT: /* Number col[3],e,d; unsigned int lType; OSBasics::read(&lType, sizeof(unsigned int), 1, inFile); OSBasics::read(&e, sizeof(Number), 1, inFile); OSBasics::read(&d, sizeof(Number), 1, inFile); OSBasics::read(col, sizeof(Number), 3, inFile); SceneLight *newLight = new SceneLight(lType, e, d, this); newLight->lightColor.setColor(col[0],col[1],col[2],1.0f); addLight(newLight); newEntity = (SceneEntity*)newLight; */ break; } if(newEntity != NULL) { unsigned int numProps; OSBasics::read(&numProps, sizeof(unsigned int), 1, inFile); for(int i=0; i < numProps; i++) { String propName,propValue; propName = readString(inFile); propValue = readString(inFile); EntityProp prop; prop.propName = propName; prop.propValue = propValue; newEntity->entityProps.push_back(prop); } if(objectType == ENTITY_MESH) { if(newEntity->getEntityProp("vertexnormals") == "false") ((SceneMesh*)newEntity)->getMesh()->useVertexNormals(false); if(newEntity->getEntityProp("collision") != "") { if(newEntity->getEntityProp("collision") == "mesh") { loadCollisionChild(newEntity, false, 3); } } } newEntity->setPosition(t[0], t[1], t[2]); newEntity->setRotationQuat(rq[0], rq[1], rq[2], rq[3]); newEntity->rebuildTransformMatrix(); } } if(!hasLightmaps) { OSBasics::close(inFile); return; } /* unsigned int lmsize,numLightmaps,imageWidth, imageHeight; OSBasics::read(&numLightmaps, sizeof(unsigned int), 1, inFile); packer = new LightmapPacker(this); char *imageData; for(int i=0 ; i < numLightmaps; i++) { OSBasics::read(&imageWidth, sizeof(unsigned int), 1, inFile); OSBasics::read(&imageHeight, sizeof(unsigned int), 1, inFile); OSBasics::read(&lmsize, sizeof(unsigned int), 1, inFile); imageData = (char*)malloc(lmsize); OSBasics::read(imageData, 1, lmsize, inFile); Image *newImage = new Image(imageData, imageWidth, imageHeight); packer->images.push_back(newImage); free(imageData); } packer->bindTextures(); */ OSBasics::close(inFile); }