-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sky sphere layer and shader (OpenGl), add getter for inverse vp m…
…atrix and camera position in camera interfaces
- Loading branch information
1 parent
d1941d5
commit 18ff1e4
Showing
44 changed files
with
1,217 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
android/src/main/cpp/graphics/shader/SkySphereShaderOpenGl.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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; | ||
|
||
void main() { | ||
gl_Position = vPosition; // in screen coordinates | ||
v_screenPos = texCoordinate; | ||
} | ||
); | ||
} | ||
|
||
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; | ||
out vec4 fragmentColor; | ||
|
||
const float PI = 3.1415926536; | ||
|
||
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) | ||
vec4 dirCamera = normalize(posCart - uCameraPosition); | ||
|
||
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 | ||
); | ||
|
||
ivec2 textureSize = textureSize(textureSampler, 0); | ||
vec2 size = vec2(1.0 / float(textureSize.x), 1.0 / float(textureSize.y)); | ||
size.y *= cos(decl); | ||
|
||
// Mutli-sampling | ||
/*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); | ||
|
||
/*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
44
android/src/main/cpp/graphics/shader/SkySphereShaderOpenGl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
8 changes: 8 additions & 0 deletions
8
.../android/java/io/openmobilemaps/mapscore/shared/graphics/shader/ShaderFactoryInterface.kt
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
...ndroid/java/io/openmobilemaps/mapscore/shared/graphics/shader/SkySphereShaderInterface.kt
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.