Graphics culling;

This commit is contained in:
bjorn 2016-10-03 12:02:49 -07:00
parent 2e48c00876
commit 5ef7a6a588
4 changed files with 83 additions and 0 deletions

View File

@ -50,6 +50,8 @@ void lovrGraphicsReset() {
lovrGraphicsSetColorMask(1, 1, 1, 1);
lovrGraphicsSetScissorEnabled(0);
lovrGraphicsSetLineWidth(1);
lovrGraphicsSetCullingEnabled(0);
lovrGraphicsSetPolygonWinding(POLYGON_WINDING_COUNTERCLOCKWISE);
}
void lovrGraphicsClear(int color, int depth) {
@ -207,6 +209,28 @@ void lovrGraphicsSetLineWidth(float width) {
glLineWidth(width);
}
char lovrGraphicsIsCullingEnabled() {
return state.isCullingEnabled;
}
void lovrGraphicsSetCullingEnabled(char isEnabled) {
state.isCullingEnabled = isEnabled;
if (isEnabled) {
glEnable(GL_CULL_FACE);
} else {
glDisable(GL_CULL_FACE);
}
}
PolygonWinding lovrGraphicsGetPolygonWinding() {
return state.polygonWinding;
}
void lovrGraphicsSetPolygonWinding(PolygonWinding winding) {
state.polygonWinding = winding;
glFrontFace(winding);
}
int lovrGraphicsPush() {
vec_mat4_t* transforms = &state.transforms;
if (transforms->length >= 64) { return 1; }

View File

@ -25,6 +25,11 @@ typedef enum {
DRAW_MODE_LINE
} DrawMode;
typedef enum {
POLYGON_WINDING_CLOCKWISE = GL_CW,
POLYGON_WINDING_COUNTERCLOCKWISE = GL_CCW
} PolygonWinding;
typedef struct {
Shader* activeShader;
Shader* defaultShader;
@ -43,6 +48,8 @@ typedef struct {
vec_float_t shapeData;
vec_uint_t shapeIndices;
float lineWidth;
char isCullingEnabled;
PolygonWinding polygonWinding;
} GraphicsState;
#endif
@ -67,6 +74,10 @@ void lovrGraphicsSetProjection(float near, float far, float fov);
void lovrGraphicsSetProjectionRaw(mat4 projection);
float lovrGraphicsGetLineWidth();
void lovrGraphicsSetLineWidth(float width);
char lovrGraphicsIsCullingEnabled();
void lovrGraphicsSetCullingEnabled(char isEnabled);
PolygonWinding lovrGraphicsGetPolygonWinding();
void lovrGraphicsSetPolygonWinding(PolygonWinding winding);
int lovrGraphicsPush();
int lovrGraphicsPop();
void lovrGraphicsOrigin();

View File

@ -22,6 +22,10 @@ const luaL_Reg lovrGraphics[] = {
{ "setProjection", l_lovrGraphicsSetProjection },
{ "getLineWidth", l_lovrGraphicsGetLineWidth },
{ "setLineWidth", l_lovrGraphicsSetLineWidth },
{ "isCullingEnabled", l_lovrGraphicsIsCullingEnabled },
{ "setCullingEnabled", l_lovrGraphicsSetCullingEnabled },
{ "getPolygonWinding", l_lovrGraphicsGetPolygonWinding },
{ "setPolygonWinding", l_lovrGraphicsSetPolygonWinding },
{ "push", l_lovrGraphicsPush },
{ "pop", l_lovrGraphicsPop },
{ "origin", l_lovrGraphicsOrigin },
@ -61,6 +65,10 @@ int l_lovrGraphicsInit(lua_State* L) {
map_set(&DrawModes, "fill", DRAW_MODE_FILL);
map_set(&DrawModes, "line", DRAW_MODE_LINE);
map_init(&PolygonWindings);
map_set(&PolygonWindings, "clockwise", POLYGON_WINDING_CLOCKWISE);
map_set(&PolygonWindings, "counterclockwise", POLYGON_WINDING_COUNTERCLOCKWISE);
lovrGraphicsInit();
return 1;
}
@ -212,6 +220,41 @@ int l_lovrGraphicsSetLineWidth(lua_State* L) {
return 0;
}
int l_lovrGraphicsIsCullingEnabled(lua_State* L) {
lua_pushboolean(L, lovrGraphicsIsCullingEnabled());
return 1;
}
int l_lovrGraphicsSetCullingEnabled(lua_State* L) {
lovrGraphicsSetCullingEnabled(lua_toboolean(L, 1));
return 0;
}
int l_lovrGraphicsGetPolygonWinding(lua_State* L) {
switch (lovrGraphicsGetPolygonWinding()) {
case POLYGON_WINDING_CLOCKWISE:
lua_pushstring(L, "clockwise");
return 1;
case POLYGON_WINDING_COUNTERCLOCKWISE:
lua_pushstring(L, "counterclockwise");
return 1;
}
return 0;
}
int l_lovrGraphicsSetPolygonWinding(lua_State* L) {
const char* userWinding = luaL_checkstring(L, 1);
PolygonWinding* winding = (PolygonWinding*) map_get(&PolygonWindings, userWinding);
if (!winding) {
return luaL_error(L, "Invalid winding: '%s'", userWinding);
}
lovrGraphicsSetPolygonWinding(*winding);
return 0;
}
int l_lovrGraphicsPush(lua_State* L) {
if (lovrGraphicsPush()) {
return luaL_error(L, "Unbalanced matrix stack (more pushes than pops?)");

View File

@ -6,6 +6,7 @@
map_int_t BufferDrawModes;
map_int_t BufferUsages;
map_int_t DrawModes;
map_int_t PolygonWindings;
extern const luaL_Reg lovrGraphics[];
int l_lovrGraphicsInit(lua_State* L);
@ -25,6 +26,10 @@ int l_lovrGraphicsSetShader(lua_State* L);
int l_lovrGraphicsSetProjection(lua_State* L);
int l_lovrGraphicsGetLineWidth(lua_State* L);
int l_lovrGraphicsSetLineWidth(lua_State* L);
int l_lovrGraphicsIsCullingEnabled(lua_State* L);
int l_lovrGraphicsSetCullingEnabled(lua_State* L);
int l_lovrGraphicsGetPolygonWinding(lua_State* L);
int l_lovrGraphicsSetPolygonWinding(lua_State* L);
int l_lovrGraphicsPush(lua_State* L);
int l_lovrGraphicsPop(lua_State* L);
int l_lovrGraphicsOrigin(lua_State* L);