Skip to content

Commit ad0b1f5

Browse files
committed
new sample
1 parent 8346566 commit ad0b1f5

File tree

2 files changed

+156
-46
lines changed

2 files changed

+156
-46
lines changed

glut.cpp

+88-10
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ lua_State* G_L;
2121

2222
// prefix `gl' functions
2323

24-
int begin(lua_State* L)
24+
int primitive(lua_State* L)
2525
{
2626
GLenum mode = luaL_checkinteger(L, 1);
27-
glBegin(mode);
27+
if(lua_isfunction(L, 2))
28+
{
29+
glBegin(mode);
30+
lua_call(L, 0, 0);
31+
glEnd();
32+
}
2833
return 0;
2934
}
3035

36+
3137
int clear(lua_State* L)
3238
{
3339
GLbitfield mask = luaL_checkinteger(L, 1);
@@ -45,6 +51,15 @@ int clear_color(lua_State* L)
4551
return 0;
4652
}
4753

54+
int color3d(lua_State* L)
55+
{
56+
GLdouble red = luaL_checknumber(L, 1);
57+
GLdouble green = luaL_checknumber(L, 2);
58+
GLdouble blue = luaL_checknumber(L, 3);
59+
glColor3d(red, green, blue);
60+
return 0;
61+
}
62+
4863
int color3dv(lua_State* L)
4964
{
5065
GLdouble rgb[3];
@@ -55,9 +70,9 @@ int color3dv(lua_State* L)
5570
return 0;
5671
}
5772

58-
int end(lua_State* L)
73+
int flush(lua_State* L)
5974
{
60-
glEnd();
75+
glFlush();
6176
return 0;
6277
}
6378

@@ -138,11 +153,33 @@ int create_window(lua_State* L)
138153

139154
int init(lua_State* L)
140155
{
141-
int argc = lua_gettop(L);
142-
char** argv = new char*[argc];
156+
if( ! lua_istable(L, 1))
157+
{
158+
return 0;
159+
}
160+
161+
int argc = 0;
162+
for(;;)
163+
{
164+
lua_rawgeti(L, 1, argc);
165+
if(lua_isnil(L, -1))
166+
{
167+
lua_pop(L, 1);
168+
break;
169+
}
170+
++argc;
171+
}
172+
173+
if(argc == 0)
174+
{
175+
return 0;
176+
}
177+
178+
char** argv = new char*[argc];
179+
143180
for(int i = 0; i < argc; ++i)
144181
{
145-
argv[i] = const_cast<char*>(luaL_checkstring(L, i + 1));
182+
argv[i] = const_cast<char*>(luaL_checkstring(L, i - argc));
146183
}
147184
glutInit(&argc, argv);
148185
delete[] argv;
@@ -171,6 +208,7 @@ int init_window_size(lua_State* L)
171208
glutInitWindowSize(width, height);
172209
return 0;
173210
}
211+
174212
void* getFont(int font_id)
175213
{
176214
switch(font_id)
@@ -269,7 +307,7 @@ void timer(int n)
269307

270308
lua_pushvalue(G_L, 1);
271309
lua_pushinteger(G_L, n);
272-
lua_call(G_L, 1, 0);
310+
lua_call(G_L, 2, 0);
273311
lua_getfield(G_L, 1, "interval");
274312
unsigned int interval = static_cast<unsigned int>(luaL_checkinteger(G_L, -1));
275313
glutTimerFunc(interval, timer, 0);
@@ -320,24 +358,27 @@ int main_loop(lua_State* L)
320358

321359
const luaL_Reg gllib[] =
322360
{
323-
{ "begin", begin },
361+
{ "primitive", primitive },
324362
{ "clear", clear },
325363
{ "clear_color", clear_color },
364+
{ "color3d", color3d },
326365
{ "color3dv", color3dv },
327-
{ "end", end },
366+
{ "flush", flush },
328367
{ "load_identity", load_identity },
329368
{ "ortho", ortho },
330369
{ "pop_matrix", pop_matrix },
331370
{ "push_matrix", push_matrix },
332371
{ "scalef", scalef },
333372
{ "translatef", translatef },
334373
{ "vertex2i", vertex2i },
374+
{ "viewport", viewport },
335375
{ NULL, NULL }
336376
};
337377

338378
const luaL_Reg glutlib[] =
339379
{
340380
{ "create_window", create_window },
381+
{ "init", init },
341382
{ "init_display_mode", init_display_mode },
342383
{ "init_window_position", init_window_position },
343384
{ "init_window_size", init_window_size },
@@ -349,9 +390,46 @@ const luaL_Reg glutlib[] =
349390

350391
} // anonymous namespace
351392

393+
#define REGISTER_CONST(pre, name) (lua_pushnumber(L, pre##name), lua_setfield(L, -2, #name))
394+
352395
LUALIB_API int luaopen_glut(lua_State* L)
353396
{
354397
luaL_register(L, "gl", gllib);
398+
REGISTER_CONST(GL_, POINTS);
399+
REGISTER_CONST(GL_, LINES);
400+
REGISTER_CONST(GL_, LINE_LOOP);
401+
REGISTER_CONST(GL_, LINE_STRIP);
402+
REGISTER_CONST(GL_, TRIANGLES);
403+
REGISTER_CONST(GL_, TRIANGLE_STRIP);
404+
REGISTER_CONST(GL_, TRIANGLE_FAN);
405+
REGISTER_CONST(GL_, QUADS);
406+
REGISTER_CONST(GL_, QUAD_STRIP);
407+
REGISTER_CONST(GL_, POLYGON);
408+
409+
REGISTER_CONST(GL_, CURRENT_BIT);
410+
REGISTER_CONST(GL_, POINT_BIT);
411+
REGISTER_CONST(GL_, LINE_BIT);
412+
REGISTER_CONST(GL_, POLYGON_BIT);
413+
REGISTER_CONST(GL_, POLYGON_STIPPLE_BIT);
414+
REGISTER_CONST(GL_, PIXEL_MODE_BIT);
415+
REGISTER_CONST(GL_, LIGHTING_BIT);
416+
REGISTER_CONST(GL_, FOG_BIT);
417+
REGISTER_CONST(GL_, DEPTH_BUFFER_BIT);
418+
REGISTER_CONST(GL_, ACCUM_BUFFER_BIT);
419+
REGISTER_CONST(GL_, STENCIL_BUFFER_BIT);
420+
REGISTER_CONST(GL_, VIEWPORT_BIT);
421+
REGISTER_CONST(GL_, TRANSFORM_BIT);
422+
REGISTER_CONST(GL_, ENABLE_BIT);
423+
REGISTER_CONST(GL_, COLOR_BUFFER_BIT);
424+
REGISTER_CONST(GL_, HINT_BIT);
425+
REGISTER_CONST(GL_, EVAL_BIT);
426+
REGISTER_CONST(GL_, LIST_BIT);
427+
REGISTER_CONST(GL_, TEXTURE_BIT);
428+
REGISTER_CONST(GL_, SCISSOR_BIT);
429+
REGISTER_CONST(GL_, ALL_ATTRIB_BITS);
430+
355431
luaL_register(L, "glut", glutlib);
432+
REGISTER_CONST(GLUT_, RGBA);
433+
356434
return 1;
357435
}

sample.lua

+68-36
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
1-
sample =
2-
{
3-
ctrl_c = 3,
4-
5-
interval = 100,
6-
width = 160,
7-
height = 120,
8-
9-
pos1 = { x = 10, y = 10 },
10-
vel1 = { x = 8, y = -2 },
11-
pos2 = { x = 75, y = 55 },
12-
vel2 = { x = -6, y = 4 }
13-
}
14-
15-
16-
function sample:keyboard(key, x, y)
17-
if key == self.ctrl_c then os.exit() end
18-
end
19-
20-
function sample:display()
21-
glut.vertex2i(self.pos1.x, self.pos1.y)
22-
glut.vertex2i(self.pos2.x, self.pos2.y)
23-
end
24-
25-
function sample:timer()
26-
self.pos1.x = self.pos1.x + self.vel1.x
27-
self.pos1.y = self.pos1.y + self.vel1.y
28-
self.pos2.x = self.pos2.x + self.vel2.x
29-
self.pos2.y = self.pos2.y + self.vel2.y
30-
if (self.pos1.x < 0) or (self.width < self.pos1.x) then self.vel1.x = -self.vel1.x end
31-
if (self.pos1.y < 0) or (self.height < self.pos1.y) then self.vel1.y = -self.vel1.y end
32-
if (self.pos2.x < 0) or (self.width < self.pos2.x) then self.vel2.x = -self.vel2.x end
33-
if (self.pos2.y < 0) or (self.height < self.pos2.y) then self.vel2.y = -self.vel2.y end
34-
end
35-
36-
glut.main_loop(sample)
1+
sample =
2+
{
3+
pos1 = { x= 10, y = 20 },
4+
vel1 = { x= 8, y = -2 },
5+
pos2 = { x= 310, y = 230 },
6+
vel2 = { x= -6, y = 4 },
7+
8+
interval = 200,
9+
10+
width = 320,
11+
height = 240,
12+
13+
ctrl_c = 3
14+
}
15+
16+
function sample:display()
17+
gl.clear(gl.COLOR_BUFFER_BIT)
18+
19+
gl.color3d(0.0, 0.0, 0.0)
20+
gl.primitive(gl.LINES, function()
21+
gl.vertex2i(self.pos1.x, self.pos1.y)
22+
gl.vertex2i(self.pos2.x, self.pos2.y)
23+
end)
24+
25+
gl.flush()
26+
end
27+
28+
function sample:reshape(w, h)
29+
gl.viewport(0, 0, w, h)
30+
gl.load_identity()
31+
gl.ortho(-0.5, w - 0.5, h - 0.5, -0.5, -1.0, 1.0)
32+
self.width = w
33+
self.height = h
34+
end
35+
36+
function sample:keyboard(key, x, y)
37+
if key == self.ctrl_c then
38+
os.exit()
39+
end
40+
end
41+
42+
function sample:timer(value)
43+
self.pos1.x = self.pos1.x + self.vel1.x
44+
self.pos1.y = self.pos1.y + self.vel1.y
45+
self.pos2.x = self.pos2.x + self.vel2.x
46+
self.pos2.y = self.pos2.y + self.vel2.y
47+
if (self.pos1.x < 0) or (self.width < self.pos1.x) then self.vel1.x = -self.vel1.x end
48+
if (self.pos1.y < 0) or (self.height < self.pos1.y) then self.vel1.y = -self.vel1.y end
49+
if (self.pos2.x < 0) or (self.width < self.pos2.x) then self.vel2.x = -self.vel2.x end
50+
if (self.pos2.y < 0) or (self.height < self.pos2.y) then self.vel2.y = -self.vel2.y end
51+
52+
gl.color3d(0.0, 0.0, 0.0)
53+
gl.primitive(gl.LINES, function()
54+
gl.vertex2i(self.pos1.x, self.pos1.y)
55+
gl.vertex2i(self.pos2.x, self.pos2.y)
56+
end)
57+
58+
gl.flush()
59+
end
60+
61+
glut.init_window_position(100, 100)
62+
glut.init_window_size(sample.width, sample.height)
63+
glut.init(arg)
64+
glut.init_display_mode(glut.RGBA)
65+
glut.create_window(arg[0])
66+
gl.clear_color(1.0, 1.0, 1.0, 1.0)
67+
68+
glut.main_loop(sample)

0 commit comments

Comments
 (0)