Update shaders_deferred_render.c
This commit is contained in:
parent
1407f6eb46
commit
aca854ccbf
1 changed files with 65 additions and 52 deletions
|
@ -2,7 +2,7 @@
|
||||||
*
|
*
|
||||||
* raylib [shaders] example - deferred rendering
|
* raylib [shaders] example - deferred rendering
|
||||||
*
|
*
|
||||||
* NOTE: This example requires raylib OpenGL 3.3 or ES 3 versions.
|
* NOTE: This example requires raylib OpenGL 3.3 or OpenGL ES 3.0
|
||||||
*
|
*
|
||||||
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
||||||
*
|
*
|
||||||
|
@ -15,12 +15,9 @@
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <GLES3/gl3.h>
|
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "rlgl.h"
|
|
||||||
|
|
||||||
|
#include "rlgl.h"
|
||||||
#include "raymath.h"
|
#include "raymath.h"
|
||||||
|
|
||||||
#define RLIGHTS_IMPLEMENTATION
|
#define RLIGHTS_IMPLEMENTATION
|
||||||
|
@ -32,7 +29,9 @@
|
||||||
#define GLSL_VERSION 100
|
#define GLSL_VERSION 100
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
#include <stdlib.h> // Required for: NULL
|
||||||
|
|
||||||
|
typedef struct GBuffer {
|
||||||
unsigned int framebuffer;
|
unsigned int framebuffer;
|
||||||
|
|
||||||
unsigned int positionTexture;
|
unsigned int positionTexture;
|
||||||
|
@ -42,7 +41,18 @@ typedef struct {
|
||||||
unsigned int depthRenderbuffer;
|
unsigned int depthRenderbuffer;
|
||||||
} GBuffer;
|
} GBuffer;
|
||||||
|
|
||||||
int main(void) {
|
typedef enum {
|
||||||
|
DEFERRED_POSITION,
|
||||||
|
DEFERRED_NORMAL,
|
||||||
|
DEFERRED_ALBEDO,
|
||||||
|
DEFERRED_SHADING
|
||||||
|
} DeferredMode;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
// -------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------
|
||||||
const int screenWidth = 800;
|
const int screenWidth = 800;
|
||||||
|
@ -73,11 +83,12 @@ int main(void) {
|
||||||
GBuffer gBuffer = { 0 };
|
GBuffer gBuffer = { 0 };
|
||||||
gBuffer.framebuffer = rlLoadFramebuffer(screenWidth, screenHeight);
|
gBuffer.framebuffer = rlLoadFramebuffer(screenWidth, screenHeight);
|
||||||
|
|
||||||
if(!gBuffer.framebuffer)
|
if (!gBuffer.framebuffer)
|
||||||
{
|
{
|
||||||
TraceLog(LOG_WARNING, "Failed to create framebuffer");
|
TraceLog(LOG_WARNING, "Failed to create framebuffer");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
rlEnableFramebuffer(gBuffer.framebuffer);
|
rlEnableFramebuffer(gBuffer.framebuffer);
|
||||||
|
|
||||||
// Since we are storing position and normal data in these textures,
|
// Since we are storing position and normal data in these textures,
|
||||||
|
@ -104,7 +115,7 @@ int main(void) {
|
||||||
// Make sure our framebuffer is complete.
|
// Make sure our framebuffer is complete.
|
||||||
// NOTE: rlFramebufferComplete() automatically unbinds the framebuffer, so we don't have
|
// NOTE: rlFramebufferComplete() automatically unbinds the framebuffer, so we don't have
|
||||||
// to rlDisableFramebuffer() here.
|
// to rlDisableFramebuffer() here.
|
||||||
if(rlFramebufferComplete(gBuffer.framebuffer) != true)
|
if (!rlFramebufferComplete(gBuffer.framebuffer))
|
||||||
{
|
{
|
||||||
TraceLog(LOG_WARNING, "Framebuffer is not complete");
|
TraceLog(LOG_WARNING, "Framebuffer is not complete");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -137,22 +148,18 @@ int main(void) {
|
||||||
const float CUBE_SCALE = 0.25;
|
const float CUBE_SCALE = 0.25;
|
||||||
Vector3 cubePositions[MAX_CUBES];
|
Vector3 cubePositions[MAX_CUBES];
|
||||||
float cubeRotations[MAX_CUBES];
|
float cubeRotations[MAX_CUBES];
|
||||||
|
|
||||||
for(int i = 0; i < MAX_CUBES; i++)
|
for(int i = 0; i < MAX_CUBES; i++)
|
||||||
{
|
{
|
||||||
cubePositions[i] = (Vector3) {
|
cubePositions[i] = (Vector3){
|
||||||
.x = (float)(rand() % 10) - 5,
|
.x = (float)(rand()%10) - 5,
|
||||||
.y = (float)(rand() % 5),
|
.y = (float)(rand()%5),
|
||||||
.z = (float)(rand() % 10) - 5,
|
.z = (float)(rand()%10) - 5,
|
||||||
};
|
};
|
||||||
cubeRotations[i] = (float)(rand() % 360);
|
cubeRotations[i] = (float)(rand()%360);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum {
|
DeferredMode mode = DEFERRED_SHADING;
|
||||||
POSITION,
|
|
||||||
NORMAL,
|
|
||||||
ALBEDO,
|
|
||||||
DEFERRED_SHADING
|
|
||||||
} activeTexture = DEFERRED_SHADING;
|
|
||||||
|
|
||||||
rlEnableDepthTest();
|
rlEnableDepthTest();
|
||||||
|
|
||||||
|
@ -177,11 +184,10 @@ int main(void) {
|
||||||
if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
|
if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
|
||||||
|
|
||||||
// Check key inputs to switch between G-buffer textures
|
// Check key inputs to switch between G-buffer textures
|
||||||
if(IsKeyPressed(KEY_ONE)) activeTexture = POSITION;
|
if (IsKeyPressed(KEY_ONE)) mode = DEFERRED_POSITION;
|
||||||
if(IsKeyPressed(KEY_TWO)) activeTexture = NORMAL;
|
if (IsKeyPressed(KEY_TWO)) mode = DEFERRED_NORMAL;
|
||||||
if(IsKeyPressed(KEY_THREE)) activeTexture = ALBEDO;
|
if (IsKeyPressed(KEY_THREE)) mode = DEFERRED_ALBEDO;
|
||||||
if(IsKeyPressed(KEY_FOUR)) activeTexture = DEFERRED_SHADING;
|
if (IsKeyPressed(KEY_FOUR)) mode = DEFERRED_SHADING;
|
||||||
|
|
||||||
|
|
||||||
// Update light values (actually, only enable/disable them)
|
// Update light values (actually, only enable/disable them)
|
||||||
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(deferredShader, lights[i]);
|
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(deferredShader, lights[i]);
|
||||||
|
@ -190,14 +196,16 @@ int main(void) {
|
||||||
// Draw
|
// Draw
|
||||||
// ---------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
// Draw to the geometry buffer by first activating it.
|
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
// Draw to the geometry buffer by first activating it
|
||||||
rlEnableFramebuffer(gBuffer.framebuffer);
|
rlEnableFramebuffer(gBuffer.framebuffer);
|
||||||
rlClearScreenBuffers(); // Clear color & depth buffer
|
rlClearScreenBuffers(); // Clear color and depth buffer
|
||||||
|
|
||||||
rlDisableColorBlend();
|
rlDisableColorBlend();
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
// NOTE:
|
// NOTE: We have to use rlEnableShader here. `BeginShaderMode` or thus `rlSetShader`
|
||||||
// We have to use rlEnableShader here. `BeginShaderMode` or thus `rlSetShader`
|
|
||||||
// will not work, as they won't immediately load the shader program.
|
// will not work, as they won't immediately load the shader program.
|
||||||
rlEnableShader(gbufferShader.id);
|
rlEnableShader(gbufferShader.id);
|
||||||
// When drawing a model here, make sure that the material's shaders
|
// When drawing a model here, make sure that the material's shaders
|
||||||
|
@ -205,10 +213,9 @@ int main(void) {
|
||||||
DrawModel(model, Vector3Zero(), 1.0f, WHITE);
|
DrawModel(model, Vector3Zero(), 1.0f, WHITE);
|
||||||
DrawModel(cube, (Vector3) { 0.0, 1.0f, 0.0 }, 1.0f, WHITE);
|
DrawModel(cube, (Vector3) { 0.0, 1.0f, 0.0 }, 1.0f, WHITE);
|
||||||
|
|
||||||
for(int i = 0; i < MAX_CUBES; i++)
|
for (int i = 0; i < MAX_CUBES; i++)
|
||||||
{
|
{
|
||||||
Vector3 position = cubePositions[i];
|
Vector3 position = cubePositions[i];
|
||||||
|
|
||||||
DrawModelEx(cube, position, (Vector3) { 1, 1, 1 }, cubeRotations[i], (Vector3) { CUBE_SCALE, CUBE_SCALE, CUBE_SCALE }, WHITE);
|
DrawModelEx(cube, position, (Vector3) { 1, 1, 1 }, cubeRotations[i], (Vector3) { CUBE_SCALE, CUBE_SCALE, CUBE_SCALE }, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,9 +227,10 @@ int main(void) {
|
||||||
rlDisableFramebuffer();
|
rlDisableFramebuffer();
|
||||||
rlClearScreenBuffers(); // Clear color & depth buffer
|
rlClearScreenBuffers(); // Clear color & depth buffer
|
||||||
|
|
||||||
switch(activeTexture)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case DEFERRED_SHADING:
|
case DEFERRED_SHADING:
|
||||||
|
{
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
rlDisableColorBlend();
|
rlDisableColorBlend();
|
||||||
rlEnableShader(deferredShader.id);
|
rlEnableShader(deferredShader.id);
|
||||||
|
@ -243,11 +251,10 @@ int main(void) {
|
||||||
rlEnableColorBlend();
|
rlEnableColorBlend();
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
|
|
||||||
// As a last step, we now copy over the depth buffer from our g-buffer to the
|
// As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer.
|
||||||
// default framebuffer.
|
rlEnableFramebuffer(gBuffer.framebuffer); //glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer.framebuffer);
|
||||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer.framebuffer);
|
rlEnableFramebuffer(0); //glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT
|
||||||
glBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
|
|
||||||
rlDisableFramebuffer();
|
rlDisableFramebuffer();
|
||||||
|
|
||||||
// Since our shader is now done and disabled, we can draw our lights in default
|
// Since our shader is now done and disabled, we can draw our lights in default
|
||||||
|
@ -256,44 +263,50 @@ int main(void) {
|
||||||
rlEnableShader(rlGetShaderIdDefault());
|
rlEnableShader(rlGetShaderIdDefault());
|
||||||
for(int i = 0; i < MAX_LIGHTS; i++)
|
for(int i = 0; i < MAX_LIGHTS; i++)
|
||||||
{
|
{
|
||||||
if(lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
|
if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
|
||||||
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
|
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
|
||||||
}
|
}
|
||||||
rlDisableShader();
|
rlDisableShader();
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
DrawText("FINAL RESULT", 10, screenHeight - 30, 20, DARKGREEN);
|
DrawText("FINAL RESULT", 10, screenHeight - 30, 20, DARKGREEN);
|
||||||
break;
|
} break;
|
||||||
case POSITION:
|
|
||||||
DrawTextureRec((Texture2D) {
|
case DEFERRED_POSITION:
|
||||||
|
{
|
||||||
|
DrawTextureRec((Texture2D){
|
||||||
.id = gBuffer.positionTexture,
|
.id = gBuffer.positionTexture,
|
||||||
.width = screenWidth,
|
.width = screenWidth,
|
||||||
.height = screenHeight,
|
.height = screenHeight,
|
||||||
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
||||||
DrawText("POSITION TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
DrawText("POSITION TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
||||||
break;
|
} break;
|
||||||
case NORMAL:
|
|
||||||
DrawTextureRec((Texture2D) {
|
case DEFERRED_NORMAL:
|
||||||
|
{
|
||||||
|
DrawTextureRec((Texture2D){
|
||||||
.id = gBuffer.normalTexture,
|
.id = gBuffer.normalTexture,
|
||||||
.width = screenWidth,
|
.width = screenWidth,
|
||||||
.height = screenHeight,
|
.height = screenHeight,
|
||||||
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
||||||
DrawText("NORMAL TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
DrawText("NORMAL TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
||||||
break;
|
} break;
|
||||||
|
|
||||||
case ALBEDO:
|
case DEFERRED_ALBEDO:
|
||||||
DrawTextureRec((Texture2D) {
|
{
|
||||||
|
DrawTextureRec((Texture2D){
|
||||||
.id = gBuffer.albedoSpecTexture,
|
.id = gBuffer.albedoSpecTexture,
|
||||||
.width = screenWidth,
|
.width = screenWidth,
|
||||||
.height = screenHeight,
|
.height = screenHeight,
|
||||||
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
}, (Rectangle) { 0, 0, screenWidth, -screenHeight }, Vector2Zero(), RAYWHITE);
|
||||||
DrawText("ALBEDO TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
DrawText("ALBEDO TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
|
||||||
break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DrawText("Toggle lights keys: [Y][R][G][B]", 10, 40, 20, DARKGRAY);
|
||||||
|
DrawText("Switch G-buffer textures: [1][2][3][4]", 10, 70, 20, DARKGRAY);
|
||||||
|
|
||||||
DrawFPS(10, 10);
|
DrawFPS(10, 10);
|
||||||
|
|
||||||
DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY);
|
|
||||||
DrawText("Use keys [1]-[4] to switch between G-buffer textures", 10, 70, 20, DARKGRAY);
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue