diff --git a/core/Camera.h b/core/Camera.h index b9accd0..aff6f1e 100644 --- a/core/Camera.h +++ b/core/Camera.h @@ -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: diff --git a/core/Drawable.h b/core/Drawable.h new file mode 100644 index 0000000..9c0f558 --- /dev/null +++ b/core/Drawable.h @@ -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 */ diff --git a/core/Mesh.h b/core/Mesh.h index 4b95af5..751fcc6 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -4,18 +4,18 @@ #include #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; diff --git a/core/Node.cpp b/core/Node.cpp new file mode 100644 index 0000000..2a49360 --- /dev/null +++ b/core/Node.cpp @@ -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); +} diff --git a/core/Node.h b/core/Node.h new file mode 100644 index 0000000..79a023d --- /dev/null +++ b/core/Node.h @@ -0,0 +1,22 @@ +#ifndef NODE_H +#define NODE_H + +#include + +class Node; // Forwards declare +class NodeList : public std::vector { +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 */ diff --git a/core/ScriptingLanguage.h b/core/ScriptingLanguage.h new file mode 100644 index 0000000..bd492ea --- /dev/null +++ b/core/ScriptingLanguage.h @@ -0,0 +1,10 @@ +#ifndef SCRIPTINGLANGUAGE_H +#define SCRIPTINGLANGUAGE_H + +class ScriptingLangauge { + void Initialize(); + void Update(); + void Close(); +} + +#endif /* SCRIPTINGLANGUAGE_H */ diff --git a/core/Spatial.h b/core/Spatial.h new file mode 100644 index 0000000..35777fd --- /dev/null +++ b/core/Spatial.h @@ -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 */ diff --git a/core/Transform.cpp b/core/Transform.cpp index 5ed80c4..d8fd740 100644 --- a/core/Transform.cpp +++ b/core/Transform.cpp @@ -2,6 +2,7 @@ Transform::Transform() { position = Vector3(0.0f); + rotation = Vector3(0.0f); } Transform::Transform(Vector3 position, Vector3 rotation) { diff --git a/core/couch.cpp b/core/couch.cpp index 46ebec0..cd085bf 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -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(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(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); diff --git a/main.lua b/main.lua index d6079cf..c39c9b7 100644 --- a/main.lua +++ b/main.lua @@ -1,4 +1,5 @@ local ball +local ball1 local camera local LEFT = 263 @@ -21,10 +22,10 @@ function init() camera.transform:Translate(0.0, 0.0, 10.0) ball = couch.Ball() ball:SetupMesh() - couch.AddMeshToList(ball) + couch.Node.GetRoot().children:Append(ball) ball1 = couch.Ball() ball1:SetupMesh() - couch.AddMeshToList(ball1) + couch.Node.GetRoot().children:Append(ball1) ball1.transform:Translate(0.0, 3.0, 0.0) end diff --git a/roadmap.md b/roadmap.md index 86bed51..c3f0350 100644 --- a/roadmap.md +++ b/roadmap.md @@ -5,7 +5,8 @@ - GLEW to wrangle extensions - GLFW3 for handling windows and whatnot - 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 ## 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) 3. Camera, to look at 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? diff --git a/scripting/couch.i b/scripting/couch.i index fc0f8b0..372790e 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -4,18 +4,22 @@ $1 = (cfloat) lua_tonumber(L, $input); } %{ +#include "Node.h" #include "Transform.h" +#include "Spatial.h" +#include "Drawable.h" #include "Mesh.h" #include "Ball.h" #include "Camera.h" -extern void AddMeshToList(Mesh &mesh); %} +typedef float cfloat; struct Vector3 { - double x, y, z; + cfloat x, y, z; }; +%include "Node.h" +%include "Spatial.h" %include "Transform.h" +%include "Drawable.h" %include "Mesh.h" %include "Ball.h" %include "Camera.h" - -extern void AddMeshToList(Mesh &mesh);