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
|
|
@ -102,7 +102,7 @@ namespace components {
|
|||
|
||||
struct Scene {
|
||||
components::Shader shader;
|
||||
components::Shader light_shader;
|
||||
components::Shader screen_shader;
|
||||
std::unordered_map<std::string, Material> materials;
|
||||
std::unordered_map<std::string, Model> models;
|
||||
std::vector<Thing> things;
|
||||
|
|
@ -110,5 +110,5 @@ namespace components {
|
|||
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_MINOR, 3);
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -34,15 +34,19 @@ void Scene::draw_thing(Shader& with_shader, Thing& thing)
|
|||
}
|
||||
|
||||
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 projection = glm::mat4(1.0f);
|
||||
|
||||
// fov, aspect, near plane, far plane
|
||||
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.setMat4("view", view);
|
||||
shader.setMat4("projection", projection);
|
||||
|
|
@ -50,6 +54,19 @@ void Scene::render(GLFWwindow* window) {
|
|||
for(auto& thing : things) {
|
||||
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() {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ struct Thing {
|
|||
|
||||
struct Scene {
|
||||
Shader shader;
|
||||
Shader light_shader;
|
||||
Shader screen_shader;
|
||||
|
||||
std::unordered_map<std::string, components::Material> materials;
|
||||
Camera camera;
|
||||
|
|
@ -36,10 +36,27 @@ struct Scene {
|
|||
|
||||
float deltaTime = 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):
|
||||
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},
|
||||
camera{
|
||||
.position=config.camera.position,
|
||||
|
|
@ -64,8 +81,46 @@ struct Scene {
|
|||
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.apply_lighting(light);
|
||||
|
||||
screen_shader.use();
|
||||
screen_shader.setInt("screenTexture", 0);
|
||||
}
|
||||
|
||||
void update();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue