Using vec4 for color (adding alpha)

This commit is contained in:
brunoherbelin
2020-04-11 22:30:19 +02:00
parent 3c12baffc5
commit bf953b328a
22 changed files with 308 additions and 230 deletions

View File

@@ -219,6 +219,7 @@ set(VMIX_SRCS
ImGuiVisitor.cpp ImGuiVisitor.cpp
GstToolkit.cpp GstToolkit.cpp
tinyxml2Toolkit.cpp tinyxml2Toolkit.cpp
ColladaToolkit.cpp
) )
set(VMIX_RSC_FILES set(VMIX_RSC_FILES
@@ -239,6 +240,9 @@ set(VMIX_RSC_FILES
./rsc/models/shadow.png ./rsc/models/shadow.png
./rsc/models/square_border.obj ./rsc/models/square_border.obj
./rsc/models/shadow.mtl ./rsc/models/shadow.mtl
./rsc/models/disk.png
./rsc/models/disk.obj
./rsc/models/disk.mtl
) )
add_executable(${VMIX_BINARY} add_executable(${VMIX_BINARY}

View File

@@ -96,7 +96,7 @@ 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.getResource().c_str());
if (n.getMediaPlayer()) if (n.getMediaPlayer())
n.getMediaPlayer()->accept(*this); n.getMediaPlayer()->accept(*this);
@@ -104,7 +104,6 @@ void ImGuiVisitor::visit(MediaSurface &n)
void ImGuiVisitor::visit(MediaPlayer &n) void ImGuiVisitor::visit(MediaPlayer &n)
{ {
ImGui::Text("Media Player"); ImGui::Text("Media Player");
} }
@@ -132,6 +131,11 @@ void ImGuiVisitor::visit(LineCircle &n)
ImGui::Text("Circle"); ImGui::Text("Circle");
} }
void ImGuiVisitor::visit(ObjModel &n)
{
ImGui::Text("ObjModel");
}
void ImGuiVisitor::visit(Scene &n) void ImGuiVisitor::visit(Scene &n)
{ {
ImGui::SetNextItemOpen(true, ImGuiCond_Once); ImGui::SetNextItemOpen(true, ImGuiCond_Once);

View File

@@ -19,6 +19,7 @@ public:
void visit(LineStrip& n) override; void visit(LineStrip& n) override;
void visit(LineSquare& n) override; void visit(LineSquare& n) override;
void visit(LineCircle& n) override; void visit(LineCircle& n) override;
void visit(ObjModel& n) override;
// Elements with attributes // Elements with attributes
void visit(MediaPlayer& n) override; void visit(MediaPlayer& n) override;

View File

@@ -207,6 +207,7 @@ void MediaPlayer::close()
} }
// nothing to display // nothing to display
glDeleteTextures(1, &textureindex_);
textureindex_ = Resource::getTextureBlack(); textureindex_ = Resource::getTextureBlack();
// un-ready the media player // un-ready the media player

View File

@@ -21,7 +21,8 @@
static const std::vector<glm::vec3> square_points { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), static const std::vector<glm::vec3> 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 square_vao = 0;
static uint circle_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) ImageSurface::ImageSurface(const std::string& path) : Primitive(), textureindex_(0)
{ {
// for image texture // for image texture
filename_ = path; resource_ = path;
// geometry // geometry
points_ = std::vector<glm::vec3> { glm::vec3( -1.f, -1.f, 0.f ), glm::vec3( -1.f, 1.f, 0.f ), points_ = std::vector<glm::vec3> { 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 ) };
colors_ = std::vector<glm::vec3> { glm::vec3( 1.f, 1.f, 1.f ), glm::vec3( 1.f, 1.f, 1.f ), colors_ = std::vector<glm::vec4> { glm::vec4( 1.f, 1.f, 1.f , 1.f ), glm::vec4( 1.f, 1.f, 1.f, 1.f ),
glm::vec3( 1.f, 1.f, 1.f ), glm::vec3( 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> { glm::vec2( 0.f, 1.f ), glm::vec2( 0.f, 0.f ), texCoords_ = std::vector<glm::vec2> { glm::vec2( 0.f, 1.f ), glm::vec2( 0.f, 0.f ),
glm::vec2( 1.f, 1.f ), glm::vec2( 1.f, 0.f ) }; glm::vec2( 1.f, 1.f ), glm::vec2( 1.f, 0.f ) };
indices_ = std::vector<uint> { 0, 1, 2, 3 }; indices_ = std::vector<uint> { 0, 1, 2, 3 };
@@ -46,11 +47,11 @@ ImageSurface::ImageSurface(const std::string& path) : Primitive(), textureindex_
void ImageSurface::init() void ImageSurface::init()
{ {
// load image if specified // load image if specified
if ( filename_.empty()) if ( resource_.empty())
textureindex_ = Resource::getTextureBlack(); textureindex_ = Resource::getTextureBlack();
else { else {
float ar = 1.0; float ar = 1.0;
textureindex_ = Resource::getTextureImage(filename_, &ar); textureindex_ = Resource::getTextureImage(resource_, &ar);
scale_.x = ar; scale_.x = ar;
} }
// create shader for textured image // create shader for textured image
@@ -58,17 +59,17 @@ void ImageSurface::init()
// use static global vertex array object // use static global vertex array object
if (square_vao) { if (square_vao) {
// only init Node (not the primitive vao // 1. only init Node (not the primitive vao)
Node::init(); Node::init();
// if set, use the global vertex array object // 2. use the global vertex array object
vao_ = square_vao; vao_ = square_vao;
} }
else { else {
// 1. init as usual (only once) // 1. init the Primitive (only once)
Primitive::init(); Primitive::init();
// 2. remember global vao // 2. remember global vertex array object
square_vao = vao_; 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() MediaSurface::MediaSurface(const std::string& path) : ImageSurface()
{ {
filename_ = path; resource_ = path;
mediaplayer_ = new MediaPlayer; mediaplayer_ = new MediaPlayer;
} }
@@ -103,12 +104,12 @@ MediaSurface::~MediaSurface()
void MediaSurface::init() void MediaSurface::init()
{ {
std::string tmp = filename_; std::string tmp = resource_;
filename_ = ""; resource_ = "";
ImageSurface::init(); ImageSurface::init();
filename_ = tmp; resource_ = tmp;
mediaplayer_->open(filename_); mediaplayer_->open(resource_);
mediaplayer_->play(true); mediaplayer_->play(true);
} }
@@ -149,7 +150,8 @@ void MediaSurface::accept(Visitor& v)
v.visit(*this); v.visit(*this);
} }
LineStrip::LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewidth) : Primitive()
Points::Points(std::vector<glm::vec3> points, glm::vec4 color, uint pointsize) : Primitive()
{ {
for(size_t i = 0; i < points.size(); ++i) for(size_t i = 0; i < points.size(); ++i)
{ {
@@ -158,7 +160,42 @@ LineStrip::LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewi
indices_.push_back ( i ); 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<glm::vec3> 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; 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<glm::vec3>(), color, linewidth)
{ {
points_.clear(); static int N = 72;
colors_.clear(); static float a = glm::two_pi<float>() / static_cast<float>(N);
indices_.clear();
int N = 72;
float a = glm::two_pi<float>() / static_cast<float>(N);
glm::vec3 P(1.f, 0.f, 0.f); glm::vec3 P(1.f, 0.f, 0.f);
for (int i = 0; i < N ; i++ ){ for (int i = 0; i < N ; i++ ){
points_.push_back( glm::vec3(P) ); 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); 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() void LineCircle::init()
@@ -236,33 +273,32 @@ void LineCircle::accept(Visitor& v)
ObjModel::ObjModel(const std::string& path) : Primitive(), textureindex_(0) ObjModel::ObjModel(const std::string& path) : Primitive(), textureindex_(0)
{ {
// for obj model // for obj model
filename_ = path; resource_ = path;
// load geometry // load geometry
std::vector<glm::vec3> normals; // ignored std::vector<glm::vec3> normals; // ignored
std::string material_filename; std::string material_filename;
bool okay = loadObject( Resource::getText(filename_), points_, normals, texCoords_, indices_, material_filename, 1.0 ); bool okay = loadObject( Resource::getText(resource_), points_,
if ( !okay ) { normals, texCoords_, indices_, material_filename );
Log::Warning("Failed to load OBJ model %s", path.c_str()); 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 // load materials
std::string rsc_path = filename_.substr(0, filename_.rfind('/')) + "/"; std::map<std::string,Material*> 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 // fill colors
std::map<std::string,Material*> material_library; for (int i = 0; i < points_.size(); i++)
okay = loadMaterialLibrary(Resource::getText( rsc_path + material_filename ), material_library); colors_.push_back( glm::vec4( material_->diffuse, 1.0) );
if (okay) {
Material *material_ = material_library.begin()->second; // default use first material
// fill colors if (!material_->diffuseTexture.empty()) {
for (int i = 0; i < points_.size(); i++) texture_filename_ = rsc_path + material_->diffuseTexture;
colors_.push_back(material_->diffuse); }
delete material_;
if (!material_->diffuseTexture.empty()) {
texture_filename_ = rsc_path + material_->diffuseTexture;
} }
delete material_;
} }
drawingPrimitive_ = GL_TRIANGLES; drawingPrimitive_ = GL_TRIANGLES;

View File

@@ -17,10 +17,10 @@ public:
void draw (glm::mat4 modelview, glm::mat4 projection) override; void draw (glm::mat4 modelview, glm::mat4 projection) override;
void accept (Visitor& v) override; void accept (Visitor& v) override;
inline std::string getFilename() const { return filename_; } inline std::string getResource() const { return resource_; }
protected: protected:
std::string filename_; std::string resource_;
uint textureindex_; uint textureindex_;
}; };
@@ -39,7 +39,7 @@ public:
void init () override; void init () override;
void draw (glm::mat4 modelview, glm::mat4 projection) override; void draw (glm::mat4 modelview, glm::mat4 projection) override;
void accept (Visitor& v) override; void accept (Visitor& v) override;
void update ( float dt ) override; void update (float dt) override;
MediaPlayer *getMediaPlayer() { return mediaplayer_; } 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: public:
LineStrip(std::vector<glm::vec3> points, glm::vec3 color, uint linewidth = 1); Points(std::vector<glm::vec3> points, glm::vec4 color, uint pointsize = 10);
virtual void init() override; 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;
std::vector<glm::vec3> getPoints() { return points_; } std::vector<glm::vec3> 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<glm::vec3> 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<glm::vec3> getPoints() { return points_; }
glm::vec4 getColor() { return colors_[0]; }
inline void setLineWidth(uint v) { linewidth_ = v; } inline void setLineWidth(uint v) { linewidth_ = v; }
inline uint getLineWidth() const { return linewidth_; } inline uint getLineWidth() const { return linewidth_; }
@@ -80,7 +100,7 @@ public:
class LineSquare : public LineStrip { class LineSquare : public LineStrip {
public: public:
LineSquare(glm::vec3 color, uint linewidth = 1); LineSquare(glm::vec4 color, uint linewidth = 1);
}; };
class LineCircle : public LineStrip { class LineCircle : public LineStrip {
@@ -88,14 +108,13 @@ class LineCircle : public LineStrip {
void deleteGLBuffers_() override {} void deleteGLBuffers_() override {}
public: public:
LineCircle(glm::vec3 color, uint linewidth = 1); LineCircle(glm::vec4 color, uint linewidth = 1);
void init() override; void init() override;
void accept(Visitor& v) override; void accept(Visitor& v) override;
}; };
// Draw a Rectangle (triangle strip) with a texture
class ObjModel : public Primitive { class ObjModel : public Primitive {
public: public:
@@ -105,10 +124,10 @@ public:
void draw (glm::mat4 modelview, glm::mat4 projection) override; void draw (glm::mat4 modelview, glm::mat4 projection) override;
void accept (Visitor& v) override; void accept (Visitor& v) override;
inline std::string getFilename() const { return filename_; } inline std::string getResource() const { return resource_; }
protected: protected:
std::string filename_; std::string resource_;
std::string texture_filename_; std::string texture_filename_;
uint textureindex_; uint textureindex_;
}; };

View File

@@ -132,6 +132,15 @@ bool Rendering::Init()
g_setenv ("GST_GL_API", "opengl3", FALSE); g_setenv ("GST_GL_API", "opengl3", FALSE);
gst_init (NULL, NULL); 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 #if GST_GL_HAVE_PLATFORM_WGL
global_gl_context = gst_gl_context_new_wrapped (display, (guintptr) wglGetCurrentContext (), global_gl_context = gst_gl_context_new_wrapped (display, (guintptr) wglGetCurrentContext (),
GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL); GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL);
@@ -300,16 +309,22 @@ void Rendering::PopAttrib()
draw_attributes_.pop_front(); draw_attributes_.pop_front();
// set attribute element to default // set attribute element to default
RenderingAttrib ra = main_window_attributes_; RenderingAttrib ra = currentAttrib();
// if there is an element at top, use it
if (draw_attributes_.size() > 0)
ra = draw_attributes_.front();
// apply Changes to OpenGL // apply Changes to OpenGL
glViewport(0, 0, ra.viewport.x, ra.viewport.y); glViewport(0, 0, ra.viewport.x, ra.viewport.y);
glClearColor(ra.clear_color.r, ra.clear_color.g, ra.clear_color.b, 1.f); 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() glm::mat4 Rendering::Projection()
{ {

View File

@@ -58,6 +58,7 @@ public:
// push and pop rendering attributes // push and pop rendering attributes
void PushAttrib(RenderingAttrib ra); void PushAttrib(RenderingAttrib ra);
void PopAttrib(); void PopAttrib();
RenderingAttrib currentAttrib();
// request screenshot // request screenshot
void RequestScreenshot(); void RequestScreenshot();

View File

@@ -10,9 +10,11 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <glm/ext/vector_float3.hpp> #include <glm/ext/vector_float3.hpp>
#include <glm/ext/vector_float4.hpp> #include <glm/ext/vector_float4.hpp>
#include <glm/ext/vector_float4.hpp>
#include <glm/ext/matrix_float4x4.hpp> #include <glm/ext/matrix_float4x4.hpp>
#include <glm/gtc/matrix_access.hpp> #include <glm/gtc/matrix_access.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/random.hpp>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
@@ -82,8 +84,8 @@ void Primitive::init()
glBindVertexArray( vao_ ); glBindVertexArray( vao_ );
// compute the memory needs for points normals and indicies // compute the memory needs for points normals and indicies
std::size_t sizeofPoints = sizeof(glm::vec3)*points_.size(); std::size_t sizeofPoints = sizeof(glm::vec3) * points_.size();
std::size_t sizeofColors = sizeof(glm::vec3)*colors_.size(); std::size_t sizeofColors = sizeof(glm::vec4) * colors_.size();
std::size_t sizeofTexCoords = sizeof(glm::vec2) * texCoords_.size(); std::size_t sizeofTexCoords = sizeof(glm::vec2) * texCoords_.size();
// setup the array buffers for vertices // 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) // 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 ); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void *)0 );
glEnableVertexAttribArray(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); glEnableVertexAttribArray(1);
if ( sizeofTexCoords ) { if ( sizeofTexCoords ) {
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void *)(sizeofPoints + sizeofColors) ); 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<glm::mat4>();
// animation_ = glm::translate(glm::identity<glm::mat4>(), 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::mat4>(), glm::vec3(delta.x, delta.y, 0.f));
}
void Animation::accept(Visitor& v)
{
Node::accept(v);
v.visit(*this);
}
void Scene::accept(Visitor& v) void Scene::accept(Visitor& v)
{ {
v.visit(*this); v.visit(*this);

74
Scene.h
View File

@@ -51,6 +51,32 @@ public:
glm::vec3 scale_, rotation_, translation_; 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<glm::vec3> points_;
std::vector<glm::vec4> colors_;
std::vector<glm::vec2> texCoords_;
std::vector<uint> indices_;
virtual void deleteGLBuffers_();
};
// Other Nodes establish hierarchy with a group of nodes
struct z_comparator struct z_comparator
{ {
@@ -73,32 +99,6 @@ private:
typedef std::multiset<Node*, z_comparator> NodeSet; typedef std::multiset<Node*, z_comparator> 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<glm::vec3> points_;
std::vector<glm::vec3> colors_;
std::vector<glm::vec2> texCoords_;
std::vector<uint> indices_;
virtual void deleteGLBuffers_();
};
// Other Nodes establish hierarchy with a group of nodes
class Group : public Node { class Group : public Node {
public: public:
@@ -144,6 +144,28 @@ protected:
NodeSet::iterator active_; 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 // A scene contains a root node and gives a simplified API to add nodes
class Scene { class Scene {

View File

@@ -81,8 +81,8 @@ void SessionVisitor::visit(ImageSurface &n)
// Node of a different type // Node of a different type
xmlCurrent_->SetAttribute("type", "ImageSurface"); xmlCurrent_->SetAttribute("type", "ImageSurface");
XMLText *filename = xmlDoc_->NewText( n.getFilename().c_str() ); XMLText *filename = xmlDoc_->NewText( n.getResource().c_str() );
XMLElement *image = xmlDoc_->NewElement("filename"); XMLElement *image = xmlDoc_->NewElement("resource");
image->InsertEndChild(filename); image->InsertEndChild(filename);
xmlCurrent_->InsertEndChild(image); xmlCurrent_->InsertEndChild(image);
} }
@@ -166,6 +166,16 @@ void SessionVisitor::visit(LineCircle &n)
xmlCurrent_->InsertEndChild(color); 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) void SessionVisitor::visit(Scene &n)
{ {

View File

@@ -26,6 +26,7 @@ public:
void visit(LineStrip& n) override; void visit(LineStrip& n) override;
void visit(LineSquare&) override; void visit(LineSquare&) override;
void visit(LineCircle& n) override; void visit(LineCircle& n) override;
void visit(ObjModel& n) override;
// Elements with attributes // Elements with attributes
void visit(MediaPlayer& n) override; void visit(MediaPlayer& n) override;

View File

@@ -120,6 +120,12 @@ void ShadingProgram::setUniform<glm::vec4>(const std::string& name, glm::vec4 va
glUniform4fv(glGetUniformLocation(id_, name.c_str()), 1, glm::value_ptr(v)); glUniform4fv(glGetUniformLocation(id_, name.c_str()), 1, glm::value_ptr(v));
} }
template<>
void ShadingProgram::setUniform<glm::vec3>(const std::string& name, glm::vec3 val) {
glm::vec3 v(val);
glUniform4fv(glGetUniformLocation(id_, name.c_str()), 1, glm::value_ptr(v));
}
template<> template<>
void ShadingProgram::setUniform<glm::mat4>(const std::string& name, glm::mat4 val) { void ShadingProgram::setUniform<glm::mat4>(const std::string& name, glm::mat4 val) {
glm::mat4 m(val); glm::mat4 m(val);
@@ -184,6 +190,9 @@ void Shader::use()
program_->setUniform("modelview", modelview); program_->setUniform("modelview", modelview);
program_->setUniform("color", color); program_->setUniform("color", color);
resolution = glm::vec3( Rendering::manager().currentAttrib().viewport, 0.f);
program_->setUniform("resolution", resolution);
// Blending Function // Blending Function
if ( blending != BLEND_NONE) { if ( blending != BLEND_NONE) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
@@ -199,6 +208,7 @@ void Shader::reset()
{ {
projection = glm::identity<glm::mat4>(); projection = glm::identity<glm::mat4>();
modelview = glm::identity<glm::mat4>(); modelview = glm::identity<glm::mat4>();
resolution = glm::vec3(1280.f, 720.f, 0.f);
color = glm::vec4(1.f, 1.f, 1.f, 1.f); color = glm::vec4(1.f, 1.f, 1.f, 1.f);
} }

View File

@@ -47,6 +47,7 @@ public:
glm::mat4 projection; glm::mat4 projection;
glm::mat4 modelview; glm::mat4 modelview;
glm::vec3 resolution;
glm::vec4 color; glm::vec4 color;
typedef enum { typedef enum {

View File

@@ -14,6 +14,7 @@ class MediaSurface;
class LineStrip; class LineStrip;
class LineSquare; class LineSquare;
class LineCircle; class LineCircle;
class ObjModel;
class MediaPlayer; class MediaPlayer;
class Shader; class Shader;
class ImageShader; class ImageShader;
@@ -33,6 +34,7 @@ public:
virtual void visit(LineStrip& n) = 0; virtual void visit(LineStrip& n) = 0;
virtual void visit(LineSquare& n) = 0; virtual void visit(LineSquare& n) = 0;
virtual void visit(LineCircle& n) = 0; virtual void visit(LineCircle& n) = 0;
virtual void visit(ObjModel& n) = 0;
virtual void visit(MediaPlayer& n) = 0; virtual void visit(MediaPlayer& n) = 0;
virtual void visit(Shader& n) = 0; virtual void visit(Shader& n) = 0;
virtual void visit(ImageShader& n) = 0; virtual void visit(ImageShader& n) = 0;

View File

@@ -139,8 +139,7 @@ makeMtlFilename ( std::string mtlfile, std::string objfile )
} }
bool loadMaterialLibrary ( string mtlText, bool loadMaterialLibrary ( string mtlText, map<string,Material*> &outMaterials)
map<string,Material*> &outMaterials)
{ {
Material *mat = nullptr; Material *mat = nullptr;
stringstream ifs(mtlText); stringstream ifs(mtlText);
@@ -180,10 +179,9 @@ bool loadObject(string objText,
vector<vec3> &outNormal, vector<vec3> &outNormal,
vector<vec2> &outUv, vector<vec2> &outUv,
vector<unsigned int> &outIndices, vector<unsigned int> &outIndices,
std::string &outMtlfilename, std::string &outMtlfilename
float scale) )
{ {
vector<vec3> positions; vector<vec3> positions;
vector<vec3> normals; vector<vec3> normals;
vector<vec2> uvs; vector<vec2> uvs;
@@ -242,7 +240,7 @@ bool loadObject(string objText,
outIndices.push_back(cachedIndex->second); outIndices.push_back(cachedIndex->second);
} else { } else {
int vertexIndex = outPositions.size(); int vertexIndex = outPositions.size();
outPositions.push_back(positions[index.position-1] * scale); outPositions.push_back(positions[index.position-1]);
if (index.normal != -1){ if (index.normal != -1){
outNormal.push_back(normals[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; return (outPositions.size()>0);
//printDebug(outPositions, outIndices);
return true;
} }
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 { struct Group {
vector<TriangleString> triangles; vector<TriangleString> triangles;
Material *mat; Material *mat;
@@ -288,30 +270,17 @@ bool loadObjectGroups ( string filename,
vector<vec3> &outNormal, vector<vec3> &outNormal,
vector<vec2> &outUv, vector<vec2> &outUv,
vector< vector<unsigned int> > &outIndices, // per group vector< vector<unsigned int> > &outIndices, // per group
vector< Material* >&outMaterials, // per group vector< Material* >&outMaterials // per group
float scale) )
{ {
map<string,Material*> materials;
vector<vec3> positions; vector<vec3> positions;
vector<vec3> normals; vector<vec3> normals;
vector<vec2> uvs; vector<vec2> uvs;
map<string,Group*> groups; map<string,Group*> groups;
string currentGroupName(""); string currentGroupName("");
map<string,Material*> materials;
vector<string> diffuseTextures;
vector<string> bumpTextures;
static int groupnum = 1; 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 ); string path = std::string( filename );
ifstream ifs ( path.c_str() , ifstream::in ); ifstream ifs ( path.c_str() , ifstream::in );
@@ -359,7 +328,7 @@ bool loadObjectGroups ( string filename,
std::cout << "materials[token] == " << materials[token] << std::endl; std::cout << "materials[token] == " << materials[token] << std::endl;
} }
if ( materials[token] == 0 ) { if ( materials[token] == 0 ) {
materials[token] = makeDefaultMaterial(); materials[token] = new Material;
} }
printDebug ( materials[token] ); printDebug ( materials[token] );
groups[currentGroupName]->mat = materials[token]; groups[currentGroupName]->mat = materials[token];
@@ -391,7 +360,7 @@ bool loadObjectGroups ( string filename,
oss << "dummy" << groupnum++; oss << "dummy" << groupnum++;
currentGroupName=oss.str(); currentGroupName=oss.str();
groups[currentGroupName] = new Group; 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); groupIndices.push_back(cachedIndex->second);
} else { } else {
int vertexIndex = outPositions.size(); int vertexIndex = outPositions.size();
outPositions.push_back(positions[index.position-1] * scale); outPositions.push_back(positions[index.position-1]);
if (index.normal != -1){ if (index.normal != -1){
outNormal.push_back(normals[index.normal-1]); outNormal.push_back(normals[index.normal-1]);
} }
@@ -473,73 +442,3 @@ void printDebug(Material *m){
cout << "bump texture " << m->bumpTexture << endl; 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<unsigned int> (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> 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();
// }
//}

View File

@@ -66,8 +66,7 @@ bool loadObject (std::string objText,
std::vector<glm::vec3> &outNormal, std::vector<glm::vec3> &outNormal,
std::vector<glm::vec2> &outUv, std::vector<glm::vec2> &outUv,
std::vector<unsigned int> &outIndices, std::vector<unsigned int> &outIndices,
std::string &outMtlfilename, std::string &outMtlfilename
float scale = 1.0f
); );
@@ -81,8 +80,7 @@ bool loadObjectGroups (std::string filename,
std::vector<glm::vec3> &outNormal, std::vector<glm::vec3> &outNormal,
std::vector<glm::vec2> &outUv, std::vector<glm::vec2> &outUv,
std::vector<std::vector<unsigned int> > &outIndices, // per group std::vector<std::vector<unsigned int> > &outIndices, // per group
std::vector<Material *> &outMaterials, // per group (w/textures) std::vector<Material *> &outMaterials // per group (w/textures)
float scale=1.0f
); );
#endif #endif

View File

@@ -255,20 +255,34 @@ int main(int, char**)
// init elements to the scene // init elements to the scene
//testnode3.getShader()->blending = Shader::BLEND_OPACITY; //testnode3.getShader()->blending = Shader::BLEND_OPACITY;
glm::vec3 color( 0.8f, 0.8f, 0.f ); ObjModel disk("models/disk.obj");
// LineCircle border(color); glm::vec4 pink( 0.8f, 0.f, 0.8f, 1.f );
LineSquare border(color, 2); 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"); ObjModel shadow("models/square_border.obj");
Group g1; 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); g1.scale_ = glm::vec3(1.2f, 1.2f, 1.f);
Group g2; Group g2;
g2.translation_ = glm::vec3(-1.f, -1.f, 0.f); g2.translation_ = glm::vec3(-1.f, -1.f, 0.4f);
// g2.rotation_ = glm::vec3(0.0f, 0.0f, 1.0f);
Animation A;
A.speed_ = 0.01f;
A.axis_ = glm::vec3(1.f, 1.f, 1.f);
std::vector<glm::vec3> pts = std::vector<glm::vec3> { glm::vec3( 0.f, 0.f, 0.f ) };
Points P(pts, pink);
P.setPointSize(60);
// build tree // build tree
scene.root_.addChild(&disk);
scene.root_.addChild(&circle);
g1.addChild(&shadow); g1.addChild(&shadow);
g1.addChild(&testnode3); g1.addChild(&testnode3);
g1.addChild(&border); g1.addChild(&border);
@@ -277,7 +291,11 @@ int main(int, char**)
g2.addChild(&shadow); g2.addChild(&shadow);
g2.addChild(&testnode1); g2.addChild(&testnode1);
g2.addChild(&border); g2.addChild(&border);
scene.root_.addChild(&g2);
// A.addChild(&g2);
A.addChild(&P);
scene.root_.addChild(&A);
// init output FBO // init output FBO
output = new FrameBuffer(1280, 720); output = new FrameBuffer(1280, 720);

View File

@@ -2,11 +2,12 @@
out vec4 FragColor; 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() void main()
{ {
FragColor = color * vec4(vertexColor,1.0); FragColor = color * vertexColor;
} }

View File

@@ -1,9 +1,9 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 position; 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 modelview;
uniform mat4 projection; uniform mat4 projection;

View File

@@ -2,19 +2,20 @@
out vec4 FragColor; out vec4 FragColor;
in vec3 vertexColor; in vec4 vertexColor;
in vec2 vertexUV; in vec2 vertexUV;
uniform sampler2D sourceTexture; uniform sampler2D sourceTexture;
uniform vec4 color; uniform vec4 color;
uniform float contrast; uniform float contrast;
uniform float brightness; uniform float brightness;
uniform vec3 resolution; // viewport resolution (in pixels)
void main() void main()
{ {
vec4 texturecolor = texture(sourceTexture, vertexUV); vec4 texturecolor = texture(sourceTexture, vertexUV);
vec3 transformedRGB = mix(vec3(0.62), texturecolor.rgb, contrast + 1.0) + brightness; 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);
} }

View File

@@ -1,10 +1,10 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 position; layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color; layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texCoord; layout (location = 2) in vec2 texCoord;
out vec3 vertexColor; out vec4 vertexColor;
out vec2 vertexUV; out vec2 vertexUV;
uniform mat4 modelview; uniform mat4 modelview;