First edit for framebuffers but it's slapped together and doesn't work yet.
This commit is contained in:
parent
c32e7bd00d
commit
ea80851450
8 changed files with 113 additions and 11 deletions
|
|
@ -8,6 +8,10 @@
|
||||||
"vertex_path": "shaders/19-vert.glsl",
|
"vertex_path": "shaders/19-vert.glsl",
|
||||||
"frag_path": "shaders/19-lightsource.frag.glsl"
|
"frag_path": "shaders/19-lightsource.frag.glsl"
|
||||||
},
|
},
|
||||||
|
"screen_shader": {
|
||||||
|
"vertex_path": "shaders/21-screen.vert.glsl",
|
||||||
|
"frag_path": "shaders/21-screen.frag.glsl"
|
||||||
|
},
|
||||||
"materials": {
|
"materials": {
|
||||||
"default": {
|
"default": {
|
||||||
"ambient": [0.5, 0.5, 0.5],
|
"ambient": [0.5, 0.5, 0.5],
|
||||||
|
|
@ -57,11 +61,11 @@
|
||||||
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
||||||
"scale": 1.0,
|
"scale": 1.0,
|
||||||
"material": "default"},
|
"material": "default"},
|
||||||
{"model": "window", "position": [1.75, 0.25, 0.25],
|
{"model": "window", "position": [1.5, 0.25, 0.0],
|
||||||
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
||||||
"scale": 1.0,
|
"scale": 1.0,
|
||||||
"material": "shiny"},
|
"material": "shiny"},
|
||||||
{"model": "window", "position": [1.5, 0.25, 0.0],
|
{"model": "window", "position": [1.75, 0.25, 0.25],
|
||||||
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
"rotation": {"angle": 90.0, "axes": [1.0,0.0,0.0]},
|
||||||
"scale": 1.0,
|
"scale": 1.0,
|
||||||
"material": "shiny"}
|
"material": "shiny"}
|
||||||
|
|
|
||||||
11
21-framebuffers/shaders/21-screen.frag.glsl
Normal file
11
21-framebuffers/shaders/21-screen.frag.glsl
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
uniform sampler2D screenTexture;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(screenTexture, TexCoords);
|
||||||
|
}
|
||||||
11
21-framebuffers/shaders/21-screen.vert.glsl
Normal file
11
21-framebuffers/shaders/21-screen.vert.glsl
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoords;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||||
|
TexCoords = aTexCoords;
|
||||||
|
}
|
||||||
|
|
@ -102,7 +102,7 @@ namespace components {
|
||||||
|
|
||||||
struct Scene {
|
struct Scene {
|
||||||
components::Shader shader;
|
components::Shader shader;
|
||||||
components::Shader light_shader;
|
components::Shader screen_shader;
|
||||||
std::unordered_map<std::string, Material> materials;
|
std::unordered_map<std::string, Material> materials;
|
||||||
std::unordered_map<std::string, Model> models;
|
std::unordered_map<std::string, Model> models;
|
||||||
std::vector<Thing> things;
|
std::vector<Thing> things;
|
||||||
|
|
@ -110,5 +110,5 @@ namespace components {
|
||||||
Lighting light;
|
Lighting light;
|
||||||
};
|
};
|
||||||
|
|
||||||
ENROLL_COMPONENT(Scene, shader, light_shader, materials, models, things, camera, light);
|
ENROLL_COMPONENT(Scene, shader, screen_shader, materials, models, things, camera, light);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ void init_glfw() {
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow *, int width, int height) {
|
||||||
|
|
|
||||||
|
|
@ -34,15 +34,19 @@ void Scene::draw_thing(Shader& with_shader, Thing& thing)
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::render(GLFWwindow* window) {
|
void Scene::render(GLFWwindow* window) {
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
glm::mat4 view = camera.look_at();
|
glm::mat4 view = camera.look_at();
|
||||||
glm::mat4 projection = glm::mat4(1.0f);
|
glm::mat4 projection = glm::mat4(1.0f);
|
||||||
|
|
||||||
// fov, aspect, near plane, far plane
|
// fov, aspect, near plane, far plane
|
||||||
projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
projection = glm::perspective(glm::radians(camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
|
|
||||||
|
// framebuffer stuff here
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// render the scene
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
shader.setMat4("projection", projection);
|
shader.setMat4("projection", projection);
|
||||||
|
|
@ -50,6 +54,19 @@ void Scene::render(GLFWwindow* window) {
|
||||||
for(auto& thing : things) {
|
for(auto& thing : things) {
|
||||||
draw_thing(shader, thing);
|
draw_thing(shader, thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// disable the frame buffer
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
// render the screen quad
|
||||||
|
screen_shader.use();
|
||||||
|
glBindVertexArray(quadVAO);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureColorBuffer);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::cleanup() {
|
void Scene::cleanup() {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ struct Thing {
|
||||||
|
|
||||||
struct Scene {
|
struct Scene {
|
||||||
Shader shader;
|
Shader shader;
|
||||||
Shader light_shader;
|
Shader screen_shader;
|
||||||
|
|
||||||
std::unordered_map<std::string, components::Material> materials;
|
std::unordered_map<std::string, components::Material> materials;
|
||||||
Camera camera;
|
Camera camera;
|
||||||
|
|
@ -36,10 +36,27 @@ struct Scene {
|
||||||
|
|
||||||
float deltaTime = 0.0f;
|
float deltaTime = 0.0f;
|
||||||
float lastFrame = 0.0f;
|
float lastFrame = 0.0f;
|
||||||
|
unsigned int framebuffer = 0;
|
||||||
|
unsigned int textureColorBuffer = 0;
|
||||||
|
unsigned int quadVAO = 0;
|
||||||
|
unsigned int quadVBO = 0;
|
||||||
|
unsigned int rbo = 0;
|
||||||
|
|
||||||
|
float quadVertices[24] = {
|
||||||
|
// vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
|
||||||
|
// positions // texCoords
|
||||||
|
-1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, 0.0f, 0.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
-1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
Scene(components::Scene& config):
|
Scene(components::Scene& config):
|
||||||
shader{config.shader.vertex_path, config.shader.frag_path},
|
shader{config.shader.vertex_path, config.shader.frag_path},
|
||||||
light_shader{config.light_shader.vertex_path, config.shader.frag_path},
|
screen_shader{config.screen_shader.vertex_path, config.shader.frag_path},
|
||||||
materials{config.materials},
|
materials{config.materials},
|
||||||
camera{
|
camera{
|
||||||
.position=config.camera.position,
|
.position=config.camera.position,
|
||||||
|
|
@ -64,8 +81,46 @@ struct Scene {
|
||||||
thing.scale);
|
thing.scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// screen quadVAO
|
||||||
|
glGenVertexArrays(1, &quadVAO);
|
||||||
|
glGenBuffers(1, &quadVBO);
|
||||||
|
glBindVertexArray(quadVAO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
|
||||||
|
|
||||||
|
|
||||||
|
// framebuffer config
|
||||||
|
glGenFramebuffers(1, &framebuffer);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||||
|
|
||||||
|
// create a color attachment texture
|
||||||
|
glGenTextures(1, &textureColorBuffer);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureColorBuffer);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCR_WIDTH, SCR_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
|
||||||
|
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, textureColorBuffer, 0);
|
||||||
|
|
||||||
|
// create renderbuffer object for depth and stencil attachment
|
||||||
|
glGenRenderbuffers(1, &rbo);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, SCR_WIDTH, SCR_HEIGHT);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
|
||||||
|
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||||
|
|
||||||
|
dbc::check(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Frame buffer not complete.");
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.apply_lighting(light);
|
shader.apply_lighting(light);
|
||||||
|
|
||||||
|
screen_shader.use();
|
||||||
|
screen_shader.setInt("screenTexture", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@
|
||||||
"material": "default"
|
"material": "default"
|
||||||
},
|
},
|
||||||
"shader_test": {
|
"shader_test": {
|
||||||
"vertex_path": "shaders/16-vert.glsl",
|
"vertex_path": "shaders/19-vert.glsl",
|
||||||
"frag_path": "shaders/16-frag.glsl"
|
"frag_path": "shaders/19-frag.glsl"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue