luax_pushvertex;

This commit is contained in:
bjorn 2018-01-25 18:51:07 -08:00
parent ea7f3d48a2
commit dc801d8590
3 changed files with 23 additions and 17 deletions

View File

@ -62,24 +62,9 @@ int l_lovrMeshGetVertexCount(lua_State* L) {
int l_lovrMeshGetVertex(lua_State* L) {
Mesh* mesh = luax_checktype(L, 1, Mesh);
int index = luaL_checkint(L, 2) - 1;
char* vertex = lovrMeshMap(mesh, index, 1, true, false);
uint8_t* vertex = lovrMeshMap(mesh, index, 1, true, false);
VertexFormat format = lovrMeshGetVertexFormat(mesh);
int total = 0;
for (int i = 0; i < format.length; i++) {
Attribute attribute = format.data[i];
total += attribute.count;
for (int j = 0; j < attribute.count; j++) {
switch (attribute.type) {
case ATTR_FLOAT: lua_pushnumber(L, *((float*) vertex)); break;
case ATTR_BYTE: lua_pushnumber(L, *((uint8_t*) vertex)); break;
case ATTR_INT: lua_pushnumber(L, *((int*) vertex)); break;
}
vertex += AttributeSizes[attribute.type];
}
}
return total;
return luax_pushvertex(L, vertex, format);
}
int l_lovrMeshSetVertex(lua_State* L) {

View File

@ -196,3 +196,22 @@ Color luax_checkcolor(lua_State* L, int index) {
return color;
}
int luax_pushvertex(lua_State* L, uint8_t* vertex, VertexFormat format) {
int count = 0;
for (int i = 0; i < format.length; i++) {
Attribute attribute = format.data[i];
count += attribute.count;
for (int j = 0; j < attribute.count; j++) {
switch (attribute.type) {
case ATTR_FLOAT: lua_pushnumber(L, *((float*) vertex)); break;
case ATTR_BYTE: lua_pushnumber(L, *((uint8_t*) vertex)); break;
case ATTR_INT: lua_pushnumber(L, *((int*) vertex)); break;
}
vertex += AttributeSizes[attribute.type];
}
}
return count;
}

View File

@ -2,6 +2,7 @@
#include <lauxlib.h>
#include <lualib.h>
#include "lib/map/map.h"
#include "lib/vertex.h"
#include "util.h"
#pragma once
@ -42,3 +43,4 @@ void luax_pushenum(lua_State* L, map_int_t* map, int value);
void* luax_checkenum(lua_State* L, int index, map_int_t* map, const char* typeName);
void* luax_optenum(lua_State* L, int index, const char* fallback, map_int_t* map, const char* typeName);
Color luax_checkcolor(lua_State* L, int index);
int luax_pushvertex(lua_State* L, uint8_t* vertex, VertexFormat format);