Introducing scene graph

This commit is contained in:
brunoherbelin
2020-03-31 17:01:33 +02:00
parent cf5a3b3fc9
commit 2fc01e8435
17 changed files with 837 additions and 184 deletions

80
Scene.h
View File

@@ -1,88 +1,67 @@
#ifndef SCENE_H
#define SCENE_H
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_access.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
class Visitor;
#include "Visitor.h"
// Forward declare classes referenced
class Shader;
// Base virtual class for all Node types
// Manages modelview transformations and culling
class Node {
public:
Node() : parent_(), visible_(true), count_(0), localToWorld_(glm::mat4()), worldToLocal_(glm::mat4()) {}
virtual ~ModelNode() {}
Node();
virtual ~Node() {}
virtual void init() = 0;
virtual void update( float dt ) = 0;
virtual void draw ( glm::mat4 modelview, glm::mat4 projection) = 0;
virtual void Accept(Visitor& dispatcher) = 0;
virtual void draw( glm::mat4 modelview, glm::mat4 projection) = 0;
virtual void accept(Visitor& v) = 0;
virtual void update( float dt );
virtual glm::mat4 getWorldToLocalMatrix() {return worldToLocal_;}
virtual glm::mat4 getLocalToWorldMatrix() {return localToWorld_;}
protected:
Node* parent_;
bool visible_;
glm::mat4 worldToLocal_;
glm::mat4 localToWorld_;
glm::mat4 transform_;
};
// Forward declare different kind of Node
class Primitive;
class Group;
// Declares the interface for the visitors
class Visitor {
public:
// Declare overloads for each kind of Node to visit
virtual void Visit(Primitive& file) = 0;
virtual void Visit(Group& file) = 0;
};
// Leaf Nodes are primitives that can be rendered
class Primitive : public Node {
public:
Primitive() : Node(), effect(nullptr), vao_(0), arrayBuffer_(0), elementBuffer_(0), drawingPrimitive_(0) {}
Primitive() : Node(), shader_(nullptr), vao_(0), arrayBuffer_(0), elementBuffer_(0), drawingPrimitive_(0) {}
virtual ~Primitive();
virtual void init ();
virtual void update ( float dt );
virtual void draw ( glm::mat4 modelview, glm::mat4 projection);
virtual void Accept(Visitor& dispatcher) override {
dispatcher.Visit(*this);
}
virtual void accept(Visitor& v) override { v.visit(*this); }
virtual glm::mat4 getWorldToLocalMatrix();
virtual glm::mat4 getLocalToWorldMatrix();
virtual void setEffect( Effect* e );
virtual void setDrawingPrimitive ( GLuint prim );
virtual void generateAndLoadArrayBuffer();
Shader *getShader() { return shader_; }
void setShader( Shader* e ) { shader_ = e; }
protected:
Effect* effect_;
Shader* shader_;
unsigned int vao_;
unsigned int arrayBuffer_;
unsigned int elementBuffer_;
unsigned int drawingPrimitive_;
std::vector<glm::vec3> points_;
std::vector<glm::vec3> normals_;
std::vector<unsigned int> indices_;
std::vector<glm::vec2> texCoords_;
std::vector<glm::vec3> colors_;
std::vector<glm::vec2> texCoords_;
std::vector<unsigned int> indices_;
void deleteGLBuffers_();
};
// Other Nodes establish hierarchy with a group of nodes
class Group : public Node {
public:
@@ -92,22 +71,29 @@ public:
virtual void init ();
virtual void update ( float dt );
virtual void draw ( glm::mat4 modelview, glm::mat4 projection);
virtual void Accept(Visitor& dispatcher)override {
dispatcher.Visit(*this);
}
virtual glm::mat4 getWorldToLocalMatrix();
virtual glm::mat4 getLocalToWorldMatrix();
virtual void accept(Visitor& v) override { v.visit(*this); }
virtual void addChild ( Node *child );
virtual Node* getChild ( int i );
virtual int numChildren();
virtual int numChildren();
protected:
std::vector< Node* > children_;
};
// A scene contains a root node and gives a simplified API to add nodes
class Scene {
public:
Scene() {}
Group *getRoot() { return &root_; }
void accept(Visitor& v) { v.visit(*this); }
Group root_;
};