mirror of https://github.com/bjornbytes/lovr.git
getCanvas; setCanvas; Canvas attachments;
This commit is contained in:
parent
d39e9f1687
commit
7e74441afd
|
@ -71,6 +71,7 @@ extern const luaL_Reg lovrWorld[];
|
|||
|
||||
// Enums
|
||||
extern const char* ArcModes[];
|
||||
extern const char* AttachmentTypes[];
|
||||
extern const char* AttributeTypes[];
|
||||
extern const char* BlendAlphaModes[];
|
||||
extern const char* BlendModes[];
|
||||
|
|
|
@ -20,6 +20,12 @@ const char* ArcModes[] = {
|
|||
NULL
|
||||
};
|
||||
|
||||
const char* AttachmentTypes[] = {
|
||||
[ATTACHMENT_COLOR] = "color",
|
||||
[ATTACHMENT_DEPTH] = "depth",
|
||||
NULL
|
||||
};
|
||||
|
||||
const char* AttributeTypes[] = {
|
||||
[ATTR_FLOAT] = "float",
|
||||
[ATTR_BYTE] = "byte",
|
||||
|
@ -439,10 +445,14 @@ int l_lovrGraphicsSetBlendMode(lua_State* L) {
|
|||
}
|
||||
|
||||
int l_lovrGraphicsGetCanvas(lua_State* L) {
|
||||
return 0;
|
||||
Canvas* canvas = lovrGraphicsGetCanvas();
|
||||
luax_pushobject(L, canvas);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsSetCanvas(lua_State* L) {
|
||||
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
||||
lovrGraphicsSetCanvas(canvas);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,47 @@
|
|||
#include "api.h"
|
||||
#include "graphics/canvas.h"
|
||||
|
||||
static int luax_checkattachment(lua_State* L, int index, Attachment* attachment) {
|
||||
attachment->texture = luax_checktype(L, index++, Texture);
|
||||
attachment->slice = lua_type(L, index) == LUA_TNUMBER ? lua_tointeger(L, index++) - 1 : 0;
|
||||
attachment->level = lua_type(L, index) == LUA_TNUMBER ? lua_tointeger(L, index++) - 1 : 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
int l_lovrCanvasGetTexture(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrCanvasSetTexture(lua_State* L) {
|
||||
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
||||
|
||||
int index = 2;
|
||||
AttachmentType type = ATTACHMENT_COLOR;
|
||||
if (lua_type(L, index) == LUA_TSTRING) {
|
||||
type = luaL_checkoption(L, index++, NULL, AttachmentTypes);
|
||||
}
|
||||
|
||||
if (type == ATTACHMENT_COLOR) {
|
||||
Attachment attachments[MAX_COLOR_ATTACHMENTS];
|
||||
int top = lua_gettop(L);
|
||||
int count;
|
||||
|
||||
for (count = 0; count < MAX_COLOR_ATTACHMENTS && index <= top; count++) {
|
||||
index = luax_checkattachment(L, index, attachments + count);
|
||||
}
|
||||
|
||||
//lovrCanvasSetAttachments(type, attachments, count);
|
||||
} else if (type == ATTACHMENT_DEPTH) {
|
||||
Attachment attachment;
|
||||
luax_checkattachment(L, index, &attachment);
|
||||
//lovrCanvasSetAttachments(type, &attachment, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg lovrCanvas[] = {
|
||||
{ "getTexture", l_lovrCanvasGetTexture },
|
||||
{ "setTexture", l_lovrCanvasSetTexture },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
|
|
@ -1,5 +1,20 @@
|
|||
#include "graphics/texture.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MAX_COLOR_ATTACHMENTS 4
|
||||
|
||||
typedef enum {
|
||||
ATTACHMENT_COLOR,
|
||||
ATTACHMENT_DEPTH
|
||||
} AttachmentType;
|
||||
|
||||
typedef struct {
|
||||
Texture* texture;
|
||||
int slice;
|
||||
int level;
|
||||
} Attachment;
|
||||
|
||||
typedef struct Canvas Canvas;
|
||||
|
||||
Canvas* lovrCanvasCreate();
|
||||
|
|
|
@ -173,11 +173,13 @@ void lovrGraphicsReset() {
|
|||
void lovrGraphicsPushPipeline() {
|
||||
lovrAssert(++state.pipeline < MAX_PIPELINES, "Unbalanced pipeline stack (more pushes than pops?)");
|
||||
memcpy(&state.pipelines[state.pipeline], &state.pipelines[state.pipeline - 1], sizeof(Pipeline));
|
||||
lovrRetain(state.pipelines[state.pipeline].canvas);
|
||||
lovrRetain(state.pipelines[state.pipeline].font);
|
||||
lovrRetain(state.pipelines[state.pipeline].shader);
|
||||
}
|
||||
|
||||
void lovrGraphicsPopPipeline() {
|
||||
lovrRelease(state.pipelines[state.pipeline].canvas);
|
||||
lovrRelease(state.pipelines[state.pipeline].font);
|
||||
lovrRelease(state.pipelines[state.pipeline].shader);
|
||||
lovrAssert(--state.pipeline >= 0, "Unbalanced pipeline stack (more pops than pushes?)");
|
||||
|
@ -201,6 +203,14 @@ void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode) {
|
|||
state.pipelines[state.pipeline].blendAlphaMode = alphaMode;
|
||||
}
|
||||
|
||||
Canvas* lovrGraphicsGetCanvas() {
|
||||
return state.pipelines[state.pipeline].canvas;
|
||||
}
|
||||
|
||||
void lovrGraphicsSetCanvas(Canvas* canvas) {
|
||||
state.pipelines[state.pipeline].canvas = canvas;
|
||||
}
|
||||
|
||||
Color lovrGraphicsGetColor() {
|
||||
return state.pipelines[state.pipeline].color;
|
||||
}
|
||||
|
|
|
@ -106,6 +106,7 @@ typedef struct {
|
|||
Color backgroundColor;
|
||||
BlendMode blendMode;
|
||||
BlendAlphaMode blendAlphaMode;
|
||||
Canvas* canvas;
|
||||
Color color;
|
||||
bool culling;
|
||||
CompareMode depthTest;
|
||||
|
@ -190,6 +191,8 @@ Color lovrGraphicsGetBackgroundColor();
|
|||
void lovrGraphicsSetBackgroundColor(Color color);
|
||||
void lovrGraphicsGetBlendMode(BlendMode* mode, BlendAlphaMode* alphaMode);
|
||||
void lovrGraphicsSetBlendMode(BlendMode mode, BlendAlphaMode alphaMode);
|
||||
Canvas* lovrGraphicsGetCanvas();
|
||||
void lovrGraphicsSetCanvas(Canvas* canvas);
|
||||
Color lovrGraphicsGetColor();
|
||||
void lovrGraphicsSetColor(Color color);
|
||||
bool lovrGraphicsIsCullingEnabled();
|
||||
|
|
|
@ -111,6 +111,8 @@ struct Texture {
|
|||
struct Canvas {
|
||||
Ref ref;
|
||||
uint32_t framebuffer;
|
||||
Texture* color[MAX_COLOR_ATTACHMENTS];
|
||||
Texture* depth;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
@ -1005,6 +1007,7 @@ void lovrTextureAllocate(Texture* texture, int width, int height, int depth, Tex
|
|||
texture->allocated = true;
|
||||
texture->width = width;
|
||||
texture->height = height;
|
||||
texture->depth = depth;
|
||||
texture->format = format;
|
||||
|
||||
if (texture->mipmaps) {
|
||||
|
@ -1199,11 +1202,18 @@ Canvas* lovrCanvasCreate() {
|
|||
Canvas* canvas = lovrAlloc(Canvas, lovrCanvasDestroy);
|
||||
if (!canvas) return NULL;
|
||||
|
||||
glGenFramebuffers(1, &canvas->framebuffer);
|
||||
|
||||
return canvas;
|
||||
}
|
||||
|
||||
void lovrCanvasDestroy(void* ref) {
|
||||
Canvas* canvas = ref;
|
||||
glDeleteFramebuffers(1, &canvas->framebuffer);
|
||||
for (int i = 0; i < MAX_COLOR_ATTACHMENTS; i++) {
|
||||
lovrRelease(canvas->color[i]);
|
||||
}
|
||||
lovrRelease(canvas->depth);
|
||||
free(ref);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue