Coding style updates

This commit is contained in:
Alexander Bock
2020-01-11 21:07:38 +01:00
parent 684ba237ec
commit 76240c3edd
9 changed files with 131 additions and 198 deletions

View File

@@ -9,7 +9,7 @@ asset.onInitialize(function ()
openspace.globebrowsing.goToGeo("Earth", 58.5877, 16.1924, 20000000)
openspace.markInterestingNodes({ "Earth", "Mars", "Moon", "Sun"})
openspace.markInterestingNodes({ "Earth", "Mars", "Moon", "Sun" })
end)
asset.onDeinitialize(function ()

View File

@@ -267,12 +267,12 @@ Fragment getFragment() {
#if SHADOW_MAPPING_ENABLED
float shadow = 1.0;
if ( shadowCoords.w > 1 ) {
if (shadowCoords.w > 1) {
vec4 normalizedShadowCoords = shadowCoords;
normalizedShadowCoords.z = normalizeFloat(zFightingPercentage * normalizedShadowCoords.w);
normalizedShadowCoords.xy = normalizedShadowCoords.xy / normalizedShadowCoords.w;
normalizedShadowCoords.w = 1.0;
float sum = 0;
for (int i = 0; i < nShadowSamples; ++i) {
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(-nShadowSamples + i, -nShadowSamples + i));
@@ -285,7 +285,7 @@ Fragment getFragment() {
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( nShadowSamples - i, nShadowSamples - i));
}
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(0, 0));
shadow = sum / (8.f * nShadowSamples + 1.f);
shadow = sum / (8.0 * nShadowSamples + 1.f);
}
frag.color.xyz *= shadow < 0.99 ? clamp(shadow + 0.5, 0.0, 1.0) : shadow;
#endif

View File

@@ -50,8 +50,9 @@ Fragment getFragment() {
float radius = length(st);
// We only want to consider ring-like objects so we need to discard everything else
if (radius > 1.0)
if (radius > 1.0) {
discard;
}
// Remapping the texture coordinates
// Radius \in [0,1], texCoord \in [textureOffset.x, textureOffset.y]
@@ -61,7 +62,7 @@ Fragment getFragment() {
if (texCoord < 0.f || texCoord > 1.f) {
discard;
}
vec4 diffuse = texture(ringTexture, texCoord);
float colorValue = length(diffuse.rgb);
// times 3 as length of vec3(1.0, 1.0, 1.0) will return 3 and we want
@@ -90,7 +91,7 @@ Fragment getFragment() {
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2( nShadowSamples - i, nShadowSamples - i));
}
sum += textureProjOffset(shadowMapTexture, normalizedShadowCoords, ivec2(0, 0));
shadow = sum / (8.f * nShadowSamples + 1.f);
shadow = sum / (8.0 * nShadowSamples + 1.f);
}
// The normal for the one plane depends on whether we are dealing

View File

@@ -39,8 +39,9 @@ Fragment getFragment() {
float radius = length(st);
// We only want to consider ring-like objects so we need to discard everything else
if (radius > 1.0)
if (radius > 1.0) {
discard;
}
// Remapping the texture coordinates
// Radius \in [0,1], texCoord \in [textureOffset.x, textureOffset.y]

View File

@@ -41,17 +41,13 @@ uniform dmat4 modelViewProjectionMatrix;
// where textureCoordsMatrix is just a scale and bias computation: [-1,1] to [0,1]
uniform dmat4 shadowMatrix;
void main() {
vs_st = in_st;
dvec4 positionClipSpace = modelViewProjectionMatrix *
dvec4(in_position, 0.0, 1.0);
dvec4 positionClipSpace = modelViewProjectionMatrix * dvec4(in_position, 0.0, 1.0);
vec4 positionClipSpaceZNorm = z_normalization(vec4(positionClipSpace));
shadowCoords = vec4(shadowMatrix * dvec4(in_position, 0.0, 1.0));
vs_screenSpaceDepth = positionClipSpaceZNorm.w;
gl_Position = positionClipSpaceZNorm;
}

View File

@@ -1350,7 +1350,9 @@ void RenderableGlobe::renderChunkGlobally(const Chunk& chunk, const RenderData&
}
void RenderableGlobe::renderChunkLocally(const Chunk& chunk, const RenderData& data,
const ShadowComponent::ShadowMapData& shadowData, const bool renderGeomOnly) {
const ShadowComponent::ShadowMapData& shadowData,
bool renderGeomOnly)
{
//PerfMeasure("locally");
const TileIndex& tileIndex = chunk.tileIndex;
ghoul::opengl::ProgramObject& program = *_localRenderer.program;

View File

@@ -38,80 +38,74 @@
#include <ghoul/opengl/texture.h>
#include <ghoul/opengl/uniformcache.h>
namespace ghoul {
class Dictionary;
}
namespace ghoul { class Dictionary; }
namespace ghoul::filesystem { class File; }
namespace ghoul::opengl {
class ProgramObject;
} // namespace ghoul::opengl
namespace ghoul::opengl { class ProgramObject; }
namespace openspace {
struct RenderData;
struct UpdateData;
namespace documentation { struct Documentation; }
namespace documentation { struct Documentation; }
class RingsComponent : public properties::PropertyOwner {
public:
enum RenderPass {
GeometryOnly,
GeometryAndShading
};
public:
RingsComponent(const ghoul::Dictionary& dictionary);
void initialize();
void initializeGL();
void deinitializeGL();
bool isReady() const;
void draw(
const RenderData& data,
const RingsComponent::RenderPass renderPass,
const ShadowComponent::ShadowMapData& shadowData = {}
);
void update(const UpdateData& data);
static documentation::Documentation Documentation();
bool isEnabled() const;
private:
void loadTexture();
void createPlane();
properties::StringProperty _texturePath;
properties::FloatProperty _size;
properties::Vec2Property _offset;
properties::FloatProperty _nightFactor;
properties::FloatProperty _transparency;
properties::BoolProperty _enabled;
properties::FloatProperty _zFightingPercentage;
properties::IntProperty _nShadowSamples;
std::unique_ptr<ghoul::opengl::ProgramObject> _shader;
std::unique_ptr<ghoul::opengl::ProgramObject> _geometryOnlyShader;
UniformCache(modelViewProjectionMatrix, textureOffset, transparency, nightFactor,
sunPosition, ringTexture, shadowMatrix, shadowMapTexture,
nShadowSamples, zFightingPercentage
) _uniformCache;
UniformCache(modelViewProjectionMatrix, textureOffset, ringTexture)
_geomUniformCache;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
ghoul::Dictionary _ringsDictionary;
bool _textureIsDirty = false;
GLuint _quad = 0;
GLuint _vertexPositionBuffer = 0;
bool _planeIsDirty = false;
glm::vec3 _sunPosition;
class RingsComponent : public properties::PropertyOwner {
public:
enum RenderPass {
GeometryOnly,
GeometryAndShading
};
RingsComponent(const ghoul::Dictionary& dictionary);
void initialize();
void initializeGL();
void deinitializeGL();
bool isReady() const;
void draw(
const RenderData& data,
const RingsComponent::RenderPass renderPass,
const ShadowComponent::ShadowMapData& shadowData = {}
);
void update(const UpdateData& data);
static documentation::Documentation Documentation();
bool isEnabled() const;
private:
void loadTexture();
void createPlane();
properties::StringProperty _texturePath;
properties::FloatProperty _size;
properties::Vec2Property _offset;
properties::FloatProperty _nightFactor;
properties::FloatProperty _transparency;
properties::BoolProperty _enabled;
properties::FloatProperty _zFightingPercentage;
properties::IntProperty _nShadowSamples;
std::unique_ptr<ghoul::opengl::ProgramObject> _shader;
std::unique_ptr<ghoul::opengl::ProgramObject> _geometryOnlyShader;
UniformCache(modelViewProjectionMatrix, textureOffset, transparency, nightFactor,
sunPosition, ringTexture, shadowMatrix, shadowMapTexture, nShadowSamples,
zFightingPercentage
) _uniformCache;
UniformCache(modelViewProjectionMatrix, textureOffset, ringTexture)
_geomUniformCache;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::unique_ptr<ghoul::filesystem::File> _textureFile;
ghoul::Dictionary _ringsDictionary;
bool _textureIsDirty = false;
GLuint _quad = 0;
GLuint _vertexPositionBuffer = 0;
bool _planeIsDirty = false;
glm::vec3 _sunPosition;
};
} // namespace openspace

View File

@@ -75,6 +75,8 @@ namespace {
"The depth map size in pixels. You must entry the width and height values."
};
constexpr const GLfloat ShadowBorder[] = { 1.f, 1.f, 1.f, 1.f };
void checkFrameBufferState(const std::string& codePosition) {
if (glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
LERROR("Framework not built. " + codePosition);
@@ -232,7 +234,6 @@ void ShadowComponent::deinitializeGL() {
glDeleteTextures(1, &_shadowDepthTexture);
glDeleteTextures(1, &_positionInLightSpaceTexture);
glDeleteFramebuffers(1, &_shadowFBO);
checkGLError("ShadowComponent::deinitializeGL() -- Deleted Textures and Framebuffer");
}
RenderData ShadowComponent::begin(const RenderData& data) {
@@ -245,15 +246,15 @@ RenderData ShadowComponent::begin(const RenderData& data) {
glm::dvec3 lightDirection = glm::normalize(diffVector);
// Percentage of the original light source distance (to avoid artifacts)
/*double multiplier = originalLightDistance *
(static_cast<double>(_distanceFraction)/1.0E5);*/
//double multiplier = originalLightDistance *
// (static_cast<double>(_distanceFraction)/1.0E5);
double multiplier = originalLightDistance *
(static_cast<double>(_distanceFraction) / 1E17);
// New light source position
/*glm::dvec3 lightPosition = data.modelTransform.translation +
(lightDirection * multiplier);*/
//glm::dvec3 lightPosition = data.modelTransform.translation +
// (lightDirection * multiplier);
glm::dvec3 lightPosition = data.modelTransform.translation +
(diffVector * multiplier);
@@ -338,14 +339,14 @@ RenderData ShadowComponent::begin(const RenderData& data) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*glEnable(GL_CULL_FACE);
checkGLError("begin() -- enabled cull face");
glCullFace(GL_FRONT);
checkGLError("begin() -- set cullface to front");*/
/*glEnable(GL_POLYGON_OFFSET_FILL);
checkGLError("begin() -- enabled polygon offset fill");
glPolygonOffset(2.5f, 10.0f);
checkGLError("begin() -- set values for polygon offset");*/
//glEnable(GL_CULL_FACE);
//checkGLError("begin() -- enabled cull face");
//glCullFace(GL_FRONT);
//checkGLError("begin() -- set cullface to front");
//glEnable(GL_POLYGON_OFFSET_FILL);
//checkGLError("begin() -- enabled polygon offset fill");
//glPolygonOffset(2.5f, 10.0f);
//checkGLError("begin() -- set values for polygon offset");
RenderData lightRenderData{
*_lightCamera,
@@ -437,12 +438,13 @@ void ShadowComponent::end() {
}
}
void ShadowComponent::update(const UpdateData& /*data*/) {
void ShadowComponent::update(const UpdateData&) {
_sunPosition = global::renderEngine.scene()->sceneGraphNode("Sun")->worldPosition();
glm::ivec2 renderingResolution = global::renderEngine.renderingResolution();
if (_dynamicDepthTextureRes && ((_shadowDepthTextureWidth != renderingResolution.x) ||
(_shadowDepthTextureHeight != renderingResolution.y))) {
(_shadowDepthTextureHeight != renderingResolution.y)))
{
_shadowDepthTextureWidth = renderingResolution.x * 2;
_shadowDepthTextureHeight = renderingResolution.y * 2;
updateDepthTexture();
@@ -453,8 +455,6 @@ void ShadowComponent::createDepthTexture() {
glGenTextures(1, &_shadowDepthTexture);
updateDepthTexture();
checkGLError("createDepthTexture() -- Depth texture created");
_shadowData.shadowDepthTexture = _shadowDepthTexture;
//_shadowData.positionInLightSpaceTexture = _positionInLightSpaceTexture;
}
@@ -472,14 +472,13 @@ void ShadowComponent::createShadowFBO() {
0
);
/*glFramebufferTexture(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
_positionInLightSpaceTexture,
0
);*/
//glFramebufferTexture(
// GL_FRAMEBUFFER,
// GL_COLOR_ATTACHMENT0,
// _positionInLightSpaceTexture,
// 0
//);
checkGLError("createShadowFBO() -- Created Shadow Framebuffer");
//GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_NONE, GL_NONE };
GLenum drawBuffers[] = { GL_NONE, GL_NONE, GL_NONE };
glDrawBuffers(3, drawBuffers);
@@ -492,15 +491,15 @@ void ShadowComponent::createShadowFBO() {
void ShadowComponent::updateDepthTexture() {
glBindTexture(GL_TEXTURE_2D, _shadowDepthTexture);
/*
glTexStorage2D(
GL_TEXTURE_2D,
1,
GL_DEPTH_COMPONENT32F,
_shadowDepthTextureWidth,
_shadowDepthTextureHeight
);
*/
//glTexStorage2D(
// GL_TEXTURE_2D,
// 1,
// GL_DEPTH_COMPONENT32F,
// _shadowDepthTextureWidth,
// _shadowDepthTextureHeight
//);
glTexImage2D(
GL_TEXTURE_2D,
0,
@@ -521,30 +520,30 @@ void ShadowComponent::updateDepthTexture() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
/*glGenTextures(1, &_positionInLightSpaceTexture);
glBindTexture(GL_TEXTURE_2D, _positionInLightSpaceTexture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB32F,
_shadowDepthTextureWidth,
_shadowDepthTextureHeight,
0,
GL_RGBA,
GL_FLOAT,
nullptr
);
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_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);*/
//glGenTextures(1, &_positionInLightSpaceTexture);
//glBindTexture(GL_TEXTURE_2D, _positionInLightSpaceTexture);
//glTexImage2D(
// GL_TEXTURE_2D,
// 0,
// GL_RGB32F,
// _shadowDepthTextureWidth,
// _shadowDepthTextureHeight,
// 0,
// GL_RGBA,
// GL_FLOAT,
// nullptr
//);
//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_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
}
void ShadowComponent::saveDepthBuffer() {
int size = _shadowDepthTextureWidth * _shadowDepthTextureHeight;
GLubyte* buffer = new GLubyte[size];
std::vector<GLubyte> buffer(size);
glReadPixels(
0,
@@ -553,10 +552,9 @@ void ShadowComponent::saveDepthBuffer() {
_shadowDepthTextureHeight,
GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE,
buffer
buffer.data()
);
checkGLError("readDepthBuffer To buffer");
std::fstream ppmFile;
ppmFile.open("depthBufferShadowMapping.ppm", std::fstream::out);
@@ -582,9 +580,9 @@ void ShadowComponent::saveDepthBuffer() {
std::cout << "Texture saved to file depthBufferShadowMapping.ppm\n\n";
}
delete[] buffer;
buffer.clear();
GLfloat* bBuffer = new GLfloat[size * 4];
std::vector<GLfloat> bBuffer(size * 4);
glReadBuffer(GL_COLOR_ATTACHMENT3);
glReadPixels(
@@ -594,10 +592,9 @@ void ShadowComponent::saveDepthBuffer() {
_shadowDepthTextureHeight,
GL_RGBA,
GL_FLOAT,
bBuffer
bBuffer.data()
);
checkGLError("readPositionBuffer To buffer");
ppmFile.clear();
ppmFile.open("positionBufferShadowMapping.ppm", std::fstream::out);
@@ -636,56 +633,7 @@ void ShadowComponent::saveDepthBuffer() {
ppmFile.close();
std::cout << "Texture saved to file positionBufferShadowMapping.ppm\n\n";
}
delete[] bBuffer;
}
void ShadowComponent::checkGLError(const std::string & where) const {
const GLenum error = glGetError();
switch (error) {
case GL_NO_ERROR:
break;
case GL_INVALID_ENUM:
LERRORC(
"OpenGL Invalid State",
fmt::format("Function {}: GL_INVALID_ENUM", where)
);
break;
case GL_INVALID_VALUE:
LERRORC(
"OpenGL Invalid State",
fmt::format("Function {}: GL_INVALID_VALUE", where)
);
break;
case GL_INVALID_OPERATION:
LERRORC(
"OpenGL Invalid State",
fmt::format(
"Function {}: GL_INVALID_OPERATION", where
));
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
LERRORC(
"OpenGL Invalid State",
fmt::format(
"Function {}: GL_INVALID_FRAMEBUFFER_OPERATION",
where
)
);
break;
case GL_OUT_OF_MEMORY:
LERRORC(
"OpenGL Invalid State",
fmt::format("Function {}: GL_OUT_OF_MEMORY", where)
);
break;
default:
LERRORC(
"OpenGL Invalid State",
fmt::format("Unknown error code: {0:x}", static_cast<int>(error))
);
LINFO("Texture saved to file positionBufferShadowMapping.ppm");
}
}

View File

@@ -41,15 +41,9 @@
#include <string>
#include <sstream>
namespace ghoul {
class Dictionary;
}
namespace ghoul { class Dictionary; }
namespace ghoul::filesystem { class File; }
namespace ghoul::opengl {
class ProgramObject;
} // namespace ghoul::opengl
namespace ghoul::opengl { class ProgramObject; }
namespace openspace {
struct RenderData;
@@ -57,8 +51,6 @@ namespace openspace {
namespace documentation { struct Documentation; }
static const GLfloat ShadowBorder[] = { 1.f, 1.f, 1.f, 1.f };
class ShadowComponent : public properties::PropertyOwner {
public:
struct ShadowMapData {
@@ -94,7 +86,6 @@ private:
// Debug
void saveDepthBuffer();
void checkGLError(const std::string & where) const;
ShadowMapData _shadowData;