Skip to content

Commit

Permalink
Color layout in renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Mar 22, 2024
1 parent bfedc1d commit 6307d0b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/core/include/core/chemdb/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace VTX::Core::ChemDB::Color
{
using ColorLayout = std::array<Util::Color::Rgba, 256>;
using ColorLayout = std::vector<Util::Color::Rgba>;

constexpr int LAYOUT_OFFSET_ATOMS = 0;
constexpr int LAYOUT_OFFSET_RESIDUES = 130;
Expand Down
6 changes: 3 additions & 3 deletions lib/renderer/bench/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ int main( int, char ** )
}
else if ( p_key == SDL_SCANCODE_F5 )
{
static VTX::Core::ChemDB::Color::ColorLayout colorLayout;
VTX::Core::ChemDB::Color::ColorLayout colorLayout( 255 );
std::generate( colorLayout.begin(), colorLayout.end(), [] { return Color::Rgba::random(); } );
renderer.setProxyColorLayout( colorLayout );
scene.setColorLayout( colorLayout );
}
};

Expand All @@ -129,7 +129,7 @@ int main( int, char ** )
auto proxyVoxels = Renderer::Proxy::Voxels { &mins, &maxs };
renderer.setProxyVoxels( proxyVoxels );

renderer.setProxyColorLayout( ChemDB::Color::COLOR_LAYOUT_JMOL );
renderer.setProxyColorLayout( scene.getProxyColorLayout() );

Renderer::Proxy::Representation representation1, representation2, representation3;

Expand Down
35 changes: 26 additions & 9 deletions lib/renderer/bench/src/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __VTX_BENCH_SCENE__

#include "util.hpp"
#include <core/chemdb/color.hpp>
#include <core/struct/molecule.hpp>
#include <renderer/proxy/molecule.hpp>
#include <renderer/renderer.hpp>
Expand All @@ -14,6 +15,12 @@ namespace VTX::Bench
class Scene
{
public:
Scene()
{
_colorLayout = Core::ChemDB::Color::COLOR_LAYOUT_JMOL;
_proxyLayoutColor.colors = &_colorLayout;
}

Renderer::Proxy::Molecule & addMolecule( const std::string & p_name )
{
using namespace Util;
Expand All @@ -30,33 +37,33 @@ namespace VTX::Bench
_molecules.back()->transform
= Math::translate( _molecules.back()->transform, Math::randomVec3f() * 200.f - 100.f );
IO::Util::SecondaryStructure::computeStride( *_molecules.back() );
_proxies.emplace_back( _proxify( *_molecules.back() ) );
_proxyMolecules.emplace_back( _proxify( *_molecules.back() ) );
_directions.emplace_back( Math::randomVec3f() * 2.f - 1.f );

return *_proxies.back();
return *_proxyMolecules.back();
};

void removeMolecule( const size_t p_index )
{
_proxies[ p_index ]->onRemove();
_proxyMolecules[ p_index ]->onRemove();
_molecules.erase( _molecules.begin() + p_index );
_proxies.erase( _proxies.begin() + p_index );
_proxyMolecules.erase( _proxyMolecules.begin() + p_index );
_directions.erase( _directions.begin() + p_index );
}

// TODO: remove renderer from here.
void removeAllMolecules( Renderer::Renderer * const p_renderer )
{
std::vector<Renderer::Proxy::Molecule *> proxies;
for ( auto & proxy : _proxies )
for ( auto & proxy : _proxyMolecules )
{
proxies.push_back( proxy.get() );
}

p_renderer->removeProxyMolecules( proxies );

_molecules.clear();
_proxies.clear();
_proxyMolecules.clear();
_directions.clear();
}

Expand All @@ -71,23 +78,33 @@ namespace VTX::Bench
for ( auto & molecule : _molecules )
{
molecule->transform = Util::Math::rotate( molecule->transform, p_deltaTime, _directions[ i ] );
_proxies[ i++ ]->onTransform();
_proxyMolecules[ i++ ]->onTransform();
}
}

inline const std::vector<std::unique_ptr<Core::Struct::Molecule>> & getMolecules() const { return _molecules; }
inline const std::vector<std::unique_ptr<Renderer::Proxy::Molecule>> & getProxiesMolecules() const
{
return _proxies;
return _proxyMolecules;
}
inline const Core::ChemDB::Color::ColorLayout & getColorLayout() const { return _colorLayout; }
inline void setColorLayout( const Core::ChemDB::Color::ColorLayout & p_colorLayout )
{
_colorLayout = p_colorLayout;
_proxyLayoutColor.onChange();
}
inline Renderer::Proxy::ColorLayout & getProxyColorLayout() { return _proxyLayoutColor; }

bool isUpdate = false;

private:
std::vector<std::unique_ptr<Core::Struct::Molecule>> _molecules;
std::vector<std::unique_ptr<Renderer::Proxy::Molecule>> _proxies;
std::vector<std::unique_ptr<Renderer::Proxy::Molecule>> _proxyMolecules;
std::vector<Vec3f> _directions;

Core::ChemDB::Color::ColorLayout _colorLayout;
Renderer::Proxy::ColorLayout _proxyLayoutColor;

std::unique_ptr<Renderer::Proxy::Molecule> _proxify( const Core::Struct::Molecule & p_molecule )
{
const size_t sizeAtoms = p_molecule.trajectory.frames.front().size();
Expand Down
14 changes: 7 additions & 7 deletions lib/renderer/bench/src/user_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace VTX::Bench
ImGui::NewFrame();

// Menu bar.
_drawMenuBar( p_camera, p_renderer );
_drawMenuBar( p_camera, p_renderer, p_scene );

if ( _drawUi )
{
Expand Down Expand Up @@ -160,7 +160,7 @@ namespace VTX::Bench
bool _vsync = true;
bool _drawUi = true;

void _drawMenuBar( Camera * const p_camera, Renderer::Renderer * const p_renderer )
void _drawMenuBar( Camera * const p_camera, Renderer::Renderer * const p_renderer, Scene * const p_scene )
{
if ( ImGui::BeginMainMenuBar() )
{
Expand Down Expand Up @@ -199,23 +199,23 @@ namespace VTX::Bench
{
if ( ImGui::MenuItem( "JMol" ) )
{
p_renderer->setProxyColorLayout( VTX::Core::ChemDB::Color::COLOR_LAYOUT_JMOL );
p_scene->setColorLayout( VTX::Core::ChemDB::Color::COLOR_LAYOUT_JMOL );
}
if ( ImGui::MenuItem( "Random" ) )
{
VTX::Core::ChemDB::Color::ColorLayout colorLayout;
VTX::Core::ChemDB::Color::ColorLayout colorLayout( 255 );
std::generate(
colorLayout.begin(), colorLayout.end(), [] { return Util::Color::Rgba::random(); }
);
p_renderer->setProxyColorLayout( colorLayout );
p_scene->setColorLayout( colorLayout );
}
if ( ImGui::MenuItem( "Random pastel" ) )
{
VTX::Core::ChemDB::Color::ColorLayout colorLayout;
VTX::Core::ChemDB::Color::ColorLayout colorLayout( 255 );
std::generate(
colorLayout.begin(), colorLayout.end(), [] { return Util::Color::Rgba::randomPastel(); }
);
p_renderer->setProxyColorLayout( colorLayout );
p_scene->setColorLayout( colorLayout );
}

ImGui::EndMenu();
Expand Down
15 changes: 9 additions & 6 deletions lib/renderer/src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace VTX::Renderer
[ this, &p_proxy ]( const bool p_perspective ) { setUniform( p_perspective, "Is perspective" ); };
}

#pragma region Molecules
#pragma region Proxy molecules

void Renderer::addProxyMolecule( Proxy::Molecule & p_proxy )
{
Expand Down Expand Up @@ -242,7 +242,7 @@ namespace VTX::Renderer
_cacheRibbons.erase( &p_proxy );
}

#pragma endregion Molecules
#pragma endregion Proxy molecules

// void Renderer::addProxyMeshes( Proxy::Mesh & p_proxy ) {}

Expand All @@ -251,11 +251,14 @@ namespace VTX::Renderer
assert( hasContext() );

_proxyColorLayout = &p_proxy;
_context->setUniforms<Util::Color::Rgba>(
std::vector<Util::Color::Rgba>( p_proxy.begin(), p_proxy.end() ), "Color layout"
);

_context->setUniforms<Util::Color::Rgba>( *p_proxy.colors, "Color layout" );
setNeedUpdate( true );

p_proxy.onChange += [ this, &p_proxy ]()
{
_context->setUniforms<Util::Color::Rgba>( *p_proxy.colors, "Color layout" );
setNeedUpdate( true );
};
}

void Renderer::setProxyRepresentations( Proxy::Representations & p_proxy )
Expand Down

0 comments on commit 6307d0b

Please sign in to comment.