Skip to content

Commit

Permalink
Merge pull request #1056 from dlyr/refactor-textures
Browse files Browse the repository at this point in the history
Refactor textures
  • Loading branch information
dlyr authored Oct 22, 2023
2 parents 5b6634e + 1434a1a commit 23b87ca
Show file tree
Hide file tree
Showing 50 changed files with 1,447 additions and 1,179 deletions.
1 change: 1 addition & 0 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ if(DOXYGEN_FOUND)
set(DOXYGEN_HAVE_DOT "YES")
set(DOXYGEN_HIDE_UNDOC_CLASSES "YES")
set(DOXYGEN_HIDE_UNDOC_RELATIONS "NO")
set(DOXYGEN_HTML_COLORSTYLE "LIGHT")
set(DOXYGEN_HTML_COLORSTYLE_GAMMA "160")
set(DOXYGEN_HTML_COLORSTYLE_HUE "40")
set(DOXYGEN_HTML_COLORSTYLE_SAT "50")
Expand Down
1 change: 1 addition & 0 deletions doc/developer.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Radium offers several cmake functions to configure and build your extension.
\page engine Engine

- \subpage develrendering
- \subpage develTextures
- \subpage develmaterials
- \subpage develLights
- \subpage develpicking
Expand Down
21 changes: 21 additions & 0 deletions doc/developer/material.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ public:

See the [Render technique management](#render-technique) for documentation on how to build such an helper function.

## Textures {#material-textures}

The MaterialTextureSet template ease a material's texture management.
First define an enum with the managed texture semantic you want to have, by convention declare this enum in Ra::Engine::Data::TextureSemantics, named as your Material class. e.g. for BlinnPhongMaterial:

\snippet Engine/Data/BlinnPhongMaterial.hpp TextureSemantics

Then to set a texture according to the semantic:

\snippet TexturedQuadDynamic/main.cpp Add texture to material

\see

- @ref develTextures
- Ra::Engine::Data::MaterialTextureSet
- Ra::Engine::Data::TextureManager
- Ra::Engine::Data::Texture
- Ra::Engine::Data::TextureParameters
- Ra::Engine::Data::SamplerParameters
- Ra::Engine::Data::ImageParameters

### Making a material editable {#editable-interface}

Material which implement the Ra::Engine::Data::ParameterSetEditionInterface might be modified at runtime.
Expand Down
21 changes: 21 additions & 0 deletions doc/developer/textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
\page develTextures Textures management
[TOC]

Texture data and management class.
First setup Ra::Engine::Data::TextureParameters.
Optionnaly load image with TextureManager::loadTextureImage
Create a Texture directly, or use TextureManager and get access to Texture by TextureManager::getTexture

Loading Texture to the gpu is done asynchronously, when needed, a texture register gpu task to the engine, to load or delete texture.
If you have an active GL context, and want immediate gpu texture upload or delete, call Texture::initializeNow()
or Texture::destroyNow()

\see

- @ref material-textures in Material
- Ra::Engine::Data::MaterialTextureSet
- Ra::Engine::Data::TextureManager
- Ra::Engine::Data::Texture
- Ra::Engine::Data::TextureParameters
- Ra::Engine::Data::SamplerParameters
- Ra::Engine::Data::ImageParameters
36 changes: 19 additions & 17 deletions examples/DrawPrimitives/AllPrimitivesComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,31 @@ void updateCellCorner( Vector3& cellCorner, const Scalar cellSize, const int nCe
void AllPrimitivesComponent::initialize() {
auto rp = Resources::getResourcesPath();

auto blinnPhongMaterial = make_shared<BlinnPhongMaterial>( "Shaded Material" );
blinnPhongMaterial->m_perVertexColor = true;
blinnPhongMaterial->m_ks = Color::White();
blinnPhongMaterial->m_ns = 100_ra;
auto blinnPhongMaterial = make_shared<BlinnPhongMaterial>( "Shaded Material" );
blinnPhongMaterial->setColoredByVertexAttrib( true );
blinnPhongMaterial->setSpecularColor( Color::White() );
blinnPhongMaterial->setSpecularExponent( 100_ra );

auto blinnPhongTexturedMaterial = make_shared<BlinnPhongMaterial>( "Shaded Textured Material" );
blinnPhongTexturedMaterial->m_perVertexColor = true;
blinnPhongTexturedMaterial->m_ks = Color::White();
blinnPhongTexturedMaterial->m_ns = 100_ra;
blinnPhongTexturedMaterial->setColoredByVertexAttrib( true );
blinnPhongTexturedMaterial->setSpecularColor( Color::White() );
blinnPhongTexturedMaterial->setSpecularExponent( 100_ra );

Ra::Engine::Data::TextureParameters textureParameters;
textureParameters.name = *rp + "/Examples/DrawPrimitives/Assets/grid.png";
textureParameters.wrapS = GL_REPEAT;
textureParameters.wrapT = GL_REPEAT;
textureParameters.minFilter = GL_LINEAR_MIPMAP_LINEAR;
textureParameters.name = "grid";
textureParameters.image =
TextureManager::loadTextureImage( *rp + "/Examples/DrawPrimitives/Assets/grid.png", true );
textureParameters.sampler.wrapS = GL_REPEAT;
textureParameters.sampler.wrapT = GL_REPEAT;
textureParameters.sampler.minFilter = GL_LINEAR_MIPMAP_LINEAR;
blinnPhongTexturedMaterial->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_DIFFUSE,
textureParameters );

auto plainMaterial = make_shared<PlainMaterial>( "Plain Material" );
plainMaterial->m_perVertexColor = true;
auto plainMaterial = make_shared<PlainMaterial>( "Plain Material" );
plainMaterial->setColoredByVertexAttrib( true );

auto lambertianMaterial = make_shared<LambertianMaterial>( "Lambertian Material" );
lambertianMaterial->m_perVertexColor = true;
auto lambertianMaterial = make_shared<LambertianMaterial>( "Lambertian Material" );
lambertianMaterial->setColoredByVertexAttrib( true );

//// setup ////
Scalar colorBoost = 1_ra; /// since simple primitive are ambient only, boost their color
Expand Down Expand Up @@ -725,8 +727,8 @@ void AllPrimitivesComponent::initialize() {
roMaterial.reset( mat );
}
else {
auto mat = new Data::BlinnPhongMaterial( "_DefaultBPMaterial" );
mat->m_renderAsSplat = mesh->getNumFaces() == 0;
auto mat = new Data::BlinnPhongMaterial( "_DefaultBPMaterial" );
mat->setRenderAsSplat( mesh->getNumFaces() == 0 );
roMaterial.reset( mat );
}

Expand Down
5 changes: 5 additions & 0 deletions examples/DrawPrimitives/minimalapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ void MinimalApp::frame() {
// to check the time from last frame.
const Scalar dt = 1.f / Scalar( m_targetFps );

m_viewer->processPicking();

// Collect and run tasks
m_engine->getTasks( m_taskQueue.get(), dt );
m_taskQueue->startTasks();
m_taskQueue->waitForTasks();
m_taskQueue->flushTaskQueue();

m_viewer->makeCurrent();
m_engine->runGpuTasks();
m_viewer->doneCurrent();
// Starts the renderer
m_viewer->startRendering( dt );

Expand Down
3 changes: 1 addition & 2 deletions examples/SimpleAnimation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class KeyFramedGeometryComponent : public Ra::Engine::Scene::TriangleMeshCompone
m_colorController.m_updater = [colors, material]( const Scalar& t ) {
auto C =
colors->at( t, Ra::Core::Animation::linearInterpolate<Ra::Core::Utils::Color> );
material->m_kd = C;
material->needUpdate();
material->setDiffuseColor( C );
};
//! [Attach the color KeyFrames to a controller]
//! [Creating the color KeyFrames]
Expand Down
40 changes: 19 additions & 21 deletions examples/TexturedQuad/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Include Radium base application and its simple Gui
#include "Engine/Data/BlinnPhongMaterial.hpp"
#include "Engine/Data/LambertianMaterial.hpp"
#include "Engine/Data/SimpleMaterial.hpp"
#include <Gui/BaseApplication.hpp>
#include <Gui/RadiumWindow/SimpleWindowFactory.hpp>

Expand All @@ -11,20 +14,17 @@
#include <Engine/Scene/GeometrySystem.hpp>

int main( int argc, char* argv[] ) {
//! [Creating the application]
Ra::Gui::BaseApplication app( argc, argv );
app.initialize( Ra::Gui::SimpleWindowFactory {} );
//! [Creating the application]

//! [Creating a quad geometry with texture coordinates]
auto quad = Ra::Core::Geometry::makeZNormalQuad( { 1_ra, 1_ra }, {}, true );
//! [Creating a quad geometry with texture coordinates]

//! [Creating a texture]
constexpr int width = 192;
constexpr int height = 512;
constexpr int size = width * height;
unsigned char data[size];
std::shared_ptr<unsigned char[]> data( new unsigned char[size] );

// fill with some function
for ( int i = 0; i < width; ++i ) {
for ( int j = 0; j < height; j++ ) {
Expand All @@ -33,32 +33,30 @@ int main( int argc, char* argv[] ) {
std::cos( j * i * M_PI / 96.0 ) ) );
}
}
auto& textureParameters =
app.m_engine->getTextureManager()->addTexture( "myTexture", width, height, data );

// these values will be used when engine initialize texture GL representation.
textureParameters.format = gl::GLenum::GL_RED;
textureParameters.internalFormat = gl::GLenum::GL_R8;
Ra::Engine::Data::TextureParameters textureParameters { "myTexture", {}, {} };
textureParameters.image.format = gl::GLenum::GL_RED;
textureParameters.image.internalFormat = gl::GLenum::GL_R8;
textureParameters.image.width = width;
textureParameters.image.height = height;
textureParameters.image.texels = data;

auto textureHandle = app.m_engine->getTextureManager()->addTexture( textureParameters );
//! [Creating a texture]

//! [Create an entity and component to draw or data]
auto e = app.m_engine->getEntityManager()->createEntity( "Textured quad" );

Ra::Core::Asset::BlinnPhongMaterialData matData( "myMaterialData" );
// remove glossy highlight
matData.m_specular = Ra::Core::Utils::Color::Black();
matData.m_hasSpecular = true;

matData.m_hasTexDiffuse = true;
// this name has to be the same as texManager added texture name
matData.m_texDiffuse = "myTexture";
auto material = std::make_shared<Ra::Engine::Data::LambertianMaterial>( "myMaterialData" );
material->addTexture( Ra::Engine::Data::TextureSemantics::LambertianMaterial::TEX_COLOR,
textureHandle );

// the entity get's this new component ownership. a bit wired since hidden in ctor.
new Ra::Engine::Scene::TriangleMeshComponent( "Quad Mesh", e, std::move( quad ), &matData );
// the entity get's this new component ownership. A bit wired since hidden in ctor.
new Ra::Engine::Scene::TriangleMeshComponent( "Quad Mesh", e, std::move( quad ), material );
//! [Create an entity and component to draw or data]

//! [Tell the window that something is to be displayed]
app.m_mainWindow->prepareDisplay();
//! [Tell the window that something is to be displayed]

return app.exec();
}
54 changes: 25 additions & 29 deletions examples/TexturedQuadDynamic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
#include <thread>

int main( int argc, char* argv[] ) {
//! [Creating the application]
Ra::Gui::BaseApplication app( argc, argv );
app.initialize( Ra::Gui::SimpleWindowFactory {} );
//! [Creating the application]

//! [Creating a quad geometry with texture coordinates]
auto quad = Ra::Core::Geometry::makeZNormalQuad( { 1_ra, 1_ra }, {}, true );
//! [Creating a quad geometry with texture coordinates]

//! [Creating a texture]
constexpr int width = 192;
constexpr int height = 512;
constexpr int size = width * height;
unsigned char data[size];
// make shared do not support array before c++ 20,
auto data = std::shared_ptr<unsigned char[]>( new unsigned char[size]() );
// fill with some function
for ( int i = 0; i < width; ++i ) {
for ( int j = 0; j < height; j++ ) {
Expand All @@ -38,38 +34,37 @@ int main( int argc, char* argv[] ) {
std::cos( j * i * M_PI / 96.0 ) ) );
}
}
auto& textureParameters =
app.m_engine->getTextureManager()->addTexture( "myTexture", width, height, data );
// these values will be used when engine initialize texture GL representation.
textureParameters.format = gl::GLenum::GL_RED;
textureParameters.internalFormat = gl::GLenum::GL_R8;
//! [Creating a texture]

//! [Create an entity and component to draw or data]
Ra::Engine::Data::TextureParameters textureParameters { "myTexture", {}, {} };
textureParameters.image.format = gl::GLenum::GL_RED;
textureParameters.image.internalFormat = gl::GLenum::GL_R8;
textureParameters.image.width = width;
textureParameters.image.height = height;
textureParameters.image.texels = data;
textureParameters.sampler.minFilter = gl::GLenum::GL_LINEAR_MIPMAP_LINEAR;

auto e = app.m_engine->getEntityManager()->createEntity( "Textured quad" );

Ra::Core::Asset::BlinnPhongMaterialData matData( "myMaterialData" );
// remove glossy highlight
matData.m_specular = Ra::Core::Utils::Color::Black();
matData.m_hasSpecular = true;
//! [Add texture to material]
auto textureManager = app.m_engine->getTextureManager();
auto textureHandle = textureManager->addTexture( textureParameters );
auto texture = textureManager->getTexture( textureHandle );
auto material = std::make_shared<Ra::Engine::Data::BlinnPhongMaterial>( "myMaterialData" );
material->addTexture( Ra::Engine::Data::TextureSemantics::BlinnPhongMaterial::TEX_DIFFUSE,
textureHandle );
//! [Add texture to material]

matData.m_hasTexDiffuse = true;
// this name has to be the same as texManager added texture name
matData.m_texDiffuse = "myTexture";
// remove glossy highlight
material->setSpecularColor( Ra::Core::Utils::Color::Black() );

// the entity get's this new component ownership. a bit wired since hidden in ctor.
new Ra::Engine::Scene::TriangleMeshComponent( "Quad Mesh", e, std::move( quad ), &matData );
new Ra::Engine::Scene::TriangleMeshComponent( "Quad Mesh", e, std::move( quad ), material );
//! [Create an entity and component to draw or data]

//! [Tell the window that something is to be displayed]
app.m_mainWindow->prepareDisplay();
//! [Tell the window that something is to be displayed]

auto viewer = app.m_mainWindow->getViewer();
viewer->makeCurrent();
auto texture = app.m_engine->getTextureManager()->getOrLoadTexture( textureParameters );
viewer->doneCurrent();

constexpr int nSec = 4;
// terminate the app after nSec second (approximatively). Camera can be moved using mouse moves.
auto thread = std::thread( [=, &app, &texture]() {
Expand All @@ -80,7 +75,8 @@ int main( int argc, char* argv[] ) {
const auto& endChrono = startChrono + ( iSec + 1 ) * std::chrono::milliseconds( 1000 );

while ( std::chrono::high_resolution_clock::now() < endChrono ) {
unsigned char newData[size];
auto newData = std::shared_ptr<unsigned char[]>( new unsigned char[size]() );

for ( int i = 0; i < width; ++i ) {
for ( int j = 0; j < height; j++ ) {
newData[( i * height + j )] =
Expand All @@ -103,13 +99,13 @@ int main( int argc, char* argv[] ) {
auto thread2 = std::thread( [=, &texture, &exitSignal]() {
auto future = exitSignal.get_future();
while ( future.wait_for( std::chrono::milliseconds( 1 ) ) == std::future_status::timeout ) {
unsigned char newData[size];
auto newData = std::shared_ptr<unsigned char[]>( new unsigned char[size]() );
for ( int i = 0; i < width; ++i ) {
for ( int j = 0; j < height; j++ ) {
newData[( i * height + j )] = ( i * 255 ) / width;
}
texture->updateData( newData );
}
texture->updateData( newData );
std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
}
} );
Expand Down
Loading

0 comments on commit 23b87ca

Please sign in to comment.