Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sky Sphere #669

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ include_directories(../bridging/android/jni/graphics/shader)
include_directories(../bridging/android/jni/map)
include_directories(../bridging/android/jni/map/controls)
include_directories(../bridging/android/jni/map/layers)
include_directories(../bridging/android/jni/map/layers/effect)
include_directories(../bridging/android/jni/map/layers/icon)
include_directories(../bridging/android/jni/map/layers/line)
include_directories(../bridging/android/jni/map/layers/objects)
include_directories(../bridging/android/jni/map/layers/polygon)
include_directories(../bridging/android/jni/map/layers/skysphere)
include_directories(../bridging/android/jni/map/layers/text)
include_directories(../bridging/android/jni/map/layers/tiled)
include_directories(../bridging/android/jni/map/layers/tiled/raster)
include_directories(../bridging/android/jni/map/layers/tiled/vector)
include_directories(../bridging/android/jni/map/layers/polygon)
include_directories(../bridging/android/jni/map/layers/icon)
include_directories(../bridging/android/jni/map/layers/line)
include_directories(../bridging/android/jni/map/layers/text)
include_directories(../bridging/android/jni/map/loader)
include_directories(../bridging/android/jni/map/scheduling)
include_directories(../bridging/android/jni/map/coordinates)
Expand All @@ -81,7 +83,13 @@ include_directories(../shared/src/map/camera)
include_directories(../shared/src/map/controls)
include_directories(../shared/src/map/coordinates)
include_directories(../shared/src/map/layers)
include_directories(../shared/src/map/layers/effect)
include_directories(../shared/src/map/layers/icon)
include_directories(../shared/src/map/layers/objects)
include_directories(../shared/src/map/layers/polygon)
include_directories(../shared/src/map/layers/line)
include_directories(../shared/src/map/layers/skysphere)
include_directories(../shared/src/map/layers/text)
include_directories(../shared/src/map/layers/tiled)
include_directories(../shared/src/map/layers/tiled/raster)
include_directories(../shared/src/map/layers/tiled/wmts)
Expand All @@ -102,10 +110,6 @@ include_directories(../shared/src/map/layers/tiled/vector/sublayers/background)
include_directories(../shared/src/map/layers/tiled/vector/symbol)
include_directories(../shared/src/map/layers/tiled/vector/description)
include_directories(../shared/src/map/layers/tiled/vector/parsing)
include_directories(../shared/src/map/layers/polygon)
include_directories(../shared/src/map/layers/icon)
include_directories(../shared/src/map/layers/line)
include_directories(../shared/src/map/layers/text)
include_directories(../shared/src/map/scheduling)
include_directories(../shared/src/utils)
include_directories(src/main/cpp)
Expand Down
17 changes: 11 additions & 6 deletions android/src/main/cpp/graphics/objects/Quad2dOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ void Quad2dOpenGl::clear() {

void Quad2dOpenGl::setIsInverseMasked(bool inversed) { isMaskInversed = inversed; }

void Quad2dOpenGl::setFrame(const Quad3dD &frame, const RectD &textureCoordinates, const Vec3D &origin, bool is3D) {
void Quad2dOpenGl::setFrame(const Quad3dD &frame, const RectD &textureCoordinates, const Vec3D &origin, bool is3d) {
std::lock_guard<std::recursive_mutex> lock(dataMutex);
ready = false;
this->frame = frame;
this->textureCoordinates = textureCoordinates;
this->quadOrigin = origin;
this->is3D = is3D;
this->is3d = is3d;
}

void Quad2dOpenGl::setSubdivisionFactor(int32_t factor) {
Expand Down Expand Up @@ -79,7 +79,7 @@ void Quad2dOpenGl::computeGeometry(bool texCoordsOnly) {
// Data mutex covered by caller Quad2dOpenGL::setup()
if (subdivisionFactor == 0) {
if (!texCoordsOnly) {
if (is3D) {
if (is3d) {
vertices = {
(float) (1.0 * std::sin(frame.topLeft.y) * std::cos(frame.topLeft.x) - quadOrigin.x),
(float) (1.0 * cos(frame.topLeft.y) - quadOrigin.y),
Expand Down Expand Up @@ -152,8 +152,8 @@ void Quad2dOpenGl::computeGeometry(bool texCoordsOnly) {
if (!texCoordsOnly) {
double x = originX + deltaDX;
double y = originY + deltaDY;
double z = is3D ? originZ + deltaDZ : 0.0;
if (is3D) {
double z = is3d ? originZ + deltaDZ : 0.0;
if (is3d) {
vertices.push_back((float) (1.0 * std::sin(y) * std::cos(x) - quadOrigin.x));
vertices.push_back((float) (1.0 * cos(y) - quadOrigin.y));
vertices.push_back((float) (-1.0 * std::sin(x) * std::sin(y) - quadOrigin.z));
Expand Down Expand Up @@ -206,6 +206,8 @@ void Quad2dOpenGl::prepareGlData(int program) {
vpMatrixHandle = glGetUniformLocation(program, "uvpMatrix");
mMatrixHandle = glGetUniformLocation(program, "umMatrix");
originOffsetHandle = glGetUniformLocation(program, "uOriginOffset");
originHandle = glGetUniformLocation(program, "uOrigin");

glDataBuffersGenerated = true;
}

Expand Down Expand Up @@ -289,7 +291,7 @@ void Quad2dOpenGl::renderAsMask(const std::shared_ptr<::RenderingContextInterfac
render(context, renderPass, vpMatrix, mMatrix, origin, false, screenPixelAsRealMeterFactor);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}

#include "Logger.h"
void Quad2dOpenGl::render(const std::shared_ptr<::RenderingContextInterface> &context, const RenderPassConfig &renderPass,
int64_t vpMatrix, int64_t mMatrix, const ::Vec3D &origin, bool isMasked,
double screenPixelAsRealMeterFactor) {
Expand Down Expand Up @@ -337,6 +339,9 @@ void Quad2dOpenGl::render(const std::shared_ptr<::RenderingContextInterface> &co
glUniformMatrix4fv(vpMatrixHandle, 1, false, (GLfloat *)vpMatrix);
glUniformMatrix4fv(mMatrixHandle, 1, false, (GLfloat *)mMatrix);
glUniform4f(originOffsetHandle, quadOrigin.x - origin.x, quadOrigin.y - origin.y, quadOrigin.z - origin.z, 0.0);
if (originHandle >= 0) {
glUniform4f(originHandle, origin.x, origin.y, origin.z, 1.0);
}

// Draw the triangles
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
Expand Down
3 changes: 2 additions & 1 deletion android/src/main/cpp/graphics/objects/Quad2dOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Quad2dOpenGl : public GraphicsObjectInterface,
int vpMatrixHandle;
int mMatrixHandle;
int originOffsetHandle;
int originHandle;
int positionHandle;
GLuint vertexBuffer;
std::vector<GLfloat> vertices;
Expand All @@ -87,7 +88,7 @@ class Quad2dOpenGl : public GraphicsObjectInterface,
GLuint indexBuffer;
std::vector<GLushort> indices;
Vec3D quadOrigin = Vec3D(0.0, 0.0, 0.0);
bool is3D = false;
bool is3d = false;

std::shared_ptr<TextureHolderInterface> textureHolder;
int texturePointer;
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/cpp/graphics/shader/ShaderFactoryOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "StretchInstancedShaderOpenGl.h"
#include "IcosahedronColorShaderOpenGl.h"
#include "SphereEffectShaderOpenGl.h"
#include "SkySphereShaderOpenGl.h"

std::shared_ptr<AlphaShaderInterface> ShaderFactoryOpenGl::createAlphaShader() {
return std::make_shared<AlphaShaderOpenGl>(false);
Expand Down Expand Up @@ -109,3 +110,7 @@ std::shared_ptr<ColorShaderInterface> ShaderFactoryOpenGl::createIcosahedronColo
std::shared_ptr<SphereEffectShaderInterface> ShaderFactoryOpenGl::createSphereEffectShader() {
return std::make_shared<SphereEffectShaderOpenGl>();
}

std::shared_ptr<SkySphereShaderInterface> ShaderFactoryOpenGl::createSkySphereShader() {
return std::make_shared<SkySphereShaderOpenGl>();
}
2 changes: 2 additions & 0 deletions android/src/main/cpp/graphics/shader/ShaderFactoryOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ class ShaderFactoryOpenGl : public ShaderFactoryInterface {
std::shared_ptr<ColorShaderInterface> createIcosahedronColorShader() override;

std::shared_ptr<SphereEffectShaderInterface> createSphereEffectShader() override;

std::shared_ptr<SkySphereShaderInterface> createSkySphereShader() override;
};
130 changes: 130 additions & 0 deletions android/src/main/cpp/graphics/shader/SkySphereShaderOpenGl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

#include "SkySphereShaderOpenGl.h"
#include "OpenGlContext.h"

const std::string SkySphereShaderOpenGl::programName = "UBMAP_SkySphereShaderOpenGl";

std::string SkySphereShaderOpenGl::getProgramName() {
return programName;
}

void SkySphereShaderOpenGl::setupProgram(const std::shared_ptr<::RenderingContextInterface> &context) {
std::shared_ptr<OpenGlContext> openGlContext = std::static_pointer_cast<OpenGlContext>(context);
// prepare shaders and OpenGL program
int vertexShader = loadShader(GL_VERTEX_SHADER, getVertexShader());
int fragmentShader = loadShader(GL_FRAGMENT_SHADER, getFragmentShader());

int program = glCreateProgram(); // create empty OpenGL Program
glAttachShader(program, vertexShader); // add the vertex shader to program
glDeleteShader(vertexShader);
glAttachShader(program, fragmentShader); // add the fragment shader to program
glDeleteShader(fragmentShader);

glLinkProgram(program); // create OpenGL program executables

openGlContext->storeProgram(programName, program);

inverseVPMatrixHandle = glGetUniformLocation(program, "uInverseVPMatrix");
cameraPositionHandle = glGetUniformLocation(program, "uCameraPosition");
}

std::shared_ptr<ShaderProgramInterface> SkySphereShaderOpenGl::asShaderProgramInterface() {
return shared_from_this();
}

void SkySphereShaderOpenGl::setCameraProperties(const std::vector<float> &inverseVP,
const Vec3D &cameraPosition) {
std::lock_guard<std::mutex> lock(dataMutex);
this->inverseVPMatrix = inverseVP;
this->cameraPosition = cameraPosition;
}

void SkySphereShaderOpenGl::preRender(const std::shared_ptr<::RenderingContextInterface> &context) {
BaseShaderProgramOpenGl::preRender(context);
{
std::lock_guard<std::mutex> lock(dataMutex);
glUniformMatrix4fv(inverseVPMatrixHandle, 1, false, (GLfloat *)inverseVPMatrix.data());
glUniform4f(cameraPositionHandle, cameraPosition.x, cameraPosition.y, cameraPosition.z, 1.0);
}
}

std::string SkySphereShaderOpenGl::getVertexShader() {
return OMMVersionedGlesShaderCode(320 es,
uniform mat4 uvpMatrix;
uniform mat4 umMatrix;
in vec4 vPosition;
in vec2 texCoordinate;
out vec2 v_screenPos;
out vec2 v_textureScaleFactors;

void main() {
gl_Position = vPosition; // in screen coordinates
v_screenPos = vPosition.xy;
v_textureScaleFactors = abs(texCoordinate) / abs(vPosition.xy);
});
}

std::string SkySphereShaderOpenGl::getFragmentShader() {
return OMMVersionedGlesShaderCode(320 es,
precision mediump float;
uniform vec4 uOriginOffset;
uniform vec4 uOrigin;
uniform vec4 uCameraPosition;
uniform mat4 uInverseVPMatrix;
uniform sampler2D textureSampler;

in vec2 v_screenPos;
in vec2 v_textureScaleFactors;
out vec4 fragmentColor;

const float PI = 3.141592653589793;

void main() {
vec4 posCart = uInverseVPMatrix * vec4(v_screenPos.x, v_screenPos.y, 1.0, 1.0);
posCart /= posCart.w;

// Assume the sky on a sphere around the camera (and not the earth's center)
vec3 dirCamera = normalize(posCart.xyz - uCameraPosition.xyz);

float rasc = atan(dirCamera.z, dirCamera.x) + PI;
float decl = asin(dirCamera.y);

vec2 texCoords = vec2(
-(rasc / (2.0 * PI)) + 1.0,
-decl / PI + 0.5
) * v_textureScaleFactors;

// Mutli-sampling
/*ivec2 textureSize = textureSize(textureSampler, 0);
vec2 size = vec2(1.0 / float(textureSize.x), 1.0 / float(textureSize.y));
size.y *= cos(decl);

fragmentColor = 0.2837 * texture(textureSampler, texCoords);
fragmentColor = fragmentColor + 0.179083648 * texture(textureSampler, texCoords + (vec2(0.0, -2.0)) * size);
fragmentColor = fragmentColor + 0.179083648 * texture(textureSampler, texCoords + (vec2(2.0, 0.0)) * size);
fragmentColor = fragmentColor + 0.179083648 * texture(textureSampler, texCoords + (vec2(0.0, 2.0)) * size);
fragmentColor = fragmentColor + 0.179083648 * texture(textureSampler, texCoords + (vec2(-2.0, 0.0)) * size);*/

// Single sample
fragmentColor = texture(textureSampler, texCoords);

// Debug rendering
/*if (abs(mod(texCoords.x, 0.125)) < 0.001) {
fragmentColor = vec4(0.0, 0.0, 1.0, 1.0);
} else if (abs(mod(texCoords.y, 0.125)) < 0.001) {
fragmentColor = vec4(0.0, 1.0, 1.0, 1.0);
} else {
fragmentColor = vec4(texCoords, 0.0, 1.0);
}*/
}
);
}
44 changes: 44 additions & 0 deletions android/src/main/cpp/graphics/shader/SkySphereShaderOpenGl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

#pragma once

#include "BaseShaderProgramOpenGl.h"
#include "SkySphereShaderInterface.h"

class SkySphereShaderOpenGl
: public BaseShaderProgramOpenGl,
public SkySphereShaderInterface,
public std::enable_shared_from_this<SkySphereShaderOpenGl> {
public:
std::string getProgramName() override;

void setupProgram(const std::shared_ptr<::RenderingContextInterface> &context) override;

std::shared_ptr<ShaderProgramInterface> asShaderProgramInterface() override;

void setCameraProperties(const std::vector<float> &inverseVP, const Vec3D &cameraPosition) override;

void preRender(const std::shared_ptr<::RenderingContextInterface> &context) override;

std::string getVertexShader() override;

protected:
std::string getFragmentShader() override;

private:
const static std::string programName;

std::mutex dataMutex;
std::vector<GLfloat> inverseVPMatrix = {1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0};
Vec3D cameraPosition = {0.0, 0.0, 0.0};
GLint inverseVPMatrixHandle;
GLint cameraPositionHandle;
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading