mirror of https://github.com/bjornbytes/lovr.git
Blend modes;
This commit is contained in:
parent
c6b635a2ec
commit
062086991e
|
@ -6,12 +6,14 @@
|
|||
#include "filesystem/filesystem.h"
|
||||
#include <math.h>
|
||||
|
||||
map_int_t MeshAttributeTypes;
|
||||
map_int_t MeshDrawModes;
|
||||
map_int_t MeshUsages;
|
||||
map_int_t BlendAlphaModes;
|
||||
map_int_t BlendModes;
|
||||
map_int_t CompareModes;
|
||||
map_int_t DrawModes;
|
||||
map_int_t FilterModes;
|
||||
map_int_t MeshAttributeTypes;
|
||||
map_int_t MeshDrawModes;
|
||||
map_int_t MeshUsages;
|
||||
map_int_t PolygonWindings;
|
||||
map_int_t TextureProjections;
|
||||
map_int_t WrapModes;
|
||||
|
@ -71,21 +73,19 @@ int l_lovrGraphicsInit(lua_State* L) {
|
|||
luax_registertype(L, "Skybox", lovrSkybox);
|
||||
luax_registertype(L, "Texture", lovrTexture);
|
||||
|
||||
map_init(&MeshAttributeTypes);
|
||||
map_set(&MeshAttributeTypes, "float", MESH_FLOAT);
|
||||
map_set(&MeshAttributeTypes, "byte", MESH_BYTE);
|
||||
map_set(&MeshAttributeTypes, "int", MESH_INT);
|
||||
map_init(&BlendAlphaModes);
|
||||
map_set(&BlendAlphaModes, "alphamultiply", BLEND_ALPHA_MULTIPLY);
|
||||
map_set(&BlendAlphaModes, "premultiplied", BLEND_PREMULTIPLIED);
|
||||
|
||||
map_init(&MeshDrawModes);
|
||||
map_set(&MeshDrawModes, "points", MESH_POINTS);
|
||||
map_set(&MeshDrawModes, "strip", MESH_TRIANGLE_STRIP);
|
||||
map_set(&MeshDrawModes, "triangles", MESH_TRIANGLES);
|
||||
map_set(&MeshDrawModes, "fan", MESH_TRIANGLE_FAN);
|
||||
|
||||
map_init(&MeshUsages);
|
||||
map_set(&MeshUsages, "static", MESH_STATIC);
|
||||
map_set(&MeshUsages, "dynamic", MESH_DYNAMIC);
|
||||
map_set(&MeshUsages, "stream", MESH_STREAM);
|
||||
map_init(&BlendModes);
|
||||
map_set(&BlendModes, "alpha", BLEND_ALPHA);
|
||||
map_set(&BlendModes, "add", BLEND_ADD);
|
||||
map_set(&BlendModes, "subtract", BLEND_SUBTRACT);
|
||||
map_set(&BlendModes, "multiply", BLEND_MULTIPLY);
|
||||
map_set(&BlendModes, "lighten", BLEND_LIGHTEN);
|
||||
map_set(&BlendModes, "darken", BLEND_DARKEN);
|
||||
map_set(&BlendModes, "screen", BLEND_SCREEN);
|
||||
map_set(&BlendModes, "replace", BLEND_REPLACE);
|
||||
|
||||
map_init(&DrawModes);
|
||||
map_set(&DrawModes, "fill", DRAW_MODE_FILL);
|
||||
|
@ -107,6 +107,22 @@ int l_lovrGraphicsInit(lua_State* L) {
|
|||
map_set(&FilterModes, "nearest", FILTER_NEAREST);
|
||||
map_set(&FilterModes, "linear", FILTER_LINEAR);
|
||||
|
||||
map_init(&MeshAttributeTypes);
|
||||
map_set(&MeshAttributeTypes, "float", MESH_FLOAT);
|
||||
map_set(&MeshAttributeTypes, "byte", MESH_BYTE);
|
||||
map_set(&MeshAttributeTypes, "int", MESH_INT);
|
||||
|
||||
map_init(&MeshDrawModes);
|
||||
map_set(&MeshDrawModes, "points", MESH_POINTS);
|
||||
map_set(&MeshDrawModes, "strip", MESH_TRIANGLE_STRIP);
|
||||
map_set(&MeshDrawModes, "triangles", MESH_TRIANGLES);
|
||||
map_set(&MeshDrawModes, "fan", MESH_TRIANGLE_FAN);
|
||||
|
||||
map_init(&MeshUsages);
|
||||
map_set(&MeshUsages, "static", MESH_STATIC);
|
||||
map_set(&MeshUsages, "dynamic", MESH_DYNAMIC);
|
||||
map_set(&MeshUsages, "stream", MESH_STREAM);
|
||||
|
||||
map_init(&WrapModes);
|
||||
map_set(&WrapModes, "clamp", WRAP_CLAMP);
|
||||
map_set(&WrapModes, "repeat", WRAP_REPEAT);
|
||||
|
@ -162,6 +178,22 @@ int l_lovrGraphicsSetBackgroundColor(lua_State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsGetBlendMode(lua_State* L) {
|
||||
BlendMode mode;
|
||||
BlendAlphaMode alphaMode;
|
||||
lovrGraphicsGetBlendMode(&mode, &alphaMode);
|
||||
luax_pushenum(L, &BlendModes, mode);
|
||||
luax_pushenum(L, &BlendAlphaModes, alphaMode);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsSetBlendMode(lua_State* L) {
|
||||
BlendMode mode = *(BlendMode*) luax_checkenum(L, 1, &BlendModes, "blend mode");
|
||||
BlendAlphaMode alphaMode = *(BlendAlphaMode*) luax_optenum(L, 2, "alphamultiply", &BlendAlphaModes, "alpha blend mode");
|
||||
lovrGraphicsSetBlendMode(mode, alphaMode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsGetColor(lua_State* L) {
|
||||
unsigned char r, g, b, a;
|
||||
lovrGraphicsGetColor(&r, &g, &b, &a);
|
||||
|
@ -711,6 +743,8 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ "present", l_lovrGraphicsPresent },
|
||||
{ "getBackgroundColor", l_lovrGraphicsGetBackgroundColor },
|
||||
{ "setBackgroundColor", l_lovrGraphicsSetBackgroundColor },
|
||||
{ "getBlendMode", l_lovrGraphicsGetBlendMode },
|
||||
{ "setBlendMode", l_lovrGraphicsSetBlendMode },
|
||||
{ "getColor", l_lovrGraphicsGetColor },
|
||||
{ "setColor", l_lovrGraphicsSetColor },
|
||||
{ "getColorMask", l_lovrGraphicsGetColorMask },
|
||||
|
|
|
@ -28,9 +28,8 @@ extern const luaL_Reg lovrTexture[];
|
|||
extern const luaL_Reg lovrTimer[];
|
||||
extern const luaL_Reg lovrTransform[];
|
||||
|
||||
extern map_int_t MeshAttributeTypes;
|
||||
extern map_int_t MeshDrawModes;
|
||||
extern map_int_t MeshUsages;
|
||||
extern map_int_t BlendAlphaModes;
|
||||
extern map_int_t BlendModes;
|
||||
extern map_int_t CompareModes;
|
||||
extern map_int_t ControllerAxes;
|
||||
extern map_int_t ControllerButtons;
|
||||
|
@ -38,6 +37,9 @@ extern map_int_t DrawModes;
|
|||
extern map_int_t EventTypes;
|
||||
extern map_int_t FilterModes;
|
||||
extern map_int_t HeadsetEyes;
|
||||
extern map_int_t MeshAttributeTypes;
|
||||
extern map_int_t MeshDrawModes;
|
||||
extern map_int_t MeshUsages;
|
||||
extern map_int_t PolygonWindings;
|
||||
extern map_int_t TextureProjections;
|
||||
extern map_int_t TimeUnits;
|
||||
|
|
|
@ -117,6 +117,61 @@ void lovrGraphicsSetBackgroundColor(float r, float g, float b, float a) {
|
|||
glClearColor(r / 255, g / 255, b / 255, a / 255);
|
||||
}
|
||||
|
||||
void lovrGraphicsGetBlendMode(BlendMode* mode, BlendAlphaMode* alphaMode) {
|
||||
*mode = state.blendMode;
|
||||
*alphaMode = state.blendAlphaMode;
|
||||
}
|
||||
|
||||
void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode) {
|
||||
GLenum srcRGB = mode == BLEND_MULTIPLY ? GL_DST_COLOR : GL_ONE;
|
||||
|
||||
if (srcRGB == GL_ONE && alphaMode == BLEND_ALPHA_MULTIPLY) {
|
||||
srcRGB = GL_SRC_ALPHA;
|
||||
}
|
||||
|
||||
switch (mode) {
|
||||
case BLEND_ALPHA:
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(srcRGB, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
|
||||
case BLEND_ADD:
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(srcRGB, GL_ONE, GL_ZERO, GL_ONE);
|
||||
break;
|
||||
|
||||
case BLEND_SUBTRACT:
|
||||
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
|
||||
glBlendFuncSeparate(srcRGB, GL_ONE, GL_ZERO, GL_ONE);
|
||||
break;
|
||||
|
||||
case BLEND_MULTIPLY:
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_DST_COLOR, GL_ZERO);
|
||||
break;
|
||||
|
||||
case BLEND_LIGHTEN:
|
||||
glBlendEquation(GL_MAX);
|
||||
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
|
||||
break;
|
||||
|
||||
case BLEND_DARKEN:
|
||||
glBlendEquation(GL_MIN);
|
||||
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
|
||||
break;
|
||||
|
||||
case BLEND_SCREEN:
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(srcRGB, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_COLOR);
|
||||
break;
|
||||
|
||||
case BLEND_REPLACE:
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void lovrGraphicsGetColor(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a) {
|
||||
*r = LOVR_COLOR_R(state.color);
|
||||
*g = LOVR_COLOR_G(state.color);
|
||||
|
|
|
@ -11,6 +11,22 @@
|
|||
#define MAX_TRANSFORMS 64
|
||||
#define MAX_CANVASES 4
|
||||
|
||||
typedef enum {
|
||||
BLEND_ALPHA,
|
||||
BLEND_ADD,
|
||||
BLEND_SUBTRACT,
|
||||
BLEND_MULTIPLY,
|
||||
BLEND_LIGHTEN,
|
||||
BLEND_DARKEN,
|
||||
BLEND_SCREEN,
|
||||
BLEND_REPLACE
|
||||
} BlendMode;
|
||||
|
||||
typedef enum {
|
||||
BLEND_ALPHA_MULTIPLY,
|
||||
BLEND_PREMULTIPLIED
|
||||
} BlendAlphaMode;
|
||||
|
||||
typedef enum {
|
||||
DRAW_MODE_FILL,
|
||||
DRAW_MODE_LINE
|
||||
|
@ -59,6 +75,8 @@ typedef struct {
|
|||
int canvas;
|
||||
unsigned int color;
|
||||
char colorMask;
|
||||
BlendMode blendMode;
|
||||
BlendAlphaMode blendAlphaMode;
|
||||
int isScissorEnabled;
|
||||
ScissorRectangle scissor;
|
||||
GLuint shapeArray;
|
||||
|
@ -85,6 +103,8 @@ void lovrGraphicsPrepare();
|
|||
// State
|
||||
void lovrGraphicsGetBackgroundColor(float* r, float* g, float* b, float* a);
|
||||
void lovrGraphicsSetBackgroundColor(float r, float g, float b, float a);
|
||||
void lovrGraphicsGetBlendMode(BlendMode* mode, BlendAlphaMode* alphaMode);
|
||||
void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode);
|
||||
void lovrGraphicsGetColor(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a);
|
||||
void lovrGraphicsSetColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
|
||||
void lovrGraphicsGetColorMask(char* r, char* g, char* b, char* a);
|
||||
|
|
Loading…
Reference in New Issue