Created
May 1, 2018 11:02
-
-
Save dotxnc/3739c7719b94a9277fe432078afeca31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gbuffer.h" | |
/* gbuffer_t type | |
typedef struct gbuffer_t { | |
unsigned int id; | |
int width; | |
int height; | |
Texture2D color; | |
Texture2D normal; | |
Texture2D position; | |
} gbuffer_t; | |
*/ | |
void gbuffer_init(gbuffer_t* gbuffer, int width, int height) | |
{ | |
gbuffer->id = 0; | |
gbuffer->width = width; | |
gbuffer->height = height; | |
gbuffer->color.id = 0; | |
gbuffer->color.width = width; | |
gbuffer->color.height = height; | |
gbuffer->color.format = UNCOMPRESSED_R8G8B8A8; | |
gbuffer->color.mipmaps = 1; | |
gbuffer->normal.id = 0; | |
gbuffer->normal.width = width; | |
gbuffer->normal.height = height; | |
gbuffer->normal.format = UNCOMPRESSED_R8G8B8A8; | |
gbuffer->normal.mipmaps = 1; | |
gbuffer->position.id = 0; | |
gbuffer->position.width = width; | |
gbuffer->position.height = height; | |
gbuffer->position.format = UNCOMPRESSED_R8G8B8A8; | |
gbuffer->position.mipmaps = 1; | |
glGenFramebuffers(1, &gbuffer->id); | |
glBindFramebuffer(GL_FRAMEBUFFER, gbuffer->id); | |
glGenTextures(1, &gbuffer->color.id); | |
glBindTexture(GL_TEXTURE_2D, gbuffer->color.id); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gbuffer->color.id, 0); | |
// it's not this | |
unsigned int buffers[1] = {GL_COLOR_ATTACHMENT0}; | |
glDrawBuffers(1, buffers); | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
} | |
void gbuffer_begin(gbuffer_t* gbuffer) | |
{ | |
rlglDraw(); | |
rlEnableRenderTexture(gbuffer->id); | |
rlClearScreenBuffers(); | |
rlViewport(0, 0, gbuffer->width, gbuffer->height); | |
rlMatrixMode(RL_PROJECTION); | |
rlLoadIdentity(); | |
rlOrtho(0, gbuffer->width, gbuffer->height, 0, 0, 1); | |
rlMatrixMode(RL_MODELVIEW); | |
rlLoadIdentity(); | |
} | |
void gbuffer_end() | |
{ | |
rlglDraw(); | |
rlDisableRenderTexture(); | |
rlViewport(0, 0, GetScreenWidth(), GetScreenHeight()); | |
rlMatrixMode(RL_PROJECTION); | |
rlLoadIdentity(); | |
rlOrtho(0, GetScreenWidth(), GetScreenHeight(), 0, 0, 1); | |
rlMatrixMode(RL_MODELVIEW); | |
rlLoadIdentity(); | |
} | |
void gbuffer_free(gbuffer_t* gbuffer) | |
{ | |
glDeleteFramebuffers(1, &gbuffer->id); | |
glDeleteTextures(1, &gbuffer->color.id); | |
glDeleteTextures(1, &gbuffer->normal.id); | |
glDeleteTextures(1, &gbuffer->position.id); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment