diff --git a/.gitignore b/.gitignore index 285bbad..458c8c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.o -core/couch +couch diff --git a/Makefile b/Makefile index 0d78d07..2d0eff5 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,14 @@ -all: - $(MAKE) -C core +include common.mk + +CORE_SRC = $(wildcard core/*.cpp) +CORE_OBJ = $(CORE_SRC:.cpp=.o) + +all: couch +couch: $(CORE_OBJ) scripting/couch_wrap.o + $(CXX) $(LIBS) -o $@ $^ + +$(CORE_OBJ): %.o: %.cpp + $(MAKE) -C core $(notdir $@) + +scripting/couch_wrap.o: scripting/couch.i + $(MAKE) -C scripting $(notdir $@) diff --git a/common.mk b/common.mk index ca37990..f20804c 100644 --- a/common.mk +++ b/common.mk @@ -1,3 +1,3 @@ -CXX = g++ -CXXFLAGS = `pkg-config --cflags gl glew glfw3 guile-2.2` -g -LIBS = `pkg-config --libs gl glew glfw3 guile-2.2` +CXX := g++ +CXXFLAGS := `pkg-config --cflags gl glew glfw3 lua` -fPIC -g +LIBS := `pkg-config --libs gl glew glfw3 lua` diff --git a/core/Ball.cpp b/core/Ball.cpp index 7ee30f2..be4d8b8 100644 --- a/core/Ball.cpp +++ b/core/Ball.cpp @@ -1,12 +1,35 @@ #include "Ball.h" Ball::Ball() { + // It's a cube really + // Front + vertices.push_back(Vertex(1.0f, 1.0f, 1.0f)); + vertices.push_back(Vertex(1.0f, -1.0f, 1.0f)); + vertices.push_back(Vertex(-1.0f, 1.0f, 1.0f)); + vertices.push_back(Vertex(-1.0f, -1.0f, 1.0f)); + // Back vertices.push_back(Vertex(1.0f, 1.0f, -1.0f)); vertices.push_back(Vertex(1.0f, -1.0f, -1.0f)); vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f)); vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f)); + //Front indices.push_back(Index(0, 1, 2)); indices.push_back(Index(1, 2, 3)); + //Back + indices.push_back(Index(4, 5, 6)); + indices.push_back(Index(5, 6, 7)); + // Top + indices.push_back(Index(0, 4, 6)); + indices.push_back(Index(0, 2, 6)); + // Bottom + indices.push_back(Index(1, 3, 7)); + indices.push_back(Index(1, 5, 7)); + // Left side + indices.push_back(Index(0, 1, 5)); + indices.push_back(Index(0, 4, 5)); + // Right side + indices.push_back(Index(2, 3, 7)); + indices.push_back(Index(2, 6, 7)); } diff --git a/core/Camera.cpp b/core/Camera.cpp new file mode 100644 index 0000000..e1fa948 --- /dev/null +++ b/core/Camera.cpp @@ -0,0 +1,17 @@ +#include "Camera.h" + +Camera::Camera() { + if (!currentCamera) { + currentCamera = this; + } +} + +void Camera::MakeCurrent() { + currentCamera = this; +} + +Camera*Camera::GetCurrentCamera() { + return currentCamera; +} + +Camera *Camera::currentCamera = nullptr; diff --git a/core/Camera.h b/core/Camera.h new file mode 100644 index 0000000..b9accd0 --- /dev/null +++ b/core/Camera.h @@ -0,0 +1,16 @@ +#ifndef CAMERA_H +#define CAMERA_H + +#include "Transform.h" + +class Camera { +public: + Camera(); + Transform transform; + void MakeCurrent(); + static Camera *GetCurrentCamera(); +private: + static Camera *currentCamera; +}; + +#endif /* CAMERA_H */ diff --git a/core/Makefile b/core/Makefile index 414f981..ea22c7d 100644 --- a/core/Makefile +++ b/core/Makefile @@ -1,8 +1,8 @@ include ../common.mk +CXXFLAGS := $(CXXFLAGS) -DLUA_SCRIPTING + SOURCES = $(wildcard *.cpp) OBJS = $(SOURCES:.cpp=.o) -all: couch -couch: $(OBJS) - $(CXX) $(LIBS) -o $@ $^ +all: $(OBJS) diff --git a/core/Mesh.h b/core/Mesh.h index 2ae9d6a..4b95af5 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -1,6 +1,8 @@ #ifndef MESH_H #define MESH_H +#include + #include "types.h" #include "Vertex.h" #include "Index.h" @@ -19,4 +21,6 @@ private: Id VAO, VBO, EBO; }; +typedef std::list MeshList; + #endif /* MESH_H */ diff --git a/core/Transform.cpp b/core/Transform.cpp index 6d9ac4d..b72bc50 100644 --- a/core/Transform.cpp +++ b/core/Transform.cpp @@ -7,3 +7,7 @@ Transform::Transform() { Transform::Transform(Vector3 position) { this->position = position; } + +void Transform::Translate(cfloat x, cfloat y, cfloat z) { + position = position + Vector3(x, y, z); +} diff --git a/core/Transform.h b/core/Transform.h index 11cc46b..49f1970 100644 --- a/core/Transform.h +++ b/core/Transform.h @@ -7,6 +7,7 @@ struct Transform { Transform(); Transform(Vector3 position); Vector3 position; + void Translate(cfloat x, cfloat y, cfloat z); }; #endif /* TRANSFORM_H */ diff --git a/core/couch.cpp b/core/couch.cpp index 6028a1b..a42717e 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -1,21 +1,45 @@ +// C++ includes #include +#ifdef LUA_SCRIPTING +// Lua includes +extern "C" { +#include +#include +#include +} +#endif // LUA_SCRIPTING + +// OpenGL Includes #include #include - #include #include "types.h" #include "Shader.h" #include "Ball.h" +#include "Camera.h" Window *window; const int width = 800; const int height = 600; +MeshList meshes; + +void AddMeshToList(Mesh &mesh) { + meshes.push_front(&mesh); +} +#ifdef LUA_SCRIPTING + +extern "C" int luaopen_couch(lua_State* L); + +#endif // LUA_SCRIPTING + + int main() { + int err; glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -38,29 +62,70 @@ int main() { glViewport(0, 0, width, height); +#ifdef LUA_SCRIPTING + lua_State *L; + L = luaL_newstate(); + luaopen_base(L); + luaopen_couch(L); + if (luaL_loadfile(L, "main.lua") == 0){ + lua_call(L, 0, 0); + lua_getglobal(L, "init"); + lua_call(L, 0, 0); + } else { + std::cerr << "Could not find main.lua" << std::endl; + return 1; + } +#endif // LUA_SCRIPTING + + Camera defaultCamera; + Shader shader("shaders/flat.vert", "shaders/flat.frag"); shader.Use(); - Ball ball; - ball.SetupMesh(); - Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f); shader.UpdateProjection(projection); - Matrix view(1.0f); - shader.UpdateView(view); + + double lastTime = glfwGetTime(); + double delta = 0.0; while(!glfwWindowShouldClose(window)) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - Matrix model(1.0f); - model = glm::translate(model, ball.transform.position); - shader.UpdateModel(model); - ball.Draw(); + + Matrix view(1.0f); + Camera *camera = Camera::GetCurrentCamera(); + view = glm::translate(view, -camera->transform.position); + shader.UpdateView(view); + + +#ifdef LUA_SCRIPTING + lua_getglobal(L, "update"); + lua_pushnumber(L, delta); + lua_call(L, 1, 0); +#endif // LUA_SCRIPTING + + for (Mesh *mesh : meshes) { + Matrix model(1.0f); + model = glm::translate(model, mesh->transform.position); + shader.UpdateModel(model); + mesh->Draw(); + } + glfwSwapBuffers(window); glfwPollEvents(); + + double curTime = glfwGetTime(); + delta = curTime - lastTime; + lastTime = curTime; } + +#ifdef LUA_SCRIPTING + + lua_close(L); + +#endif // LUA_SCRIPTING glfwDestroyWindow(window); glfwTerminate(); diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..b283af4 --- /dev/null +++ b/main.lua @@ -0,0 +1,14 @@ +local ball +local camera + +function init() + camera = couch.Camera() + camera:MakeCurrent() + ball = couch.Ball() + ball:SetupMesh() + couch.AddMeshToList(ball) +end + +function update(delta) + camera.transform:Translate(0.0, 0.0, 1.0 * delta); +end diff --git a/roadmap.md b/roadmap.md index 7a10651..86bed51 100644 --- a/roadmap.md +++ b/roadmap.md @@ -5,7 +5,7 @@ - GLEW to wrangle extensions - GLFW3 for handling windows and whatnot - Targets Windows, Linux -- Guile with GOOPS as scripting language +- ~~Guile with GOOPS as scripting language~~ Lua is the scripting language with Guile as a strech goal - No editor planned ## Roadmap diff --git a/scripting/.gitignore b/scripting/.gitignore new file mode 100644 index 0000000..12a311c --- /dev/null +++ b/scripting/.gitignore @@ -0,0 +1,2 @@ +couch_wrap.cpp +couch_wrap.o \ No newline at end of file diff --git a/scripting/Makefile b/scripting/Makefile new file mode 100644 index 0000000..cdc18e7 --- /dev/null +++ b/scripting/Makefile @@ -0,0 +1,12 @@ +include ../common.mk + +SWIG := swig +SWIG_OPTS := -lua + +all: couch_wrap.o + +couch_wrap.o: couch_wrap.cpp + $(CXX) $(CXXFLAGS) -I../core -c -o $@ $^ + +couch_wrap.cpp: couch.i + $(SWIG) -c++ $(SWIG_OPTS) -I../core -o $@ $^ diff --git a/scripting/couch.i b/scripting/couch.i new file mode 100644 index 0000000..d4ad312 --- /dev/null +++ b/scripting/couch.i @@ -0,0 +1,20 @@ +%module couch + +%typemap(in) cfloat { + $1 = (cfloat) lua_tonumber(L, $input); +} + +%{ +#include "types.h" +#include "Transform.h" +#include "Mesh.h" +#include "Ball.h" +#include "Camera.h" +extern void AddMeshToList(Mesh &mesh); +%} +%include "types.h" +%include "Transform.h" +%include "Mesh.h" +%include "Ball.h" +%include "Camera.h" +extern void AddMeshToList(Mesh &mesh);