Skip to content

Commit

Permalink
new sample
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsan committed Jan 9, 2010
1 parent 8346566 commit ad0b1f5
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 46 deletions.
98 changes: 88 additions & 10 deletions glut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ lua_State* G_L;

// prefix `gl' functions

int begin(lua_State* L)
int primitive(lua_State* L)
{
GLenum mode = luaL_checkinteger(L, 1);
glBegin(mode);
if(lua_isfunction(L, 2))
{
glBegin(mode);
lua_call(L, 0, 0);
glEnd();
}
return 0;
}


int clear(lua_State* L)
{
GLbitfield mask = luaL_checkinteger(L, 1);
Expand All @@ -45,6 +51,15 @@ int clear_color(lua_State* L)
return 0;
}

int color3d(lua_State* L)
{
GLdouble red = luaL_checknumber(L, 1);
GLdouble green = luaL_checknumber(L, 2);
GLdouble blue = luaL_checknumber(L, 3);
glColor3d(red, green, blue);
return 0;
}

int color3dv(lua_State* L)
{
GLdouble rgb[3];
Expand All @@ -55,9 +70,9 @@ int color3dv(lua_State* L)
return 0;
}

int end(lua_State* L)
int flush(lua_State* L)
{
glEnd();
glFlush();
return 0;
}

Expand Down Expand Up @@ -138,11 +153,33 @@ int create_window(lua_State* L)

int init(lua_State* L)
{
int argc = lua_gettop(L);
char** argv = new char*[argc];
if( ! lua_istable(L, 1))
{
return 0;
}

int argc = 0;
for(;;)
{
lua_rawgeti(L, 1, argc);
if(lua_isnil(L, -1))
{
lua_pop(L, 1);
break;
}
++argc;
}

if(argc == 0)
{
return 0;
}

char** argv = new char*[argc];

for(int i = 0; i < argc; ++i)
{
argv[i] = const_cast<char*>(luaL_checkstring(L, i + 1));
argv[i] = const_cast<char*>(luaL_checkstring(L, i - argc));
}
glutInit(&argc, argv);
delete[] argv;
Expand Down Expand Up @@ -171,6 +208,7 @@ int init_window_size(lua_State* L)
glutInitWindowSize(width, height);
return 0;
}

void* getFont(int font_id)
{
switch(font_id)
Expand Down Expand Up @@ -269,7 +307,7 @@ void timer(int n)

lua_pushvalue(G_L, 1);
lua_pushinteger(G_L, n);
lua_call(G_L, 1, 0);
lua_call(G_L, 2, 0);
lua_getfield(G_L, 1, "interval");
unsigned int interval = static_cast<unsigned int>(luaL_checkinteger(G_L, -1));
glutTimerFunc(interval, timer, 0);
Expand Down Expand Up @@ -320,24 +358,27 @@ int main_loop(lua_State* L)

const luaL_Reg gllib[] =
{
{ "begin", begin },
{ "primitive", primitive },
{ "clear", clear },
{ "clear_color", clear_color },
{ "color3d", color3d },
{ "color3dv", color3dv },
{ "end", end },
{ "flush", flush },
{ "load_identity", load_identity },
{ "ortho", ortho },
{ "pop_matrix", pop_matrix },
{ "push_matrix", push_matrix },
{ "scalef", scalef },
{ "translatef", translatef },
{ "vertex2i", vertex2i },
{ "viewport", viewport },
{ NULL, NULL }
};

const luaL_Reg glutlib[] =
{
{ "create_window", create_window },
{ "init", init },
{ "init_display_mode", init_display_mode },
{ "init_window_position", init_window_position },
{ "init_window_size", init_window_size },
Expand All @@ -349,9 +390,46 @@ const luaL_Reg glutlib[] =

} // anonymous namespace

#define REGISTER_CONST(pre, name) (lua_pushnumber(L, pre##name), lua_setfield(L, -2, #name))

LUALIB_API int luaopen_glut(lua_State* L)
{
luaL_register(L, "gl", gllib);
REGISTER_CONST(GL_, POINTS);
REGISTER_CONST(GL_, LINES);
REGISTER_CONST(GL_, LINE_LOOP);
REGISTER_CONST(GL_, LINE_STRIP);
REGISTER_CONST(GL_, TRIANGLES);
REGISTER_CONST(GL_, TRIANGLE_STRIP);
REGISTER_CONST(GL_, TRIANGLE_FAN);
REGISTER_CONST(GL_, QUADS);
REGISTER_CONST(GL_, QUAD_STRIP);
REGISTER_CONST(GL_, POLYGON);

REGISTER_CONST(GL_, CURRENT_BIT);
REGISTER_CONST(GL_, POINT_BIT);
REGISTER_CONST(GL_, LINE_BIT);
REGISTER_CONST(GL_, POLYGON_BIT);
REGISTER_CONST(GL_, POLYGON_STIPPLE_BIT);
REGISTER_CONST(GL_, PIXEL_MODE_BIT);
REGISTER_CONST(GL_, LIGHTING_BIT);
REGISTER_CONST(GL_, FOG_BIT);
REGISTER_CONST(GL_, DEPTH_BUFFER_BIT);
REGISTER_CONST(GL_, ACCUM_BUFFER_BIT);
REGISTER_CONST(GL_, STENCIL_BUFFER_BIT);
REGISTER_CONST(GL_, VIEWPORT_BIT);
REGISTER_CONST(GL_, TRANSFORM_BIT);
REGISTER_CONST(GL_, ENABLE_BIT);
REGISTER_CONST(GL_, COLOR_BUFFER_BIT);
REGISTER_CONST(GL_, HINT_BIT);
REGISTER_CONST(GL_, EVAL_BIT);
REGISTER_CONST(GL_, LIST_BIT);
REGISTER_CONST(GL_, TEXTURE_BIT);
REGISTER_CONST(GL_, SCISSOR_BIT);
REGISTER_CONST(GL_, ALL_ATTRIB_BITS);

luaL_register(L, "glut", glutlib);
REGISTER_CONST(GLUT_, RGBA);

return 1;
}
104 changes: 68 additions & 36 deletions sample.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
sample =
{
ctrl_c = 3,

interval = 100,
width = 160,
height = 120,

pos1 = { x = 10, y = 10 },
vel1 = { x = 8, y = -2 },
pos2 = { x = 75, y = 55 },
vel2 = { x = -6, y = 4 }
}


function sample:keyboard(key, x, y)
if key == self.ctrl_c then os.exit() end
end

function sample:display()
glut.vertex2i(self.pos1.x, self.pos1.y)
glut.vertex2i(self.pos2.x, self.pos2.y)
end

function sample:timer()
self.pos1.x = self.pos1.x + self.vel1.x
self.pos1.y = self.pos1.y + self.vel1.y
self.pos2.x = self.pos2.x + self.vel2.x
self.pos2.y = self.pos2.y + self.vel2.y
if (self.pos1.x < 0) or (self.width < self.pos1.x) then self.vel1.x = -self.vel1.x end
if (self.pos1.y < 0) or (self.height < self.pos1.y) then self.vel1.y = -self.vel1.y end
if (self.pos2.x < 0) or (self.width < self.pos2.x) then self.vel2.x = -self.vel2.x end
if (self.pos2.y < 0) or (self.height < self.pos2.y) then self.vel2.y = -self.vel2.y end
end

glut.main_loop(sample)
sample =
{
pos1 = { x= 10, y = 20 },
vel1 = { x= 8, y = -2 },
pos2 = { x= 310, y = 230 },
vel2 = { x= -6, y = 4 },

interval = 200,

width = 320,
height = 240,

ctrl_c = 3
}

function sample:display()
gl.clear(gl.COLOR_BUFFER_BIT)

gl.color3d(0.0, 0.0, 0.0)
gl.primitive(gl.LINES, function()
gl.vertex2i(self.pos1.x, self.pos1.y)
gl.vertex2i(self.pos2.x, self.pos2.y)
end)

gl.flush()
end

function sample:reshape(w, h)
gl.viewport(0, 0, w, h)
gl.load_identity()
gl.ortho(-0.5, w - 0.5, h - 0.5, -0.5, -1.0, 1.0)
self.width = w
self.height = h
end

function sample:keyboard(key, x, y)
if key == self.ctrl_c then
os.exit()
end
end

function sample:timer(value)
self.pos1.x = self.pos1.x + self.vel1.x
self.pos1.y = self.pos1.y + self.vel1.y
self.pos2.x = self.pos2.x + self.vel2.x
self.pos2.y = self.pos2.y + self.vel2.y
if (self.pos1.x < 0) or (self.width < self.pos1.x) then self.vel1.x = -self.vel1.x end
if (self.pos1.y < 0) or (self.height < self.pos1.y) then self.vel1.y = -self.vel1.y end
if (self.pos2.x < 0) or (self.width < self.pos2.x) then self.vel2.x = -self.vel2.x end
if (self.pos2.y < 0) or (self.height < self.pos2.y) then self.vel2.y = -self.vel2.y end

gl.color3d(0.0, 0.0, 0.0)
gl.primitive(gl.LINES, function()
gl.vertex2i(self.pos1.x, self.pos1.y)
gl.vertex2i(self.pos2.x, self.pos2.y)
end)

gl.flush()
end

glut.init_window_position(100, 100)
glut.init_window_size(sample.width, sample.height)
glut.init(arg)
glut.init_display_mode(glut.RGBA)
glut.create_window(arg[0])
gl.clear_color(1.0, 1.0, 1.0, 1.0)

glut.main_loop(sample)

0 comments on commit ad0b1f5

Please sign in to comment.