Pass:donut;

This commit is contained in:
bjorn 2022-06-23 19:52:37 -07:00
parent 8c63f47b8a
commit dc73d2309a
4 changed files with 71 additions and 3 deletions

View File

@ -517,6 +517,16 @@ static int l_lovrPassCircle(lua_State* L) {
return 0;
}
static int l_lovrPassTorus(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
float transform[16];
int index = luax_readmat4(L, 2, transform, -2);
uint32_t segmentsT = luax_optu32(L, index++, 64);
uint32_t segmentsP = luax_optu32(L, index++, 32);
lovrPassTorus(pass, transform, segmentsT, segmentsP);
return 0;
}
static int l_lovrPassText(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
Font* font = luax_totype(L, 2, Font);
@ -779,6 +789,7 @@ const luaL_Reg lovrPass[] = {
{ "cube", l_lovrPassCube },
{ "box", l_lovrPassBox },
{ "circle", l_lovrPassCircle },
{ "torus", l_lovrPassTorus },
{ "text", l_lovrPassText },
{ "mesh", l_lovrPassMesh },
{ "multimesh", l_lovrPassMultimesh },

View File

@ -70,6 +70,9 @@ int luax_readscale(lua_State* L, int index, vec3 v, int components, const char*
case LUA_TNUMBER:
if (components == 1) {
v[0] = v[1] = v[2] = luax_optfloat(L, index++, 0.f);
} else if (components == -2) { // -2 is special and means "2 components: xy and z"
v[0] = v[1] = luax_tofloat(L, index++);
v[2] = luax_optfloat(L, index++, 1.f);
} else {
v[0] = 1.f;
for (int i = 0; i < components; i++) {

View File

@ -2882,14 +2882,14 @@ void lovrPassBox(Pass* pass, float* transform, DrawStyle style) {
}
void lovrPassCircle(Pass* pass, float* transform, DrawStyle style, float angle1, float angle2, uint32_t segments) {
ShapeVertex* vertices;
uint16_t* indices;
if (fabsf(angle1 - angle2) >= 2.f * (float) M_PI) {
angle1 = 0.f;
angle2 = 2.f * (float) M_PI;
}
ShapeVertex* vertices;
uint16_t* indices;
if (style == STYLE_LINE) {
uint32_t vertexCount = segments + 1;
uint32_t indexCount = segments * 2;
@ -2942,6 +2942,59 @@ void lovrPassCircle(Pass* pass, float* transform, DrawStyle style, float angle1,
}
}
void lovrPassTorus(Pass* pass, float* transform, uint32_t segmentsT, uint32_t segmentsP) {
float sx = vec3_length(transform + 0);
float sy = vec3_length(transform + 4);
float sz = vec3_length(transform + 8);
vec3_scale(transform + 0, 1.f / sx);
vec3_scale(transform + 4, 1.f / sy);
vec3_scale(transform + 8, 1.f / sz);
float radius = sx * .5f;
float thickness = sz * .5f;
uint32_t vertexCount = segmentsT * segmentsP;
uint32_t indexCount = segmentsT * segmentsP * 6;
ShapeVertex* vertices;
uint16_t* indices;
lovrPassDraw(pass, &(Draw) {
.mode = VERTEX_TRIANGLES,
.transform = transform,
.vertex.pointer = (void**) &vertices,
.vertex.count = vertexCount,
.index.pointer = (void**) &indices,
.index.count = indexCount
});
// T and P stand for toroidal and poloidal, or theta and phi
float dt = (2.f * (float) M_PI) / segmentsT;
float dp = (2.f * (float) M_PI) / segmentsP;
for (uint32_t t = 0; t < segmentsT; t++) {
float theta = t * dt;
float tx = cosf(theta);
float ty = sinf(theta);
for (uint32_t p = 0; p < segmentsP; p++) {
float phi = p * dp;
float nx = cosf(phi) * tx;
float ny = cosf(phi) * ty;
float nz = sinf(phi);
*vertices++ = (ShapeVertex) {
.position = { tx * radius + nx * thickness, ty * radius + ny * thickness, nz * thickness },
.normal = { nx, ny, nz }
};
uint16_t a = (t + 0) * segmentsP + p;
uint16_t b = (t + 1) % segmentsT * segmentsP + p;
uint16_t c = (t + 0) * segmentsP + (p + 1) % segmentsP;
uint16_t d = (t + 1) % segmentsT * segmentsP + (p + 1) % segmentsP;
uint16_t quad[] = { a, b, c, b, c, d };
memcpy(indices, quad, sizeof(quad));
indices += COUNTOF(quad);
}
}
}
void lovrPassText(Pass* pass, Font* font, const char* text, uint32_t length, float* transform, float wrap, HorizontalAlign halign, VerticalAlign valign) {
if (!font) {
font = lovrGraphicsGetDefaultFont();

View File

@ -478,6 +478,7 @@ void lovrPassLine(Pass* pass, uint32_t count, float** vertices);
void lovrPassPlane(Pass* pass, float* transform, DrawStyle style, uint32_t cols, uint32_t rows);
void lovrPassBox(Pass* pass, float* transform, DrawStyle style);
void lovrPassCircle(Pass* pass, float* transform, DrawStyle style, float angle1, float angle2, uint32_t segments);
void lovrPassTorus(Pass* pass, float* transform, uint32_t segmentsT, uint32_t segmentsP);
void lovrPassText(Pass* pass, Font* font, const char* text, uint32_t length, float* transform, float wrap, HorizontalAlign halign, VerticalAlign valign);
void lovrPassMesh(Pass* pass, Buffer* vertices, Buffer* indices, float* transform, uint32_t start, uint32_t count, uint32_t instances);
void lovrPassMultimesh(Pass* pass, Buffer* vertices, Buffer* indices, Buffer* indirect, uint32_t count, uint32_t offset, uint32_t stride);