Pass:monkey;

Questionable.
This commit is contained in:
bjorn 2022-06-21 20:05:57 -07:00
parent 75e8df58df
commit 4021d4e893
8 changed files with 2368 additions and 2 deletions

View File

@ -437,7 +437,7 @@ src += config.modules.thread and 'src/lib/tinycthread/*.c' or nil
-- embed resource files with xxd
res = { 'etc/*.lua', 'etc/*.ttf', 'etc/shaders/*.glsl' }
res = { 'etc/boot.lua', 'etc/*.ttf', 'etc/shaders/*.glsl' }
tup.foreach_rule(res, '^ XD %b^ xxd -i %f > %o', '%f.h')
for i, pattern in ipairs(res) do

BIN
etc/monkey.glb Normal file

Binary file not shown.

2265
etc/monkey.h Normal file

File diff suppressed because it is too large Load Diff

53
etc/monkeycrush.lua Normal file
View File

@ -0,0 +1,53 @@
-- Usage: lovr etc/monkeycrush.lua > etc/monkey.h
local etc = lovr.filesystem.getSource()
local success, model = assert(pcall(lovr.data.newModelData, 'monkey.glb'))
local min, max = lovr.math.newVec3(math.huge), lovr.math.newVec3(-math.huge)
for i = 1, model:getMeshVertexCount(1) do
local x, y, z, nx, ny, nz = model:getMeshVertex(1, i)
min.x, min.y, min.z = math.min(x, min.x), math.min(y, min.y), math.min(z, min.z)
max.x, max.y, max.z = math.max(x, max.x), math.max(y, max.y), math.max(z, max.z)
end
local size = lovr.math.newVec3(max - min)
local scale = .5
min:mul(scale)
max:mul(scale)
size:mul(scale)
io.write(('float monkey_size[3] = { %ff, %ff, %ff };\n'):format(size:unpack()))
io.write(('float monkey_offset[3] = { %ff, %ff, %ff };\n'):format(min:unpack()))
io.write('\n')
io.write('uint8_t monkey_vertices[] = {\n')
for i = 1, model:getMeshVertexCount(1) do
local x, y, z, nx, ny, nz = model:getMeshVertex(1, i)
local position = vec3(x, y, z):mul(scale)
local normal = vec3(nx, ny, nz)
local qx, qy, qz = ((position - min) / size * 255 + .5):unpack()
local qnx, qny, qnz = ((normal / 2 + .5) * 255 + .5):unpack()
qx, qy, qz = math.floor(qx), math.floor(qy), math.floor(qz)
qnx, qny, qnz = math.floor(qnx), math.floor(qny), math.floor(qnz)
io.write((' %d, %d, %d, %d, %d, %d,\n'):format(qx, qy, qz, qnx, qny, qnz))
lovr.math.drain()
end
io.write('};\n\n')
io.write('uint16_t monkey_indices[] = {\n ')
for i = 1, model:getMeshIndexCount(1) do
local index = model:getMeshIndex(1, i)
io.write((' %d,'):format(index))
if i % 10 == 0 then
io.write('\n ')
end
end
io.write('\n};\n')
lovr.event.quit()

View File

@ -336,9 +336,20 @@ static int l_lovrModelDataGetMeshVertex(lua_State* L) {
ModelAttribute* attribute = mesh->attributes[i];
if (!attribute) continue;
uint32_t stride = model->buffers[attribute->buffer].stride;
if (!stride) {
switch (attribute->type) {
case I8: case U8: stride = attribute->components * 1; break;
case I16: case U16: stride = attribute->components * 2; break;
case I32: case U32: case F32: stride = attribute->components * 4; break;
default: break;
}
}
AttributeData data = { .raw = model->buffers[attribute->buffer].data };
data.u8 += attribute->offset;
data.u8 += vertex * model->buffers[attribute->buffer].stride;
data.u8 += vertex * stride;
for (uint32_t j = 0; j < attribute->components; j++) {
switch (attribute->type) {

View File

@ -573,6 +573,14 @@ static int l_lovrPassFill(lua_State* L) {
return 0;
}
static int l_lovrPassMonkey(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
float transform[16];
luax_readmat4(L, 2, transform, 1);
lovrPassMonkey(pass, transform);
return 0;
}
static int l_lovrPassMesh(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
Buffer* vertices = !lua_toboolean(L, 2) ? NULL : luax_totype(L, 2, Buffer);
@ -825,6 +833,7 @@ const luaL_Reg lovrPass[] = {
{ "cylinder", l_lovrPassCylinder },
{ "text", l_lovrPassText },
{ "fill", l_lovrPassFill },
{ "monkey", l_lovrPassMonkey },
{ "mesh", l_lovrPassMesh },
{ "multimesh", l_lovrPassMultimesh },

View File

@ -9,6 +9,7 @@
#include "core/spv.h"
#include "core/os.h"
#include "util.h"
#include "monkey.h"
#include "shaders.h"
#include <math.h>
#include <stdlib.h>
@ -3496,6 +3497,32 @@ void lovrPassFill(Pass* pass, Texture* texture) {
});
}
void lovrPassMonkey(Pass* pass, float* transform) {
uint32_t vertexCount = COUNTOF(monkey_vertices) / 6;
ShapeVertex* vertices;
lovrPassDraw(pass, &(Draw) {
.mode = VERTEX_TRIANGLES,
.vertex.count = vertexCount,
.vertex.pointer = (void**) &vertices,
.index.count = COUNTOF(monkey_indices),
.index.data = monkey_indices,
.transform = transform
});
// Manual vertex format conversion to avoid another format (and sn8x3 isn't always supported)
for (uint32_t i = 0; i < vertexCount; i++) {
vertices[i] = (ShapeVertex) {
.position.x = monkey_vertices[6 * i + 0] / 255.f * monkey_size[0] + monkey_offset[0],
.position.y = monkey_vertices[6 * i + 1] / 255.f * monkey_size[1] + monkey_offset[1],
.position.z = monkey_vertices[6 * i + 2] / 255.f * monkey_size[2] + monkey_offset[2],
.normal.x = monkey_vertices[6 * i + 3] / 255.f * 2.f - 1.f,
.normal.y = monkey_vertices[6 * i + 4] / 255.f * 2.f - 1.f,
.normal.z = monkey_vertices[6 * i + 5] / 255.f * 2.f - 1.f,
};
}
}
void lovrPassMesh(Pass* pass, Buffer* vertices, Buffer* indices, float* transform, uint32_t start, uint32_t count, uint32_t instances) {
if (count == ~0u) {
count = (indices ? indices : vertices)->info.length - start;

View File

@ -484,6 +484,7 @@ void lovrPassCylinder(Pass* pass, float* transform, bool capped, float angle1, f
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 lovrPassFill(Pass* pass, Texture* texture);
void lovrPassMonkey(Pass* pass, float* transform);
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);
void lovrPassCompute(Pass* pass, uint32_t x, uint32_t y, uint32_t z, Buffer* indirect, uint32_t offset);