void GLWidget::mouseMoveEvent(QMouseEvent *event) { last.x = event->x(); last.y = event->y(); vec3 begin = pointOnVirtualTrackball(first); vec3 end = pointOnVirtualTrackball(last); float dotProduct = dot(normalize(begin), normalize(end)); float angle = acos(dotProduct); vec3 crossP = cross(begin, end); if(length(crossP) > .00001f) { rotationMatrix = rotate(mat4(1.0), angle, normalize(crossP)) * rotationMatrix; glUseProgram(cubeProg); glUniformMatrix4fv(cubeRotationMatrixLoc, 1, false, value_ptr(rotationMatrix)); glUseProgram(gridProg); glUniformMatrix4fv(gridRotationMatrixLoc, 1, false, value_ptr(rotationMatrix)); update(); } first.x = last.x; first.y = last.y; }
bool SceneGraph::InitializeGraph() { // Create objects // Add the objects into the actor map // Once an object is in the SceneGraph, object destruction is responsibility of the SceneGraph using AntiMatter::AppLog; using std::string; using std::vector; using glm::mat4; using glm::vec3; using glm::translate; using glm::rotate; // just calling to test the WavefrontObj parser // WavefrontObj j( g_Cfg.AssetsDir() + string("obj\\house\\house.obj") ); // Initialize the SceneLights collection (lights need access to the SceneGraph in order to initialize) // because of this, light initialization can't be done through plain old RAII if( ! m_lights.Initialize(this) ) { AppLog::Ref().LogMsg("SceneGraph lights failed to initialize"); return false; } // add lights to the scene graph typedef vector<Light*>::const_iterator CItr; const vector<Light*> lights = m_lights.Lights(); for( CItr n = lights.begin(); n != lights.end(); n ++ ) { Light* pLight = (*n); m_map.insert( ActorIdPair( pLight->Id(), pLight ) ); } mat4 tr; mat4 mW(1.0); // Terrain Terrain* pTerrain = new (std::nothrow) Terrain( this, &m_RootNode, string("terrain"), g_Cfg.AssetsDir() + string("heightfield4.bmp"), // height map g_Cfg.AssetsDir() + string("grassC.jpg"), // texture map (1 of n ?) g_Cfg.AssetsDir() + string("256-circle-alphamap.bmp"), // alphamap 128,128, 4.0f, 4.0f, 0.1f ); if( ! pTerrain || ! pTerrain->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to initialize Terrain"); return false; } m_map.insert( ActorIdPair( pTerrain->Id(), pTerrain) ); tr = translate( mat4(1.0), vec3(0, -275, 0) ); pTerrain->GetNodeData().W() *= tr; // Globe Globe* pGlobe = new (std::nothrow) Globe( this, &m_RootNode, std::string("globe"), 350.0f, 30, 30, string(""), glm::vec4(1.0, 1.0, 1.0, 0.8) ); // position the globe if( ! pGlobe || ! pGlobe->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to intialize the Globe"); return false; } tr = translate( glm::mat4(1.0), vec3(0, 0, 0) ); pGlobe->GetNodeData().W() *= tr; m_map.insert( ActorIdPair( pGlobe->Id(), pGlobe) ); // snow Snow* pSnow = new (std::nothrow) Snow( this, &m_RootNode, pGlobe, string("snowfall"), g_Cfg.SnowCfg() ); if( ! pSnow || ! pSnow->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to allocate heap for snow"); return false; } tr = translate( mat4(1.0), vec3(0, 380, 0) ); pSnow->GetNodeData().W() *= tr; m_map.insert( ActorIdPair( pSnow->Id(), pSnow) ); /* // Snowfall Snowfall* pSnowfall = new (std::nothrow) Snowfall( this, &m_RootNode, string("snowfall"), g_Cfg.SnowfallCfg() ); if( ! pSnowfall || ! pSnowfall->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to allocate heap for snowfall"); return false; } tr = translate( mat4(1.0), vec3(0, 350, 0) ); pSnowfall->GetNodeData().W() *= tr; m_map.insert( ActorIdPair( pSnowfall->Id(), pSnowfall) ); */ // lake Lake* pLake = new (std::nothrow) Lake( this, pTerrain, string("lake"), 180, 390, g_Cfg.AssetsDir() + string("water.jpg"), g_Cfg.AssetsDir() + string("water-alphamap.bmp"), string("") ); if( ! pLake || ! pLake->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to initialized the Lake"); return false; } tr = translate( mat4(1.0), vec3(0,344,168) ); mW = rotate( mW, -90.0f, vec3(0, 1, 0) ); pLake->GetNodeData().W() *= (tr * mW); m_map.insert( ActorIdPair( pLake->Id(), pLake) ); // house House* pHouse = new (std::nothrow) House( this, pTerrain, string("house"), g_Cfg.HouseCfg() ); if( ! pHouse || ! pHouse->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed initialize the house"); return false; } tr = translate( mat4(1.0), vec3(20, 412, -125) ); pHouse->GetNodeData().W() *= tr; m_map.insert( ActorIdPair( pHouse->Id(), pHouse) ); // Tree Tree* pTree = new (std::nothrow) Tree( this, pTerrain, string("Tree"), g_Cfg.AssetsDir() + string("bark_1.jpg"), g_Cfg.AssetsDir() + string("bark_1_bump.jpg") ); if( ! pTree || ! pTree->Initialized() ) { AppLog::Ref().LogMsg("SceneGraph failed to allocate heap for Tree"); return false; } tr = translate( mat4(1.0), vec3(0, 313, 45) ); pTree->GetNodeData().W() *= tr; m_map.insert( ActorIdPair( pTree->Id(), pTree) ); return true; }
mat4 const& Model::getModelMatrix() const { if (!validModelMatrix) { mat4 scaleMatrix = glm::scale(mat4(), scale); mat4 rotateX = rotate(mat4(), rotation.x, vec3(1, 0, 0)); mat4 rotateY = rotate(mat4(), rotation.y, vec3(0, 1, 0)); mat4 rotateZ = rotate(mat4(), rotation.z, vec3(0, 0, 1)); mat4 translateMatrix = translate(mat4(), position); modelMatrix = translateMatrix * rotateZ * rotateY * rotateX * scaleMatrix; validModelMatrix = true; validInverseModelMatrix = false; } return modelMatrix; }
/// <summary> /// Adds a mirror box of a given size, centered at 0, with no back. /// </summary> /// <param name="wallSize">Size of the walls.</param> void Scene::addMirrorBox(const float wallSize) { using glm::translate; using glm::scale; using glm::rotate; int matIdx = materialsVec.size(); materialsVec.push_back(Material(vec3(1.0f, 1.0f, 0.8f), 0.7f)); //white (+0) materialsVec.push_back(Material(vec3(1.0f, 0.0f, 0.0f), 0.7f)); //red (+1) materialsVec.push_back(Material(vec3(0.0f, 1.0f, 0.0f), 0.7f)); //green (+2) materialsVec.push_back(Material(vec3(1.0f, 1.0f, 1.0f))); //white light (+3) materialsVec.push_back(Material(vec3(0.0f, 0.0f, 0.0f), 0.0f, vec3(1, 1, 1), INFINITY, .9f, 5.8f)); //mirror (+4) materialsVec[matIdx + 4].flags |= MAT_FLAG_PURE_REFLECTION; materialsVec.push_back(Material(vec3(1.0f, 0.6f, 1.0f))); //violet light const float offset = wallSize / 2; const mat4 scaleToWall = scale(vec3(wallSize, wallSize, wallSize)); //floor mat4 trans = translate(vec3(0, -offset, -offset)) * rotate(-(glm::mediump_float)90, vec3(1, 0, 0)) * scaleToWall; addRectangularModel(trans, matIdx); //ceiling trans = translate(vec3(0, offset, -offset)) * rotate((glm::mediump_float)90, vec3(1, 0, 0)) * scaleToWall; addRectangularModel(trans, matIdx + 4); //left wall trans = translate(vec3(-offset + .2 * offset, 0, -offset)) * rotate((glm::mediump_float)88, vec3(0, 1, 0)) * scaleToWall; addRectangularModel(trans, matIdx + 4); //right wall trans = translate(vec3(offset, 0, -offset)) * rotate((glm::mediump_float) - 90, vec3(0, 1, 0)) * scaleToWall; addRectangularModel(trans, matIdx + 4); //back wall trans = translate(vec3(0, 0, -wallSize)) * // rotate((glm::mediump_float)90, vec3(1, 0, 0)) * scaleToWall; addRectangularModel(trans, matIdx); //front wall s trans = translate(vec3(0, 0, 0)) * rotate((glm::mediump_float)180, vec3(0, 1, 0)) * scaleToWall; addRectangularModel(trans, matIdx); //light float power = 400; trans = translate(vec3(0, offset - 0.01f, -offset)) * rotate((glm::mediump_float) 90, vec3(1, 0, 0)) * scale(vec3(2.5f, 2.5f, 2.5f)); addAreaLight(trans, matIdx + 3, vec3(power / 4, power, power)); trans = translate(vec3(0, -offset + 0.01f, -offset)) * rotate((glm::mediump_float) -90, vec3(1, 0, 0)) * scale(vec3(1.5f, 1.5f, 1.5f)); addAreaLight(trans, matIdx + 5, vec3(power / 3, 0, power / 3)); }