mirror of https://github.com/bjornbytes/lovr.git
Model;
This commit is contained in:
parent
6a50fae9a3
commit
d20d70e695
|
@ -1,4 +1,5 @@
|
|||
#include "event.h"
|
||||
#include "lovr.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
extern GLFWwindow* window;
|
||||
|
@ -10,8 +11,7 @@ int lovrEventPoll(lua_State* L) {
|
|||
}
|
||||
|
||||
int lovrEventQuit(lua_State* L) {
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
lovrDestroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
#include "glfw.h"
|
||||
#include <stdlib.h>
|
||||
#include "lovr.h"
|
||||
#include "util.h"
|
||||
|
||||
void initGlfw() {
|
||||
glfwSetErrorCallback(lovrOnError);
|
||||
|
||||
if (!glfwInit()) {
|
||||
error("Error initializing glfw");
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
|
||||
// TODO make configurable
|
||||
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwSetWindowCloseCallback(window, lovrOnClose);
|
||||
glfwMakeContextCurrent(window);
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
#define GLFW_INCLUDE_GLCOREARB
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
GLFWwindow* window;
|
||||
|
||||
void initGlfw();
|
|
@ -1,13 +1,11 @@
|
|||
#include "glfw.h"
|
||||
#include "graphics.h"
|
||||
#include "model.h"
|
||||
#include "util.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
extern GLFWwindow* window;
|
||||
typedef const struct aiScene* Model;
|
||||
|
||||
int lovrGraphicsClear(lua_State* L) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
|
@ -22,13 +20,13 @@ int lovrGraphicsPresent(lua_State* L) {
|
|||
|
||||
int lovrGraphicsNewModel(lua_State* L) {
|
||||
const char* path = luaL_checkstring(L, -1);
|
||||
Model model = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
|
||||
const struct aiScene* scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
|
||||
|
||||
if (model) {
|
||||
Model* userdata = (Model*) luaPushType(L, "model");
|
||||
*userdata = model;
|
||||
if (scene) {
|
||||
Model* model = scene->mMeshes[0];
|
||||
luax_pushmodel(L, model);
|
||||
} else {
|
||||
// error
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
@ -40,16 +38,3 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ "newModel", lovrGraphicsNewModel },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
int lovrModelGetVertexCount(lua_State* L) {
|
||||
const struct aiScene* scene = *(const struct aiScene**) luaL_checkudata(L, -1, "model");
|
||||
|
||||
lua_pushnumber(L, scene->mMeshes[0]->mNumVertices);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrModel[] = {
|
||||
{ "getVertexCount", lovrModelGetVertexCount },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#include <stdlib.h>
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
@ -6,6 +5,4 @@
|
|||
int lovrGraphicsClear(lua_State* L);
|
||||
int lovrGraphicsPresent(lua_State* L);
|
||||
int lovrGraphicsNewModel(lua_State* L);
|
||||
|
||||
extern const luaL_Reg lovrGraphics[];
|
||||
extern const luaL_Reg lovrModel[];
|
||||
|
|
29
src/lovr.c
29
src/lovr.c
|
@ -2,8 +2,11 @@
|
|||
#include "util.h"
|
||||
|
||||
#include "graphics.h"
|
||||
#include "model.h"
|
||||
#include "event.h"
|
||||
|
||||
extern lua_State* L;
|
||||
|
||||
void lovrInit(lua_State* L) {
|
||||
|
||||
// Write top-level lovr global
|
||||
|
@ -15,7 +18,7 @@ void lovrInit(lua_State* L) {
|
|||
luaRegisterModule(L, "graphics", lovrGraphics);
|
||||
|
||||
// Register types
|
||||
luaRegisterType(L, "model", lovrModel);
|
||||
luaRegisterType(L, "Model", lovrModel);
|
||||
|
||||
// Run "main.lua" which will override/define pieces of lovr
|
||||
if (luaL_dofile(L, "main.lua")) {
|
||||
|
@ -25,6 +28,11 @@ void lovrInit(lua_State* L) {
|
|||
}
|
||||
}
|
||||
|
||||
void lovrDestroy() {
|
||||
glfwTerminate();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void lovrRun(lua_State* L) {
|
||||
|
||||
// lovr.run()
|
||||
|
@ -32,3 +40,22 @@ void lovrRun(lua_State* L) {
|
|||
lua_getfield(L, -1, "run");
|
||||
lua_call(L, 0, 0);
|
||||
}
|
||||
|
||||
void lovrOnError(int code, const char* description) {
|
||||
error(description);
|
||||
}
|
||||
|
||||
void lovrOnClose(GLFWwindow* _window) {
|
||||
if (_window == window) {
|
||||
|
||||
// lovr.quit()
|
||||
lua_getglobal(L, "lovr");
|
||||
lua_getfield(L, -1, "quit");
|
||||
lua_call(L, 0, 0);
|
||||
|
||||
if (glfwWindowShouldClose(window)) {
|
||||
glfwDestroyWindow(window);
|
||||
lovrDestroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "glfw.h"
|
||||
|
||||
void lovrInit(lua_State* L);
|
||||
void lovrDestroy();
|
||||
void lovrRun(lua_State* L);
|
||||
void lovrOnError(int code, const char* description);
|
||||
void lovrOnClose(GLFWwindow* window);
|
||||
|
|
40
src/main.c
40
src/main.c
|
@ -1,47 +1,13 @@
|
|||
#define GLFW_INCLUDE_GLCOREARB
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <luajit.h>
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "lovr.h"
|
||||
#include "glfw.h"
|
||||
|
||||
GLFWwindow* window;
|
||||
|
||||
void onError(int code, const char* description) {
|
||||
error(description);
|
||||
}
|
||||
|
||||
void initGlfw() {
|
||||
glfwSetErrorCallback(onError);
|
||||
|
||||
if (!glfwInit()) {
|
||||
error("Error initializing glfw");
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
|
||||
// TODO make configurable
|
||||
window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
|
||||
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
}
|
||||
lua_State* L;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
lua_State* L = luaL_newstate();
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
|
||||
lovrInit(L);
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
#include "model.h"
|
||||
#include "util.h"
|
||||
#include <assimp/scene.h>
|
||||
|
||||
void luax_pushmodel(lua_State* L, Model* model) {
|
||||
Model** userdata = (Model**) lua_newuserdata(L, sizeof(Model*));
|
||||
|
||||
luaL_getmetatable(L, "Model");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
*userdata = model;
|
||||
}
|
||||
|
||||
Model* luax_checkmodel(lua_State* L, int index) {
|
||||
return *(Model**) luaL_checkudata(L, index, "Model");
|
||||
}
|
||||
|
||||
int lovrModelDraw(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
|
||||
float x = luaL_checknumber(L, 2);
|
||||
float y = luaL_checknumber(L, 3);
|
||||
float z = luaL_checknumber(L, 4);
|
||||
|
||||
if (model) {
|
||||
printf("I just drew your fuckign model at %f %f %f\n", x, y, z);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lovrModelGetVertexCount(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
|
||||
lua_pushinteger(L, model->mNumVertices);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrModelGetColors(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
int colorSetIndex = 0;
|
||||
|
||||
if (lua_gettop(L) > 1) {
|
||||
colorSetIndex = luaL_checkinteger(L, 2);
|
||||
}
|
||||
|
||||
if (colorSetIndex >= AI_MAX_NUMBER_OF_COLOR_SETS || !model->mColors[colorSetIndex]) {
|
||||
lua_pushnil(L);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct aiColor4D* colorSet = model->mColors[colorSetIndex];
|
||||
|
||||
lua_createtable(L, model->mNumVertices, 0);
|
||||
|
||||
for (int i = 0; i < model->mNumVertices; i++) {
|
||||
lua_createtable(L, 4, 0);
|
||||
|
||||
lua_pushnumber(L, colorSet[i].r);
|
||||
lua_rawseti(L, -2, 1);
|
||||
|
||||
lua_pushnumber(L, colorSet[i].g);
|
||||
lua_rawseti(L, -2, 2);
|
||||
|
||||
lua_pushnumber(L, colorSet[i].b);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
lua_pushnumber(L, colorSet[i].a);
|
||||
lua_rawseti(L, -2, 4);
|
||||
|
||||
lua_rawseti(L, -1, i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrModelGetNormals(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
|
||||
if (!model->mNormals) {
|
||||
lua_pushnil(L);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_createtable(L, model->mNumVertices, 0);
|
||||
|
||||
for (int i = 0; i < model->mNumVertices; i++) {
|
||||
lua_createtable(L, 3, 0);
|
||||
|
||||
lua_pushnumber(L, model->mNormals[i].x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
|
||||
lua_pushnumber(L, model->mNormals[i].y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
|
||||
lua_pushnumber(L, model->mNormals[i].z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
lua_rawseti(L, -2, i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrModelGetTangents(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
|
||||
if (!model->mTangents) {
|
||||
lua_pushnil(L);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_createtable(L, model->mNumFaces, 0);
|
||||
|
||||
for (int i = 0; i < model->mNumFaces; i++) {
|
||||
lua_createtable(L, 3, 0);
|
||||
|
||||
lua_pushnumber(L, model->mTangents[i].x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
|
||||
lua_pushnumber(L, model->mTangents[i].y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
|
||||
lua_pushnumber(L, model->mTangents[i].z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
lua_rawseti(L, -2, i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrModelGetUVs(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
int uvSetIndex = 0;
|
||||
|
||||
if (lua_gettop(L) > 1) {
|
||||
uvSetIndex = luaL_checkinteger(L, 2);
|
||||
}
|
||||
|
||||
if (uvSetIndex >= AI_MAX_NUMBER_OF_TEXTURECOORDS || !model->mTextureCoords[uvSetIndex]) {
|
||||
lua_pushnil(L);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct aiVector3D* uvSet = model->mTextureCoords[uvSetIndex];
|
||||
|
||||
lua_createtable(L, model->mNumVertices, 0);
|
||||
|
||||
for (int i = 0; i < model->mNumVertices; i++) {
|
||||
lua_createtable(L, 4, 0);
|
||||
|
||||
lua_pushnumber(L, uvSet->x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
|
||||
lua_pushnumber(L, uvSet->y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
|
||||
lua_pushnumber(L, uvSet->z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
lua_rawseti(L, -1, i);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lovrModelGetVertex(lua_State* L) {
|
||||
Model* model = luax_checkmodel(L, 1);
|
||||
int index = luaL_checkint(L, 2);
|
||||
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_pushnumber(L, model->mVertices[index].x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, model->mVertices[index].y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, model->mVertices[index].z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrModel[] = {
|
||||
{ "draw", lovrModelDraw },
|
||||
{ "getVertexCount", lovrModelGetVertexCount },
|
||||
{ "getColors", lovrModelGetColors },
|
||||
{ "getNormals", lovrModelGetNormals },
|
||||
{ "getTangents", lovrModelGetTangents },
|
||||
{ "getUVs", lovrModelGetUVs },
|
||||
{ "getVertex", lovrModelGetVertex },
|
||||
{ NULL, NULL }
|
||||
};
|
|
@ -0,0 +1,12 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
typedef const struct aiMesh Model;
|
||||
void luax_pushmodel(lua_State* L, Model* model);
|
||||
Model* luax_checkmodel(lua_State* L, int index);
|
||||
|
||||
int lovrModelDraw(lua_State* L);
|
||||
int lovrModelGetVertexCount(lua_State* L);
|
||||
int lovrModelGetVertexColors(lua_State* L);
|
||||
extern const luaL_Reg lovrModel[];
|
13
src/util.c
13
src/util.c
|
@ -78,16 +78,3 @@ void luaRegisterType(lua_State* L, const char* name, const luaL_Reg* functions)
|
|||
// Pop metatable
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
void* luaPushType(lua_State* L, const char* type) {
|
||||
|
||||
// Allocate space for a single pointer
|
||||
void* userdata = (void*) lua_newuserdata(L, sizeof(void*));
|
||||
|
||||
// Set the metatable of the userdata to the desired type
|
||||
luaL_getmetatable(L, type);
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
// Return the pointer to the object
|
||||
return userdata;
|
||||
}
|
||||
|
|
|
@ -6,4 +6,3 @@ void error(const char* format, ...);
|
|||
char* loadFile(char* filename);
|
||||
void luaRegisterModule(lua_State* L, const char* name, const luaL_Reg* module);
|
||||
void luaRegisterType(lua_State* L, const char* name, const luaL_Reg* functions);
|
||||
void* luaPushType(lua_State* L, const char* type);
|
||||
|
|
Loading…
Reference in New Issue