Scene tree
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include "Transform.h"
|
||||
#include "Spatial.h"
|
||||
|
||||
class Camera {
|
||||
class Camera : public Spatial {
|
||||
public:
|
||||
Camera();
|
||||
Transform transform;
|
||||
void MakeCurrent();
|
||||
static Camera *GetCurrentCamera();
|
||||
private:
|
||||
|
||||
12
core/Drawable.h
Normal file
12
core/Drawable.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef DRAWABLE_H
|
||||
#define DRAWABLE_H
|
||||
|
||||
#include "Node.h"
|
||||
|
||||
class Drawable : virtual public Node {
|
||||
public:
|
||||
virtual bool IsDrawable() const {return true;}
|
||||
virtual void Draw() = 0;
|
||||
};
|
||||
|
||||
#endif /* DRAWABLE_H */
|
||||
@@ -4,18 +4,18 @@
|
||||
#include <list>
|
||||
|
||||
#include "types.h"
|
||||
#include "Spatial.h"
|
||||
#include "Drawable.h"
|
||||
#include "Vertex.h"
|
||||
#include "Index.h"
|
||||
#include "Transform.h"
|
||||
|
||||
class Mesh {
|
||||
class Mesh : public Spatial, public Drawable {
|
||||
public:
|
||||
VertexList vertices;
|
||||
IndexList indices;
|
||||
Transform transform;
|
||||
Mesh();
|
||||
Mesh(VertexList vertices, IndexList indices);
|
||||
void Draw();
|
||||
virtual void Draw();
|
||||
virtual void SetupMesh();
|
||||
private:
|
||||
Id VAO, VBO, EBO;
|
||||
|
||||
16
core/Node.cpp
Normal file
16
core/Node.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "Node.h"
|
||||
|
||||
Node *Node::root = {new Node()};
|
||||
Node *Node::GetRoot() {
|
||||
return root;
|
||||
}
|
||||
bool Node::IsDrawable() const {
|
||||
return false;
|
||||
}
|
||||
bool Node::IsTransformable() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void NodeList::Append(Node *node) {
|
||||
push_back(node);
|
||||
}
|
||||
22
core/Node.h
Normal file
22
core/Node.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Node; // Forwards declare
|
||||
class NodeList : public std::vector<Node*> {
|
||||
public:
|
||||
void Append(Node *node);
|
||||
};
|
||||
|
||||
class Node {
|
||||
public:
|
||||
NodeList children;
|
||||
static Node *GetRoot();
|
||||
virtual bool IsDrawable() const;
|
||||
virtual bool IsTransformable() const;
|
||||
private:
|
||||
static Node *root;
|
||||
};
|
||||
|
||||
#endif /* NODE_H */
|
||||
10
core/ScriptingLanguage.h
Normal file
10
core/ScriptingLanguage.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef SCRIPTINGLANGUAGE_H
|
||||
#define SCRIPTINGLANGUAGE_H
|
||||
|
||||
class ScriptingLangauge {
|
||||
void Initialize();
|
||||
void Update();
|
||||
void Close();
|
||||
}
|
||||
|
||||
#endif /* SCRIPTINGLANGUAGE_H */
|
||||
13
core/Spatial.h
Normal file
13
core/Spatial.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef SPATIAL_H
|
||||
#define SPATIAL_H
|
||||
|
||||
#include "Node.h"
|
||||
#include "Transform.h"
|
||||
|
||||
class Spatial : virtual public Node {
|
||||
public:
|
||||
Transform transform;
|
||||
virtual bool IsTransformable() const { return true;}
|
||||
};
|
||||
|
||||
#endif /* SPATIAL_H */
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
Transform::Transform() {
|
||||
position = Vector3(0.0f);
|
||||
rotation = Vector3(0.0f);
|
||||
}
|
||||
|
||||
Transform::Transform(Vector3 position, Vector3 rotation) {
|
||||
|
||||
@@ -21,23 +21,39 @@ extern "C" {
|
||||
#include "Ball.h"
|
||||
#include "Camera.h"
|
||||
#include "Input.h"
|
||||
#include "Node.h"
|
||||
|
||||
Window *window;
|
||||
|
||||
const int width = 800;
|
||||
const int height = 600;
|
||||
|
||||
MeshList meshes;
|
||||
Node *root;
|
||||
|
||||
void AddMeshToList(Mesh &mesh) {
|
||||
meshes.push_front(&mesh);
|
||||
}
|
||||
#ifdef LUA_SCRIPTING
|
||||
|
||||
extern "C" int luaopen_couch(lua_State* L);
|
||||
|
||||
#endif // LUA_SCRIPTING
|
||||
|
||||
void render(Node *curr, Shader shader, Matrix model) {
|
||||
if (curr->IsDrawable()) {
|
||||
if (curr->IsTransformable()) {
|
||||
Spatial *spatial = dynamic_cast<Spatial*>(curr);
|
||||
model = glm::rotate(model, spatial->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
|
||||
model = glm::rotate(model, spatial->transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::rotate(model, spatial->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
|
||||
model = glm::translate(model, spatial->transform.position);
|
||||
shader.UpdateModel(model);
|
||||
}
|
||||
Drawable *drawable = dynamic_cast<Drawable*>(curr);
|
||||
drawable->Draw();
|
||||
}
|
||||
for (Node *child : curr->children) {
|
||||
render(child, shader, model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -63,6 +79,8 @@ int main() {
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
root = Node::GetRoot();
|
||||
|
||||
#ifdef LUA_SCRIPTING
|
||||
lua_State *L;
|
||||
L = luaL_newstate();
|
||||
@@ -113,15 +131,8 @@ int main() {
|
||||
lua_call(L, 1, 0);
|
||||
#endif // LUA_SCRIPTING
|
||||
|
||||
for (Mesh *mesh : meshes) {
|
||||
Matrix model(1.0f);
|
||||
model = glm::rotate(model, mesh->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
|
||||
model = glm::rotate(model, mesh->transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::rotate(model, mesh->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
|
||||
model = glm::translate(model, mesh->transform.position);
|
||||
shader.UpdateModel(model);
|
||||
mesh->Draw();
|
||||
}
|
||||
// Render the scene tree
|
||||
render(root, shader, Matrix(1.0f));
|
||||
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
Reference in New Issue
Block a user