summaryrefslogtreecommitdiff
path: root/src/texture
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-10-31 22:43:41 -0500
committersanine-a <sanine.not@pm.me>2020-10-31 22:43:41 -0500
commit42d42c9ba3b741d167eaa2196c686962559686f1 (patch)
tree46f9d9c51d1d4fe1ccbacf1d1f73c066e93dfc32 /src/texture
parentad75604ec79d70d328595f114e65bac80db9999f (diff)
add basic framebuffer operations
Diffstat (limited to 'src/texture')
-rw-r--r--src/texture/texture.c89
-rw-r--r--src/texture/texture.h21
2 files changed, 96 insertions, 14 deletions
diff --git a/src/texture/texture.c b/src/texture/texture.c
index 6df19a7..9ddf441 100644
--- a/src/texture/texture.c
+++ b/src/texture/texture.c
@@ -6,6 +6,37 @@ static int honey_lua_texture_new(lua_State* L)
return 1;
}
+static int honey_lua_texture_create(lua_State* L)
+{
+ honey_texture* texture;
+ int width, height;
+ char* type;
+ honey_lua_parse_arguments(L, 4,
+ HONEY_USERDATA, &texture,
+ HONEY_STRING, &type,
+ HONEY_INTEGER, &width,
+ HONEY_INTEGER, &height);
+
+ if (strcmp(type, "greyscale") == 0)
+ honey_texture_new_greyscale(texture, width, height, NULL);
+ else if (strcmp(type, "rgb") == 0)
+ honey_texture_new_rgb(texture, width, height, NULL);
+ else if (strcmp(type, "rgba") == 0)
+ honey_texture_new_rgba(texture, width, height, NULL);
+ else if (strcmp(type, "depth") == 0)
+ honey_texture_new_depth(texture, width, height, NULL);
+ else {
+ char* error;
+ honey_format_string(&error,
+ "unknown texture type '%s'",
+ type);
+ lua_pushstring(L, error);
+ free(error);
+ lua_error(L);
+ }
+ return 0;
+}
+
static int honey_lua_texture_load(lua_State* L)
{
honey_texture* texture;
@@ -38,15 +69,43 @@ static int honey_lua_texture_use(lua_State* L)
return 0;
}
+static int honey_lua_framebuffer_new(lua_State* L)
+{
+ honey_texture* draw, *depth;
+ if (lua_isuserdata(L, 1))
+ draw = lua_touserdata(L, 1);
+ else
+ draw = NULL;
+
+ if (lua_isuserdata(L, 2))
+ depth = lua_touserdata(L, 2);
+ else
+ depth = NULL;
+
+ int width, height;
+ honey_lua_parse_arguments(L, 4, HONEY_ANY, HONEY_ANY,
+ HONEY_INTEGER, &width,
+ HONEY_INTEGER, &height);
+
+ unsigned int framebuffer;
+ honey_texture_framebuffer_object_new(&framebuffer,
+ draw, depth,
+ width, height);
+ lua_pushinteger(L, framebuffer);
+ return 1;
+}
+
void honey_setup_texture(lua_State* L)
{
honey_lua_element texture_elements[] = {
{ "new", HONEY_FUNCTION, { .function = honey_lua_texture_new } },
+ { "new_framebuffer", HONEY_FUNCTION, { .function = honey_lua_framebuffer_new } },
+ { "create", HONEY_FUNCTION, { .function = honey_lua_texture_create } },
{ "load", HONEY_FUNCTION, { .function = honey_lua_texture_load } },
{ "use", HONEY_FUNCTION, { .function = honey_lua_texture_use } },
};
- honey_lua_create_table(L, texture_elements, 3);
+ honey_lua_create_table(L, texture_elements, 5);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@@ -182,10 +241,26 @@ void honey_texture_use(honey_texture texture, int texture_unit) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-//honey_result honey_texture_framebuffer_object_new(unsigned int* destination,
-// int width, int height)
-//{
-// glGenFramebuffers(1, destination);
-// glBindFramebuffer(GL_FRAMEBUFFER, *destination);
+void honey_texture_framebuffer_object_new(unsigned int* destination,
+ honey_texture* draw,
+ honey_texture* depth,
+ int width, int height)
+{
+ glGenFramebuffers(1, destination);
+ glBindFramebuffer(GL_FRAMEBUFFER, *destination);
+
+ if (draw != NULL)
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, draw->id, 0);
+ else {
+ glDrawBuffer(GL_NONE);
+ glReadBuffer(GL_NONE);
+ }
-
+ if (depth != NULL)
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth->id, 0);
+
+ if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+ printf("framebuffer is not complete!\n");
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+}
diff --git a/src/texture/texture.h b/src/texture/texture.h
index 785fef0..9dc0308 100644
--- a/src/texture/texture.h
+++ b/src/texture/texture.h
@@ -37,7 +37,7 @@ void honey_setup_texture(lua_State* L);
* @param[in] height The height in pixels of the texture to create.
* @param[in] data The data to populate the texture with, or NULL to leave it unpopulated.
*
- * @returns HONEY_OK on success, and appropriate error on failure.
+ * @returns Nothing.
*/
void honey_texture_new_greyscale(honey_texture* texture,
int width, int height,
@@ -50,7 +50,7 @@ void honey_texture_new_greyscale(honey_texture* texture,
* @param[in] height The height in pixels of the texture to create.
* @param[in] data The data to populate the texture with, or NULL to leave it unpopulated.
*
- * @returns HONEY_OK on success, and appropriate error on failure.
+ * @returns Nothing.
*/
void honey_texture_new_rgb(honey_texture* texture,
int width, int height,
@@ -63,7 +63,7 @@ void honey_texture_new_rgb(honey_texture* texture,
* @param[in] height The height in pixels of the texture to create.
* @param[in] data The data to populate the texture with, or NULL to leave it unpopulated.
*
- * @returns HONEY_OK on success, and appropriate error on failure.
+ * @returns Nothing.
*/
void honey_texture_new_rgba(honey_texture* texture,
int width, int height,
@@ -76,7 +76,7 @@ void honey_texture_new_rgba(honey_texture* texture,
* @param[in] height The height in pixels of the texture to create.
* @param[in] data The data to populate the texture with, or NULL to leave it unpopulated.
*
- * @returns HONEY_OK on success, and appropriate error on failure.
+ * @returns Nothing.
*/
void honey_texture_new_depth(honey_texture* texture,
int width, int height,
@@ -102,13 +102,20 @@ void honey_texture_use(honey_texture texture, int texture_unit);
/** @brief Create a framebuffer object.
*
+ * You must specify at least one of draw and depth; otherwise, the framebuffer will
+ * be incomplete and fail.
+ *
* @param[out] destination Pointer to store the resulting OpenGL handle in.
+ * @param[in] draw Pointer to a texture to draw to.
+ * @param[in] depth Pointer to a depth texture.
* @param[in] width The width in pixels of the FBO.
* @param[in] height The height in pixels of the FBO.
*
- * @returns HONEY_OK on success; appropriate error otherwise.
+ * @returns Nothing.
*/
-honey_result honey_texture_framebuffer_object_new(unsigned int* destination,
- int width, int height);
+void honey_texture_framebuffer_object_new(unsigned int* destination,
+ honey_texture* draw,
+ honey_texture* depth,
+ int width, int height);
#endif