void Editor::update(){ Vector2D pos = Input::Mouse::instance()->getMousePos(); //Translation if (Input::Mouse::instance()->isPressed(MB_MIDDLE)){ Vector3D dir = Vector3D(0,0,-1); float length = (pos.y-lastPos_.y)*TRANSLATION_SPEED; lastPos_ = pos; Matrix trans = Matrix(Matrix::Translation, dir*length); Graphic::instance()->getCam().multCamTrafo(trans); } //Rotation bool active = Input::Mouse::instance()->isPressed(MB_RIGHT); arcball_->update(active,false,pos); //Graphic::instance()->setCamRotation(arcball_->getTrafo()); Graphic::instance()->getCam().multCamTrafo(arcball_->getIncTrafo()); }
void Editor::_keypress(int key){ if (key == KEY_ESCAPE) EXIT(); if (key == KEY_C){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Model* copy = new Model(*mdl); Graphic::instance()->getScene().addModel(copy); } if (key == KEY_R) editMode_ = Rotation; if (key == KEY_T) editMode_ = Translation; //arcball_->update(false,true,Vector2D()); if (key == KEY_UP){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Matrix oldMat = mdl->getTrafo(); if (editMode_ == Translation){ Vector3D translation; if (editPlane_ == XZ) translation = Vector3D(0,0,-gridStep_); else if (editPlane_ == XY) translation = Vector3D(0,gridStep_,0); else if (editPlane_ == YZ) translation = Vector3D(0,gridStep_,0); Matrix newMat = Matrix(Matrix::Translation, translation); mdl->setTrafo(newMat*oldMat); } else if (editMode_ == Rotation){ Matrix newMat; if (editPlane_ == XZ) newMat = Matrix(Matrix::Rotation, Vector3D(0,1,0), rotationStep_); else if (editPlane_ == XY) newMat = Matrix(Matrix::Rotation, Vector3D(0,0,1), rotationStep_); else if (editPlane_ == YZ) newMat = Matrix(Matrix::Rotation, Vector3D(1,0,0), rotationStep_); mdl->setTrafo(oldMat*newMat); } } if (key == KEY_DOWN){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Matrix oldMat = mdl->getTrafo(); if (editMode_ == Translation){ Vector3D translation; if (editPlane_ == XZ) translation = Vector3D(0,0,gridStep_); else if (editPlane_ == XY) translation = Vector3D(0,-gridStep_,0); else if (editPlane_ == YZ) translation = Vector3D(0,-gridStep_,0); Matrix newMat = Matrix(Matrix::Translation, translation); mdl->setTrafo(newMat*oldMat); } else if (editMode_ == Rotation){ Matrix newMat; if (editPlane_ == XZ) newMat = Matrix(Matrix::Rotation, Vector3D(0,1,0), -rotationStep_); else if (editPlane_ == XY) newMat = Matrix(Matrix::Rotation, Vector3D(0,0,1), -rotationStep_); else if (editPlane_ == YZ) newMat = Matrix(Matrix::Rotation, Vector3D(1,0,0), -rotationStep_); mdl->setTrafo(oldMat*newMat); } } if (key == KEY_LEFT){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Matrix oldMat = mdl->getTrafo(); if (editMode_ == Translation){ Vector3D translation; if (editPlane_ == XZ) translation = Vector3D(-gridStep_,0,0); else if (editPlane_ == XY) translation = Vector3D(-gridStep_,0,0); else if (editPlane_ == YZ) translation = Vector3D(0,0,gridStep_); Matrix newMat = Matrix(Matrix::Translation, translation); mdl->setTrafo(newMat*oldMat); } else if (editMode_ == Rotation){ Matrix newMat; if (editPlane_ == XZ) newMat = Matrix(Matrix::Rotation, Vector3D(0,1,0), rotationStep_); else if (editPlane_ == XY) newMat = Matrix(Matrix::Rotation, Vector3D(0,0,1), rotationStep_); else if (editPlane_ == YZ) newMat = Matrix(Matrix::Rotation, Vector3D(1,0,0), rotationStep_); mdl->setTrafo(oldMat*newMat); } } if (key == KEY_RIGHT){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Matrix oldMat = mdl->getTrafo(); Matrix newMat; if (editMode_ == Translation){ Vector3D translation; if (editPlane_ == XZ) translation = Vector3D(gridStep_,0,0); else if (editPlane_ == XY) translation = Vector3D(gridStep_,0,0); else if (editPlane_ == YZ) translation = Vector3D(0,0,-gridStep_); Matrix newMat = Matrix(Matrix::Translation, translation); mdl->setTrafo(newMat*oldMat); } else if (editMode_ == Rotation){ Matrix newMat; if (editPlane_ == XZ) newMat = Matrix(Matrix::Rotation, Vector3D(0,1,0), -rotationStep_); else if (editPlane_ == XY) newMat = Matrix(Matrix::Rotation, Vector3D(0,0,1), -rotationStep_); else if (editPlane_ == YZ) newMat = Matrix(Matrix::Rotation, Vector3D(1,0,0), -rotationStep_); mdl->setTrafo(oldMat*newMat); } } if (key == KEY_DELETE){ Model* mdl = Graphic::instance()->getCurrModel(); if (!mdl) return; Graphic::instance()->getScene().deleteModel(mdl->getID()); } }
//! load the scene void Scene::load(const std::string& filename){ TR_USE(CGE_Scene); ifstream in(filename.c_str(), ios::binary); if (!in) return; std::string cwd = CGE::Filesystem::getCwd(); cwd += "\\"; char buffer[256]; //version in.read((char*)&version_, sizeof(version_)); //read and rebuild meshes unsigned size; in.read((char*)&size, sizeof(size)); for (unsigned i = 0; i < size; i++){ unsigned length; in.read((char*)&length, sizeof(length)); in.read(buffer, length*sizeof(char)); buffer[length] = '\0'; string name = string(buffer); #ifdef UNIX Utilities::replaceWith(name, '\\', '/'); #endif Mesh* msh = new Mesh(); if (!msh->loadFromFile(cwd+name)){ TR_ERROR("cannot load file %s", filename.c_str()); return; } buffer[length] = -52; msh->buildVBO(); meshes_.push_back(msh); } //read and rebuild textures in.read((char*)&size, sizeof(size)); for (unsigned i = 0; i < size; i++){ unsigned length; in.read((char*)&length, sizeof(length)); //CGE::Log << in.bad() << " " << in.eof(); in.read(buffer, length*sizeof(char)); buffer[length] = '\0'; string name = string(buffer); #ifdef UNIX Utilities::replaceWith(name, '\\', '/'); #endif Texture* tex = Texture::create(cwd+name); textures_.push_back(tex); } //read lights in.read((char*)&size, sizeof(size)); for (unsigned i = 0; i < size; ++i){ char type; in.read(&type, 1); Vec4f pos; in.read((char*)pos.data, 4 * sizeof(float)); Light* lt = new Light((Light::Type)type, pos); Vec3f dir; in.read((char*)dir.data, 3 * sizeof(float)); lt->setDirection(dir); float flt; in.read((char*)&flt, sizeof(float)); lt->setCutoff(flt); Color c; in.read((char*)c.array, 4 * sizeof(float)); lt->setColor(c); in.read((char*)&flt, sizeof(float)); lt->setAttenuation(flt); mLights.push_back(lt); } //read models in.read((char*)&size, sizeof(size)); for (unsigned i = 0; i < size; i++){ //model id unsigned id; in.read((char*)&id, sizeof(id)); CGE::SceneNode::setIDCount(max(id+1, CGE::SceneNode::getIDCount())); //mesh link unsigned idx; in.read((char*)&idx, sizeof(idx)); Mesh* msh = meshes_[idx]; Model* mdl = new Model(msh); mdl->setID(id); mNodes.push_back(mdl); //texture links for (int i = 0; i < MAX_TEXTURES; i++){ in.read((char*)&idx, sizeof(idx)); if (idx != UINT_MAX){ Texture* tex = textures_[idx]; mdl->assignTexture(tex,i); } } //trafo float mat[16]; in.read((char*)mat, 16*sizeof(float)); mdl->setTrafo(Matrix(mat)); //attributes for (int i = 0; i < MAX_ATTRIBS; i++){ int attrib; in.read((char*)&attrib, sizeof(attrib)); mdl->setAttrib(i, attrib); } } in.close(); }