aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics/je_lua_shader.cpp
blob: e2e141366e37e15101dabf9fc48b2fbca083b9dd (plain)
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
#include "lua/modules/luax.h"
#include "lua/modules/types.h"
#include "lua/common/je_lua_common.h"
#include "libjin/jin.h"

using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Shaders;

namespace JinEngine
{
    namespace Lua
    {

        typedef Shared<Shader>& ShaderRef;

        static inline ShaderRef checkShader(lua_State* L)
        {
            Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER);
            return proxy->getShared<Shader>();
        }

        /**
        * jsl:sendNumber("variable", 0.1)
        */
        LUA_IMPLEMENT int l_sendNumber(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            float number = luax_checknumber(L, 3);
            ref->sendFloat(variable, number);
            return 0;
        }

        LUA_IMPLEMENT int l_sendTexture(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE);
            Shared<Texture>& tex = proxy->getShared<Texture>();
            ref->sendTexture(variable, tex.getObject());
            return 0;
        }

        LUA_IMPLEMENT int l_sendCanvas(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS);
            Shared<Canvas>& canvas = proxy->getShared<Canvas>();
            ref->sendCanvas(variable, canvas.getObject());
            return 0;
        }

        LUA_IMPLEMENT int l_sendVec2(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            if (!luax_istable(L, 3))
            {
                luax_typerror(L, 3, "table");
                return 1;
            }
            float x = luax_rawgetnumber(L, 3, 1);
            float y = luax_rawgetnumber(L, 3, 2);
            ref->sendVec2(variable, x, y);
            return 0;
        }

        LUA_IMPLEMENT int l_sendVec3(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            if (!luax_istable(L, 3))
            {
                luax_typerror(L, 3, "table");
                return 1;
            }
            float x = luax_rawgetnumber(L, 3, 1);
            float y = luax_rawgetnumber(L, 3, 2);
            float z = luax_rawgetnumber(L, 3, 3);
            ref->sendVec3(variable, x, y, z);
            return 0;
        }

        LUA_IMPLEMENT int l_sendVec4(lua_State* L)
        {
            ShaderRef ref = checkShader(L);
            const char* variable = luax_checkstring(L, 2);
            if (!luax_istable(L, 3))
            {
                luax_typerror(L, 3, "table");
                return 1;
            }
            float x = luax_rawgetnumber(L, 3, 1);
            float y = luax_rawgetnumber(L, 3, 2);
            float z = luax_rawgetnumber(L, 3, 3);
            float w = luax_rawgetnumber(L, 3, 4);
            ref->sendVec4(variable, x, y, z, w);
            return 0;
        }

        LUA_IMPLEMENT int l_sendColor(lua_State* L)
        {
            return l_sendVec4(L);
        }

        LUA_IMPLEMENT int l_gc(lua_State* L)
        {
            Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER);
            proxy->release();
            return 0;
        }

        LUA_IMPLEMENT const luaL_Reg f[] = {
            { "__gc",        l_gc          },
            { "sendNumber",  l_sendNumber  },
            { "sendTexture", l_sendTexture },
            { "sendCanvas",  l_sendCanvas  },
            { "sendVec2",    l_sendVec2    },
            { "sendVec3",    l_sendVec3    },
            { "sendVec4",    l_sendVec4    },
            { "sendColor",   l_sendColor   },
            { 0,             0             }
        };

        /**
        * JSL program
        */
        LUA_EXPORT int luaopen_JSL(lua_State* L)
        {
            luax_newtype(L, JIN_GRAPHICS_SHADER, f);
            return 0;
        }

    } // namespace Lua
} // namespace JinEngine