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

namespace JinEngine
{
    namespace Lua
    {
    
	    using namespace JinEngine::Graphics;

	    typedef Ref<Shader>& ShaderRef;

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

        /**
        * jsl:sendNumber("variable", 0.1)
        */
        static 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;
        }

        static 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);
            Ref<Texture>& tex = proxy->getRef<Texture>();
            ref->sendTexture(variable, tex.getObject());
            return 0;
        }

        static 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);
            Ref<Canvas>& canvas = proxy->getRef<Canvas>();
            ref->sendCanvas(variable, canvas.getObject());
            return 0;
        }

        static 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;
        }

        static 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;
        }

        static 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;
        }

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

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

        static 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
        */
        int luaopen_JSL(lua_State* L)
        {
            luax_newtype(L, JIN_GRAPHICS_SHADER, f);
            return 0;
        }

    } // namespace Lua
} // namespace JinEngine