lovr.graphics.line;

This commit is contained in:
bjorn 2016-09-29 19:39:25 -07:00
parent 8ab7c06612
commit d4f20c8379
4 changed files with 50 additions and 2 deletions

View File

@ -243,11 +243,16 @@ void lovrGraphicsSetShapeData(float* data, int count) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
}
void lovrGraphicsDrawShape() {
void lovrGraphicsDrawShape(DrawMode mode) {
lovrGraphicsPrepare();
glBindVertexArray(state.shapeArray);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_FAN, 0, state.shapeData.length / 3);
glDrawArrays(mode, 0, state.shapeData.length / 3);
}
void lovrGraphicsLine(float* points, int count) {
lovrGraphicsSetShapeData(points, count);
lovrGraphicsDrawShape(DRAW_MODE_LINE);
}
Buffer* lovrGraphicsNewBuffer(int size, BufferDrawMode drawMode, BufferUsage usage) {

View File

@ -20,6 +20,11 @@ typedef struct {
int height;
} ScissorRectangle;
typedef enum {
DRAW_MODE_FILL = GL_TRIANGLE_FAN,
DRAW_MODE_LINE = GL_LINE_STRIP
} DrawMode;
typedef struct {
Shader* activeShader;
Shader* defaultShader;
@ -67,6 +72,7 @@ void lovrGraphicsTransform(mat4 transform);
void lovrGraphicsGetDimensions(int* width, int* height);
void lovrGraphicsSetShapeData(float* data, int count);
void lovrGraphicsDrawShape();
void lovrGraphicsLine(float* points, int count);
Buffer* lovrGraphicsNewBuffer(int size, BufferDrawMode drawMode, BufferUsage usage);
Model* lovrGraphicsNewModel(const char* path);
Shader* lovrGraphicsNewShader(const char* vertexSource, const char* fragmentSource);

View File

@ -25,6 +25,7 @@ const luaL_Reg lovrGraphics[] = {
{ "translate", l_lovrGraphicsTranslate },
{ "rotate", l_lovrGraphicsRotate },
{ "scale", l_lovrGraphicsScale },
{ "line", l_lovrGraphicsLine },
{ "getWidth", l_lovrGraphicsGetWidth },
{ "getHeight", l_lovrGraphicsGetHeight },
{ "getDimensions", l_lovrGraphicsGetDimensions },
@ -244,6 +245,41 @@ int l_lovrGraphicsScale(lua_State* L) {
return 0;
}
int l_lovrGraphicsLine(lua_State* L) {
vec_float_t points;
vec_init(&points);
int isTable = lua_istable(L, 1);
if (!isTable && !lua_isnumber(L, 1)) {
return luaL_error(L, "Expected number or table, got '%s'", lua_typename(L, lua_type(L, 1)));
}
int count = isTable ? lua_objlen(L, 1) : lua_gettop(L);
if (count % 3 != 0) {
vec_deinit(&points);
return luaL_error(L, "Number of coordinates must be a multiple of 3, got '%d'", count);
}
vec_reserve(&points, count);
if (isTable) {
for (int i = 1; i <= count; i++) {
lua_rawgeti(L, 1, i);
vec_push(&points, lua_tonumber(L, -1));
lua_pop(L, 1);
}
} else {
for (int i = 1; i <= count; i++) {
vec_push(&points, lua_tonumber(L, i));
}
}
lovrGraphicsSetShapeData(points.data, count);
lovrGraphicsDrawShape(DRAW_MODE_LINE);
vec_deinit(&points);
return 0;
}
int l_lovrGraphicsGetWidth(lua_State* L) {
int width;
lovrGraphicsGetDimensions(&width, NULL);

View File

@ -28,6 +28,7 @@ int l_lovrGraphicsOrigin(lua_State* L);
int l_lovrGraphicsTranslate(lua_State* L);
int l_lovrGraphicsRotate(lua_State* L);
int l_lovrGraphicsScale(lua_State* L);
int l_lovrGraphicsLine(lua_State* L);
int l_lovrGraphicsGetWidth(lua_State* L);
int l_lovrGraphicsGetHeight(lua_State* L);
int l_lovrGraphicsGetDimensions(lua_State* L);