lovr.graphics.points;

This commit is contained in:
bjorn 2016-11-07 23:16:33 -08:00
parent 88a8afd775
commit 48ff1fa93c
4 changed files with 49 additions and 27 deletions

View File

@ -362,6 +362,11 @@ void lovrGraphicsDrawFilledShape() {
glBindVertexArray(0);
}
void lovrGraphicsPoints(float* points, int count) {
lovrGraphicsSetShapeData(points, count, NULL, 0);
lovrGraphicsDrawLinedShape(GL_POINTS);
}
void lovrGraphicsLine(float* points, int count) {
lovrGraphicsSetShapeData(points, count, NULL, 0);
lovrGraphicsDrawLinedShape(GL_LINE_STRIP);

View File

@ -93,6 +93,7 @@ void lovrGraphicsGetDimensions(int* width, int* height);
void lovrGraphicsSetShapeData(float* data, int dataCount, unsigned int* indices, int indicesCount);
void lovrGraphicsDrawLinedShape(GLenum mode);
void lovrGraphicsDrawFilledShape();
void lovrGraphicsPoints(float* points, int count);
void lovrGraphicsLine(float* points, int count);
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz);
void lovrGraphicsCube(DrawMode mode, float x, float y, float z, float size, float angle, float axisX, float axisY, float axisZ);

View File

@ -8,6 +8,36 @@
#include "../util.h"
#include <math.h>
static void luax_readvertices(lua_State* L, int index, vec_float_t* points) {
int isTable = lua_istable(L, index);
if (!isTable && !lua_isnumber(L, index)) {
luaL_error(L, "Expected number or table, got '%s'", lua_typename(L, lua_type(L, 1)));
return;
}
int count = isTable ? lua_objlen(L, index) : lua_gettop(L);
if (count % 3 != 0) {
vec_deinit(points);
luaL_error(L, "Number of coordinates must be a multiple of 3, got '%d'", count);
return;
}
vec_reserve(points, count);
if (isTable) {
for (int i = 1; i <= count; i++) {
lua_rawgeti(L, index, i);
vec_push(points, lua_tonumber(L, -1));
lua_pop(L, 1);
}
} else {
for (int i = 0; i < count; i++) {
vec_push(points, lua_tonumber(L, index + i));
}
}
}
const luaL_Reg lovrGraphics[] = {
{ "reset", l_lovrGraphicsReset },
{ "clear", l_lovrGraphicsClear },
@ -35,6 +65,7 @@ const luaL_Reg lovrGraphics[] = {
{ "translate", l_lovrGraphicsTranslate },
{ "rotate", l_lovrGraphicsRotate },
{ "scale", l_lovrGraphicsScale },
{ "points", l_lovrGraphicsPoints },
{ "line", l_lovrGraphicsLine },
{ "plane", l_lovrGraphicsPlane },
{ "cube", l_lovrGraphicsCube },
@ -317,36 +348,20 @@ int l_lovrGraphicsScale(lua_State* L) {
return 0;
}
int l_lovrGraphicsPoints(lua_State* L) {
vec_float_t points;
vec_init(&points);
luax_readvertices(L, 1, &points);
lovrGraphicsPoints(points.data, points.length);
vec_deinit(&points);
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));
}
}
lovrGraphicsLine(points.data, count);
luax_readvertices(L, 1, &points);
lovrGraphicsLine(points.data, points.length);
vec_deinit(&points);
return 0;
}

View File

@ -37,6 +37,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_lovrGraphicsPoints(lua_State* L);
int l_lovrGraphicsLine(lua_State* L);
int l_lovrGraphicsPlane(lua_State* L);
int l_lovrGraphicsCube(lua_State* L);