From 83ed904f1dd963eea6991b403a80b3827fd63a04 Mon Sep 17 00:00:00 2001 From: michal Date: Fri, 28 Nov 2014 13:49:18 -0500 Subject: [PATCH] changes to projective texturing, some cleanup. --- .../planets/renderableplanetprojection.h | 3 - scripts/default_startup.lua | 2 +- src/interaction/interactionhandler.cpp | 8 +- .../planets/renderableplanetprojection.cpp | 105 ++++++++++++++---- src/rendering/renderabletrail.cpp | 2 +- 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/include/openspace/rendering/planets/renderableplanetprojection.h b/include/openspace/rendering/planets/renderableplanetprojection.h index e2c3b1a5ce..2417b8fafb 100644 --- a/include/openspace/rendering/planets/renderableplanetprojection.h +++ b/include/openspace/rendering/planets/renderableplanetprojection.h @@ -62,10 +62,7 @@ private: properties::StringProperty _colorTexturePath; properties::StringProperty _projectionTexturePath; - ghoul::opengl::FramebufferObject fbo; - ghoul::opengl::ProgramObject* _programObject; - ghoul::opengl::ProgramObject* _writeToTextureProgramObject; ghoul::opengl::Texture* _texture; ghoul::opengl::Texture* _textureProj; diff --git a/scripts/default_startup.lua b/scripts/default_startup.lua index d02f3f2890..4a683b5673 100644 --- a/scripts/default_startup.lua +++ b/scripts/default_startup.lua @@ -1,5 +1,5 @@ --openspace.setPropertyValue('Earth.renderable.colorTexture', '${OPENSPACE_DATA}/modules/mars/textures/mars.png') -openspace.time.setTime("2007-02-26T17:38:00.00") +openspace.time.setTime("2007-02-28T11:48:00.00") --openspace.time.setTime("2006-08-22T20:00:00") --openspace.time.setDeltaTime(200000.0) diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 7ab85a2b73..3e10b3cbc9 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -752,22 +752,22 @@ void InteractionHandler::keyboardCallback(int key, int action) { Time::ref().retreatTime(sgct::Engine::instance()->getDt()); } if (key == 262) { - glm::vec3 euler(0.0, speed * dt, 0.0); + glm::vec3 euler(0.0, speed * dt*0.4, 0.0); glm::quat rot = glm::quat(euler); rotateDelta(rot); } if (key == 263) { - glm::vec3 euler(0.0, -speed * dt, 0.0); + glm::vec3 euler(0.0, -speed * dt*0.4, 0.0); glm::quat rot = glm::quat(euler); rotateDelta(rot); } if (key == 264) { - glm::vec3 euler(speed * dt, 0.0, 0.0); + glm::vec3 euler(speed * dt*0.4, 0.0, 0.0); glm::quat rot = glm::quat(euler); rotateDelta(rot); } if (key == 265) { - glm::vec3 euler(-speed * dt, 0.0, 0.0); + glm::vec3 euler(-speed * dt*0.4, 0.0, 0.0); glm::quat rot = glm::quat(euler); rotateDelta(rot); } diff --git a/src/rendering/planets/renderableplanetprojection.cpp b/src/rendering/planets/renderableplanetprojection.cpp index 0d6953d808..4cd26f5c4c 100644 --- a/src/rendering/planets/renderableplanetprojection.cpp +++ b/src/rendering/planets/renderableplanetprojection.cpp @@ -38,7 +38,8 @@ #include #include - +#define _USE_MATH_DEFINES +#include namespace { const std::string _loggerCat = "RenderablePlanetProjection"; @@ -51,7 +52,6 @@ RenderablePlanetProjection::RenderablePlanetProjection(const ghoul::Dictionary& , _colorTexturePath("colorTexture", "Color Texture") , _projectionTexturePath("colorTexture", "Color Texture") , _programObject(nullptr) - , _writeToTextureProgramObject(nullptr) , _texture(nullptr) , _textureProj(nullptr) , _geometry(nullptr) @@ -103,9 +103,6 @@ bool RenderablePlanetProjection::initialize(){ if (_programObject == nullptr) completeSuccess &= OsEng.ref().configurationManager().getValue("projectiveProgram", _programObject); - if (_writeToTextureProgramObject == nullptr) - completeSuccess - &= OsEng.ref().configurationManager().getValue("writeToTextureProgram", _writeToTextureProgramObject); loadTexture(); completeSuccess &= (_texture != nullptr); @@ -211,25 +208,88 @@ void RenderablePlanetProjection::render(const RenderData& data) // disable shader _programObject->deactivate(); + + static int callCount = 0; + callCount++; - /* - fbo.activate(); - //glViewport(0, 0, 1024, 1024); - _writeToTextureProgramObject->activate(); - GLfloat vertices[] = { -1, -1, 0, // bottom left corner - -1, 1, 0, // top left corner - 1, 1, 0, // top right corner - 1, -1, 0 }; // bottom right corner - - GLubyte indices[] = { 0, 1, 2, // first triangle (bottom left - top left - top right) - 0, 2, 3 }; // second triangle (bottom left - top right - bottom right) + if (callCount > 1000){ + callCount = 0; - glVertexPointer(3, GL_FLOAT, 0, vertices); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); + _textureProj->downloadTexture(); + _texture->downloadTexture(); + + auto uvToModel = [](float u, float v, float radius[2], float fsegments)->glm::vec4{ + + const float fj = u * fsegments; + const float fi = v * fsegments; + + const float theta = fi * float(M_PI) / fsegments; // 0 -> PI + const float phi = fj * float(M_PI) * 2.0f / fsegments; + + const float x = radius[0] * sin(phi) * sin(theta); // + const float y = radius[0] * cos(theta); // up + const float z = radius[0] * cos(phi) * sin(theta); // + + return glm::vec4(x, y, z, radius[1]); + }; + + auto uvToIndex = [](const glm::vec2 &uv, int w, int h, int &i, int &j){ + i = static_cast(uv.x * float(w)); + j = static_cast(uv.y * float(h)); + }; + + auto inRange = [](int x, int a, int b)->bool{ + return (x >= a && x <= b); + }; + + auto pscToMeter = [](glm::vec4 v1, glm::vec2 v2)->glm::vec4{ + float factor = v2.x * pow(10, v2.y + v1.w); + return glm::vec4(v1.xyz * factor, 1.0); + }; + + const float w = _texture->width(); + const float h = _texture->height(); + for (int i = 0; i < _texture->width(); ++i) { + for (int j = 0; j < _texture->height(); ++j) { + // "Shader code" + + // Texture coordinates + float u = float(i) / w; + float v = float(j) / h; + + // Psc scaling + glm::vec2 scaling = data.camera.scaling(); + + // Convert texture coordinates to model coordinates + float radius[2] = { 0.71492f, 8.f }; + glm::vec4 in_position = uvToModel(u, v, radius, 200); + + // Convert psc to meters + glm::vec4 raw_pos = pscToMeter(in_position, scaling); + + // Transform model coordinates to world coordinates + glm::vec4 projected = m * transform * raw_pos; + + // To do : use bilinear interpolation + int x, y; + glm::vec2 uv; + uv.x = projected.x / projected.w; + uv.y = projected.y / projected.w; + uvToIndex(uv, _textureProj->width(), _textureProj->height(), x, y); + + if (inRange(x, 0, _textureProj->width() - 1) && inRange(y, 0, _textureProj->height() - 1)){ + + _texture->texel >(i, j) = _textureProj->texel>(x, y); + } + } + } + + // Upload textures + //_textureProj->uploadTexture(); + _texture->uploadTexture(); + } + - _writeToTextureProgramObject->deactivate(); - fbo.deactivate(); - */ } void RenderablePlanetProjection::update(const UpdateData& data){ @@ -252,9 +312,6 @@ void RenderablePlanetProjection::loadTexture() // Textures of planets looks much smoother with AnisotropicMipMap rather than linear _texture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); - fbo.activate(); - fbo.attachTexture(_texture, GL_COLOR_ATTACHMENT0, 0, 0); - fbo.deactivate(); } } diff --git a/src/rendering/renderabletrail.cpp b/src/rendering/renderabletrail.cpp index 585b4eef86..2b9557a40f 100644 --- a/src/rendering/renderabletrail.cpp +++ b/src/rendering/renderabletrail.cpp @@ -175,7 +175,7 @@ bool RenderableTrail::initialize(){ completeSuccess &= (_texture != nullptr); _startTrail; - SpiceManager::ref().getETfromDate("2007 feb 26 17:38:00.000", _startTrail); + SpiceManager::ref().getETfromDate("2007 feb 27 11:48:00.000", _startTrail); _dtEt = _startTrail; fullYearSweep();