Files
OpenSpace/modules/base/rendering/pointcloud/renderablepolygoncloud.cpp
Emma Broman f36868d1c4 Feature/textured points (#3068)
* WIP: Start usign texture arrays instead of just a single texture

Now the texture array is sucessfully created, sent over and sampled on the GPU

* Include information about the texture format alpha channel and do a conversion

* Make one draw wcall per texture array

* Add scale to size mapping and move to a separate component

* WIP: Make single textures work again, with texture array

Although this breaks the polygon cloud..

* Also make the polygon cloud work again

* Refactor rendering code

* handle array layer seprately from texture coordinates

* Make sure use size mapping uniform is always set

Fixes point cloud disappearing when multi-textures points are enabled

* Add has value check to size mapping

* Fix indentation

* Make sure points are rendered even when no texture is used

* Clean up texture handling a bit and add comment about storage creation

* Add comment and temporary asset changes

* Clean up handling of color mode (number of colro channels)

* Make interpolated points work with new rendering code

* Refactor

* Bring back check for valid index for color and size data

* Make sure to check if the provided data file exists

* Fix full path ont showing in error message

* Refactor rendering code a bit

* Change how the multitexture setup is configured in the asset and add documentation

Separating made documentation a lot easier..

* Add a todo comment for future discussion

* Add settings for texture compression

* Preserve aspects ratio of rendered textures

* Restructure input parameters for texture details

* Simplify color mode - we decided to not support grayscale

* Add option to set "useAlpha" from asset

* Enable texture per default and fix aspect ratio problem when no texture is used

* tiny refactor

* Fix polygon rendering that broke when adding texture compression

* Remove color in polygon shader

The color would be applied twice in rendering

* Restructure textures code and prevent loading the same texture twice

* Better handling of extra texture parameter in speck files

That does not lead to limitations in using dashes in texture names

* Add some docs and communicate texture mode to the user

* Fix so that single texture can be changed during runtime

* Allow changing compression and usealpha during runtime

* Update texture storage allocation to something that works in older OpenGL versions

* Add a check that checks if we use more texture layers than allowed

* Even more robust check of texture line in speck file (allow extra whitespaces)

* Update data mapping to include texture information and clean up code a bit

* Error handling and prevent loading non-used textures in texture map

* Update some docs

* Small cleanup

* Add one more error message for fault texture map file format

* Remove test version of tully images dataset

* Small refactor

* Add example asset

* Update Ghoul - for larger uniform cache

* Purge texture from ram when we're done with it

* Cleanup (comments, ugly png check, etc)

* Apply suggestions from code review

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* Apply suggestions from code review

* Adress some more review comments and fix broken asset

* More code review fixes

* Read provided sizemapping parameter from asset

* Fix warnings from trying to shift 16 bit int 32 bits :)

* simplify datamapping hash string

* Update comment that was not 100% correct. The file names may be specified as relative paths to a folder

* Small update based on previous code review comments

* Fix multi textured points gui path not same as other points

* Update Folder description to reduce some confusion

* Apply suggestions from code review

Co-authored-by: Ylva Selling <ylva.selling@gmail.com>

* Prevent updates to polygon cloud texture during runtime

This lead to rendering problems.

* Add describing comments to data files

* Clarify why speck version is disabled per default

* Update and clarify confusing size mapping parameters

* Apply suggestions from code review

Co-authored-by: Ylva Selling <ylva.selling@gmail.com>

* Apply suggestions from code review

---------

Co-authored-by: Alexander Bock <alexander.bock@liu.se>
Co-authored-by: Ylva Selling <ylva.selling@gmail.com>
2024-03-19 13:17:25 +01:00

227 lines
8.8 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2024 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/base/rendering/pointcloud/renderablepolygoncloud.h>
#include <openspace/documentation/documentation.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/glm.h>
#include <ghoul/logging/logmanager.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <optional>
namespace {
constexpr std::string_view _loggerCat = "RenderablePolygonCloud";
// A RenderablePolygonCloud is a RenderablePointCloud where the shape of the points
// is a uniform polygon with a given number of sides instead of a texture. For
// instance, PolygonSides = 5 results in the points being rendered as pentagons.
//
// Note that while this renderable inherits the texture component from
// RenderablePointCloud, any added texture information will be ignored in favor of the
// polygon shape.
//
// See documentation of RenderablePointCloud for details on the other parts of the
// point cloud rendering.
struct [[codegen::Dictionary(RenderablePolygonCloud)]] Parameters {
// The number of sides for the polygon used to represent each point. Default is
// 3, i.e. to use triangles
std::optional<int> polygonSides [[codegen::greaterequal(3)]];
};
#include "renderablepolygoncloud_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation RenderablePolygonCloud::Documentation() {
return codegen::doc<Parameters>(
"base_renderablepolygoncloud",
RenderablePointCloud::Documentation()
);
}
RenderablePolygonCloud::RenderablePolygonCloud(const ghoul::Dictionary& dictionary)
: RenderablePointCloud(dictionary)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
_nPolygonSides = p.polygonSides.value_or(_nPolygonSides);
// The texture to use for the rendering will be generated in initializeGl. Make sure
// we use it in the rendering
_hasSpriteTexture = true;
_textureMode = TextureInputMode::Other;
removePropertySubOwner(_texture);
}
void RenderablePolygonCloud::deinitializeGL() {
glDeleteBuffers(1, &_polygonVbo);
_polygonVbo = 0;
glDeleteVertexArrays(1, &_polygonVao);
_polygonVao = 0;
glDeleteTextures(1, &_pTexture);
RenderablePointCloud::deinitializeGL();
}
void RenderablePolygonCloud::initializeCustomTexture() {
ZoneScoped;
if (_textureIsInitialized) {
LWARNING("RenderablePolygonCloud texture cannot be updated during runtime");
return;
}
LDEBUG("Creating Polygon Texture");
constexpr gl::GLsizei TexSize = 512;
// We don't use the helper function for the format and internal format here,
// as we don't want the compression to be used for the polygon texture and we
// always want alpha. This is also why we do not need to update the texture
bool useAlpha = true;
gl::GLenum format = gl::GLenum(glFormat(useAlpha));
gl::GLenum internalFormat = GL_RGBA8;
glGenTextures(1, &_pTexture);
glBindTexture(GL_TEXTURE_2D, _pTexture);
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);
// Stopped using a buffer object for GL_PIXEL_UNPACK_BUFFER
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glTexImage2D(
GL_TEXTURE_2D,
0,
internalFormat,
TexSize,
TexSize,
0,
format,
GL_UNSIGNED_BYTE,
nullptr
);
renderToTexture(_pTexture, TexSize, TexSize);
// Download the data and use it to intialize the data we need to rendering.
// Allocate memory: N channels, with one byte each
constexpr unsigned int nChannels = 4;
unsigned int arraySize = TexSize * TexSize * nChannels;
std::vector<GLubyte> pixelData;
pixelData.resize(arraySize);
glBindTexture(GL_TEXTURE_2D, _pTexture);
glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, pixelData.data());
// Create array from data, size and format
unsigned int id = 0;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D_ARRAY, id);
initAndAllocateTextureArray(id, glm::uvec2(TexSize), 1, useAlpha);
fillAndUploadTextureLayer(0, 0, 0, glm::uvec2(TexSize), useAlpha, pixelData.data());
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
_textureIsInitialized = true;
}
void RenderablePolygonCloud::renderToTexture(GLuint textureToRenderTo,
GLuint textureWidth, GLuint textureHeight)
{
LDEBUG("Rendering to Texture");
// Saves initial Application's OpenGL State
GLint defaultFBO = 0;
std::array<GLint, 4> viewport;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
glGetIntegerv(GL_VIEWPORT, viewport.data());
GLuint textureFBO;
glGenFramebuffers(1, &textureFBO);
glBindFramebuffer(GL_FRAMEBUFFER, textureFBO);
const GLenum drawBuffers = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &drawBuffers);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureToRenderTo, 0);
glViewport(viewport[0], viewport[1], textureWidth, textureHeight);
if (_polygonVao == 0) {
glGenVertexArrays(1, &_polygonVao);
}
if (_polygonVbo == 0) {
glGenBuffers(1, &_polygonVbo);
}
glBindVertexArray(_polygonVao);
glBindBuffer(GL_ARRAY_BUFFER, _polygonVbo);
constexpr std::array<GLfloat, 4> VertexData = {
// x y z w
0.f, 0.f, 0.f, 1.f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), VertexData.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
renderPolygonGeometry(_polygonVao);
// Restores Applications' OpenGL State
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
glDeleteBuffers(1, &_polygonVbo);
glDeleteVertexArrays(1, &_polygonVao);
glDeleteFramebuffers(1, &textureFBO);
}
void RenderablePolygonCloud::renderPolygonGeometry(GLuint vao) {
std::unique_ptr<ghoul::opengl::ProgramObject> program =
ghoul::opengl::ProgramObject::Build(
"RenderablePointCloud_Polygon",
absPath("${MODULE_BASE}/shaders/polygon_vs.glsl"),
absPath("${MODULE_BASE}/shaders/polygon_fs.glsl"),
absPath("${MODULE_BASE}/shaders/polygon_gs.glsl")
);
program->activate();
constexpr glm::vec4 Black = glm::vec4(0.f, 0.f, 0.f, 0.f);
glClearBufferfv(GL_COLOR, 0, glm::value_ptr(Black));
program->setUniform("sides", _nPolygonSides);
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, 1);
glBindVertexArray(0);
program->deactivate();
}
} // namespace openspace