void project1() { // Create scene Scene scn; scn.SetSkyColor(Color(0.8f, 0.9f, 1.0f)); // Create boxes LambertMaterial lambert1; lambert1.SetDiffuseColor(Color(0.3f,0.3f,0.3f)); MeshObject box1; box1.MakeBox(5.0f,0.1f,5.0f, &lambert1); scn.AddObject(box1); LambertMaterial lambert2; lambert2.SetDiffuseColor(Color(0.7f,0.7f,0.7f)); MeshObject box2; box2.MakeBox(1.0f,1.0f,1.0f, &lambert2); InstanceObject inst1(box2); Matrix34 mtx; mtx.MakeRotateX(0.5f); mtx.d.y=1.0f; inst1.SetMatrix(mtx); scn.AddObject(inst1); InstanceObject inst2(box2); mtx.MakeRotateY(1.0f); mtx.d.Set(-1.0f,0.0f,1.0f); inst2.SetMatrix(mtx); scn.AddObject(inst2); // Create lights DirectLight sunlgt; sunlgt.SetBaseColor(Color(1.0f, 1.0f, 0.9f)); sunlgt.SetIntensity(0.5f); sunlgt.SetDirection(Vector3(-0.5f, -1.0f, -0.5f)); scn.AddLight(sunlgt); PointLight redlgt; redlgt.SetBaseColor(Color(1.0f, 0.2f, 0.2f)); redlgt.SetIntensity(2.0f); redlgt.SetPosition(Vector3(2.0f, 2.0f, 0.0f)); scn.AddLight(redlgt); // Create camera Camera cam; cam.LookAt(Vector3(2.0f,2.0f,5.0f), Vector3(0.0f,0.0f,0.0f)); cam.SetResolution(800,600); cam.SetFOV(40.0f); cam.SetAspect(1.33f); // Render image cam.Render(scn); cam.SaveBitmap("project1.bmp"); }
void LoadLight(TiXmlElement *element) { Light *light = NULL; // name const char* name = element->Attribute("name"); printf("Light ["); if ( name ) printf("%s",name); printf("]"); // type const char* type = element->Attribute("type"); if ( type ) { if ( COMPARE(type,"ambient") ) { printf(" - Ambient\n"); AmbientLight *l = new AmbientLight(); light = l; for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "intensity" ) ) { Color c(1,1,1); ReadColor( child, c ); l->SetIntensity(c); printf(" intensity %f %f %f\n",c.r,c.g,c.b); } } } else if ( COMPARE(type,"direct") ) { printf(" - Direct\n"); DirectLight *l = new DirectLight(); light = l; for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "intensity" ) ) { Color c(1,1,1); ReadColor( child, c ); l->SetIntensity(c); printf(" intensity %f %f %f\n",c.r,c.g,c.b); } else if ( COMPARE( child->Value(), "direction" ) ) { Point3 v(1,1,1); ReadVector( child, v ); l->SetDirection(v); printf(" direction %f %f %f\n",v.x,v.y,v.z); } } } else if ( COMPARE(type,"point") ) { printf(" - Point\n"); PointLight *l = new PointLight(); light = l; for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) { if ( COMPARE( child->Value(), "intensity" ) ) { Color c(1,1,1); ReadColor( child, c ); l->SetIntensity(c); printf(" intensity %f %f %f\n",c.r,c.g,c.b); } else if ( COMPARE( child->Value(), "position" ) ) { Point3 v(0,0,0); ReadVector( child, v ); l->SetPosition(v); printf(" position %f %f %f\n",v.x,v.y,v.z); } } } else { printf(" - UNKNOWN\n"); } } if ( light ) { light->SetName(name); lights.push_back(light); } }
void DeferredRenderingApplication::OnStart() { mpMainCamera->GetGameObject()->GetTransform()->SetPosition(glm::vec3(0.0f, FLOOR_SIZE * 0.5f, (float)FLOOR_SIZE)); mpMainCamera->GetGameObject()->GetTransform()->RotateAround(-20.0f, glm::vec3(1.0f, 0.0f, 0.0f)); std::vector<PointLight*> pointLights; GameObject* pPointLightGameObject; PointLight* pPointLight; float depthIncrement = FLOOR_SIZE / (float)LIGHT_GRID_DEPTH; float horizontalIncrement = FLOOR_SIZE / (float)LIGHT_GRID_WIDTH; float intensity = 0.2f; for (float z = 1; z < FLOOR_SIZE; z += depthIncrement) { for (float x = 1; x < FLOOR_SIZE; x += horizontalIncrement) { pPointLightGameObject = GameObject::Instantiate(); pPointLightGameObject->GetTransform()->SetPosition(glm::vec3(-(FLOOR_SIZE * 0.5f - (2.0f * SPHERE_RADIUS)) + x, SPHERE_RADIUS * 2.5f, -(FLOOR_SIZE * 0.5f - (2.0f * SPHERE_RADIUS)) + z)); pPointLight = PointLight::Instantiate(pPointLightGameObject); glm::vec4 color = Colors::COMMON_LIGHT_COLORS[Random::Range(0, Colors::NUMBER_OF_COMMON_LIGHT_COLORS)]; pPointLight->SetDiffuseColor(color); pPointLight->SetSpecularColor(color); pPointLight->SetIntensity(intensity); pointLights.push_back(pPointLight); } } mpCheckersColorMapTexture = TextureImporter::Import("textures/CheckersColorMap.png"); mpFloorMaterial = new Material(ShaderRegistry::Find("Specular")); mpFloorMaterial->SetTexture("colorMap", mpCheckersColorMapTexture); mpFloorMaterial->SetVec4("diffuseColor", Colors::WHITE); mpFloorMaterial->SetVec4("specularColor", Colors::WHITE); mpFloorMaterial->SetFloat("shininess", 5.0f); mpFloorMesh = StandardGeometries::CreateXZPlane((float)FLOOR_SIZE, (float)FLOOR_SIZE, 1, 1, glm::vec3(0, 0, 0)); GameObject* pFloorGameObject = GameObject::Instantiate(); MeshRenderer* pMeshRenderer = MeshRenderer::Instantiate(pFloorGameObject); pMeshRenderer->AddMesh(mpFloorMesh); MeshFilter* pMeshFilter = MeshFilter::Instantiate(pFloorGameObject); pMeshFilter->SetMaterial(mpFloorMaterial); mpSphereMaterial = new Material(ShaderRegistry::Find("SolidColor")); mpSphereMaterial->SetVec4("diffuseColor", Colors::WHITE); mpSphereMaterial->SetVec4("specularColor", Colors::WHITE); mpSphereMaterial->SetFloat("shininess", 30.0f); mpSphereMesh = StandardGeometries::CreateSphere(SPHERE_RADIUS, NUMBER_OF_SPHERE_SLICES); std::vector<GameObject*> spheres; for (float z = 0; z < FLOOR_SIZE; z += 1.0f) { for (float x = 0; x < FLOOR_SIZE; x += 1.0f) { GameObject* pSphereGameObject = GameObject::Instantiate(); pMeshRenderer = MeshRenderer::Instantiate(pSphereGameObject); pMeshRenderer->AddMesh(mpSphereMesh); pMeshFilter = MeshFilter::Instantiate(pSphereGameObject); pMeshFilter->SetMaterial(mpSphereMaterial); pSphereGameObject->GetTransform()->SetPosition(glm::vec3(-(FLOOR_SIZE * 0.5f - (2.0f * SPHERE_RADIUS)) + x, SPHERE_RADIUS, -(FLOOR_SIZE * 0.5f - (2.0f * SPHERE_RADIUS)) + z)); spheres.push_back(pSphereGameObject); } } GameObject* pGeneralBehavioursGameObject = GameObject::Instantiate(); LightsAnimator* pLightsAnimator = LightsAnimator::Instantiate(pGeneralBehavioursGameObject); pLightsAnimator->SetLights(pointLights); KeyBindings* pKeyBindings = KeyBindings::Instantiate(pGeneralBehavioursGameObject); FirstPersonCameraController* pFirstPersonCameraController = FirstPersonCameraController::Instantiate(pGeneralBehavioursGameObject); pFirstPersonCameraController->SetWalkSpeed(WALK_SPEED); pFirstPersonCameraController->SetTurnSpeed(TURN_SPEED); pFirstPersonCameraController->SetFlying(true); }
void project2() { // Create scene Scene scn; scn.SetSkyColor(Color(0.8f, 0.8f, 1.0f)); // Create ground MeshObject ground; ground.MakeBox(5.0f,0.1f,5.0f); scn.AddObject(ground); // Create dragon MeshObject dragon; dragon.LoadPLY("dragon.ply"); dragon.Smooth(); std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); std::cout << "Constructing the tree ..." << std::endl; BoxTreeObject tree; tree.Construct(dragon); scn.AddObject(tree); end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end-start; std::cout << "Tree construcion done in " << elapsed_seconds.count() * 1000 << " milliseconds" << std::endl; // Create instance InstanceObject inst(tree); Matrix34 mtx; mtx.MakeRotateY(PI); mtx.d.Set(-0.05f,0.0f,-0.1f); inst.SetMatrix(mtx); scn.AddObject(inst); // Create lights DirectLight sunlgt; sunlgt.SetBaseColor(Color(1.0f, 1.0f, 0.9f)); sunlgt.SetIntensity(1.0f); sunlgt.SetDirection(Vector3(2.0f, -3.0f, -2.0f)); scn.AddLight(sunlgt); PointLight redlgt; redlgt.SetBaseColor(Color(1.0f, 0.2f, 0.2f)); redlgt.SetIntensity(0.02f); redlgt.SetPosition(Vector3(-0.2f, 0.2f, 0.2f)); scn.AddLight(redlgt); PointLight bluelgt; bluelgt.SetBaseColor(Color(0.2f, 0.2f, 1.0f)); bluelgt.SetIntensity(0.02f); bluelgt.SetPosition(Vector3(0.1f, 0.1f, 0.3f)); scn.AddLight(bluelgt); // Create camera Camera cam; cam.LookAt(Vector3(-0.1f,0.1f,0.2f),Vector3(-0.05f,0.12f,0.0f)); cam.SetFOV(40.0f); cam.SetAspect(1.33f); cam.SetResolution(800,600); start = std::chrono::system_clock::now(); std::cout << "Rendering the scene ..." << std::endl; // Render image cam.Render(scn); cam.SaveBitmap("project2.bmp"); end = std::chrono::system_clock::now(); elapsed_seconds = end-start; std::cout << "Scene rendering done in " << elapsed_seconds.count() * 1000 << " milliseconds" << std::endl; }