@@ -21,13 +21,19 @@ lua_State* G_L;
21
21
22
22
// prefix `gl' functions
23
23
24
- int begin (lua_State* L)
24
+ int primitive (lua_State* L)
25
25
{
26
26
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
+ }
28
33
return 0 ;
29
34
}
30
35
36
+
31
37
int clear (lua_State* L)
32
38
{
33
39
GLbitfield mask = luaL_checkinteger (L, 1 );
@@ -45,6 +51,15 @@ int clear_color(lua_State* L)
45
51
return 0 ;
46
52
}
47
53
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
+
48
63
int color3dv (lua_State* L)
49
64
{
50
65
GLdouble rgb[3 ];
@@ -55,9 +70,9 @@ int color3dv(lua_State* L)
55
70
return 0 ;
56
71
}
57
72
58
- int end (lua_State* L)
73
+ int flush (lua_State* L)
59
74
{
60
- glEnd ();
75
+ glFlush ();
61
76
return 0 ;
62
77
}
63
78
@@ -138,11 +153,33 @@ int create_window(lua_State* L)
138
153
139
154
int init (lua_State* L)
140
155
{
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
+
143
180
for (int i = 0 ; i < argc; ++i)
144
181
{
145
- argv[i] = const_cast <char *>(luaL_checkstring (L, i + 1 ));
182
+ argv[i] = const_cast <char *>(luaL_checkstring (L, i - argc ));
146
183
}
147
184
glutInit (&argc, argv);
148
185
delete[] argv;
@@ -171,6 +208,7 @@ int init_window_size(lua_State* L)
171
208
glutInitWindowSize (width, height);
172
209
return 0 ;
173
210
}
211
+
174
212
void * getFont (int font_id)
175
213
{
176
214
switch (font_id)
@@ -269,7 +307,7 @@ void timer(int n)
269
307
270
308
lua_pushvalue (G_L, 1 );
271
309
lua_pushinteger (G_L, n);
272
- lua_call (G_L, 1 , 0 );
310
+ lua_call (G_L, 2 , 0 );
273
311
lua_getfield (G_L, 1 , " interval" );
274
312
unsigned int interval = static_cast <unsigned int >(luaL_checkinteger (G_L, -1 ));
275
313
glutTimerFunc (interval, timer, 0 );
@@ -320,24 +358,27 @@ int main_loop(lua_State* L)
320
358
321
359
const luaL_Reg gllib[] =
322
360
{
323
- { " begin " , begin },
361
+ { " primitive " , primitive },
324
362
{ " clear" , clear },
325
363
{ " clear_color" , clear_color },
364
+ { " color3d" , color3d },
326
365
{ " color3dv" , color3dv },
327
- { " end " , end },
366
+ { " flush " , flush },
328
367
{ " load_identity" , load_identity },
329
368
{ " ortho" , ortho },
330
369
{ " pop_matrix" , pop_matrix },
331
370
{ " push_matrix" , push_matrix },
332
371
{ " scalef" , scalef },
333
372
{ " translatef" , translatef },
334
373
{ " vertex2i" , vertex2i },
374
+ { " viewport" , viewport },
335
375
{ NULL , NULL }
336
376
};
337
377
338
378
const luaL_Reg glutlib[] =
339
379
{
340
380
{ " create_window" , create_window },
381
+ { " init" , init },
341
382
{ " init_display_mode" , init_display_mode },
342
383
{ " init_window_position" , init_window_position },
343
384
{ " init_window_size" , init_window_size },
@@ -349,9 +390,46 @@ const luaL_Reg glutlib[] =
349
390
350
391
} // anonymous namespace
351
392
393
+ #define REGISTER_CONST (pre, name ) (lua_pushnumber(L, pre##name), lua_setfield(L, -2 , #name))
394
+
352
395
LUALIB_API int luaopen_glut (lua_State* L)
353
396
{
354
397
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
+
355
431
luaL_register (L, " glut" , glutlib);
432
+ REGISTER_CONST (GLUT_, RGBA);
433
+
356
434
return 1 ;
357
435
}
0 commit comments