lua integration

This commit is contained in:
Dane Johnson 2021-01-13 16:47:16 -06:00
parent d5788228f6
commit 86f6203efc
16 changed files with 210 additions and 20 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
*.o *.o
core/couch couch

View File

@ -1,2 +1,14 @@
all: include common.mk
$(MAKE) -C core
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 $@)

View File

@ -1,3 +1,3 @@
CXX = g++ CXX := g++
CXXFLAGS = `pkg-config --cflags gl glew glfw3 guile-2.2` -g CXXFLAGS := `pkg-config --cflags gl glew glfw3 lua` -fPIC -g
LIBS = `pkg-config --libs gl glew glfw3 guile-2.2` LIBS := `pkg-config --libs gl glew glfw3 lua`

View File

@ -1,12 +1,35 @@
#include "Ball.h" #include "Ball.h"
Ball::Ball() { 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));
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(0, 1, 2));
indices.push_back(Index(1, 2, 3)); 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));
} }

17
core/Camera.cpp Normal file
View File

@ -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;

16
core/Camera.h Normal file
View File

@ -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 */

View File

@ -1,8 +1,8 @@
include ../common.mk include ../common.mk
CXXFLAGS := $(CXXFLAGS) -DLUA_SCRIPTING
SOURCES = $(wildcard *.cpp) SOURCES = $(wildcard *.cpp)
OBJS = $(SOURCES:.cpp=.o) OBJS = $(SOURCES:.cpp=.o)
all: couch all: $(OBJS)
couch: $(OBJS)
$(CXX) $(LIBS) -o $@ $^

View File

@ -1,6 +1,8 @@
#ifndef MESH_H #ifndef MESH_H
#define MESH_H #define MESH_H
#include <list>
#include "types.h" #include "types.h"
#include "Vertex.h" #include "Vertex.h"
#include "Index.h" #include "Index.h"
@ -19,4 +21,6 @@ private:
Id VAO, VBO, EBO; Id VAO, VBO, EBO;
}; };
typedef std::list<Mesh*> MeshList;
#endif /* MESH_H */ #endif /* MESH_H */

View File

@ -7,3 +7,7 @@ Transform::Transform() {
Transform::Transform(Vector3 position) { Transform::Transform(Vector3 position) {
this->position = position; this->position = position;
} }
void Transform::Translate(cfloat x, cfloat y, cfloat z) {
position = position + Vector3(x, y, z);
}

View File

@ -7,6 +7,7 @@ struct Transform {
Transform(); Transform();
Transform(Vector3 position); Transform(Vector3 position);
Vector3 position; Vector3 position;
void Translate(cfloat x, cfloat y, cfloat z);
}; };
#endif /* TRANSFORM_H */ #endif /* TRANSFORM_H */

View File

@ -1,21 +1,45 @@
// C++ includes
#include <iostream> #include <iostream>
#ifdef LUA_SCRIPTING
// Lua includes
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#endif // LUA_SCRIPTING
// OpenGL Includes
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "types.h" #include "types.h"
#include "Shader.h" #include "Shader.h"
#include "Ball.h" #include "Ball.h"
#include "Camera.h"
Window *window; Window *window;
const int width = 800; const int width = 800;
const int height = 600; 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 main() {
int err; int err;
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -38,29 +62,70 @@ int main() {
glViewport(0, 0, width, height); 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 shader("shaders/flat.vert", "shaders/flat.frag");
shader.Use(); shader.Use();
Ball ball;
ball.SetupMesh();
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f); Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
shader.UpdateProjection(projection); shader.UpdateProjection(projection);
Matrix view(1.0f);
shader.UpdateView(view); double lastTime = glfwGetTime();
double delta = 0.0;
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
Matrix model(1.0f);
model = glm::translate(model, ball.transform.position); Matrix view(1.0f);
shader.UpdateModel(model); Camera *camera = Camera::GetCurrentCamera();
ball.Draw(); 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); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
double curTime = glfwGetTime();
delta = curTime - lastTime;
lastTime = curTime;
} }
#ifdef LUA_SCRIPTING
lua_close(L);
#endif // LUA_SCRIPTING
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();

14
main.lua Normal file
View File

@ -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

View File

@ -5,7 +5,7 @@
- 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 - ~~Guile with GOOPS as scripting language~~ Lua is the scripting language with Guile as a strech goal
- No editor planned - No editor planned
## Roadmap ## Roadmap

2
scripting/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
couch_wrap.cpp
couch_wrap.o

12
scripting/Makefile Normal file
View File

@ -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 $@ $^

20
scripting/couch.i Normal file
View File

@ -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);