-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathlua-example-lines.cpp
More file actions
171 lines (147 loc) · 4.89 KB
/
Copy pathlua-example-lines.cpp
File metadata and controls
171 lines (147 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include "sre/SDLRenderer.hpp"
#include "sre/SpriteAtlas.hpp"
#include "sre/Texture.hpp"
#include "sre/Material.hpp"
#include "sre/Inspector.hpp"
#include "glm/gtc/random.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/transform.hpp"
#include "glm/ext.hpp"
// lua headers
#include "sol.hpp"
using namespace sre;
using namespace std;
using namespace sol;
class LuaExample {
public:
LuaExample()
{
r.init();
r.setWindowTitle("Lua example");
objPosition = {5.5f,0,0};
mat = Shader::getUnlit()->createMaterial();
mat->setColor({1,0,0,1});
mesh = Mesh::create().withSphere().build();
lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::math);
// Register callback functions
lua.set_function("pi", [&](sol::this_state state, sol::variadic_args va){
return glm::pi<double>();
});
lua.set_function("fmod", [&](sol::this_state state, sol::variadic_args va){
double numer = va[0];
double denum = va[1];
return fmod(numer,denum);
});
lua.set_function("remainder", [&](sol::this_state state, sol::variadic_args va){
double s = va[0];
double denum = va[1];
return remainder(s,denum);
});
lua.set_function("abs", [&](sol::this_state state, sol::variadic_args va){
double s = va[0];
return abs(s);
});
lua.set_function("sin", [&](sol::this_state state, sol::variadic_args va){
double s = va[0];
return sin(s);
});
lua.set_function("cos", [&](sol::this_state state, sol::variadic_args va){
double s = va[0];
return cos(s);
});
lua.set_function("pow", [&](sol::this_state state, sol::variadic_args va){
double s = va[0];
double p = va[1];
return pow(s,p);
});
updateLuaScript();
// init objects
for (float x = -5;x<=5;x=x+0.01f){
positions.push_back({x,0,0});
}
camera.setOrthographicProjection(5,-1,1);
r.frameUpdate = [&](float deltaTime){
update(deltaTime);
};
r.frameRender = [&](){
render();
};
r.startEventLoop();
}
void updateLuaScript() {
lua.script(luaScript); // evaluate lua script
sol::function fn = lua["easing"]; // get lua function update pos
updatepos = fn; // get C++ functional reference to lua script
}
void update(float deltaTime){
static float totalTime = 0;
totalTime += deltaTime;
try {
if (error.size()==0){
objPosition.y = (float) updatepos(totalTime);
}
} catch (sol::error err){
error = err.what();
}
}
void render(){
try {
if (error.size()==0){
int i = 0;
for (float x = -5; x <= 5; x = x + 0.01f) {
positions[i] = {x, (float) updatepos(x), 0};
i++;
}
}
} catch (sol::error err){
error = err.what();
}
RenderPass rp = RenderPass::create()
.withCamera(camera)
.build();
// draw grid
for (int i=-5;i<=5;i++){
Color color = (i==0?Color{1.0f,1.0f,1.0f,1.0f}:Color{0.2f,0.2f,0.2f,1.0f});
rp.drawLines({{i,-5,0},{i,5,0}},color, MeshTopology::LineStrip);
rp.drawLines({{-5,i,0},{5,i,0}},color, MeshTopology::LineStrip);
}
rp.drawLines(positions,{1.0,1.0,1.0,1.0}, MeshTopology::LineStrip);
rp.draw(mesh, glm::translate(objPosition)*glm::scale(glm::vec3{0.5f,0.5f,0.5f}), mat);
ImGui::InputTextMultiline("Lua script", luaScript , luaScriptSize);
if (error.size() > 0){
ImGui::LabelText("Error", error.c_str());
}
if (ImGui::Button("Update")){
try {
updateLuaScript();
error = "";
} catch (sol::error err){
error = err.what();
}
}
}
private:
static const int luaScriptSize = 2048;
// initial script
char luaScript[luaScriptSize] =
"-- C functions exposed:\n"
"-- fmod(a,b), remainder(a,b), abs(a), sin(a), cos(a), pow(a), pi()\n"
"function easing (t) \n"
" return t \n"
"end";
std::vector<glm::vec3> positions;
glm::vec3 objPosition;
std::shared_ptr<sre::Mesh> mesh;
std::shared_ptr<sre::Material> mat;
SDLRenderer r;
Camera camera;
std::string error;
sol::state lua;
std::function<double(double)> updatepos;
};
int main() {
std::cout << "https://github.com/mortennobel/SimpleRenderEngineProject" << std::endl;
new LuaExample();
return 0;
}