void Light::addDirectionalLight(D3DXVECTOR3 position, D3DXVECTOR3 direction, float intensity, D3DXVECTOR3 color, float screenNear, float screenDepth) { //GENERATE VIEW MATRIX // D3DXVECTOR3 up; D3DXMATRIX viewMatrix; up.x = 0.0f; up.y = 1.0f; up.z = 0.0f; D3DXMatrixLookAtLH(&viewMatrix, &position, &direction, &up); //GENERATE PROJECTION MATRIX // D3DXMATRIX projectionMatrix; float fieldOfView, screenAspect; // Setup field of view and screen aspect for a square light source. fieldOfView = (float)D3DX_PI / 2.0f; screenAspect = 1.0f; D3DXMatrixPerspectiveFovLH(&projectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth); D3DXVec3Normalize(&direction, &direction); // ADD LIGHT TO ARRAY // directionalLights[m_addedDirectionalLights] = DirectionalLight(position, direction, intensity, color, viewMatrix, projectionMatrix); m_addedDirectionalLights++; }
void Game::Init() { //Vertex data[3] = {Vertex(Vector3f(-1,-1,0)), // Vertex(Vector3f(0,1,0)), // Vertex(Vector3f(1,-1,0))}; //mesh.addVertices(data, ARRAY_SIZE(data)); //Vertex data[] = {Vertex(Vector3f(-1.0f, -1.0f, 0.5773f), Vector2f(0.0f, 0.0f)), // Vertex(Vector3f(0.0f, -1.0f, -1.15475f), Vector2f(0.5f, 0.0f)), // Vertex(Vector3f(1.0f, -1.0f, 0.5773f), Vector2f(1.0f, 0.0f)), // Vertex(Vector3f(0.0f, 1.0f, 0.0f), Vector2f(0.5f, 1.0f))}; //int indices[] = { 0, 3, 1, // 1, 3, 2, // 2, 3, 0, // 1, 2, 0}; float fieldDepth = 10.0f; float fieldWidth = 10.0f; Vertex data[] = { Vertex( Vector3f(-fieldWidth, 0.0f, -fieldDepth), Vector2f(0.0f, 0.0f)), Vertex(Vector3f(-fieldWidth, 0.0f, fieldDepth * 3), Vector2f(0.0f, 1.0f)), Vertex(Vector3f(fieldWidth * 3, 0.0f, -fieldDepth), Vector2f(1.0f, 0.0f)), Vertex(Vector3f(fieldWidth * 3, 0.0f, fieldDepth * 3),Vector2f(1.0f, 1.0f))}; int indices[] = { 0, 1, 2, 2, 1, 3}; m_mesh.AddVertices(data, ARRAY_SIZE_IN_ELEMENTS(data), indices, ARRAY_SIZE_IN_ELEMENTS(indices)); m_color.Set(1,1,1); //m_texture = Texture("test.png"); m_texture = new Texture("test.png"); m_material = Material(m_texture, m_color, 1, 8); m_shader = PhongShader::GetInstance(); //PhongShader::SetAmbientLight(Vector3f(0.1f,0.1f,0.1f)); PhongShader::SetDirectionalLight(DirectionalLight(BaseLight(Vector3f(1,1,1),0.1f),Vector3f(1,1,1))); m_pLights = new PointLight[2]; m_pLights[0] = PointLight(BaseLight(Vector3f(1,0.5f,0), 0.8f), Attenuation(0,0,1), Vector3f(-2,0,5), 10); m_pLights[1] = PointLight(BaseLight(Vector3f(0,0.5f,1), 0.8f), Attenuation(0,0,1), Vector3f(2,0,7), 10); m_sLights = new SpotLight[1]; m_sLights[0] = SpotLight(PointLight(BaseLight(Vector3f(0,1,1), 0.8f), Attenuation(0,0,0.1f), Vector3f(-2,0,5), 30),Vector3f(1,1,1).Normalized(),0.8f); PhongShader::SetPointLights(m_pLights, 2); PhongShader::SetSpotLights(m_sLights, 1); m_camera = Camera(); Transform::SetProjection(70.0f, (float)Window::GetWidth(), (float)Window::GetHeight(), 0.1f, 1000.0f); Transform::SetCamera(m_camera); }
DirectionalLight loadDirectionalLight( const Scene& scene, const tinyxml2::XMLElement& elt) { auto pExitantPower = elt.FirstChildElement("ExitantPower"); if(pExitantPower) { Vec3f wi(0, 1, 0); getChildAttribute(elt, "IncidentDirection", wi); Vec3f exitantPower = zero<Vec3f>(); getChildAttribute(elt, "ExitantPower", exitantPower); return DirectionalLight(wi, exitantPower, scene); } return { normalize(loadVector(elt)), loadColor(elt) }; }
void TestGame::init() { m_root.setEngine(m_engine); IndexedModel model = IndexedModel(); std::vector<Vertex> vertices; std::vector<unsigned int> indices; // vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f(-1, 1, -1), Vector2f(0, 0))); // vertices.push_back(Vertex(Vector3f( 1, 1, -1), Vector2f(0, 1))); // vertices.push_back(Vertex(Vector3f( 1, -1, -1), Vector2f(1, 1))); // // vertices.push_back(Vertex(Vector3f(-1, -1, 1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f(-1, 1, 1), Vector2f(0, 0))); // vertices.push_back(Vertex(Vector3f( 1, 1, 1), Vector2f(0, 1))); // vertices.push_back(Vertex(Vector3f( 1, -1, 1), Vector2f(1, 1))); // // vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(0, 1))); // vertices.push_back(Vertex(Vector3f(-1, -1, 1), Vector2f(1, 1))); // vertices.push_back(Vertex(Vector3f( 1, -1, 1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f( 1, -1, -1), Vector2f(0, 0))); // // vertices.push_back(Vertex(Vector3f(-1, 1, -1), Vector2f(0, 1))); // vertices.push_back(Vertex(Vector3f(-1, 1, 1), Vector2f(1, 1))); // vertices.push_back(Vertex(Vector3f( 1, 1, 1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f( 1, 1, -1), Vector2f(0, 0))); // // vertices.push_back(Vertex(Vector3f(-1, -1, -1), Vector2f(1, 1))); // vertices.push_back(Vertex(Vector3f(-1, -1, 1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f(-1, 1, 1), Vector2f(0, 0))); // vertices.push_back(Vertex(Vector3f(-1, 1, -1), Vector2f(0, 1))); // // vertices.push_back(Vertex(Vector3f(1, -1, -1), Vector2f(1, 1))); // vertices.push_back(Vertex(Vector3f(1, -1, 1), Vector2f(1, 0))); // vertices.push_back(Vertex(Vector3f(1, 1, 1), Vector2f(0, 0))); // vertices.push_back(Vertex(Vector3f(1, 1, -1), Vector2f(0, 1))); // // indices.push_back(0); // indices.push_back(1); // indices.push_back(2); // indices.push_back(0); // indices.push_back(2); // indices.push_back(3); // // indices.push_back(6); // indices.push_back(5); // indices.push_back(4); // indices.push_back(7); // indices.push_back(6); // indices.push_back(4); // // indices.push_back(10); // indices.push_back(9); // indices.push_back(8); // indices.push_back(11); // indices.push_back(10); // indices.push_back(8); // // indices.push_back(12); // indices.push_back(13); // indices.push_back(14); // indices.push_back(12); // indices.push_back(14); // indices.push_back(15); // // indices.push_back(16); // indices.push_back(17); // indices.push_back(18); // indices.push_back(16); // indices.push_back(18); // indices.push_back(19); // // indices.push_back(22); // indices.push_back(21); // indices.push_back(20); // indices.push_back(23); // indices.push_back(22); // indices.push_back(20); vertices.push_back(Vertex(Vector3f(-1.0f, -1.0f, 0.5773f), Vector2f(0.0f, 0.0f))); vertices.push_back(Vertex(Vector3f(0.0f, -1.0f, -1.15475f), Vector2f(0.5f, 0.0f))); vertices.push_back(Vertex(Vector3f(1.0f, -1.0f, 0.5773f), Vector2f(1.0f, 0.0f))); vertices.push_back(Vertex(Vector3f(0.0f, 1.0f, 0.0f), Vector2f(0.5f, 1.0f))); indices.push_back(0); indices.push_back(3); indices.push_back(1); indices.push_back(1); indices.push_back(3); indices.push_back(2); indices.push_back(2); indices.push_back(3); indices.push_back(0); indices.push_back(1); indices.push_back(2); indices.push_back(0); model.addVertices(vertices, indices, true); Material material = Material(m_texture, Vector3f(1.0f, 1.0f, 1.0f), 2.0f, 32.0f); Mesh mesh0 = Mesh(model, material); // IndexedModel model1 = IndexedModel(); // model1.addVertex(Vertex(Vector3f(-5, -3, -5), Vector2f(0, 1))); // model1.addVertex(Vertex(Vector3f(-5, -3, 5), Vector2f(1, 1))); // model1.addVertex(Vertex(Vector3f( 5, -3, 5), Vector2f(1, 0))); // model1.addVertex(Vertex(Vector3f( 5, -3, -5), Vector2f(0, 0))); // model1.addFace(Vector3i(2, 1, 0)); // model1.addFace(Vector3i(3, 2, 0)); // Mesh mesh1 = Mesh(model1); // m_camera = new Camera(Vector3f(0.0f, 0.0f, -3.0f), Vector3f(0.0f, 0.0f, 1.0f), Vector3f(0.0f, 1.0f, 0.0f), 70.0f, m_engine->getWindow()->getAspectRatio(), 0.1f, 1000.0f); m_object0 = (new Entity())->addComponent(new MeshRenderer(mesh0)); add(m_object0); m_object0->getTransform().setRotation(Quaternion(Vector3f(1.0f, 0.0f, 1.0f).normalized(), MATH_PI)); // m_object1 = (new Entity())->addComponent(new MeshRenderer(mesh1)); // add(m_object1); add((new Entity())->addComponent(new CameraComponent(70.0f, m_engine->getWindow()->getAspectRatio(), 0.1f, 1000.0f)) ->addComponent(new FreeMove(0.2f)) ->addComponent(new FreeLook(m_engine->getWindow()->getCenter(), 0.2f))); m_engine->getRenderingEngine()->setAmbientLight(Vector3f(0.1f, 0.1f, 0.1f)); m_engine->getRenderingEngine()->setDirectionalLight(DirectionalLight(Light(Vector3f(1.0f, 1.0f, 1.0f), 0.8f), Vector3f(0.0f, 1.0f, 0.0f).normalized())); }
void renderingTh(Tiin* t) { t->display->makeActive(true); glm::vec3 dir = glm::vec3 (-0.7,-0.8,0.5); PhongShader skyShader; skyShader.setAmbient( glm::vec3 (0.1, 0.1, 0.1)); skyShader.setDirLight( DirectionalLight (BaseLight( glm::vec3 (1.f,1.f,1.f), 2.0f), dir )); PhongTerrainShader shader; shader.setAmbient( glm::vec3 (0.1, 0.1, 0.1)); shader.setDirLight( DirectionalLight( BaseLight( glm::vec3 (1.f,1.f,1.f), 1.5f), dir )); Texture* tex[3]; tex[0] = new Texture(std::string(RESOURCE_FOLDER) + std::string("images/grass5.jpg"), GL_LINEAR); tex[1] = new Texture(std::string(RESOURCE_FOLDER) + std::string("images/rock3.jpg"), GL_LINEAR_MIPMAP_LINEAR); tex[2] = new Texture (std::string(RESOURCE_FOLDER) + std::string("images/rock4.jpg"), GL_LINEAR); Material terrainMaterial( glm::vec3 (1,1,1), 4.f); for ( int i = 0; i < 3 ; i++ ) { terrainMaterial.addTexture(tex[i]); } Mesh skydome("resources/models/skydome.obj"); Transform skyt( glm::vec3(0,-5,0), glm::quat(0,0,0,1), 5000.f); Material skyMaterial( glm::vec3 (1,1,1), 2.f); Texture* tex2 = new Texture(std::string(RESOURCE_FOLDER) + std::string("images/sky.png")); skyMaterial.addTexture(tex2); float height = 4000.f; Generator gen(2000.f, 128, height); sf::Clock clock; //float old_time = clock.getElapsedTime().asSeconds(); float frame = 0.0f; sf::Clock ftime; while(t->display->isRunning()) { // float delta_time; // delta_time = clock.getElapsedTime().asSeconds() - old_time; // old_time = clock.getElapsedTime().asSeconds(); float elsin = sinf(clock.getElapsedTime().asSeconds() * 0.2f); float elcos = cosf(clock.getElapsedTime().asSeconds() * 0.2f); dir = glm::vec3 ( -elsin , -elcos , 0); shader.setDirLight( DirectionalLight( BaseLight( glm::vec3 (0.8,0.8,0.8), 2.f ), dir )); glm::vec3 ddir = dir * -1.f; skyShader.setDirLight( DirectionalLight( BaseLight( glm::vec3 (0.8,0.8,0.8), 2.f ), ddir )); t->display->clear(0.5, 0.6, 0.7, 1.0); /* Render here */ stimer("Prepare draw"); glm::vec3 camPos = t->camera->getAbsolutePos(); Tmatricies pipe; pipe.view = t->camera->getView(); pipe.projection = t->camera->getProjection(); std::vector<glm::mat4> model; for (int i = 0; i < gen.counter; i++) { model.push_back(gen.m_chList[i]->getTransform().getTransMat()); } //skydome glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); //glCullFace(GL_BACK); skyt.setPos( camPos - glm::vec3(0,2000,0)); pipe.model = skyt.getTransMat(); skyShader.bind(); skyShader.updateUniforms( pipe, skyMaterial); skydome.draw(); glBindTexture(GL_TEXTURE_2D, 0); //glCullFace(GL_FRONT); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); skyShader.unbind(); etimer("Prepare draw"); if (wire) glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); else glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); // // std::cout << "wire received " << ((wire) ? "true" : "false") << std::endl; stimer("Draw"); shader.bind(); for (int i = 0; i < gen.counter; i++) { pipe.model = model[i]; shader.updateUniforms( pipe, terrainMaterial); gen.m_chList[i]->m_mesh->draw(); } etimer("Draw"); glm::vec3 plPos = t->camera->getAbsolutePos(); gen.generateChunkFromPos(plPos.x, plPos.z); frame++; if (ftime.getElapsedTime().asSeconds() >= 1.f) { std::cout << "fps: " << frame << std::endl; frame = 0; ftime.restart(); } t->display->update(); } }
void LightingPass::CreateLightBufferData(const std::vector<Light *> & lights) { m_pointLights.clear(); m_directionalLights.clear(); for (auto light : lights) { unsigned int dindex = m_directionalLights.size(); unsigned int pindex = m_pointLights.size(); switch(light->GetLightType()) { case LightType::DIRECTIONAL: m_directionalLights.push_back(DirectionalLight()); m_directionalLights[dindex].direction = Vector4(light->GetLightDirection(), 1.f); m_directionalLights[dindex].ambient = light->GetAmbientLight(); m_directionalLights[dindex].diffuse = light->GetDiffuseLight(); m_directionalLights[dindex].specular = light->GetSpecularLight(); break; case LightType::POINT: m_pointLights.push_back(PointLight()); //LOG_ERR(light->GetAttenuationConstant(), ", ", light->GetAttenuationLinear(), ", ", light->GetAttenuationQuadratic()); m_pointLights[pindex].position = Vector4(light->worldTransform.Position(), 1.f); m_pointLights[pindex].ambient = light->GetAmbientLight(); m_pointLights[pindex].diffuse = light->GetDiffuseLight(); m_pointLights[pindex].specular = light->GetSpecularLight(); m_pointLights[pindex].constant = light->GetAttenuationConstant(); m_pointLights[pindex].linear = light->GetAttenuationLinear(); m_pointLights[pindex].quadratic = light->GetAttenuationQuadratic(); break; default: LOG_WARN("Unknown light type, unable to render: ", light->GetLightType()); break; } } // XXX hack // make sure at least 2 point lights exist, and 1 directional light switch (m_directionalLights.size()) { case 0: m_directionalLights.push_back(DirectionalLight()); break; } switch (m_pointLights.size()) { case 1: m_pointLights.push_back(PointLight()); m_pointLights[m_pointLights.size() - 1].constant = 1.f; m_pointLights[m_pointLights.size() - 1].linear = 0.045f; m_pointLights[m_pointLights.size() - 1].quadratic = 0.0075f; break; case 0: for (int i = 0; i < 2; i++) { m_pointLights.push_back(PointLight()); } break; } }
static void parseCommandLine(Ref<ParseStream> cin, const FileName& path) { while (true) { std::string tag = cin->getString(); if (tag == "") return; /* parse command line parameters from a file */ else if (tag == "-c") { FileName file = path + cin->getFileName(); parseCommandLine(new ParseStream(new LineCommentFilter(file, "#")), file.path()); } /* load OBJ model*/ else if (tag == "-i") { filename = path + cin->getFileName(); } /* parse camera parameters */ else if (tag == "-vp") g_camera.from = cin->getVec3fa(); else if (tag == "-vi") g_camera.to = cin->getVec3fa(); else if (tag == "-vd") g_camera.to = g_camera.from + cin->getVec3fa(); else if (tag == "-vu") g_camera.up = cin->getVec3fa(); else if (tag == "-fov") g_camera.fov = cin->getFloat(); /* frame buffer size */ else if (tag == "-size") { g_width = cin->getInt(); g_height = cin->getInt(); } /* full screen mode */ else if (tag == "-fullscreen") g_fullscreen = true; /* output filename */ else if (tag == "-o") { outFilename = cin->getFileName(); g_interactive = false; } else if (tag == "-objlist") { keyframeList = cin->getFileName(); } /* subdivision mode */ else if (tag == "-cache") g_subdiv_mode = ",subdiv_accel=bvh4.subdivpatch1cached"; else if (tag == "-pregenerate") g_subdiv_mode = ",subdiv_accel=bvh4.grid.eager"; else if (tag == "-anim") g_anim_mode = true; else if (tag == "-instancing") { std::string mode = cin->getString(); if (mode == "none" ) g_instancing_mode = 0; else if (mode == "geometry") g_instancing_mode = 1; else if (mode == "scene" ) g_instancing_mode = 2; else throw std::runtime_error("unknown instancing mode: "+mode); } /* number of frames to render in benchmark mode */ else if (tag == "-benchmark") { g_skipBenchmarkFrames = cin->getInt(); g_numBenchmarkFrames = cin->getInt(); g_interactive = false; } /* rtcore configuration */ else if (tag == "-rtcore") g_rtcore = cin->getString(); /* number of threads to use */ else if (tag == "-threads") g_numThreads = cin->getInt(); /* ambient light source */ else if (tag == "-ambientlight") { const Vec3fa L = cin->getVec3fa(); g_scene->add(new SceneGraph::LightNode<AmbientLight>(AmbientLight(L))); } /* point light source */ else if (tag == "-pointlight") { const Vec3fa P = cin->getVec3fa(); const Vec3fa I = cin->getVec3fa(); g_scene->add(new SceneGraph::LightNode<PointLight>(PointLight(P,I))); } /* directional light source */ else if (tag == "-directionallight" || tag == "-dirlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa E = cin->getVec3fa(); g_scene->add(new SceneGraph::LightNode<DirectionalLight>(DirectionalLight(D,E))); } /* distant light source */ else if (tag == "-distantlight") { const Vec3fa D = cin->getVec3fa(); const Vec3fa L = cin->getVec3fa(); const float halfAngle = cin->getFloat(); g_scene->add(new SceneGraph::LightNode<DistantLight>(DistantLight(D,L,halfAngle))); } /* skip unknown command line parameter */ else { std::cerr << "unknown command line parameter: " << tag << " "; while (cin->peek() != "" && cin->peek()[0] != '-') std::cerr << cin->getString() << " "; std::cerr << std::endl; } } }