mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-05 23:40:02 +01:00
Using translation, rotation and scale vectors to define transform of
node (matrix computed at update). GUI to modify transform adapted accordingly. Initialization nodes done at first run of draw (prevents mistake of forgetting to init).
This commit is contained in:
@@ -31,31 +31,35 @@ void ImGuiVisitor::visit(Group &n)
|
|||||||
std::string id = std::to_string(n.id());
|
std::string id = std::to_string(n.id());
|
||||||
if (ImGui::TreeNode(id.c_str(), "Group"))
|
if (ImGui::TreeNode(id.c_str(), "Group"))
|
||||||
{
|
{
|
||||||
|
// MODEL VIEW
|
||||||
|
if (ImGuiToolkit::ButtonIcon(6, 15)) {
|
||||||
|
n.translation_.x = 0.f;
|
||||||
|
n.translation_.y = 0.f;
|
||||||
|
}
|
||||||
|
ImGui::SameLine(0, 10);
|
||||||
|
float translation[2] = { n.translation_.x, n.translation_.y};
|
||||||
|
if ( ImGui::SliderFloat2("position", translation, -5.0, 5.0) )
|
||||||
|
{
|
||||||
|
n.translation_.x = translation[0];
|
||||||
|
n.translation_.y = translation[1];
|
||||||
|
}
|
||||||
|
|
||||||
// n.transform_
|
if (ImGuiToolkit::ButtonIcon(4, 15))
|
||||||
// MODEL VIEW
|
n.rotation_.z = 0.f;
|
||||||
float translation[2] = { n.transform_[3][0], n.transform_[3][1]};
|
ImGui::SameLine(0, 10);
|
||||||
if (ImGuiToolkit::ButtonIcon(6, 15)) {
|
ImGui::SliderAngle("slider angle", &(n.rotation_.z), -180.f, 180.f) ;
|
||||||
translation[0] = 0.f;
|
|
||||||
translation[1] = 0.f;
|
|
||||||
}
|
|
||||||
ImGui::SameLine(0, 10);
|
|
||||||
ImGui::SliderFloat2("position", translation, -5.0, 5.0);
|
|
||||||
|
|
||||||
float rotation = asin(n.transform_[0][1]);
|
if (ImGuiToolkit::ButtonIcon(3, 15)) {
|
||||||
if (ImGuiToolkit::ButtonIcon(4, 15)) rotation = 0.f;
|
n.scale_.x = 1.f;
|
||||||
ImGui::SameLine(0, 10);
|
n.scale_.y = 1.f;
|
||||||
ImGui::SliderFloat("rotation", &rotation, 0, glm::two_pi<float>());
|
}
|
||||||
|
ImGui::SameLine(0, 10);
|
||||||
float scale = n.transform_[2][2];
|
float scale = n.scale_.y;
|
||||||
if (ImGuiToolkit::ButtonIcon(3, 15)) scale = 1.f;
|
if ( ImGui::SliderFloat("scale", &scale, 0.1f, 10.f, "%.3f", 3.f) )
|
||||||
ImGui::SameLine(0, 10);
|
{
|
||||||
ImGui::SliderFloat("scale", &scale, 0.1f, 10.f, "%.3f", 3.f);
|
n.scale_.x = scale;
|
||||||
|
n.scale_.y = scale;
|
||||||
glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), glm::vec3(translation[0], translation[1], 0.f));
|
}
|
||||||
View = glm::rotate(View, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
|
|
||||||
glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), glm::vec3(scale, scale, scale));
|
|
||||||
n.transform_ = View * Model;
|
|
||||||
|
|
||||||
// loop over members of a group
|
// loop over members of a group
|
||||||
for (int i = 0; i < n.numChildren(); ++i)
|
for (int i = 0; i < n.numChildren(); ++i)
|
||||||
@@ -77,8 +81,10 @@ void ImGuiVisitor::visit(Primitive &n)
|
|||||||
ImGui::PushID(n.id());
|
ImGui::PushID(n.id());
|
||||||
ImGui::Text("Primitive");
|
ImGui::Text("Primitive");
|
||||||
|
|
||||||
|
if (n.getShader())
|
||||||
|
n.getShader()->accept(*this);
|
||||||
|
|
||||||
|
|
||||||
n.getShader()->accept(*this);
|
|
||||||
ImGui::PopID();
|
ImGui::PopID();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +96,9 @@ void ImGuiVisitor::visit(ImageSurface &n)
|
|||||||
void ImGuiVisitor::visit(MediaSurface &n)
|
void ImGuiVisitor::visit(MediaSurface &n)
|
||||||
{
|
{
|
||||||
ImGui::Text("%s", n.getFilename().c_str());
|
ImGui::Text("%s", n.getFilename().c_str());
|
||||||
n.getMediaPlayer()->accept(*this);
|
|
||||||
|
if (n.getMediaPlayer())
|
||||||
|
n.getMediaPlayer()->accept(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImGuiVisitor::visit(MediaPlayer &n)
|
void ImGuiVisitor::visit(MediaPlayer &n)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ void ImageSurface::init()
|
|||||||
else {
|
else {
|
||||||
float ar = 1.0;
|
float ar = 1.0;
|
||||||
textureindex_ = Resource::getTextureImage(filename_, &ar);
|
textureindex_ = Resource::getTextureImage(filename_, &ar);
|
||||||
transform_ = glm::scale(glm::identity<glm::mat4>(), glm::vec3(ar, 1.f, 1.f));
|
scale_.x = ar;
|
||||||
}
|
}
|
||||||
// create shader for textured image
|
// create shader for textured image
|
||||||
shader_ = new ImageShader();
|
shader_ = new ImageShader();
|
||||||
@@ -74,6 +74,9 @@ void ImageSurface::init()
|
|||||||
|
|
||||||
void ImageSurface::draw(glm::mat4 modelview, glm::mat4 projection)
|
void ImageSurface::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, textureindex_);
|
glBindTexture(GL_TEXTURE_2D, textureindex_);
|
||||||
|
|
||||||
Primitive::draw(modelview, projection);
|
Primitive::draw(modelview, projection);
|
||||||
@@ -111,6 +114,9 @@ void MediaSurface::init()
|
|||||||
|
|
||||||
void MediaSurface::draw(glm::mat4 modelview, glm::mat4 projection)
|
void MediaSurface::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
if ( mediaplayer_->isOpen() )
|
if ( mediaplayer_->isOpen() )
|
||||||
mediaplayer_->bind();
|
mediaplayer_->bind();
|
||||||
else
|
else
|
||||||
@@ -126,7 +132,7 @@ void MediaSurface::update( float dt )
|
|||||||
if ( mediaplayer_->isOpen() )
|
if ( mediaplayer_->isOpen() )
|
||||||
mediaplayer_->update();
|
mediaplayer_->update();
|
||||||
|
|
||||||
transform_ = glm::scale(glm::identity<glm::mat4>(), glm::vec3(mediaplayer_->aspectRatio(), 1.f, 1.f));
|
scale_.x = mediaplayer_->aspectRatio();
|
||||||
|
|
||||||
Primitive::update( dt );
|
Primitive::update( dt );
|
||||||
}
|
}
|
||||||
@@ -146,14 +152,21 @@ LineStrip::LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewi
|
|||||||
indices_.push_back ( i );
|
indices_.push_back ( i );
|
||||||
}
|
}
|
||||||
|
|
||||||
shader_ = new Shader();
|
|
||||||
|
|
||||||
drawingPrimitive_ = GL_LINE_LOOP;
|
drawingPrimitive_ = GL_LINE_LOOP;
|
||||||
linewidth_ = linewidth;
|
linewidth_ = linewidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LineStrip::init()
|
||||||
|
{
|
||||||
|
Primitive::init();
|
||||||
|
shader_ = new Shader();
|
||||||
|
}
|
||||||
|
|
||||||
void LineStrip::draw(glm::mat4 modelview, glm::mat4 projection)
|
void LineStrip::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
glLineWidth(linewidth_);
|
glLineWidth(linewidth_);
|
||||||
Primitive::draw(modelview, projection);
|
Primitive::draw(modelview, projection);
|
||||||
}
|
}
|
||||||
@@ -201,7 +214,10 @@ void LineCircle::init()
|
|||||||
// 3. vao_ will not be deleted because deleteGLBuffers_() is empty
|
// 3. vao_ will not be deleted because deleteGLBuffers_() is empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shader_ = new Shader();
|
||||||
|
|
||||||
visible_ = true;
|
visible_ = true;
|
||||||
|
initialized_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineCircle::accept(Visitor& v)
|
void LineCircle::accept(Visitor& v)
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ class LineStrip : public Primitive {
|
|||||||
public:
|
public:
|
||||||
LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewidth = 1);
|
LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewidth = 1);
|
||||||
|
|
||||||
|
virtual void init() override;
|
||||||
virtual void draw(glm::mat4 modelview, glm::mat4 projection) override;
|
virtual void draw(glm::mat4 modelview, glm::mat4 projection) override;
|
||||||
virtual void accept(Visitor& v) override;
|
virtual void accept(Visitor& v) override;
|
||||||
|
|
||||||
|
|||||||
24
Scene.cpp
24
Scene.cpp
@@ -17,6 +17,17 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
|
|
||||||
|
glm::mat4 transform(glm::vec3 translation, glm::vec3 rotation, glm::vec3 scale)
|
||||||
|
{
|
||||||
|
glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), translation);
|
||||||
|
View = glm::rotate(View, rotation.x, glm::vec3(1.f, 0.f, 0.f));
|
||||||
|
View = glm::rotate(View, rotation.y, glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
View = glm::rotate(View, rotation.z, glm::vec3(0.f, 0.f, 1.f));
|
||||||
|
glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), scale);
|
||||||
|
return View * Model;
|
||||||
|
}
|
||||||
|
|
||||||
// Node
|
// Node
|
||||||
Node::Node() : parent_(nullptr), visible_(false), initialized_(false)
|
Node::Node() : parent_(nullptr), visible_(false), initialized_(false)
|
||||||
{
|
{
|
||||||
@@ -27,10 +38,14 @@ Node::Node() : parent_(nullptr), visible_(false), initialized_(false)
|
|||||||
worldToLocal_ = glm::identity<glm::mat4>();
|
worldToLocal_ = glm::identity<glm::mat4>();
|
||||||
localToWorld_ = glm::identity<glm::mat4>();
|
localToWorld_ = glm::identity<glm::mat4>();
|
||||||
transform_ = glm::identity<glm::mat4>();
|
transform_ = glm::identity<glm::mat4>();
|
||||||
|
scale_ = glm::vec3(1.f);
|
||||||
|
rotation_ = glm::vec3(0.f);
|
||||||
|
translation_ = glm::vec3(0.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::update( float dt )
|
void Node::update( float dt )
|
||||||
{
|
{
|
||||||
|
transform_ = transform(translation_, rotation_, scale_);
|
||||||
if ( parent_ ) {
|
if ( parent_ ) {
|
||||||
localToWorld_ = dynamic_cast<Group*>(parent_)->getLocalToWorldMatrix() * transform_;
|
localToWorld_ = dynamic_cast<Group*>(parent_)->getLocalToWorldMatrix() * transform_;
|
||||||
worldToLocal_ = glm::inverse(transform_) * dynamic_cast<Group*>(parent_)->getWorldToLocalMatrix();
|
worldToLocal_ = glm::inverse(transform_) * dynamic_cast<Group*>(parent_)->getWorldToLocalMatrix();
|
||||||
@@ -114,6 +129,9 @@ void Primitive::init()
|
|||||||
|
|
||||||
void Primitive::draw(glm::mat4 modelview, glm::mat4 projection)
|
void Primitive::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
if ( visible_ ) {
|
if ( visible_ ) {
|
||||||
//
|
//
|
||||||
// prepare and use shader
|
// prepare and use shader
|
||||||
@@ -171,6 +189,9 @@ void Group::update( float dt )
|
|||||||
|
|
||||||
void Group::draw(glm::mat4 modelview, glm::mat4 projection)
|
void Group::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
if ( visible_ ) {
|
if ( visible_ ) {
|
||||||
|
|
||||||
// append the instance transform to the ctm
|
// append the instance transform to the ctm
|
||||||
@@ -220,6 +241,9 @@ void Switch::update( float dt )
|
|||||||
|
|
||||||
void Switch::draw(glm::mat4 modelview, glm::mat4 projection)
|
void Switch::draw(glm::mat4 modelview, glm::mat4 projection)
|
||||||
{
|
{
|
||||||
|
if ( !initialized_ )
|
||||||
|
init();
|
||||||
|
|
||||||
if ( visible_ ) {
|
if ( visible_ ) {
|
||||||
// append the instance transform to the ctm
|
// append the instance transform to the ctm
|
||||||
glm::mat4 ctm = modelview * transform_;
|
glm::mat4 ctm = modelview * transform_;
|
||||||
|
|||||||
2
Scene.h
2
Scene.h
@@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
glm::mat4 transform(glm::vec3 translation, glm::vec3 rotation, glm::vec3 scale);
|
||||||
|
|
||||||
// Forward declare classes referenced
|
// Forward declare classes referenced
|
||||||
class Shader;
|
class Shader;
|
||||||
@@ -33,6 +34,7 @@ public:
|
|||||||
glm::mat4 worldToLocal_;
|
glm::mat4 worldToLocal_;
|
||||||
glm::mat4 localToWorld_;
|
glm::mat4 localToWorld_;
|
||||||
glm::mat4 transform_;
|
glm::mat4 transform_;
|
||||||
|
glm::vec3 scale_, rotation_, translation_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,12 @@ void SessionVisitor::visit(Node &n)
|
|||||||
XMLElement *newelement = xmlDoc_->NewElement("Node");
|
XMLElement *newelement = xmlDoc_->NewElement("Node");
|
||||||
newelement->SetAttribute("visible", n.visible_);
|
newelement->SetAttribute("visible", n.visible_);
|
||||||
|
|
||||||
XMLElement *transform = XMLElementFromGLM(xmlDoc_, n.transform_);
|
XMLElement *scale = XMLElementFromGLM(xmlDoc_, n.scale_);
|
||||||
newelement->InsertEndChild(transform);
|
newelement->InsertEndChild(scale);
|
||||||
|
XMLElement *translation = XMLElementFromGLM(xmlDoc_, n.translation_);
|
||||||
|
newelement->InsertEndChild(translation);
|
||||||
|
XMLElement *rotation = XMLElementFromGLM(xmlDoc_, n.rotation_);
|
||||||
|
newelement->InsertEndChild(rotation);
|
||||||
|
|
||||||
// insert into hierarchy
|
// insert into hierarchy
|
||||||
xmlCurrent_->InsertEndChild(newelement);
|
xmlCurrent_->InsertEndChild(newelement);
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ void Shader::reset()
|
|||||||
|
|
||||||
void Shader::setModelview(float x, float y, float angle, float scale, float aspect_ratio)
|
void Shader::setModelview(float x, float y, float angle, float scale, float aspect_ratio)
|
||||||
{
|
{
|
||||||
|
|
||||||
glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), glm::vec3(x, y, 0.f));
|
glm::mat4 View = glm::translate(glm::identity<glm::mat4>(), glm::vec3(x, y, 0.f));
|
||||||
View = glm::rotate(View, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
View = glm::rotate(View, angle, glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), glm::vec3(scale * aspect_ratio, scale, scale));
|
glm::mat4 Model = glm::scale(glm::identity<glm::mat4>(), glm::vec3(scale * aspect_ratio, scale, scale));
|
||||||
|
|||||||
17
main.cpp
17
main.cpp
@@ -252,29 +252,22 @@ int main(int, char**)
|
|||||||
// UserInterface::manager().OpenTextEditor( Resource::getText("shaders/texture-shader.fs") );
|
// UserInterface::manager().OpenTextEditor( Resource::getText("shaders/texture-shader.fs") );
|
||||||
|
|
||||||
// init the scene
|
// init the scene
|
||||||
scene.root_.init();
|
|
||||||
Rendering::manager().PushFrontDrawCallback(drawScene);
|
Rendering::manager().PushFrontDrawCallback(drawScene);
|
||||||
|
|
||||||
// init elements to the scene
|
// init elements to the scene
|
||||||
testnode1.init();
|
//testnode3.getShader()->blending = Shader::BLEND_OPACITY;
|
||||||
testnode2.init();
|
|
||||||
testnode3.init();
|
|
||||||
testnode3.getShader()->blending = Shader::BLEND_ADD;
|
|
||||||
|
|
||||||
glm::vec3 color( 1.f, 1.f, 0.f );
|
glm::vec3 color( 1.f, 1.f, 0.f );
|
||||||
LineCircle border(color);
|
LineCircle border(color);
|
||||||
// LineSquare border(color);
|
// LineSquare border(color);
|
||||||
border.init();
|
|
||||||
|
|
||||||
Group g1;
|
Group g1;
|
||||||
g1.init();
|
g1.translation_ = glm::vec3(1.f, 1.f, 0.f);
|
||||||
g1.transform_ = glm::translate(glm::identity<glm::mat4>(), glm::vec3(1.f, 1.f, 0.f));
|
g1.scale_ = glm::vec3(2.f, 2.f, 1.f);
|
||||||
g1.transform_ = glm::scale(g1.transform_, glm::vec3(2.f, 2.f, 2.f));
|
|
||||||
|
|
||||||
Switch g2;
|
Switch g2;
|
||||||
g2.init();
|
g2.translation_ = glm::vec3(-1.f, -1.f, 0.f);
|
||||||
g2.transform_ = glm::translate(glm::identity<glm::mat4>(), glm::vec3(-1.f, -1.f, 0.f));
|
// g2.rotation_ = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||||
g2.transform_ = glm::rotate(g2.transform_, 0.672f, glm::vec3(0.0f, 0.0f, 1.0f));
|
|
||||||
|
|
||||||
// build tree
|
// build tree
|
||||||
g1.addChild(&testnode3);
|
g1.addChild(&testnode3);
|
||||||
|
|||||||
Reference in New Issue
Block a user