From bf953b328a131b35650ced3f7a774a28d5507e59 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sat, 11 Apr 2020 22:30:19 +0200 Subject: [PATCH] Using vec4 for color (adding alpha) --- CMakeLists.txt | 4 ++ ImGuiVisitor.cpp | 8 ++- ImGuiVisitor.h | 1 + MediaPlayer.cpp | 1 + Primitives.cpp | 126 ++++++++++++++++++++++------------ Primitives.h | 45 ++++++++---- RenderingManager.cpp | 23 +++++-- RenderingManager.h | 1 + Scene.cpp | 40 ++++++++++- Scene.h | 74 +++++++++++++------- SessionVisitor.cpp | 14 +++- SessionVisitor.h | 1 + Shader.cpp | 10 +++ Shader.h | 1 + Visitor.h | 2 + ext/obj/ObjLoader.cpp | 125 ++++----------------------------- ext/obj/ObjLoader.h | 6 +- main.cpp | 32 +++++++-- rsc/shaders/simple-shader.fs | 9 +-- rsc/shaders/simple-shader.vs | 4 +- rsc/shaders/texture-shader.fs | 7 +- rsc/shaders/texture-shader.vs | 4 +- 22 files changed, 308 insertions(+), 230 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b5dd82..865573e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ set(VMIX_SRCS ImGuiVisitor.cpp GstToolkit.cpp tinyxml2Toolkit.cpp + ColladaToolkit.cpp ) set(VMIX_RSC_FILES @@ -239,6 +240,9 @@ set(VMIX_RSC_FILES ./rsc/models/shadow.png ./rsc/models/square_border.obj ./rsc/models/shadow.mtl + ./rsc/models/disk.png + ./rsc/models/disk.obj + ./rsc/models/disk.mtl ) add_executable(${VMIX_BINARY} diff --git a/ImGuiVisitor.cpp b/ImGuiVisitor.cpp index 1cf3b07..f6ebd1e 100644 --- a/ImGuiVisitor.cpp +++ b/ImGuiVisitor.cpp @@ -96,7 +96,7 @@ void ImGuiVisitor::visit(ImageSurface &n) void ImGuiVisitor::visit(MediaSurface &n) { - ImGui::Text("%s", n.getFilename().c_str()); + ImGui::Text("%s", n.getResource().c_str()); if (n.getMediaPlayer()) n.getMediaPlayer()->accept(*this); @@ -104,7 +104,6 @@ void ImGuiVisitor::visit(MediaSurface &n) void ImGuiVisitor::visit(MediaPlayer &n) { - ImGui::Text("Media Player"); } @@ -132,6 +131,11 @@ void ImGuiVisitor::visit(LineCircle &n) ImGui::Text("Circle"); } +void ImGuiVisitor::visit(ObjModel &n) +{ + ImGui::Text("ObjModel"); +} + void ImGuiVisitor::visit(Scene &n) { ImGui::SetNextItemOpen(true, ImGuiCond_Once); diff --git a/ImGuiVisitor.h b/ImGuiVisitor.h index 4160d79..3ea4a5e 100644 --- a/ImGuiVisitor.h +++ b/ImGuiVisitor.h @@ -19,6 +19,7 @@ public: void visit(LineStrip& n) override; void visit(LineSquare& n) override; void visit(LineCircle& n) override; + void visit(ObjModel& n) override; // Elements with attributes void visit(MediaPlayer& n) override; diff --git a/MediaPlayer.cpp b/MediaPlayer.cpp index 3385253..9b3b361 100644 --- a/MediaPlayer.cpp +++ b/MediaPlayer.cpp @@ -207,6 +207,7 @@ void MediaPlayer::close() } // nothing to display + glDeleteTextures(1, &textureindex_); textureindex_ = Resource::getTextureBlack(); // un-ready the media player diff --git a/Primitives.cpp b/Primitives.cpp index 9a15137..bb7ebb4 100644 --- a/Primitives.cpp +++ b/Primitives.cpp @@ -21,7 +21,8 @@ static const std::vector square_points { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), - glm::vec3( 1.f, 1.f, 0.f ), glm::vec3( 1.f, -1.f, 0.f ) }; + glm::vec3( 1.f, 1.f, 0.f ), glm::vec3( 1.f, -1.f, 0.f ), + glm::vec3( -1.f, -1.f, 0.f )}; static uint square_vao = 0; static uint circle_vao = 0; @@ -29,13 +30,13 @@ static uint circle_vao = 0; ImageSurface::ImageSurface(const std::string& path) : Primitive(), textureindex_(0) { // for image texture - filename_ = path; + resource_ = path; // geometry points_ = std::vector { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), glm::vec3( 1.f, -1.f, 0.f ), glm::vec3( 1.f, 1.f, 0.f ) }; - colors_ = std::vector { glm::vec3( 1.f, 1.f, 1.f ), glm::vec3( 1.f, 1.f, 1.f ), - glm::vec3( 1.f, 1.f, 1.f ), glm::vec3( 1.f, 1.f, 1.f ) }; + colors_ = std::vector { glm::vec4( 1.f, 1.f, 1.f , 1.f ), glm::vec4( 1.f, 1.f, 1.f, 1.f ), + glm::vec4( 1.f, 1.f, 1.f, 1.f ), glm::vec4( 1.f, 1.f, 1.f, 1.f ) }; texCoords_ = std::vector { glm::vec2( 0.f, 1.f ), glm::vec2( 0.f, 0.f ), glm::vec2( 1.f, 1.f ), glm::vec2( 1.f, 0.f ) }; indices_ = std::vector { 0, 1, 2, 3 }; @@ -46,11 +47,11 @@ ImageSurface::ImageSurface(const std::string& path) : Primitive(), textureindex_ void ImageSurface::init() { // load image if specified - if ( filename_.empty()) + if ( resource_.empty()) textureindex_ = Resource::getTextureBlack(); else { float ar = 1.0; - textureindex_ = Resource::getTextureImage(filename_, &ar); + textureindex_ = Resource::getTextureImage(resource_, &ar); scale_.x = ar; } // create shader for textured image @@ -58,17 +59,17 @@ void ImageSurface::init() // use static global vertex array object if (square_vao) { - // only init Node (not the primitive vao + // 1. only init Node (not the primitive vao) Node::init(); - // if set, use the global vertex array object + // 2. use the global vertex array object vao_ = square_vao; } else { - // 1. init as usual (only once) + // 1. init the Primitive (only once) Primitive::init(); - // 2. remember global vao + // 2. remember global vertex array object square_vao = vao_; - // 3. vao_ will NOT be deleted because deleteGLBuffers_() is empty + // 3. square_vao_ will NOT be deleted because ImageSurface::deleteGLBuffers_() is empty } } @@ -92,7 +93,7 @@ void ImageSurface::accept(Visitor& v) MediaSurface::MediaSurface(const std::string& path) : ImageSurface() { - filename_ = path; + resource_ = path; mediaplayer_ = new MediaPlayer; } @@ -103,12 +104,12 @@ MediaSurface::~MediaSurface() void MediaSurface::init() { - std::string tmp = filename_; - filename_ = ""; + std::string tmp = resource_; + resource_ = ""; ImageSurface::init(); - filename_ = tmp; + resource_ = tmp; - mediaplayer_->open(filename_); + mediaplayer_->open(resource_); mediaplayer_->play(true); } @@ -149,7 +150,8 @@ void MediaSurface::accept(Visitor& v) v.visit(*this); } -LineStrip::LineStrip(std::vector points, glm::vec3 color, uint linewidth) : Primitive() + +Points::Points(std::vector points, glm::vec4 color, uint pointsize) : Primitive() { for(size_t i = 0; i < points.size(); ++i) { @@ -158,7 +160,42 @@ LineStrip::LineStrip(std::vector points, glm::vec3 color, uint linewi indices_.push_back ( i ); } - drawingPrimitive_ = GL_LINE_LOOP; + drawingPrimitive_ = GL_POINTS; + pointsize_ = pointsize; +} + +void Points::init() +{ + Primitive::init(); + shader_ = new Shader(); +} + +void Points::draw(glm::mat4 modelview, glm::mat4 projection) +{ + if ( !initialized() ) + init(); + + glPointSize(pointsize_); + + Primitive::draw(modelview, projection); +} + +void Points::accept(Visitor& v) +{ + Primitive::accept(v); + v.visit(*this); +} + +LineStrip::LineStrip(std::vector points, glm::vec4 color, uint linewidth) : Primitive() +{ + for(size_t i = 0; i < points.size(); ++i) + { + points_.push_back( points[i] ); + colors_.push_back( color ); + indices_.push_back ( i ); + } + + drawingPrimitive_ = GL_LINE_STRIP; linewidth_ = linewidth; } @@ -185,18 +222,14 @@ void LineStrip::accept(Visitor& v) } -LineSquare::LineSquare(glm::vec3 color, uint linewidth) : LineStrip(square_points, color, linewidth) +LineSquare::LineSquare(glm::vec4 color, uint linewidth) : LineStrip(square_points, color, linewidth) { } -LineCircle::LineCircle(glm::vec3 color, uint linewidth) : LineStrip(square_points, color, linewidth) +LineCircle::LineCircle(glm::vec4 color, uint linewidth) : LineStrip(std::vector(), color, linewidth) { - points_.clear(); - colors_.clear(); - indices_.clear(); - - int N = 72; - float a = glm::two_pi() / static_cast(N); + static int N = 72; + static float a = glm::two_pi() / static_cast(N); glm::vec3 P(1.f, 0.f, 0.f); for (int i = 0; i < N ; i++ ){ points_.push_back( glm::vec3(P) ); @@ -205,6 +238,10 @@ LineCircle::LineCircle(glm::vec3 color, uint linewidth) : LineStrip(square_point P = glm::rotateZ(P, a); } + // loop + points_.push_back( glm::vec3(1.f, 0.f, 0.f) ); + colors_.push_back( color ); + indices_.push_back ( N ); } void LineCircle::init() @@ -236,33 +273,32 @@ void LineCircle::accept(Visitor& v) ObjModel::ObjModel(const std::string& path) : Primitive(), textureindex_(0) { // for obj model - filename_ = path; + resource_ = path; // load geometry std::vector normals; // ignored std::string material_filename; - bool okay = loadObject( Resource::getText(filename_), points_, normals, texCoords_, indices_, material_filename, 1.0 ); - if ( !okay ) { - Log::Warning("Failed to load OBJ model %s", path.c_str()); - } + bool okay = loadObject( Resource::getText(resource_), points_, + normals, texCoords_, indices_, material_filename ); + if ( okay ) { + // prepend path to the name of other files + std::string rsc_path = resource_.substr(0, resource_.rfind('/')) + "/"; - // prepend path to the name of other files - std::string rsc_path = filename_.substr(0, filename_.rfind('/')) + "/"; + // load materials + std::map material_library; + okay = loadMaterialLibrary(Resource::getText( rsc_path + material_filename ), material_library); + if (okay) { + Material *material_ = material_library.begin()->second; // default use first material - // load materials - std::map material_library; - okay = loadMaterialLibrary(Resource::getText( rsc_path + material_filename ), material_library); - if (okay) { - Material *material_ = material_library.begin()->second; // default use first material + // fill colors + for (int i = 0; i < points_.size(); i++) + colors_.push_back( glm::vec4( material_->diffuse, 1.0) ); - // fill colors - for (int i = 0; i < points_.size(); i++) - colors_.push_back(material_->diffuse); - - if (!material_->diffuseTexture.empty()) { - texture_filename_ = rsc_path + material_->diffuseTexture; + if (!material_->diffuseTexture.empty()) { + texture_filename_ = rsc_path + material_->diffuseTexture; + } + delete material_; } - delete material_; } drawingPrimitive_ = GL_TRIANGLES; diff --git a/Primitives.h b/Primitives.h index 1e40d30..0e98adf 100644 --- a/Primitives.h +++ b/Primitives.h @@ -17,10 +17,10 @@ public: void draw (glm::mat4 modelview, glm::mat4 projection) override; void accept (Visitor& v) override; - inline std::string getFilename() const { return filename_; } + inline std::string getResource() const { return resource_; } protected: - std::string filename_; + std::string resource_; uint textureindex_; }; @@ -39,7 +39,7 @@ public: void init () override; void draw (glm::mat4 modelview, glm::mat4 projection) override; void accept (Visitor& v) override; - void update ( float dt ) override; + void update (float dt) override; MediaPlayer *getMediaPlayer() { return mediaplayer_; } }; @@ -58,20 +58,40 @@ public: //}; -// Draw a line strip -class LineStrip : public Primitive { - uint linewidth_; +// Draw a Point +class Points : public Primitive { + + uint pointsize_; public: - LineStrip(std::vector points, glm::vec3 color, uint linewidth = 1); + Points(std::vector points, glm::vec4 color, uint pointsize = 10); virtual void init() override; virtual void draw(glm::mat4 modelview, glm::mat4 projection) override; virtual void accept(Visitor& v) override; std::vector getPoints() { return points_; } - glm::vec3 getColor() { return colors_[0]; } + glm::vec4 getColor() { return colors_[0]; } + + inline void setPointSize(uint v) { pointsize_ = v; } + inline uint getPointSize() const { return pointsize_; } +}; + +// Draw a line strip +class LineStrip : public Primitive { + + uint linewidth_; + +public: + LineStrip(std::vector points, glm::vec4 color, uint linewidth = 1); + + virtual void init() override; + virtual void draw(glm::mat4 modelview, glm::mat4 projection) override; + virtual void accept(Visitor& v) override; + + std::vector getPoints() { return points_; } + glm::vec4 getColor() { return colors_[0]; } inline void setLineWidth(uint v) { linewidth_ = v; } inline uint getLineWidth() const { return linewidth_; } @@ -80,7 +100,7 @@ public: class LineSquare : public LineStrip { public: - LineSquare(glm::vec3 color, uint linewidth = 1); + LineSquare(glm::vec4 color, uint linewidth = 1); }; class LineCircle : public LineStrip { @@ -88,14 +108,13 @@ class LineCircle : public LineStrip { void deleteGLBuffers_() override {} public: - LineCircle(glm::vec3 color, uint linewidth = 1); + LineCircle(glm::vec4 color, uint linewidth = 1); void init() override; void accept(Visitor& v) override; }; -// Draw a Rectangle (triangle strip) with a texture class ObjModel : public Primitive { public: @@ -105,10 +124,10 @@ public: void draw (glm::mat4 modelview, glm::mat4 projection) override; void accept (Visitor& v) override; - inline std::string getFilename() const { return filename_; } + inline std::string getResource() const { return resource_; } protected: - std::string filename_; + std::string resource_; std::string texture_filename_; uint textureindex_; }; diff --git a/RenderingManager.cpp b/RenderingManager.cpp index bdddef5..9f6e82f 100644 --- a/RenderingManager.cpp +++ b/RenderingManager.cpp @@ -132,6 +132,15 @@ bool Rendering::Init() g_setenv ("GST_GL_API", "opengl3", FALSE); gst_init (NULL, NULL); + // Antialiasing + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); + // This hint can improve the speed of texturing when perspective-correct texture coordinate interpolation isn't needed + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + // This hint can improve the speed of shading when dFdx dFdy aren't needed in GLSL + glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_FASTEST); + + #if GST_GL_HAVE_PLATFORM_WGL global_gl_context = gst_gl_context_new_wrapped (display, (guintptr) wglGetCurrentContext (), GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL); @@ -300,16 +309,22 @@ void Rendering::PopAttrib() draw_attributes_.pop_front(); // set attribute element to default - RenderingAttrib ra = main_window_attributes_; - // if there is an element at top, use it - if (draw_attributes_.size() > 0) - ra = draw_attributes_.front(); + RenderingAttrib ra = currentAttrib(); // apply Changes to OpenGL glViewport(0, 0, ra.viewport.x, ra.viewport.y); glClearColor(ra.clear_color.r, ra.clear_color.g, ra.clear_color.b, 1.f); } +RenderingAttrib Rendering::currentAttrib() +{ + // default rendering attrib is the main window's + RenderingAttrib ra = main_window_attributes_; + // but if there is an element at top, return it + if (draw_attributes_.size() > 0) + ra = draw_attributes_.front(); + return ra; +} glm::mat4 Rendering::Projection() { diff --git a/RenderingManager.h b/RenderingManager.h index 6356ae4..b684cb8 100644 --- a/RenderingManager.h +++ b/RenderingManager.h @@ -58,6 +58,7 @@ public: // push and pop rendering attributes void PushAttrib(RenderingAttrib ra); void PopAttrib(); + RenderingAttrib currentAttrib(); // request screenshot void RequestScreenshot(); diff --git a/Scene.cpp b/Scene.cpp index 9db196f..e2e4b12 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -10,9 +10,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -82,8 +84,8 @@ void Primitive::init() glBindVertexArray( vao_ ); // compute the memory needs for points normals and indicies - std::size_t sizeofPoints = sizeof(glm::vec3)*points_.size(); - std::size_t sizeofColors = sizeof(glm::vec3)*colors_.size(); + std::size_t sizeofPoints = sizeof(glm::vec3) * points_.size(); + std::size_t sizeofColors = sizeof(glm::vec4) * colors_.size(); std::size_t sizeofTexCoords = sizeof(glm::vec2) * texCoords_.size(); // setup the array buffers for vertices @@ -102,7 +104,7 @@ void Primitive::init() // explain how to read attributes 0, 1 and 2 (for point, color and textcoord respectively) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void *)0 ); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void *)(sizeofPoints) ); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void *)(sizeofPoints) ); glEnableVertexAttribArray(1); if ( sizeofTexCoords ) { glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void *)(sizeofPoints + sizeofColors) ); @@ -322,6 +324,38 @@ int Switch::getIndexActiveChild() const } +void Animation::init() +{ + Group::init(); + + animation_ = glm::identity(); +// animation_ = glm::translate(glm::identity(), glm::vec3(2.f, 0.f, 0.f)); +} + +void Animation::update( float dt ) +{ + Group::update(dt); + + // incremental rotation + animation_ = glm::rotate(animation_, speed_ * dt, axis_); + + // calculate translation of a point at distance radius_ after rotation by animation_ + static glm::vec3 any = glm::linearRand( glm::vec3(0.f, 0.f, 0.f), glm::vec3(1.f, 1.f, 1.f)); + glm::vec3 pos = glm::normalize( glm::cross(any, axis_) ) * radius_; + glm::vec4 delta = glm::vec4(pos, 0.f) * animation_; + + // apply this translation to the Group transform + transform_ *= glm::translate(glm::identity(), glm::vec3(delta.x, delta.y, 0.f)); + +} + +void Animation::accept(Visitor& v) +{ + Node::accept(v); + v.visit(*this); +} + + void Scene::accept(Visitor& v) { v.visit(*this); diff --git a/Scene.h b/Scene.h index a0f5267..ec2f16c 100644 --- a/Scene.h +++ b/Scene.h @@ -51,6 +51,32 @@ public: glm::vec3 scale_, rotation_, translation_; }; +// Leaf Nodes are primitives that can be rendered +class Primitive : public Node { + +public: + Primitive() : Node(), shader_(nullptr), vao_(0), drawingPrimitive_(0) {} + virtual ~Primitive(); + + virtual void init () override; + virtual void accept (Visitor& v) override; + virtual void draw (glm::mat4 modelview, glm::mat4 projection) override; + + inline Shader *getShader() const { return shader_; } + inline void setShader( Shader* e ) { shader_ = e; } + +protected: + Shader* shader_; + uint vao_; + uint drawingPrimitive_; + std::vector points_; + std::vector colors_; + std::vector texCoords_; + std::vector indices_; + virtual void deleteGLBuffers_(); +}; + +// Other Nodes establish hierarchy with a group of nodes struct z_comparator { @@ -73,32 +99,6 @@ private: typedef std::multiset NodeSet; -// Leaf Nodes are primitives that can be rendered -class Primitive : public Node { - -public: - Primitive() : Node(), shader_(nullptr), vao_(0), drawingPrimitive_(0) {} - virtual ~Primitive(); - - virtual void init () override; - virtual void accept (Visitor& v) override; - virtual void draw (glm::mat4 modelview, glm::mat4 projection) override; - - inline Shader *getShader() const { return shader_; } - inline void setShader( Shader* e ) { shader_ = e; } - -protected: - Shader* shader_; - uint vao_; - uint drawingPrimitive_; - std::vector points_; - std::vector colors_; - std::vector texCoords_; - std::vector indices_; - virtual void deleteGLBuffers_(); -}; - -// Other Nodes establish hierarchy with a group of nodes class Group : public Node { public: @@ -144,6 +144,28 @@ protected: NodeSet::iterator active_; }; +// Animation Nodes +class Animation : public Group { + +public: + Animation() : Group(), axis_(glm::vec3(0.f, 0.f, 1.f)), speed_(0.f), radius_(1.f) { } + + virtual void init () override; + virtual void update (float dt) override; + virtual void accept (Visitor& v) override; + + // circular path + glm::vec3 axis_; + float speed_; + float radius_; + +protected: + Node *child_; + glm::mat4 animation_; +}; + + + // A scene contains a root node and gives a simplified API to add nodes class Scene { diff --git a/SessionVisitor.cpp b/SessionVisitor.cpp index 86c1e62..c2e8b82 100644 --- a/SessionVisitor.cpp +++ b/SessionVisitor.cpp @@ -81,8 +81,8 @@ void SessionVisitor::visit(ImageSurface &n) // Node of a different type xmlCurrent_->SetAttribute("type", "ImageSurface"); - XMLText *filename = xmlDoc_->NewText( n.getFilename().c_str() ); - XMLElement *image = xmlDoc_->NewElement("filename"); + XMLText *filename = xmlDoc_->NewText( n.getResource().c_str() ); + XMLElement *image = xmlDoc_->NewElement("resource"); image->InsertEndChild(filename); xmlCurrent_->InsertEndChild(image); } @@ -166,6 +166,16 @@ void SessionVisitor::visit(LineCircle &n) xmlCurrent_->InsertEndChild(color); } +void SessionVisitor::visit(ObjModel &n) +{ + // Node of a different type + xmlCurrent_->SetAttribute("type", "ObjModel"); + + XMLText *filename = xmlDoc_->NewText( n.getResource().c_str() ); + XMLElement *obj = xmlDoc_->NewElement("resource"); + obj->InsertEndChild(filename); + xmlCurrent_->InsertEndChild(obj); +} void SessionVisitor::visit(Scene &n) { diff --git a/SessionVisitor.h b/SessionVisitor.h index 5d8ae8b..5e7cf04 100644 --- a/SessionVisitor.h +++ b/SessionVisitor.h @@ -26,6 +26,7 @@ public: void visit(LineStrip& n) override; void visit(LineSquare&) override; void visit(LineCircle& n) override; + void visit(ObjModel& n) override; // Elements with attributes void visit(MediaPlayer& n) override; diff --git a/Shader.cpp b/Shader.cpp index 48c5711..fc11e00 100644 --- a/Shader.cpp +++ b/Shader.cpp @@ -120,6 +120,12 @@ void ShadingProgram::setUniform(const std::string& name, glm::vec4 va glUniform4fv(glGetUniformLocation(id_, name.c_str()), 1, glm::value_ptr(v)); } +template<> +void ShadingProgram::setUniform(const std::string& name, glm::vec3 val) { + glm::vec3 v(val); + glUniform4fv(glGetUniformLocation(id_, name.c_str()), 1, glm::value_ptr(v)); +} + template<> void ShadingProgram::setUniform(const std::string& name, glm::mat4 val) { glm::mat4 m(val); @@ -184,6 +190,9 @@ void Shader::use() program_->setUniform("modelview", modelview); program_->setUniform("color", color); + resolution = glm::vec3( Rendering::manager().currentAttrib().viewport, 0.f); + program_->setUniform("resolution", resolution); + // Blending Function if ( blending != BLEND_NONE) { glEnable(GL_BLEND); @@ -199,6 +208,7 @@ void Shader::reset() { projection = glm::identity(); modelview = glm::identity(); + resolution = glm::vec3(1280.f, 720.f, 0.f); color = glm::vec4(1.f, 1.f, 1.f, 1.f); } diff --git a/Shader.h b/Shader.h index e3435be..92780db 100644 --- a/Shader.h +++ b/Shader.h @@ -47,6 +47,7 @@ public: glm::mat4 projection; glm::mat4 modelview; + glm::vec3 resolution; glm::vec4 color; typedef enum { diff --git a/Visitor.h b/Visitor.h index a876b55..586ea7f 100644 --- a/Visitor.h +++ b/Visitor.h @@ -14,6 +14,7 @@ class MediaSurface; class LineStrip; class LineSquare; class LineCircle; +class ObjModel; class MediaPlayer; class Shader; class ImageShader; @@ -33,6 +34,7 @@ public: virtual void visit(LineStrip& n) = 0; virtual void visit(LineSquare& n) = 0; virtual void visit(LineCircle& n) = 0; + virtual void visit(ObjModel& n) = 0; virtual void visit(MediaPlayer& n) = 0; virtual void visit(Shader& n) = 0; virtual void visit(ImageShader& n) = 0; diff --git a/ext/obj/ObjLoader.cpp b/ext/obj/ObjLoader.cpp index 3566a63..a5b834c 100644 --- a/ext/obj/ObjLoader.cpp +++ b/ext/obj/ObjLoader.cpp @@ -139,8 +139,7 @@ makeMtlFilename ( std::string mtlfile, std::string objfile ) } -bool loadMaterialLibrary ( string mtlText, - map &outMaterials) +bool loadMaterialLibrary ( string mtlText, map &outMaterials) { Material *mat = nullptr; stringstream ifs(mtlText); @@ -180,10 +179,9 @@ bool loadObject(string objText, vector &outNormal, vector &outUv, vector &outIndices, - std::string &outMtlfilename, - float scale) + std::string &outMtlfilename + ) { - vector positions; vector normals; vector uvs; @@ -242,7 +240,7 @@ bool loadObject(string objText, outIndices.push_back(cachedIndex->second); } else { int vertexIndex = outPositions.size(); - outPositions.push_back(positions[index.position-1] * scale); + outPositions.push_back(positions[index.position-1]); if (index.normal != -1){ outNormal.push_back(normals[index.normal-1]); } @@ -254,27 +252,11 @@ bool loadObject(string objText, } } } - //cout <<"Indices "<< outIndices.size() << endl; - //cout <<"Positions "<< outPositions.size() << endl; - //printDebug(outPositions, outIndices); - return true; + + return (outPositions.size()>0); } - -Material * -makeDefaultMaterial() -{ - Material *m = new Material; - m->ambient = vec3 ( 0.1, 0.1, 0.1 ); - m->diffuse = vec3 ( 0.8, 0.8, 0.8 ); - m->specular = vec3 ( 1.0, 1.0, 1.0 ); - m->shininess = 200.0f; - return m; -} - - - struct Group { vector triangles; Material *mat; @@ -288,30 +270,17 @@ bool loadObjectGroups ( string filename, vector &outNormal, vector &outUv, vector< vector > &outIndices, // per group - vector< Material* >&outMaterials, // per group - float scale) + vector< Material* >&outMaterials // per group + ) { - + map materials; vector positions; vector normals; vector uvs; map groups; string currentGroupName(""); - - - map materials; - vector diffuseTextures; - vector bumpTextures; - static int groupnum = 1; - // currentGroupName = "dummy1"; - // groups[currentGroupName] = new Group; - // Material* defaultMaterial = makeDefaultMaterial(); - // groups[currentGroupName]->mat = defaultMaterial; - // materials[currentGroupName] = defaultMaterial; - // outMaterials.push_back ( defaultMaterial ); - string path = std::string( filename ); ifstream ifs ( path.c_str() , ifstream::in ); @@ -359,7 +328,7 @@ bool loadObjectGroups ( string filename, std::cout << "materials[token] == " << materials[token] << std::endl; } if ( materials[token] == 0 ) { - materials[token] = makeDefaultMaterial(); + materials[token] = new Material; } printDebug ( materials[token] ); groups[currentGroupName]->mat = materials[token]; @@ -391,7 +360,7 @@ bool loadObjectGroups ( string filename, oss << "dummy" << groupnum++; currentGroupName=oss.str(); groups[currentGroupName] = new Group; - outMaterials.push_back( groups[currentGroupName]->mat = makeDefaultMaterial() ); + outMaterials.push_back( groups[currentGroupName]->mat = new Material ); } @@ -427,7 +396,7 @@ bool loadObjectGroups ( string filename, groupIndices.push_back(cachedIndex->second); } else { int vertexIndex = outPositions.size(); - outPositions.push_back(positions[index.position-1] * scale); + outPositions.push_back(positions[index.position-1]); if (index.normal != -1){ outNormal.push_back(normals[index.normal-1]); } @@ -473,73 +442,3 @@ void printDebug(Material *m){ cout << "bump texture " << m->bumpTexture << endl; } - -//// -//// ObjFilePrimitive -//// - -//ObjFilePrimitive::ObjFilePrimitive ( const char *filename ) -//{ - -// bool okay = loadObjectGroups( filename, -// points_, -// normals_, -// texCoords_, -// groupIndices_, -// materials_, -// 1.0 ); -// if ( !okay ) { -// std::cout << "load error\n" ; -// } else { -// if (DEBUG) -// std::cout << "loaded " << filename << std::endl -// << points_.size() << " points\n" -// << normals_.size() << " normals\n" -// << texCoords_.size() << " tex coords\n" -// << groupIndices_.size() << " groups\n"; -// } -// // // copy to unsigned ints. *sigh* -// // for (int i = 0; i < tmp_indices.size(); i++ ) -// // indices_.push_back( static_cast (tmp_indices[i]) ); - -// drawingPrimitive_ = GL_TRIANGLES; - -// generateAndLoadArrayBuffer(); - -// // generate the element buffers -// for ( int i = 0; i < groupIndices_.size(); i++ ) { -// groupElementArrayIds_.push_back(0); -// glGenBuffers(1, &groupElementArrayIds_[i] ); -// int sizeofIndices = groupIndices_[i].size()*sizeof(unsigned int); -// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, groupElementArrayIds_[i] ); -// glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeofIndices, &(groupIndices_[i][0]), GL_STATIC_DRAW); -// } - -// glBindVertexArray(0); -//} - - -//void -//ObjFilePrimitive::draw ( glm::mat4 modelview, -// glm::mat4 projection, -// Ptr material) { - -// // loop over all groups as they have different materials. -// for (int i = 0; i < groupIndices_.size(); i++ ) { - -// if (!material.get()) { -// setupShader ( modelview, projection, materials_[i] ); -// } else { -// setupShader ( modelview, projection, material ); -// } - -// // -// // draw -// // -// glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, groupElementArrayIds_[i] ); -// glDrawElements( drawingPrimitive_, groupIndices_[i].size(), GL_UNSIGNED_INT, 0 ); -// glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, 0 ); - -// endShader(); -// } -//} diff --git a/ext/obj/ObjLoader.h b/ext/obj/ObjLoader.h index 07088e8..45f0546 100644 --- a/ext/obj/ObjLoader.h +++ b/ext/obj/ObjLoader.h @@ -66,8 +66,7 @@ bool loadObject (std::string objText, std::vector &outNormal, std::vector &outUv, std::vector &outIndices, - std::string &outMtlfilename, - float scale = 1.0f + std::string &outMtlfilename ); @@ -81,8 +80,7 @@ bool loadObjectGroups (std::string filename, std::vector &outNormal, std::vector &outUv, std::vector > &outIndices, // per group - std::vector &outMaterials, // per group (w/textures) - float scale=1.0f + std::vector &outMaterials // per group (w/textures) ); #endif diff --git a/main.cpp b/main.cpp index 296c8e0..6ab5597 100644 --- a/main.cpp +++ b/main.cpp @@ -255,20 +255,34 @@ int main(int, char**) // init elements to the scene //testnode3.getShader()->blending = Shader::BLEND_OPACITY; - glm::vec3 color( 0.8f, 0.8f, 0.f ); -// LineCircle border(color); - LineSquare border(color, 2); + ObjModel disk("models/disk.obj"); + glm::vec4 pink( 0.8f, 0.f, 0.8f, 1.f ); + LineCircle circle(pink, 5); + + glm::vec4 color( 0.8f, 0.8f, 0.f, 1.f); + LineSquare border(color, 5); ObjModel shadow("models/square_border.obj"); Group g1; - g1.translation_ = glm::vec3(1.f, 1.f, 0.f); + g1.translation_ = glm::vec3(1.f, 1.f, 0.2f); g1.scale_ = glm::vec3(1.2f, 1.2f, 1.f); + Group g2; - g2.translation_ = glm::vec3(-1.f, -1.f, 0.f); -// g2.rotation_ = glm::vec3(0.0f, 0.0f, 1.0f); + g2.translation_ = glm::vec3(-1.f, -1.f, 0.4f); + + Animation A; + A.speed_ = 0.01f; + A.axis_ = glm::vec3(1.f, 1.f, 1.f); + + std::vector pts = std::vector { glm::vec3( 0.f, 0.f, 0.f ) }; + Points P(pts, pink); + P.setPointSize(60); // build tree + scene.root_.addChild(&disk); + scene.root_.addChild(&circle); + g1.addChild(&shadow); g1.addChild(&testnode3); g1.addChild(&border); @@ -277,7 +291,11 @@ int main(int, char**) g2.addChild(&shadow); g2.addChild(&testnode1); g2.addChild(&border); - scene.root_.addChild(&g2); + +// A.addChild(&g2); + A.addChild(&P); + + scene.root_.addChild(&A); // init output FBO output = new FrameBuffer(1280, 720); diff --git a/rsc/shaders/simple-shader.fs b/rsc/shaders/simple-shader.fs index 31000cc..2a16f3f 100644 --- a/rsc/shaders/simple-shader.fs +++ b/rsc/shaders/simple-shader.fs @@ -2,11 +2,12 @@ out vec4 FragColor; -in vec3 vertexColor; +in vec4 vertexColor; -uniform vec4 color; +uniform vec4 color; // drawing color +uniform vec3 resolution; // viewport resolution (in pixels) void main() -{ - FragColor = color * vec4(vertexColor,1.0); +{ + FragColor = color * vertexColor; } diff --git a/rsc/shaders/simple-shader.vs b/rsc/shaders/simple-shader.vs index 3c1b16d..756eac4 100644 --- a/rsc/shaders/simple-shader.vs +++ b/rsc/shaders/simple-shader.vs @@ -1,9 +1,9 @@ #version 330 core layout (location = 0) in vec3 position; -layout (location = 1) in vec3 color; +layout (location = 1) in vec4 color; -out vec3 vertexColor; +out vec4 vertexColor; uniform mat4 modelview; uniform mat4 projection; diff --git a/rsc/shaders/texture-shader.fs b/rsc/shaders/texture-shader.fs index 8f26947..00375de 100644 --- a/rsc/shaders/texture-shader.fs +++ b/rsc/shaders/texture-shader.fs @@ -2,19 +2,20 @@ out vec4 FragColor; -in vec3 vertexColor; +in vec4 vertexColor; in vec2 vertexUV; uniform sampler2D sourceTexture; uniform vec4 color; uniform float contrast; uniform float brightness; +uniform vec3 resolution; // viewport resolution (in pixels) void main() { vec4 texturecolor = texture(sourceTexture, vertexUV); vec3 transformedRGB = mix(vec3(0.62), texturecolor.rgb, contrast + 1.0) + brightness; - transformedRGB *= vertexColor; + transformedRGB *= vertexColor.rgb; - FragColor = color * vec4(transformedRGB, texturecolor.a); + FragColor = color * vec4(transformedRGB, texturecolor.a * vertexColor.a); } diff --git a/rsc/shaders/texture-shader.vs b/rsc/shaders/texture-shader.vs index 91db1ce..38edf2e 100644 --- a/rsc/shaders/texture-shader.vs +++ b/rsc/shaders/texture-shader.vs @@ -1,10 +1,10 @@ #version 330 core layout (location = 0) in vec3 position; -layout (location = 1) in vec3 color; +layout (location = 1) in vec4 color; layout (location = 2) in vec2 texCoord; -out vec3 vertexColor; +out vec4 vertexColor; out vec2 vertexUV; uniform mat4 modelview;