Skip to content

Commit

Permalink
Cache baked HDRI
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk committed Jan 16, 2023
1 parent 579c614 commit 9fd7d43
Show file tree
Hide file tree
Showing 20 changed files with 511 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/actions/vtk_commit_sha
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ea2b6b5ad1516f65684eff0972a198af873ca1c1
7ae47ffa119c6feaea43ef021fb48a3056abbec2
4 changes: 2 additions & 2 deletions cmake/testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ if(VTK_VERSION VERSION_GREATER_EQUAL 9.1.20211007)
f3d_test(NAME TestThumbnailConfigFileUp DATA suzanne.stl CONFIG ${CMAKE_SOURCE_DIR}/resources/thumbnail.json DEFAULT_LIGHTS)
endif()

# HDRI test needs https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8825
if(VTK_VERSION VERSION_GREATER_EQUAL 9.1.20220117)
# HDRI test needs https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9767
if(VTK_VERSION VERSION_GREATER_EQUAL 9.2.20221220)
f3d_test(NAME TestHDRI HDRI DATA suzanne.ply ARGS --hdri=${CMAKE_SOURCE_DIR}/testing/data/palermo_park_1k.hdr DEFAULT_LIGHTS)
f3d_test(NAME TestHDRIBlur HDRI DATA suzanne.ply ARGS -u --hdri=${CMAKE_SOURCE_DIR}/testing/data/palermo_park_1k.hdr DEFAULT_LIGHTS)
f3d_test(NAME TestHDRIBlurRatio HDRI DATA suzanne.ply RESOLUTION 600,100 ARGS -u --hdri=${CMAKE_SOURCE_DIR}/testing/data/palermo_park_1k.hdr DEFAULT_LIGHTS)
Expand Down
2 changes: 2 additions & 0 deletions library/VTKExtensions/Rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
set(classes
vtkF3DCachedLUTTexture
vtkF3DCachedSpecularTexture
vtkF3DInteractorEventRecorder
vtkF3DInteractorStyle
vtkF3DNoRenderWindow
Expand Down
1 change: 1 addition & 0 deletions library/VTKExtensions/Rendering/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ vtk_add_test_cxx(VTKExtensionsRenderingTests tests
TestF3DOpenGLGridMapper.cxx
TestF3DRenderPass.cxx
TestF3DRenderer.cxx
TestF3DCachedTextures.cxx
${CMAKE_SOURCE_DIR}/testing/ ${CMAKE_BINARY_DIR}/Testing/Temporary/)
vtk_test_cxx_executable(VTKExtensionsRenderingTests tests)
16 changes: 16 additions & 0 deletions library/VTKExtensions/Rendering/Testing/TestF3DCachedTextures.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <vtkNew.h>
#include <vtkRenderer.h>

#include "vtkF3DCachedLUTTexture.h"
#include "vtkF3DCachedSpecularTexture.h"

int TestF3DCachedTextures(int argc, char* argv[])
{
vtkNew<vtkF3DCachedLUTTexture> lut;
vtkNew<vtkF3DCachedSpecularTexture> specular;

lut->Print(cout);
specular->Print(cout);

return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions library/VTKExtensions/Rendering/vtk.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DEPENDS
VTK::opengl
PRIVATE_DEPENDS
VTK::IOImage
VTK::IOXML
VTK::InteractionWidgets
VTK::RenderingCore
f3d::VTKExtensionsCore
Expand Down
68 changes: 68 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DCachedLUTTexture.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "vtkF3DCachedLUTTexture.h"

#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkTextureObject.h"
#include "vtkXMLImageDataReader.h"

#include <vtk_glew.h>

vtkStandardNewMacro(vtkF3DCachedLUTTexture);

//------------------------------------------------------------------------------
void vtkF3DCachedLUTTexture::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "FileName: " << this->FileName << endl;
}

//------------------------------------------------------------------------------
void vtkF3DCachedLUTTexture::Load(vtkRenderer* ren)
{
if (this->GetMTime() > this->LoadTime.GetMTime())
{
vtkOpenGLRenderWindow* renWin = vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());

if (this->TextureObject == nullptr)
{
this->TextureObject = vtkTextureObject::New();
}

this->TextureObject->SetContext(renWin);
this->TextureObject->SetFormat(GL_RG);
#ifdef GL_ES_VERSION_3_0
this->TextureObject->SetInternalFormat(GL_RG8);
this->TextureObject->SetDataType(GL_UNSIGNED_BYTE);
#else
this->TextureObject->SetInternalFormat(GL_RG16);
this->TextureObject->SetDataType(GL_UNSIGNED_SHORT);
#endif
this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
this->TextureObject->SetWrapT(vtkTextureObject::ClampToEdge);
this->TextureObject->SetMinificationFilter(vtkTextureObject::Linear);
this->TextureObject->SetMagnificationFilter(vtkTextureObject::Linear);

vtkNew<vtkXMLImageDataReader> reader;
reader->SetFileName(this->FileName.c_str());
reader->Update();

vtkImageData* img = reader->GetOutput();
int dims[3];
img->GetDimensions(dims);

#ifdef GL_ES_VERSION_3_0
this->TextureObject->Create2DFromRaw(
dims[0], dims[1], 2, VTK_UNSIGNED_CHAR, img->GetScalarPointer());
#else
this->TextureObject->Create2DFromRaw(
dims[0], dims[1], 2, VTK_UNSIGNED_SHORT, img->GetScalarPointer());
#endif

this->RenderWindow = renWin;
this->LoadTime.Modified();
}

this->TextureObject->Activate();
}
42 changes: 42 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DCachedLUTTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @class vtkF3DCachedLUTTexture
* @brief create a LUT texture from a vti file
*/

#ifndef vtkF3DCachedLUTTexture_h
#define vtkF3DCachedLUTTexture_h

#include "vtkPBRLUTTexture.h"

class vtkF3DCachedLUTTexture : public vtkPBRLUTTexture
{
public:
static vtkF3DCachedLUTTexture* New();
vtkTypeMacro(vtkF3DCachedLUTTexture, vtkPBRLUTTexture);
void PrintSelf(ostream& os, vtkIndent indent) override;

///@{
/**
* Set/Get the image file name.
*/
vtkGetMacro(FileName, std::string);
vtkSetMacro(FileName, std::string);
///@}

/**
* Implement base class method.
*/
void Load(vtkRenderer*) override;

protected:
vtkF3DCachedLUTTexture() = default;
~vtkF3DCachedLUTTexture() override = default;

std::string FileName;

private:
vtkF3DCachedLUTTexture(const vtkF3DCachedLUTTexture&) = delete;
void operator=(const vtkF3DCachedLUTTexture&) = delete;
};

#endif
87 changes: 87 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DCachedSpecularTexture.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "vtkF3DCachedSpecularTexture.h"

#include "vtkImageData.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkTextureObject.h"
#include "vtkXMLMultiBlockDataReader.h"

#include <vtk_glew.h>

vtkStandardNewMacro(vtkF3DCachedSpecularTexture);

//------------------------------------------------------------------------------
void vtkF3DCachedSpecularTexture::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "FileName: " << this->FileName << endl;
}

//------------------------------------------------------------------------------
void vtkF3DCachedSpecularTexture::Load(vtkRenderer* ren)
{
if (this->GetMTime() > this->LoadTime.GetMTime())
{
vtkOpenGLRenderWindow* renWin = vtkOpenGLRenderWindow::SafeDownCast(ren->GetRenderWindow());

if (this->TextureObject == nullptr)
{
this->TextureObject = vtkTextureObject::New();
}

this->TextureObject->SetContext(renWin);
this->TextureObject->SetFormat(GL_RGB);
this->TextureObject->SetInternalFormat(GL_RGB32F);
this->TextureObject->SetDataType(GL_FLOAT);
this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
this->TextureObject->SetWrapT(vtkTextureObject::ClampToEdge);
this->TextureObject->SetWrapR(vtkTextureObject::ClampToEdge);
this->TextureObject->SetMinificationFilter(vtkTextureObject::LinearMipmapLinear);
this->TextureObject->SetMagnificationFilter(vtkTextureObject::Linear);
this->TextureObject->SetGenerateMipmap(true);

this->RenderWindow = renWin;

vtkNew<vtkXMLMultiBlockDataReader> reader;
reader->SetFileName(this->FileName.c_str());
reader->Update();

vtkMultiBlockDataSet* mb = vtkMultiBlockDataSet::SafeDownCast(reader->GetOutput());

unsigned int nbLevels = mb->GetNumberOfBlocks();

this->TextureObject->SetMaxLevel(nbLevels - 1);

vtkImageData* firstImg = vtkImageData::SafeDownCast(mb->GetBlock(0));

void* data[6];
for (int i = 0; i < 6; i++)
{
data[i] = firstImg->GetScalarPointer(0, 0, i);
}

int* firstDims = firstImg->GetDimensions();
this->TextureObject->CreateCubeFromRaw(firstDims[0], firstDims[1], 3, VTK_FLOAT, data);

// the mip levels are manually uploaded because there is no abstraction in VTK
for (unsigned int i = 1; i < nbLevels; i++)
{
vtkImageData* img = vtkImageData::SafeDownCast(mb->GetBlock(i));
int* dims = img->GetDimensions();

for (int j = 0; j < 6; j++)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, i,
this->TextureObject->GetInternalFormat(VTK_FLOAT, 3, false), dims[0], dims[1], 0,
this->TextureObject->GetFormat(VTK_FLOAT, 3, false),
this->TextureObject->GetDataType(VTK_FLOAT), img->GetScalarPointer(0, 0, j));
}
}

this->LoadTime.Modified();
}

this->TextureObject->Activate();
}
39 changes: 39 additions & 0 deletions library/VTKExtensions/Rendering/vtkF3DCachedSpecularTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @class vtkF3DCachedSpecularTexture
* @brief create a prefiltered specular texture from a vtm file
*/

#ifndef vtkF3DCachedSpecularTexture_h
#define vtkF3DCachedSpecularTexture_h

#include "vtkPBRPrefilterTexture.h"

class vtkF3DCachedSpecularTexture : public vtkPBRPrefilterTexture
{
public:
static vtkF3DCachedSpecularTexture* New();
vtkTypeMacro(vtkF3DCachedSpecularTexture, vtkPBRPrefilterTexture);
void PrintSelf(ostream& os, vtkIndent indent) override;

/**
* Set the image file name.
*/
vtkSetMacro(FileName, std::string);

/**
* Implement base class method.
*/
void Load(vtkRenderer*) override;

protected:
vtkF3DCachedSpecularTexture() = default;
~vtkF3DCachedSpecularTexture() override = default;

std::string FileName;

private:
vtkF3DCachedSpecularTexture(const vtkF3DCachedSpecularTexture&) = delete;
void operator=(const vtkF3DCachedSpecularTexture&) = delete;
};

#endif

0 comments on commit 9fd7d43

Please sign in to comment.