void Sea::initialize(){ std::shared_ptr<gland::BaseController> cameraController(new gland::CameraController(camera_, window_)); controllers_.push_back(cameraController); cameraController->generateHelpText(help_); gland::ShaderDough shaderDough; shaderDough.addShaderFromFile("../src/shaders/perlin_vs.glsl", GL_VERTEX_SHADER); shaderDough.addShaderFromFile("../src/shaders/noise_fs.glsl", GL_FRAGMENT_SHADER); gland::ShaderManager shaderManager; GLuint shaderProgram = shaderManager.cookDough(shaderDough); shaderPrograms_.push_back(shaderProgram); //Add object std::string inputfile = "../res/plane129.obj"; std::vector<tinyobj::shape_t> shapes; std::string err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> obj(new gland::Body(shapes[0])); obj->setShader(shaderProgram); standardObjects_.push_back(obj); glEnable (GL_CULL_FACE); // cull face //glCullFace (GL_BACK); // cull back face //glFrontFace (GL_CW); // GL_CCW for counter clock-wise //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); camera_->lookAt(glm::vec3(0.0f, 10.0f, 50.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); }
void ShadowMapping::initialize(){ std::shared_ptr<gland::BaseController> cameraController(new gland::CameraController(camera_, window_)); std::shared_ptr<gland::BaseController> smController(new gland::SMController(params_, window_)); controllers_.push_back(cameraController); controllers_.push_back(smController); glfwSetKeyCallback(window_, &gland::Controller<gland::SMController>::keyCallback); params_["offsetFactor"] = gland::Param("Polygon offset factor"); params_["offsetFactor"] = 4.0f; params_["offsetUnits"] = gland::Param("Polygon offset units"); params_["offsetUnits"] = 4.0f; params_["renderDepth"] = gland::Param("Render depth"); params_["renderDepth"] = false; params_["manualLight"] = gland::Param("Manual light"); params_["manualLight"] = false; params_["angle"] = 0.0f; params_["lightSpeed"] = gland::Param("Light Rotation Speed"); params_["lightSpeed"] = 1.0f; params_["lightRadius"] = gland::Param("Light circ. radius"); params_["lightRadius"] = 10.0f; cameraController->generateHelpText(help_); smController->generateHelpText(help_); gland::ShaderDough shaderDough; shaderDough.addShaderFromFile("../src/shaders/shadowmapping_vs.glsl", GL_VERTEX_SHADER); shaderDough.addShaderFromFile("../src/shaders/shadowmapping_fs.glsl", GL_FRAGMENT_SHADER); gland::ShaderManager shaderManager; nViewProgram_ = shaderManager.cookDough(shaderDough); shaderPrograms_.push_back(nViewProgram_); //Light program gland::ShaderDough lightDough; lightDough.addShaderFromFile("../src/shaders/shadowmappinglight_vs.glsl", GL_VERTEX_SHADER); lightDough.addShaderFromFile("../src/shaders/shadowmappinglight_fs.glsl", GL_FRAGMENT_SHADER); nLightProgram_ = shaderManager.cookDough(lightDough); shaderPrograms_.push_back(nLightProgram_); //Depth program gland::ShaderDough depthDough; depthDough.addShaderFromFile("../src/shaders/shadowmappingdepth_vs.glsl", GL_VERTEX_SHADER); depthDough.addShaderFromFile("../src/shaders/shadowmappingdepth_fs.glsl", GL_FRAGMENT_SHADER); nDepthProgram_ = shaderManager.cookDough(depthDough); shaderPrograms_.push_back(nDepthProgram_); //Light source program gland::ShaderDough lightSourceDough; lightSourceDough.addShaderFromFile("../src/shaders/lightsource_vs.glsl", GL_VERTEX_SHADER); lightSourceDough.addShaderFromFile("../src/shaders/lightsource_fs.glsl", GL_FRAGMENT_SHADER); nLightSourceProgram_ = shaderManager.cookDough(lightSourceDough); shaderPrograms_.push_back(nLightSourceProgram_); //Frame buffer for depth glGenFramebuffers(1, &nDepthFbo_); glBindFramebuffer(GL_FRAMEBUFFER, nDepthFbo_); glGenTextures(1, &nDepthTex_); glBindTexture(GL_TEXTURE_2D, nDepthTex_); glTexStorage2D(GL_TEXTURE_2D, 11, GL_DEPTH_COMPONENT32F, DEPTH_TEXTURE_SIZE, DEPTH_TEXTURE_SIZE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, nDepthTex_, 0); glGenTextures(1, &nDepthDebug_); glBindTexture(GL_TEXTURE_2D, nDepthDebug_); glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, DEPTH_TEXTURE_SIZE, DEPTH_TEXTURE_SIZE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, nDepthDebug_, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); //VAO for drawing depth texture glGenVertexArrays(1, &nQuadVao_); glBindVertexArray(nQuadVao_); glEnable(GL_DEPTH_TEST); //Add object std::string inputfile = "../res/sphere.obj"; std::vector<tinyobj::shape_t> shapes; std::string err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> obj(new gland::Body(shapes[0])); obj->setShader(nViewProgram_); obj->translate(3.0f, 0.0f, -3.0f); standardObjects_.push_back(obj); //Add another sphere std::shared_ptr<gland::Body> sphere2(new gland::Body(shapes[0])); sphere2->setShader(nViewProgram_); sphere2->translate(-10.0f, -0.0f, -2.0f); standardObjects_.push_back(sphere2); inputfile = "../res/thickblock.obj"; err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> floor(new gland::Body(shapes[0])); floor->setShader(nViewProgram_); floor->rotate(0.0f, 1.0f, 0.0f, 0.0f); floor->translate(0.0f, 0.0f, -5.0f); standardObjects_.push_back(floor); inputfile = "../res/monkey.obj"; err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> monkey(new gland::Body(shapes[0])); monkey->setShader(nViewProgram_); monkey->translate(-2.0f, -0.0f, 0.0f); standardObjects_.push_back(monkey); inputfile = "../res/light.obj"; err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> lightSource(new gland::Body(shapes[0])); lightSource->setShader(nLightSourceProgram_); lightSources_.push_back(lightSource); glEnable (GL_CULL_FACE); // cull face glCullFace (GL_BACK); // cull back face //glFrontFace (GL_CW); // GL_CCW for counter clock-wise //Look at center camera_->lookAt(glm::vec3(0.0f, 0.0f, 20.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); }
bool Qgs3DMapScene::updateCameraNearFarPlanes() { // Update near and far plane from the terrain. // this needs to be done with great care as we have kind of circular dependency here: // active nodes are culled based on the current frustum (which involves near + far plane) // and then based on active nodes we set near and far plane. // // All of this is just heuristics assuming that all other stuff is being rendered somewhere // around the area where the terrain is. // // Near/far plane is setup in order to make best use of the depth buffer to avoid: // 1. precision errors - if the range is too great // 2. unwanted clipping of scene - if the range is too small if ( mTerrain ) { Qt3DRender::QCamera *camera = cameraController()->camera(); QMatrix4x4 viewMatrix = camera->viewMatrix(); float fnear = 1e9; float ffar = 0; QList<QgsChunkNode *> activeNodes = mTerrain->activeNodes(); // it could be that there are no active nodes - they could be all culled or because root node // is not yet loaded - we still need at least something to understand bounds of our scene // so lets use the root node if ( activeNodes.isEmpty() ) activeNodes << mTerrain->rootNode(); Q_FOREACH ( QgsChunkNode *node, activeNodes ) { // project each corner of bbox to camera coordinates // and determine closest and farthest point. QgsAABB bbox = node->bbox(); for ( int i = 0; i < 8; ++i ) { QVector4D p( ( ( i >> 0 ) & 1 ) ? bbox.xMin : bbox.xMax, ( ( i >> 1 ) & 1 ) ? bbox.yMin : bbox.yMax, ( ( i >> 2 ) & 1 ) ? bbox.zMin : bbox.zMax, 1 ); QVector4D pc = viewMatrix * p; float dst = -pc.z(); // in camera coordinates, x grows right, y grows down, z grows to the back if ( dst < fnear ) fnear = dst; if ( dst > ffar ) ffar = dst; } } if ( fnear < 1 ) fnear = 1; // does not really make sense to use negative far plane (behind camera) if ( fnear == 1e9 && ffar == 0 ) { // the update didn't work out... this should not happen // well at least temprarily use some conservative starting values qDebug() << "oops... this should not happen! couldn't determine near/far plane. defaulting to 1...1e9"; fnear = 1; ffar = 1e9; } // set near/far plane - with some tolerance in front/behind expected near/far planes float newFar = ffar * 2; float newNear = fnear / 2; if ( !qgsFloatNear( newFar, camera->farPlane() ) || !qgsFloatNear( newNear, camera->nearPlane() ) ) { camera->setFarPlane( newFar ); camera->setNearPlane( newNear ); return true; } }
int main(int argc, const char * argv[]) { OptParse parser; // parser.addOption("s", true); parser.addOption("js", true); if(!parser.parse(argc, argv)) { std::cout << "Required Options were not specified" << std::endl; return 1; } asio::io_service io; PacketSender sender(io); sender.connect(6142); // for(int i = 0; i < 5; ++i) // { // sender.write("P"); // std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // } JoyStick joystick(parser.getOption("js")); //AudioCapture audio("hw:1,0", "S16_LE"); //AudioCapture audio("hw:1,0", ""); //AudioCapture audio; //ImageProcessor imageProcessor; //SerialComm serial; // Define the Axis to use TimedAxisController panAxis(joystick, JoyStick::Axis::HAT_H); panAxis.setInterval(50); TimedAxisController tiltAxis(joystick, JoyStick::Axis::HAT_V); tiltAxis.setInterval(100); EventAxisController leftMotorAxis(joystick, JoyStick::Axis::LEFT_STICK_V); EventAxisController rightMotorAxis(joystick, JoyStick::Axis::RIGHT_STICK_V); CameraController cameraController(panAxis, tiltAxis, 90, 90); cameraController.setPanSpeed(2.0f); cameraController.setTiltSpeed(2.0f); MotorController motorController(leftMotorAxis, rightMotorAxis); motorController.setMaxSpeed(100.0f); // main command dispatch loop // read input from the joy stick and handle appropriate events while(true) { joystick.pollEvents(); // Check for exit if(joystick.isPressed(JoyStick::Button::BACK)) { std::cout << "Exiting" << std::endl; break; } // Attempt to sync with platform if(joystick.isPressed(JoyStick::Button::START)) { std::cout << "Requesting Sync with Platform..." << std::endl; sender.write("Z"); // ... } // When the left trigger is pressed capture image from webcam // if(joystick.isPressed(JoyStick::Button::LEFT_TRIGGER1)) // { // std::cout << "Capturing Image..." << std::endl; // imageProcessor.process(); // } // Trigger Audio Capture // if(joystick.isPressed(JoyStick::Button::RIGHT_TRIGGER1)) // { // std::cout << "Capturing Audio..." << std::endl; // audio.capture(44100, 3); // } cameraController.update(sender); motorController.update(sender); } sender.close(); return 0; }
void CubeMaps::initialize(){ std::shared_ptr<gland::BaseController> cameraController(new gland::CameraController(camera_, window_)); controllers_.push_back(cameraController); cameraController->generateHelpText(help_); gland::ShaderDough shaderDough; shaderDough.addShaderFromFile("../src/shaders/cubemap_vs.glsl", GL_VERTEX_SHADER); shaderDough.addShaderFromFile("../src/shaders/cubemap_fs.glsl", GL_FRAGMENT_SHADER); gland::ShaderManager shaderManager; GLuint shaderProgram = shaderManager.cookDough(shaderDough); shaderPrograms_.push_back(shaderProgram); gland::ShaderDough skyDough; skyDough.addShaderFromFile("../src/shaders/sky_vs.glsl", GL_VERTEX_SHADER); skyDough.addShaderFromFile("../src/shaders/sky_fs.glsl", GL_FRAGMENT_SHADER); npSkyProgram_ = shaderManager.cookDough(skyDough); shaderPrograms_.push_back(npSkyProgram_); //Add object std::string inputfile = "../res/sphere.obj"; std::vector<tinyobj::shape_t> shapes; std::string err = tinyobj::LoadObj(shapes, inputfile.c_str()); std::shared_ptr<gland::Body> obj(new gland::Body(shapes[0])); obj->setShader(shaderProgram); standardObjects_.push_back(obj); glGenVertexArrays(1, &nSkyBoxVao_); glBindVertexArray(nSkyBoxVao_); glGenTextures(1, &nTexture_); glBindTexture(GL_TEXTURE_CUBE_MAP, nTexture_); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0); gland::TextureLoader tl; gland::TextureData images[6]; images[0] = tl.loadDataFromPng("../res/envmaps/posx.png"); images[1] = tl.loadDataFromPng("../res/envmaps/negx.png"); images[2] = tl.loadDataFromPng("../res/envmaps/posy.png"); images[3] = tl.loadDataFromPng("../res/envmaps/negy.png"); images[4] = tl.loadDataFromPng("../res/envmaps/posz.png"); images[5] = tl.loadDataFromPng("../res/envmaps/negz.png"); for (int face = 0; face < 6; face++){ glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB8, images[face].width, images[face].height, 0, GL_RGB, GL_UNSIGNED_BYTE, images[face].data); } glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); //glEnable (GL_CULL_FACE); // cull face //glCullFace (GL_BACK); // cull back face //glFrontFace (GL_CW); // GL_CCW for counter clock-wise //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); camera_->lookAt(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); }