Improved implementation of crop in AppearanceView. Not yet fully

satisfying though...
This commit is contained in:
brunoherbelin
2020-11-29 23:33:53 +01:00
parent 5200de2e3e
commit 93ad971fc0
6 changed files with 71 additions and 32 deletions

View File

@@ -28,7 +28,7 @@ FrameBuffer::FrameBuffer(glm::vec3 resolution, bool useAlpha, bool multiSampling
{ {
attrib_.viewport = glm::ivec2(resolution); attrib_.viewport = glm::ivec2(resolution);
attrib_.clear_color = glm::vec4(0.f, 0.f, 0.f, use_alpha_ ? 0.f : 1.f); attrib_.clear_color = glm::vec4(0.f, 0.f, 0.f, use_alpha_ ? 0.f : 1.f);
crop(glm::vec2(1.f, 1.f)); setProjectionArea(glm::vec2(1.f, 1.f));
} }
FrameBuffer::FrameBuffer(uint width, uint height, bool useAlpha, bool multiSampling): FrameBuffer::FrameBuffer(uint width, uint height, bool useAlpha, bool multiSampling):
@@ -37,7 +37,7 @@ FrameBuffer::FrameBuffer(uint width, uint height, bool useAlpha, bool multiSampl
{ {
attrib_.viewport = glm::ivec2(width, height); attrib_.viewport = glm::ivec2(width, height);
attrib_.clear_color = glm::vec4(0.f, 0.f, 0.f, use_alpha_ ? 0.f : 1.f); attrib_.clear_color = glm::vec4(0.f, 0.f, 0.f, use_alpha_ ? 0.f : 1.f);
crop(glm::vec2(1.f, 1.f)); setProjectionArea(glm::vec2(1.f, 1.f));
} }
void FrameBuffer::init() void FrameBuffer::init()
@@ -258,7 +258,7 @@ glm::vec2 FrameBuffer::projectionArea() const
return projection_crop_; return projection_crop_;
} }
void FrameBuffer::crop(glm::vec2 c) void FrameBuffer::setProjectionArea(glm::vec2 c)
{ {
projection_crop_.x = CLAMP(c.x, 0.1f, 1.f); projection_crop_.x = CLAMP(c.x, 0.1f, 1.f);
projection_crop_.y = CLAMP(c.y, 0.1f, 1.f); projection_crop_.y = CLAMP(c.y, 0.1f, 1.f);

View File

@@ -45,7 +45,7 @@ public:
// projection and crop // projection and crop
glm::mat4 projection() const; glm::mat4 projection() const;
glm::vec2 projectionArea() const; glm::vec2 projectionArea() const;
void crop(glm::vec2 c); void setProjectionArea(glm::vec2 c);
// internal pixel format // internal pixel format
inline bool use_alpha() const { return use_alpha_; } inline bool use_alpha() const { return use_alpha_; }

View File

@@ -186,6 +186,7 @@ Source::Source() : initialized_(false), active_(true), need_update_(true), symbo
// - crop & repeat UV can be managed here // - crop & repeat UV can be managed here
// - additional custom shader can be associated // - additional custom shader can be associated
texturesurface_ = new Surface(renderingshader_); texturesurface_ = new Surface(renderingshader_);
crop_ = glm::vec2(1.f, 1.f);
// will be created at init // will be created at init
renderbuffer_ = nullptr; renderbuffer_ = nullptr;
@@ -444,14 +445,15 @@ void Source::update(float dt)
// Aspect Ratio correction transform : coordinates of Appearance Frame are scaled by render buffer width // Aspect Ratio correction transform : coordinates of Appearance Frame are scaled by render buffer width
glm::mat4 Ar = glm::identity<glm::mat4>(); glm::mat4 Ar = glm::identity<glm::mat4>();
if (renderbuffer_) if (renderbuffer_)
Ar = glm::scale(glm::identity<glm::mat4>(), glm::vec3(renderbuffer_->aspectRatio(), 1.f, 1.f) ); Ar = glm::scale(glm::identity<glm::mat4>(), glm::vec3(renderbuffer_->aspectRatio() * crop_.x, crop_.y, 1.f) );
// Translation : same as Appearance Frame (modified by Ar) // Translation : same as Appearance Frame (modified by Ar)
glm::mat4 Tra = glm::translate(glm::identity<glm::mat4>(), groups_[View::APPEARANCE]->translation_); glm::mat4 Tra = glm::translate(glm::identity<glm::mat4>(), groups_[View::APPEARANCE]->translation_);
// Scaling : inverse scaling (larger UV when smaller Appearance Frame) // Scaling : inverse scaling (larger UV when smaller Appearance Frame)
glm::mat4 Sca = glm::scale(glm::identity<glm::mat4>(), glm::vec3( 1.f / groups_[View::APPEARANCE]->scale_.x, glm::mat4 Sca = glm::scale(glm::identity<glm::mat4>(), glm::vec3( crop_.x / groups_[View::APPEARANCE]->scale_.x,
1.f / groups_[View::APPEARANCE]->scale_.y, 1.f)); crop_.y / groups_[View::APPEARANCE]->scale_.y, 1.f));
// Rotation : same angle than Appearance Frame, inverted axis // Rotation : same angle than Appearance Frame, inverted axis
glm::mat4 Rot = glm::rotate(glm::identity<glm::mat4>(), groups_[View::APPEARANCE]->rotation_.z, glm::vec3(0.f, 0.f, -1.f) ); glm::mat4 Rot = glm::rotate(glm::identity<glm::mat4>(), groups_[View::APPEARANCE]->rotation_.z, glm::vec3(0.f, 0.f, -1.f) );
// Combine transformations (non transitive) in this order: // Combine transformations (non transitive) in this order:
// 1. switch to Scene coordinate system // 1. switch to Scene coordinate system
// 2. Apply the aspect ratio correction // 2. Apply the aspect ratio correction
@@ -462,12 +464,12 @@ void Source::update(float dt)
// 7. switch back to UV coordinate system // 7. switch back to UV coordinate system
texturesurface_->shader()->iTransform = glm::inverse(UVtoScene) * Sca * glm::inverse(Ar) * Rot * Tra * Ar * UVtoScene; texturesurface_->shader()->iTransform = glm::inverse(UVtoScene) * Sca * glm::inverse(Ar) * Rot * Tra * Ar * UVtoScene;
// MODIFY CROP // // MODIFY CROP
if (renderbuffer_) { // if (renderbuffer_) {
glm::vec2 crop = renderbuffer_->projectionArea(); // glm::vec2 crop = renderbuffer_->projectionArea();
groups_[View::MIXING]->scale_.x *= crop.x / crop.y; // groups_[View::MIXING]->scale_.x *= crop.x / crop.y;
groups_[View::LAYER]->scale_.x = crop.x / crop.y; // groups_[View::LAYER]->scale_.x = crop.x / crop.y;
} // }
need_update_ = false; need_update_ = false;
} }

View File

@@ -171,6 +171,7 @@ protected:
// surface to draw on // surface to draw on
Surface *texturesurface_; Surface *texturesurface_;
glm::vec2 crop_;
// mode for display // mode for display
Mode mode_; Mode mode_;

View File

@@ -1243,6 +1243,7 @@ View::Cursor GeometryView::grab (Source *s, glm::vec2 from, glm::vec2 to, std::p
} }
void GeometryView::terminate() void GeometryView::terminate()
{ {
View::terminate(); View::terminate();
@@ -1804,9 +1805,8 @@ AppearanceView::AppearanceView() : View(APPEARANCE), edit_source_(nullptr), need
// surface showing the source transparency background // surface showing the source transparency background
backgroundpreview = new ImageSurface("images/checker.dds"); backgroundpreview = new ImageSurface("images/checker.dds");
glm::mat4 Tra = glm::translate(glm::identity<glm::mat4>(), glm::vec3( -32.f, -32.f, 0.f)); static glm::mat4 Tra = glm::scale(glm::translate(glm::identity<glm::mat4>(), glm::vec3( -32.f, -32.f, 0.f)), glm::vec3( 64.f, 64.f, 1.f));
glm::mat4 Sca = glm::scale(glm::identity<glm::mat4>(), glm::vec3( 64.f, 64.f, 1.f)); backgroundpreview->shader()->iTransform = Tra;
backgroundpreview->shader()->iTransform = Tra * Sca;
backgroundpreview->translation_.z = 0.001f; backgroundpreview->translation_.z = 0.001f;
scene.bg()->attach(backgroundpreview); scene.bg()->attach(backgroundpreview);
// surface to show the texture of the source // surface to show the texture of the source
@@ -2033,7 +2033,12 @@ void AppearanceView::adjustBackground()
// update rendering frame to match edit source AR // update rendering frame to match edit source AR
image_original_width = edit_source_->frame()->aspectRatio(); image_original_width = edit_source_->frame()->aspectRatio();
surfacepreview->setTextureIndex( edit_source_->frame()->texture() ); surfacepreview->setTextureIndex( edit_source_->frame()->texture() );
image_crop_area = edit_source_->frame()->projectionArea();
// image_crop_area = edit_source_->frame()->projectionArea();
image_crop_area = edit_source_->crop_;
surfacepreview->scale_.x = image_original_width;
image_crop_area.x *= image_original_width; image_crop_area.x *= image_original_width;
} }
@@ -2045,19 +2050,19 @@ void AppearanceView::adjustBackground()
backgroundframe_->scale_.x = image_original_width; backgroundframe_->scale_.x = image_original_width;
// background of preview // background of preview
surfacepreview->scale_ = glm::vec3(image_crop_area, 1.f); // surfacepreview->scale_ = glm::vec3(image_crop_area, 1.f);
backgroundpreview->scale_ = glm::vec3(image_crop_area, 1.f); backgroundpreview->scale_ = glm::vec3(image_crop_area, 1.f);
backgroundpreview->update(0); backgroundpreview->update(0);
glm::mat4 Ar = glm::scale(glm::identity<glm::mat4>(), glm::vec3(image_crop_area, 1.f) ); glm::mat4 Ar = glm::scale(glm::identity<glm::mat4>(), glm::vec3(image_crop_area, 1.f) );
glm::mat4 Tra = glm::translate(glm::identity<glm::mat4>(), glm::vec3( -32.f, -32.f, 0.f)); static glm::mat4 Tra = glm::scale(glm::translate(glm::identity<glm::mat4>(), glm::vec3( -32.f, -32.f, 0.f)), glm::vec3( 64.f, 64.f, 1.f));
glm::mat4 Sca = glm::scale(glm::identity<glm::mat4>(), glm::vec3( 64.f, 64.f, 1.f)); backgroundpreview->shader()->iTransform = Ar * Tra;
backgroundpreview->shader()->iTransform = Ar * Tra * Sca ;
// foreground // foreground
crop_horizontal_->translation_.x = image_crop_area.x; crop_horizontal_->translation_.x = image_crop_area.x;
crop_vertical_->translation_.y = -image_crop_area.y; crop_vertical_->translation_.y = -image_crop_area.y;
crop_vertical_->translation_.x = -image_original_width - 0.1f; crop_vertical_->translation_.x = -image_original_width - 0.1f;
foregroundframe_->scale_ = glm::vec3(image_crop_area, 1.f); foregroundframe_->scale_ = glm::vec3(image_crop_area, 1.f);
} }
Source *AppearanceView::getEditOrCurrentSource() Source *AppearanceView::getEditOrCurrentSource()
@@ -2126,36 +2131,54 @@ View::Cursor AppearanceView::grab (Source *s, glm::vec2 from, glm::vec2 to, std:
glm::vec3 scene_to = Rendering::manager().unProject(to, scene.root()->transform_); glm::vec3 scene_to = Rendering::manager().unProject(to, scene.root()->transform_);
glm::vec3 scene_translation = scene_to - scene_from; glm::vec3 scene_translation = scene_to - scene_from;
// work on the given source // Not grabbing a source
if (!s) { if (!s) {
// work on the edited source
if ( edit_source_ != nullptr ) { if ( edit_source_ != nullptr ) {
// make sure matrix transform of stored status is updated // make sure matrix transform of stored status is updated
edit_source_->stored_status_->update(0); // Group *sourceNode = edit_source_->group(View::GEOMETRY); // groups_[View::GEOMETRY]
// edit_source_status_.update(0);
// picking on the resizing handles in the corners // picking on the resizing handles in the corners
if ( pick.first == crop_horizontal_ ) { if ( pick.first == crop_horizontal_ ) {
// crop horizontally // crop horizontally
glm::vec2 cropped = edit_source_->frame()->projectionArea(); // glm::vec2 cropped = edit_source_->frame()->projectionArea();
// float max_width = edit_source_->frame()->aspectRatio();
// cropped.x = CLAMP(scene_to.x, 0.2f, max_width) / max_width;
// edit_source_->frame()->setProjectionArea(cropped);
float max_width = edit_source_->frame()->aspectRatio(); float max_width = edit_source_->frame()->aspectRatio();
cropped.x = CLAMP(scene_to.x, 0.2f, max_width) / max_width; edit_source_->crop_.x = CLAMP(scene_to.x, 0.2f, max_width) / max_width;
edit_source_->frame()->crop(cropped);
// TODO scale GEOMETRY and RENDER groups // TODO scale GEOMETRY and RENDER groups
edit_source_->texturesurface_->scale_.x = edit_source_->crop_.x;
edit_source_->texturesurface_->update(0);
// edit_source_->group(View::GEOMETRY)->scale_ = edit_source_status_.scale_ * glm::vec3(cropped, 1.f);
edit_source_->touch(); edit_source_->touch();
// update background and frame // update background and frame
adjustBackground(); adjustBackground();
// cursor indication // cursor indication
ret.type = Cursor_ResizeEW; ret.type = Cursor_ResizeEW;
} }
if ( pick.first == crop_vertical_ ) { if ( pick.first == crop_vertical_ ) {
// crop vertically
glm::vec2 cropped = edit_source_->frame()->projectionArea(); // // crop vertically
cropped.y = -1.f * CLAMP(scene_to.y, -1.f, -0.2f); // glm::vec2 cropped = edit_source_->frame()->projectionArea();
edit_source_->frame()->crop(cropped); // cropped.y = -1.f * CLAMP(scene_to.y, -1.f, -0.2f);
// edit_source_->frame()->setProjectionArea(cropped);
edit_source_->crop_.y = -1.f * CLAMP(scene_to.y, -1.f, -0.2f);
// TODO scale GEOMETRY and RENDER groups // TODO scale GEOMETRY and RENDER groups
edit_source_->texturesurface_->scale_.y = edit_source_->crop_.y;
edit_source_->texturesurface_->update(0);
edit_source_->touch(); edit_source_->touch();
// update background and frame // update background and frame
adjustBackground(); adjustBackground();
// cursor indication // cursor indication
@@ -2166,7 +2189,7 @@ View::Cursor AppearanceView::grab (Source *s, glm::vec2 from, glm::vec2 to, std:
return ret; return ret;
} }
Group *sourceNode = s->group(mode_); // groups_[View::GEOMETRY] Group *sourceNode = s->group(mode_); // groups_[View::APPEARANCE]
// make sure matrix transform of stored status is updated // make sure matrix transform of stored status is updated
s->stored_status_->update(0); s->stored_status_->update(0);
@@ -2454,6 +2477,17 @@ View::Cursor AppearanceView::grab (Source *s, glm::vec2 from, glm::vec2 to, std:
} }
//void AppearanceView::initiate()
//{
// View::initiate();
//Log::Info("AppearanceView::initiate") ;
// if ( edit_source_ != nullptr ) {
// Log::Info("store edit_source_status_") ;
// edit_source_status_.copyTransform(edit_source_->group(View::GEOMETRY));
// }
//}
void AppearanceView::terminate() void AppearanceView::terminate()
{ {
View::terminate(); View::terminate();

2
View.h
View File

@@ -239,11 +239,13 @@ public:
std::pair<Node *, glm::vec2> pick(glm::vec2 P) override; std::pair<Node *, glm::vec2> pick(glm::vec2 P) override;
Cursor grab (Source *s, glm::vec2 from, glm::vec2 to, std::pair<Node *, glm::vec2> pick) override; Cursor grab (Source *s, glm::vec2 from, glm::vec2 to, std::pair<Node *, glm::vec2> pick) override;
Cursor drag (glm::vec2, glm::vec2) override; Cursor drag (glm::vec2, glm::vec2) override;
// void initiate() override;
void terminate() override; void terminate() override;
private: private:
Source *edit_source_; Source *edit_source_;
Group edit_source_status_;
bool need_edit_update_; bool need_edit_update_;
Source *getEditOrCurrentSource(); Source *getEditOrCurrentSource();