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

Support ptex mesh - step 5: improve the shader and rendering code #203

Merged
merged 7 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions src/esp/assets/PTexMeshData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ float PTexMeshData::exposure() const {
return exposure_;
}

void PTexMeshData::setExposure(const float& val) {
void PTexMeshData::setExposure(float val) {
exposure_ = val;
}

float PTexMeshData::gamma() const {
return gamma_;
}

void PTexMeshData::setGamma(const float& val) {
void PTexMeshData::setGamma(float val) {
gamma_ = val;
}

float PTexMeshData::saturation() const {
return saturation_;
}

void PTexMeshData::setSaturation(const float& val) {
void PTexMeshData::setSaturation(float val) {
saturation_ = val;
}

Expand Down
15 changes: 10 additions & 5 deletions src/esp/assets/PTexMeshData.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,28 @@ class PTexMeshData : public BaseMesh {
virtual Magnum::GL::Mesh* getMagnumGLMesh(int submeshID) override;

float exposure() const;
void setExposure(const float& val);
void setExposure(float val);

float gamma() const;
void setGamma(const float& val);
void setGamma(float val);

float saturation() const;
void setSaturation(const float& val);
void setSaturation(float val);

protected:
void loadMeshData(const std::string& meshFile);

float splitSize_ = 0.0f;
uint32_t tileSize_ = 0;

// based on ReplicaSDK
// initial values are based on ReplicaSDK
//! @brief expsure, the amount of light per unit area reaching the image
float exposure_ = 0.025f;
float gamma_ = 1.6969f;

//! @brief gamma, the exponent applied in the gamma correction
float gamma_ = 1.0f / 1.6969f;

//! @brief saturation, the intensity of a color
float saturation_ = 1.5f;

std::string atlasFolder_;
Expand Down
10 changes: 4 additions & 6 deletions src/esp/gfx/PTexMeshShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ PTexMeshShader::PTexMeshShader() {
// cache the uniform locations
MVPMatrixUniform_ = uniformLocation("MVP");
exposureUniform_ = uniformLocation("exposure");
gammaInverseUniform_ = uniformLocation("gammaInverse");
gammaUniform_ = uniformLocation("gamma");
saturationUniform_ = uniformLocation("saturation");
tileSizeUniform_ = uniformLocation("tileSize");
widthInTilesUniform_ = uniformLocation("widthInTiles");
Expand Down Expand Up @@ -100,8 +100,7 @@ PTexMeshShader& PTexMeshShader::setExposure(float exposure) {
return *this;
}
PTexMeshShader& PTexMeshShader::setGamma(float gamma) {
// Careful: we set its inverse, not gamma directly
setUniform(gammaInverseUniform_, 1.0f / gamma);
setUniform(gammaUniform_, gamma);
return *this;
}

Expand All @@ -115,10 +114,9 @@ PTexMeshShader& PTexMeshShader::setAtlasTextureSize(
uint32_t tileSize) {
setUniform(tileSizeUniform_, (int)tileSize);

// Image size in given mip level 0
// get image width in given mip level 0
int mipLevel = 0;
int widthEntry = 0;
const auto width = texture.imageSize(mipLevel)[widthEntry];
const auto width = texture.imageSize(mipLevel).x();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the rendering uses geometry shaders, I assume it's not planned to be running on WebGL, right? (If it would be, the imageSize() would be problematic, but otherwise not at all.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msbaines: May I ask you what our latest plan is on rendering the ptex mesh on WebGL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mosra : Yeah, the geometry shader is a problem as it is not available in WebGL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am currently planning on using the vertex color information from the ply mesh but at some point would like to migrate to using the ptex textures. Not sure when. It will depend on whether the vertex color-only is OK. The other issue is that the textures are 100s of MB each so may not be feasible for WebGL.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Texture size could be fixed by using Basis texture compression (see mosra/magnum-plugins#62, I'm in process of integrating that), ideally the compressed textures being upstreamed back to Replica.

For vertex colors, when I checked how a PLY file looks compared to the textured GLB, it was quite underwhelming :) For human eyes and a 4K screen at least, when rendering tiny images the difference would probably be minimal.

setUniform(widthInTilesUniform_, int(width / tileSize));
return *this;
}
Expand Down
34 changes: 33 additions & 1 deletion src/esp/gfx/PTexMeshShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,51 @@ namespace gfx {

class PTexMeshShader : public Magnum::GL::AbstractShaderProgram {
public:
//! @brief vertex positions
typedef Magnum::GL::Attribute<0, Magnum::Vector4> Position;

/**
* @brief Constructor
*/
explicit PTexMeshShader();

// ======== texture binding ========
/**
*! @brief Bind the atlas texture
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& bindAtlasTexture(Magnum::GL::Texture2D& texture);
/**
*! @brief Bind the buffer texture containing the adjacent faces
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& bindAdjFacesBufferTexture(Magnum::GL::BufferTexture& texture);

// ======== set uniforms ===========
/**
*! @brief Set modelview and projection matrix to the uniform on GPU
*! @return Reference to self (for method chaining)
*/
mosra marked this conversation as resolved.
Show resolved Hide resolved
PTexMeshShader& setMVPMatrix(const Magnum::Matrix4& matrix);
/**
*! @brief Set expsure to the uniform on GPU
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& setExposure(float exposure);
/**
*! @brief Set gamma to the uniform on GPU
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& setGamma(float gamma);
/**
*! @brief Set saturation to the uniform on GPU
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& setSaturation(float saturation);
/**
*! @brief Set the tile size of the atlas texture
*! @return Reference to self (for method chaining)
*/
PTexMeshShader& setAtlasTextureSize(Magnum::GL::Texture2D& texture,
uint32_t tileSize);

Expand All @@ -45,7 +77,7 @@ class PTexMeshShader : public Magnum::GL::AbstractShaderProgram {
// therefore, cache the locations in the constructor
int MVPMatrixUniform_;
int exposureUniform_;
int gammaInverseUniform_;
int gammaUniform_;
int saturationUniform_;
int tileSizeUniform_;
int widthInTilesUniform_;
Expand Down
4 changes: 2 additions & 2 deletions src/shaders/ptex-default-gl410.frag
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ layout(location = 0) out vec4 FragColor;
uniform sampler2D atlasTex;

uniform float exposure;
uniform float gammaInverse;
uniform float gamma;
uniform float saturation;

in vec2 uv;

void main() {
vec4 c = textureAtlas(atlasTex, gl_PrimitiveID, uv * tileSize) * exposure;
applySaturation(c, saturation);
c.rgb = pow(c.rgb, vec3(gammaInverse));
c.rgb = pow(c.rgb, vec3(gamma));
FragColor = vec4(c.rgb, 1.0f);
}