Add support for Lua in world, and create the Lua Pattern (pattern can be defined with a lua function)
This commit is contained in:
@@ -14,7 +14,13 @@ file(GLOB RAY_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_D
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/worldbuilder/*.cpp)
|
||||
|
||||
target_include_directories(rayonnement PUBLIC include pattern)
|
||||
add_dependencies(rayonnement LuaCore)
|
||||
|
||||
if (USE_LUA)
|
||||
add_dependencies(rayonnement LuaCore)
|
||||
target_link_libraries(rayonnement ${LUA_LIBRARIES})
|
||||
endif()
|
||||
|
||||
target_include_directories(rayonnement PUBLIC include ${LODEPNG_INCLUDE_FOLDER} ${LUA_INCLUDE_DIR})
|
||||
target_sources(rayonnement PRIVATE ${RAY_HEADERS} ${RAY_SOURCES})
|
||||
target_link_libraries(rayonnement LodePNG)
|
||||
|
||||
@@ -22,11 +28,14 @@ if (USE_OPENMP)
|
||||
target_link_libraries(rayonnement OpenMP::OpenMP_CXX)
|
||||
endif()
|
||||
|
||||
|
||||
# The main executable
|
||||
add_executable(dorayme main.cpp)
|
||||
|
||||
add_dependencies(dorayme LuaCore)
|
||||
target_include_directories(rayonnement PUBLIC include ${LODEPNG_INCLUDE_FOLDER} ${LUA_INCLUDE_DIR})
|
||||
target_link_libraries(dorayme rayonnement ${LUA_LIBRARIES})
|
||||
|
||||
|
||||
if (COVERALLS)
|
||||
set(COVERAGE_SRCS ${RAY_HEADERS} ${RAY_SOURCES} ${COVERAGE_SRCS} PARENT_SCOPE)
|
||||
endif()
|
||||
@@ -16,6 +16,12 @@
|
||||
#include <ray.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef ENABLE_LUA_SUPPORT
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
class World
|
||||
{
|
||||
public:
|
||||
@@ -29,6 +35,10 @@ private:
|
||||
Light* *lightList;
|
||||
Shape* *objectList;
|
||||
|
||||
#ifdef ENABLE_LUA_SUPPORT
|
||||
lua_State *L;
|
||||
#endif
|
||||
|
||||
public:
|
||||
World();
|
||||
~World();
|
||||
|
||||
107
source/pattern/luapattern.h
Normal file
107
source/pattern/luapattern.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Lua based Pattern header
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#ifndef DORAYME_LUAPATTERN_H
|
||||
#define DORAYME_LUAPATTERN_H
|
||||
|
||||
#include <pattern.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef ENABLE_LUA_SUPPORT
|
||||
#error Cannot use the Lua Pattern generator with no Lua support disabled!
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
}
|
||||
|
||||
class LuaPattern : public Pattern
|
||||
{
|
||||
private:
|
||||
lua_State *L;
|
||||
char funcName[50];
|
||||
|
||||
public:
|
||||
LuaPattern(Colour a, Colour b) : Pattern(a, b), L(nullptr) { };
|
||||
|
||||
void setLua(lua_State *L) {
|
||||
this->L = L;
|
||||
};
|
||||
|
||||
void setLuaFunctionName(const char *name) {
|
||||
strncpy(this->funcName, name, 50);
|
||||
}
|
||||
|
||||
Colour patternAt(Tuple point)
|
||||
{
|
||||
int isnum;
|
||||
double r, g, b;
|
||||
|
||||
lua_getglobal(this->L, this->funcName);
|
||||
lua_pushnumber(this->L, point.x);
|
||||
lua_pushnumber(this->L, point.y);
|
||||
lua_pushnumber(this->L, point.z);
|
||||
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_pushnumber(L, this->a.x);
|
||||
lua_setfield(L, -2, "r");
|
||||
lua_pushnumber(L, this->a.y);
|
||||
lua_setfield(L, -2, "g");
|
||||
lua_pushnumber(L, this->a.z);
|
||||
lua_setfield(L, -2, "b");
|
||||
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_pushnumber(L, this->b.x);
|
||||
lua_setfield(L, -2, "r");
|
||||
lua_pushnumber(L, this->b.y);
|
||||
lua_setfield(L, -2, "g");
|
||||
lua_pushnumber(L, this->b.z);
|
||||
lua_setfield(L, -2, "b");
|
||||
|
||||
if (lua_pcall(this->L, 5, 3, 0) != LUA_OK)
|
||||
{
|
||||
printf("Error running the Lua function '%s': %s\n", this->funcName,
|
||||
lua_tostring(this->L, -1));
|
||||
return Colour(0, 0, 0);
|
||||
}
|
||||
|
||||
r = lua_tonumberx(this->L, -3, &isnum);
|
||||
if (!isnum)
|
||||
{
|
||||
printf("Error: function '%s' must return numbers\n", this->funcName);
|
||||
lua_pop(this->L, 1);
|
||||
return Colour(0, 0, 0);
|
||||
}
|
||||
g = lua_tonumberx(this->L, -2, &isnum);
|
||||
if (!isnum)
|
||||
{
|
||||
printf("Error: function '%s' must return numbers\n", this->funcName);
|
||||
lua_pop(this->L, 1);
|
||||
return Colour(0, 0, 0);
|
||||
}
|
||||
b = lua_tonumberx(this->L, -1, &isnum);
|
||||
if (!isnum)
|
||||
{
|
||||
printf("Error: function '%s' must return numbers\n", this->funcName);
|
||||
lua_pop(this->L, 1);
|
||||
return Colour(0, 0, 0);
|
||||
}
|
||||
|
||||
lua_pop(this->L, 1);
|
||||
return Colour(r, g, b);
|
||||
}
|
||||
|
||||
void dumpMe(FILE *fp) {
|
||||
fprintf(fp, "\"Type\": \"Lua\",\n");
|
||||
Pattern::dumpMe(fp);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* DORAYME_LUAPATTERN_H */
|
||||
@@ -12,6 +12,14 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef ENABLE_LUA_SUPPORT
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
#define MIN_ALLOC (2)
|
||||
|
||||
World::World() : objectCount(0), lightCount(0)
|
||||
@@ -23,6 +31,11 @@ World::World() : objectCount(0), lightCount(0)
|
||||
this->allocatedObjectCount = MIN_ALLOC;
|
||||
this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
|
||||
this->objectCount = 0;
|
||||
|
||||
#ifdef ENABLE_LUA_SUPPORT
|
||||
this->L = luaL_newstate(); /* opens Lua */
|
||||
luaL_openlibs(L); /* opens the basic library */
|
||||
#endif
|
||||
};
|
||||
|
||||
World::~World()
|
||||
|
||||
Reference in New Issue
Block a user