Skip to content

Commit

Permalink
WIP;
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbytes committed Jun 11, 2023
1 parent 0b36eac commit 9117b92
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern StringEntry lovrHeadsetOrigin[];
extern StringEntry lovrHorizontalAlign[];
extern StringEntry lovrJointType[];
extern StringEntry lovrKeyboardKey[];
extern StringEntry lovrMeshMode[];
extern StringEntry lovrDrawMode[];
extern StringEntry lovrOriginType[];
extern StringEntry lovrPassType[];
extern StringEntry lovrPermission[];
Expand Down
65 changes: 61 additions & 4 deletions src/api/l_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ StringEntry lovrHorizontalAlign[] = {
{ 0 }
};

StringEntry lovrMeshMode[] = {
[MESH_POINTS] = ENTRY("points"),
[MESH_LINES] = ENTRY("lines"),
[MESH_TRIANGLES] = ENTRY("triangles"),
StringEntry lovrDrawMode[] = {
[DRAW_POINTS] = ENTRY("points"),
[DRAW_LINES] = ENTRY("lines"),
[DRAW_TRIANGLES] = ENTRY("triangles"),
{ 0 }
};

Expand Down Expand Up @@ -1350,6 +1350,62 @@ static int l_lovrGraphicsNewFont(lua_State* L) {
return 1;
}

static int l_lovrGraphicsNewMesh(lua_State* L) {
MeshInfo info = { 0 };
BufferField fields[16] = { 0 };
info.fields = fields;

bool hasFormat = false;
if (lua_istable(L, 1)) {
lua_rawgeti(L, 1, 1);
if (lua_istable(L, -1)) {
lua_getfield(L, -1, "type");
hasFormat = lua_type(L, -1) == LUA_TSTRING;
lua_pop(L, 1);
}
lua_pop(L, 1);
}

if (hasFormat) {
luax_checkbufferformat(L, 1, info.fields, &info.fieldCount, COUNTOF(fields));
} else {
BufferField defaultFormat[] = {
{ .length = 0, .stride = 36 },
{ .location = LOCATION_POSITION, .type = FIELD_F32x3, .offset = 0 },
{ .location = LOCATION_NORMAL, .type = FIELD_F32x3, .offset = 12 },
{ .location = LOCATION_UV, .type = FIELD_F32x2, .offset = 24 },
{ .location = LOCATION_COLOR, .type = FIELD_UN8x4, .offset = 32 }
};

info.fieldCount = COUNTOF(defaultFormat);
memcpy(info.fields, defaultFormat, sizeof(defaultFormat));
fields[0].childCount = COUNTOF(defaultFormat) - 1;
fields[0].children = fields + 1;
}

Blob* vertexBlob = NULL;
Buffer* vertexBuffer = NULL;
int vertexIndex = hasFormat ? 2 : 1;
switch (lua_type(L, vertexIndex)) {
case LUA_TNUMBER: info.vertexCount = luax_checku32(L, vertexIndex); break;
case LUA_TTABLE: info.vertexCount = luax_len(L, vertexIndex); break;
case LUA_TUSERDATA:
if ((vertexBlob = luax_totype(L, vertexIndex, Blob)) != NULL) {
info.vertexCount = vertexBlob->size / stride;
break;
} else if ((vertexBuffer = luax_totype(L, vertexIndex, Buffer)) != NULL) {
info.vertexBuffer = lovrBufferGetInfo(vertexBuffer)->length;
break;
}
default: return luax_typeerror(L, vertexIndex, "number, table, Blob, or Buffer");
}

Mesh* mesh = lovrMeshCreate(&info);
luax_pushtype(L, Mesh, mesh);
lovrRelease(mesh, lovrMeshDestroy);
return 1;
}

static int l_lovrGraphicsNewModel(lua_State* L) {
ModelInfo info = { 0 };
info.data = luax_totype(L, 1, ModelData);
Expand Down Expand Up @@ -1428,6 +1484,7 @@ static const luaL_Reg lovrGraphics[] = {
{ "newShader", l_lovrGraphicsNewShader },
{ "newMaterial", l_lovrGraphicsNewMaterial },
{ "newFont", l_lovrGraphicsNewFont },
{ "newMesh", l_lovrGraphicsNewMesh },
{ "newModel", l_lovrGraphicsNewModel },
{ "newPass", l_lovrGraphicsNewPass },
{ "getPass", l_lovrGraphicsGetPass },
Expand Down
2 changes: 1 addition & 1 deletion src/api/l_graphics_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ static int l_lovrPassSetMaterial(lua_State* L) {

static int l_lovrPassSetMeshMode(lua_State* L) {
Pass* pass = luax_checktype(L, 1, Pass);
MeshMode mode = luax_checkenum(L, 2, MeshMode, NULL);
DrawMode mode = luax_checkenum(L, 2, DrawMode, NULL);
lovrPassSetMeshMode(pass, mode);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ typedef enum {

typedef struct {
uint64_t hash;
MeshMode mode;
DrawMode mode;
DefaultShader shader;
Material* material;
float* transform;
Expand Down Expand Up @@ -390,7 +390,7 @@ typedef struct {

typedef struct {
bool dirty;
MeshMode mode;
DrawMode mode;
float color[4];
uint64_t formatHash;
gpu_pipeline_info info;
Expand Down Expand Up @@ -5221,7 +5221,7 @@ void lovrPassSetMaterial(Pass* pass, Material* material, Texture* texture) {
}
}

void lovrPassSetMeshMode(Pass* pass, MeshMode mode) {
void lovrPassSetMeshMode(Pass* pass, DrawMode mode) {
pass->pipeline->mode = mode;
}

Expand Down
41 changes: 35 additions & 6 deletions src/modules/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct Sampler Sampler;
typedef struct Shader Shader;
typedef struct Material Material;
typedef struct Font Font;
typedef struct Mesh Mesh;
typedef struct Model Model;
typedef struct Readback Readback;
typedef struct Pass Pass;
Expand Down Expand Up @@ -416,6 +417,40 @@ float lovrFontGetWidth(Font* font, ColoredString* strings, uint32_t count);
void lovrFontGetLines(Font* font, ColoredString* strings, uint32_t count, float wrap, void (*callback)(void* context, const char* string, size_t length), void* context);
void lovrFontGetVertices(Font* font, ColoredString* strings, uint32_t count, float wrap, HorizontalAlign halign, VerticalAlign valign, GlyphVertex* vertices, uint32_t* glyphCount, uint32_t* lineCount, Material** material, bool flip);

// Mesh

typedef enum {
MESH_CPU,
MESH_GPU
} MeshStorage;

typedef enum {
DRAW_POINTS,
DRAW_LINES,
DRAW_TRIANGLES
} DrawMode;

typedef struct {
MeshStorage storage;
Buffer* vertexBuffer;
} MeshInfo;

Mesh* lovrMeshCreate(const MeshInfo* info);
void lovrMeshDestroy(void* ref);
Buffer* lovrMeshGetVertexBuffer(Mesh* mesh);
Buffer* lovrMeshGetIndexBuffer(Mesh* mesh);
Buffer* lovrMeshSetIndexBuffer(Mesh* mesh);
void* lovrMeshGetIndices(Mesh* mesh, uint32_t* count, FieldType* type);
void* lovrMeshSetIndices(Mesh* mesh, uint32_t count, FieldType type);
void lovrModelDataGetBoundingBox(ModelData* data, float box[6]);
void lovrModelDataSetBoundingBox(ModelData* data, float box[6]);
Material* lovrMeshGetMaterial(Mesh* mesh);
void lovrMeshSetMaterial(Material* material);
DrawMode lovrMeshGetDrawMode(Mesh* mesh);
void lovrMeshSetDrawMode(Mesh* mesh, DrawMode mode);
void lovrMeshGetDrawRange(Mesh* mesh, uint32_t* start, uint32_t* count);
void lovrMeshSetDrawRange(Mesh* mesh, uint32_t start, uint32_t count);

// Model

typedef struct {
Expand All @@ -429,12 +464,6 @@ typedef enum {
ORIGIN_PARENT
} OriginType;

typedef enum {
MESH_POINTS,
MESH_LINES,
MESH_TRIANGLES
} MeshMode;

typedef struct {
MeshMode mode;
Material* material;
Expand Down

0 comments on commit 9117b92

Please sign in to comment.