Scene tree

This commit is contained in:
Dane Johnson 2021-01-14 11:52:01 -06:00
parent 35f43e9520
commit a3e4a0d512
12 changed files with 122 additions and 27 deletions

View File

@ -1,12 +1,11 @@
#ifndef CAMERA_H #ifndef CAMERA_H
#define CAMERA_H #define CAMERA_H
#include "Transform.h" #include "Spatial.h"
class Camera { class Camera : public Spatial {
public: public:
Camera(); Camera();
Transform transform;
void MakeCurrent(); void MakeCurrent();
static Camera *GetCurrentCamera(); static Camera *GetCurrentCamera();
private: private:

12
core/Drawable.h Normal file
View 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 */

View File

@ -4,18 +4,18 @@
#include <list> #include <list>
#include "types.h" #include "types.h"
#include "Spatial.h"
#include "Drawable.h"
#include "Vertex.h" #include "Vertex.h"
#include "Index.h" #include "Index.h"
#include "Transform.h"
class Mesh { class Mesh : public Spatial, public Drawable {
public: public:
VertexList vertices; VertexList vertices;
IndexList indices; IndexList indices;
Transform transform;
Mesh(); Mesh();
Mesh(VertexList vertices, IndexList indices); Mesh(VertexList vertices, IndexList indices);
void Draw(); virtual void Draw();
virtual void SetupMesh(); virtual void SetupMesh();
private: private:
Id VAO, VBO, EBO; Id VAO, VBO, EBO;

16
core/Node.cpp Normal file
View 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
View 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
View 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
View 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 */

View File

@ -2,6 +2,7 @@
Transform::Transform() { Transform::Transform() {
position = Vector3(0.0f); position = Vector3(0.0f);
rotation = Vector3(0.0f);
} }
Transform::Transform(Vector3 position, Vector3 rotation) { Transform::Transform(Vector3 position, Vector3 rotation) {

View File

@ -21,23 +21,39 @@ extern "C" {
#include "Ball.h" #include "Ball.h"
#include "Camera.h" #include "Camera.h"
#include "Input.h" #include "Input.h"
#include "Node.h"
Window *window; Window *window;
const int width = 800; const int width = 800;
const int height = 600; const int height = 600;
MeshList meshes; Node *root;
void AddMeshToList(Mesh &mesh) {
meshes.push_front(&mesh);
}
#ifdef LUA_SCRIPTING #ifdef LUA_SCRIPTING
extern "C" int luaopen_couch(lua_State* L); extern "C" int luaopen_couch(lua_State* L);
#endif // LUA_SCRIPTING #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() { int main() {
@ -63,6 +79,8 @@ int main() {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
root = Node::GetRoot();
#ifdef LUA_SCRIPTING #ifdef LUA_SCRIPTING
lua_State *L; lua_State *L;
L = luaL_newstate(); L = luaL_newstate();
@ -113,15 +131,8 @@ int main() {
lua_call(L, 1, 0); lua_call(L, 1, 0);
#endif // LUA_SCRIPTING #endif // LUA_SCRIPTING
for (Mesh *mesh : meshes) { // Render the scene tree
Matrix model(1.0f); render(root, shader, Matrix(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();
}
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -1,4 +1,5 @@
local ball local ball
local ball1
local camera local camera
local LEFT = 263 local LEFT = 263
@ -21,10 +22,10 @@ function init()
camera.transform:Translate(0.0, 0.0, 10.0) camera.transform:Translate(0.0, 0.0, 10.0)
ball = couch.Ball() ball = couch.Ball()
ball:SetupMesh() ball:SetupMesh()
couch.AddMeshToList(ball) couch.Node.GetRoot().children:Append(ball)
ball1 = couch.Ball() ball1 = couch.Ball()
ball1:SetupMesh() ball1:SetupMesh()
couch.AddMeshToList(ball1) couch.Node.GetRoot().children:Append(ball1)
ball1.transform:Translate(0.0, 3.0, 0.0) ball1.transform:Translate(0.0, 3.0, 0.0)
end end

View File

@ -5,7 +5,8 @@
- GLEW to wrangle extensions - GLEW to wrangle extensions
- GLFW3 for handling windows and whatnot - GLFW3 for handling windows and whatnot
- Targets Windows, Linux - Targets Windows, Linux
- ~~Guile with GOOPS as scripting language~~ Lua is the scripting language with Guile as a strech goal - ~~Guile with GOOPS as scripting language~~ ~~Lua is the scripting language with Guile as a stretch goal~~
- Python?
- No editor planned - No editor planned
## Roadmap ## Roadmap
@ -15,3 +16,8 @@ I want my balls to be programmable, I need to create a few key classes
2. Transforms, which represent position (of my balls) 2. Transforms, which represent position (of my balls)
3. Camera, to look at my balls 3. Camera, to look at my balls
4. Shaders, to render my balls 4. Shaders, to render my balls
### Programmable Balls Postmortem
Everything seems to work, but we need a system of what goes in the scene
I think a scene tree like the one in Godot would be cool
I keep switching scripting languages, can I make it so it's programmable in many languages, switchable via command line?

View File

@ -4,18 +4,22 @@
$1 = (cfloat) lua_tonumber(L, $input); $1 = (cfloat) lua_tonumber(L, $input);
} }
%{ %{
#include "Node.h"
#include "Transform.h" #include "Transform.h"
#include "Spatial.h"
#include "Drawable.h"
#include "Mesh.h" #include "Mesh.h"
#include "Ball.h" #include "Ball.h"
#include "Camera.h" #include "Camera.h"
extern void AddMeshToList(Mesh &mesh);
%} %}
typedef float cfloat;
struct Vector3 { struct Vector3 {
double x, y, z; cfloat x, y, z;
}; };
%include "Node.h"
%include "Spatial.h"
%include "Transform.h" %include "Transform.h"
%include "Drawable.h"
%include "Mesh.h" %include "Mesh.h"
%include "Ball.h" %include "Ball.h"
%include "Camera.h" %include "Camera.h"
extern void AddMeshToList(Mesh &mesh);