Pass:plane;

This commit is contained in:
bjorn 2022-06-04 01:34:13 -07:00
parent ebc6d9d3a3
commit e652ae67af
3 changed files with 74 additions and 0 deletions

View File

@ -416,6 +416,13 @@ static int l_lovrPassLine(lua_State* L) {
return 0;
}
static int l_lovrPassPlane(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
float transform[16];
int index = luax_readmat4(L, 2, transform, 2);
uint32_t hsegments = luax_optu32(L, index++, 1);
uint32_t vsegments = luax_optu32(L, index, hsegments);
lovrPassPlane(pass, transform, hsegments, vsegments);
return 0;
}
@ -596,6 +603,7 @@ const luaL_Reg lovrPass[] = {
{ "points", l_lovrPassPoints },
{ "line", l_lovrPassLine },
{ "plane", l_lovrPassPlane },
{ "clear", l_lovrPassClear },
{ "copy", l_lovrPassCopy },

View File

@ -141,6 +141,12 @@ typedef enum {
DEFAULT_FORMAT_COUNT
} DefaultFormat;
typedef struct {
struct { float x, y, z; } position;
uint32_t normal;
struct { uint16_t u, v; } uv;
} ShapeVertex;
typedef struct {
gpu_draw_mode mode;
DefaultShader shader;
@ -306,6 +312,15 @@ bool lovrGraphicsInit(bool debug, bool vsync) {
gpu_clear_buffer(transfers, state.defaultBuffer->gpu, 0, 4096);
gpu_clear_texture(transfers, state.defaultTexture->gpu, (float[4]) { 1.f, 1.f, 1.f, 1.f }, 0, ~0u, 0, ~0u);
state.vertexFormats[VERTEX_SHAPE].gpu = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 3,
.bufferStrides[1] = sizeof(ShapeVertex),
.attributes[0] = { 1, 0, offsetof(ShapeVertex, position), GPU_TYPE_F32x3 },
.attributes[1] = { 1, 1, offsetof(ShapeVertex, normal), GPU_TYPE_UN10x3 },
.attributes[2] = { 1, 2, offsetof(ShapeVertex, uv), GPU_TYPE_UN16x2 }
};
state.vertexFormats[VERTEX_POINT].gpu = (gpu_vertex_format) {
.bufferCount = 2,
.attributeCount = 1,
@ -2022,6 +2037,56 @@ void lovrPassLine(Pass* pass, uint32_t count, float** points) {
}
}
static inline uint32_t packNormal(float nx, float ny, float nz) {
uint32_t n = 0;
n |= (uint32_t) (nx * 1023.f) << 20;
n |= (uint32_t) (ny * 1023.f) << 10;
n |= (uint32_t) (nz * 1023.f) << 0;
return n;
}
void lovrPassPlane(Pass* pass, float* transform, uint32_t hsegments, uint32_t vsegments) {
ShapeVertex* vertices;
uint16_t* indices;
uint32_t vertexCount = (hsegments + 1) * (vsegments + 1);
uint32_t indexCount = (hsegments * vsegments) * 6;
lovrPassDraw(pass, &(Draw) {
.mode = GPU_DRAW_TRIANGLES,
.transform = transform,
.vertex.format = VERTEX_SHAPE,
.vertex.pointer = (void**) &vertices,
.vertex.count = vertexCount,
.index.pointer = (void**) &indices,
.index.count = indexCount
});
for (uint32_t y = 0, v = 0; y <= vsegments; y++) {
for (uint32_t x = 0; x <= hsegments; x++, v++) {
vertices[v].position.x = -.5f + (float) x / hsegments;
vertices[v].position.y = .5f - (float) y / vsegments;
vertices[v].position.z = 0.f;
vertices[v].normal = packNormal(0.f, 0.f, 1.f);
vertices[v].uv.u = (uint16_t) ((float) x / hsegments * 65535.f);
vertices[v].uv.v = (uint16_t) ((float) y / vsegments * 65535.f);
}
}
uint16_t skip = hsegments + 1;
for (uint32_t y = 0; y < vsegments; y++) {
for (uint32_t x = 0; x < hsegments; x++) {
uint16_t a = ((y + 0) * skip) + x + 0;
uint16_t b = ((y + 0) * skip) + x + 1;
uint16_t c = ((y + 1) * skip) + x + 0;
uint16_t d = ((y + 1) * skip) + x + 1;
uint16_t cell[] = { a, b, c, c, b, d };
memcpy(indices, cell, sizeof(cell));
indices += COUNTOF(cell);
}
}
}
void lovrPassClearBuffer(Pass* pass, Buffer* buffer, uint32_t offset, uint32_t extent) {
if (extent == 0) return;
if (extent == ~0u) extent = buffer->size - offset;

View File

@ -390,6 +390,7 @@ void lovrPassSendTexture(Pass* pass, const char* name, size_t length, uint32_t s
void lovrPassSendSampler(Pass* pass, const char* name, size_t length, uint32_t slot, Sampler* sampler);
void lovrPassPoints(Pass* pass, uint32_t count, float** vertices);
void lovrPassLine(Pass* pass, uint32_t count, float** vertices);
void lovrPassPlane(Pass* pass, float* transform, uint32_t hsegments, uint32_t vsegments);
void lovrPassClearBuffer(Pass* pass, Buffer* buffer, uint32_t offset, uint32_t extent);
void lovrPassClearTexture(Pass* pass, Texture* texture, float value[4], uint32_t layer, uint32_t layerCount, uint32_t level, uint32_t levelCount);
void lovrPassCopyDataToBuffer(Pass* pass, void* data, Buffer* buffer, uint32_t offset, uint32_t size);