/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2015 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ // open space includes #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { const std::string _loggerCat = "RenderablePlanet"; const std::string keyFrame = "Frame"; const std::string keyGeometry = "Geometry"; const std::string keyShading = "PerformShading"; const std::string keyBody = "Body"; } namespace openspace { RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary) : Renderable(dictionary) , _colorTexturePath("colorTexture", "Color Texture") , _programObject(nullptr) , _texture(nullptr) , _geometry(nullptr) , _performShading("performShading", "Perform Shading", true) { std::string name; bool success = dictionary.getValue(constants::scenegraphnode::keyName, name); ghoul_assert(success, "RenderablePlanet need the '" <(keyShading)) { bool shading; dictionary.getValue(keyShading, shading); _performShading = shading; } addProperty(_performShading); } RenderablePlanet::~RenderablePlanet() { } bool RenderablePlanet::initialize() { if (_programObject == nullptr) OsEng.ref().configurationManager()->getValue("pscShader", _programObject); loadTexture(); _geometry->initialize(this); return isReady(); } bool RenderablePlanet::deinitialize() { if(_geometry) { _geometry->deinitialize(); delete _geometry; } if(_texture) delete _texture; _geometry = nullptr; _texture = nullptr; return true; } bool RenderablePlanet::isReady() const { bool ready = true; ready &= (_programObject != nullptr); ready &= (_texture != nullptr); ready &= (_geometry != nullptr); return ready; } void RenderablePlanet::render(const RenderData& data) { // activate shader _programObject->activate(); // scale the planet to appropriate size since the planet is a unit sphere glm::mat4 transform = glm::mat4(1); //earth needs to be rotated for that to work. glm::mat4 rot = glm::rotate(transform, 90.f, glm::vec3(1, 0, 0)); for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ transform[i][j] = static_cast(_stateMatrix[i][j]); } } transform = transform* rot; if (_frame == "IAU_JUPITER"){ //x = 0.935126 transform *= glm::scale(glm::mat4(1), glm::vec3(1, 0.93513, 1)); } //glm::mat4 modelview = data.camera.viewMatrix()*data.camera.modelMatrix(); //glm::vec3 camSpaceEye = (-(modelview*data.position.vec4())).xyz; psc sun_pos; double lt; openspace::SpiceManager::ref().getTargetPosition("SUN", _target, "GALACTIC", "NONE", _time, sun_pos, lt); // setup the data to the shader // _programObject->setUniform("camdir", camSpaceEye); _programObject->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); _programObject->setUniform("ModelTransform", transform); setPscUniforms(_programObject, &data.camera, data.position); _programObject->setUniform("_performShading", _performShading); // Bind texture ghoul::opengl::TextureUnit unit; unit.activate(); _texture->bind(); _programObject->setUniform("texture1", unit); // render _geometry->render(); // disable shader _programObject->deactivate(); } void RenderablePlanet::update(const UpdateData& data){ // set spice-orientation in accordance to timestamp openspace::SpiceManager::ref().getPositionTransformMatrix(_frame, "GALACTIC", data.time, _stateMatrix); _time = data.time; } void RenderablePlanet::loadTexture() { delete _texture; _texture = nullptr; if (_colorTexturePath.value() != "") { _texture = ghoul::io::TextureReader::ref().loadTexture(_colorTexturePath); if (_texture) { LDEBUG("Loaded texture from '" << _colorTexturePath << "'"); _texture->uploadTexture(); // Textures of planets looks much smoother with AnisotropicMipMap rather than linear _texture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); } } } } // namespace openspace