mirror of https://github.com/bjornbytes/lovr.git
lovr.graphics.triangle;
This commit is contained in:
parent
8892b03590
commit
3d89338fec
|
@ -355,6 +355,28 @@ void lovrGraphicsLine(float* points, int count) {
|
|||
lovrGraphicsDrawLinedShape(GL_LINE_STRIP);
|
||||
}
|
||||
|
||||
void lovrGraphicsTriangle(DrawMode mode, float* points) {
|
||||
if (mode == DRAW_MODE_LINE) {
|
||||
lovrGraphicsSetShapeData(points, 9, NULL, 0);
|
||||
lovrGraphicsDrawLinedShape(GL_LINE_LOOP);
|
||||
} else {
|
||||
float n[3] = {
|
||||
points[1] * points[5] - points[2] * points[4],
|
||||
points[2] * points[3] - points[0] * points[5],
|
||||
points[0] * points[4] - points[1] * points[3]
|
||||
};
|
||||
|
||||
float data[18] = {
|
||||
points[0], points[1], points[2], n[0], n[1], n[2],
|
||||
points[3], points[4], points[5], n[0], n[1], n[2],
|
||||
points[6], points[7], points[8], n[0], n[1], n[2]
|
||||
};
|
||||
|
||||
lovrGraphicsSetShapeData(data, 18, NULL, 0);
|
||||
lovrGraphicsDrawFilledShape();
|
||||
}
|
||||
}
|
||||
|
||||
void lovrGraphicsPlane(DrawMode mode, float x, float y, float z, float size, float nx, float ny, float nz) {
|
||||
|
||||
// Normalize the normal vector
|
||||
|
|
|
@ -89,6 +89,7 @@ void lovrGraphicsDrawLinedShape(GLenum mode);
|
|||
void lovrGraphicsDrawFilledShape();
|
||||
void lovrGraphicsPoints(float* points, int count);
|
||||
void lovrGraphicsLine(float* points, int count);
|
||||
void lovrGraphicsTriangle(DrawMode mode, float* points);
|
||||
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);
|
||||
void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float az);
|
||||
|
|
|
@ -17,7 +17,7 @@ static void luax_readvertices(lua_State* L, int index, vec_float_t* points) {
|
|||
return;
|
||||
}
|
||||
|
||||
int count = isTable ? lua_objlen(L, index) : lua_gettop(L);
|
||||
int count = isTable ? lua_objlen(L, index) : lua_gettop(L) - index + 1;
|
||||
if (count % 3 != 0) {
|
||||
vec_deinit(points);
|
||||
luaL_error(L, "Number of coordinates must be a multiple of 3, got '%d'", count);
|
||||
|
@ -70,6 +70,7 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ "scale", l_lovrGraphicsScale },
|
||||
{ "points", l_lovrGraphicsPoints },
|
||||
{ "line", l_lovrGraphicsLine },
|
||||
{ "triangle", l_lovrGraphicsTriangle },
|
||||
{ "plane", l_lovrGraphicsPlane },
|
||||
{ "cube", l_lovrGraphicsCube },
|
||||
{ "getWidth", l_lovrGraphicsGetWidth },
|
||||
|
@ -384,6 +385,20 @@ int l_lovrGraphicsLine(lua_State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsTriangle(lua_State* L) {
|
||||
DrawMode* drawMode = (DrawMode*) luax_checkenum(L, 1, &DrawModes, "draw mode");
|
||||
int top = lua_gettop(L);
|
||||
if (top != 10) {
|
||||
return luaL_error(L, "Expected 9 coordinates to make a triangle, got %d values", top - 1);
|
||||
}
|
||||
vec_float_t points;
|
||||
vec_init(&points);
|
||||
luax_readvertices(L, 2, &points);
|
||||
lovrGraphicsTriangle(*drawMode, points.data);
|
||||
vec_deinit(&points);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsPlane(lua_State* L) {
|
||||
const char* userDrawMode = luaL_checkstring(L, 1);
|
||||
DrawMode* drawMode = (DrawMode*) map_get(&DrawModes, userDrawMode);
|
||||
|
|
|
@ -43,6 +43,7 @@ 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_lovrGraphicsTriangle(lua_State* L);
|
||||
int l_lovrGraphicsPlane(lua_State* L);
|
||||
int l_lovrGraphicsCube(lua_State* L);
|
||||
int l_lovrGraphicsGetWidth(lua_State* L);
|
||||
|
|
Loading…
Reference in New Issue