Rework testing code

This commit is contained in:
Shylie 2024-05-27 13:07:50 -04:00
parent 12f7d22009
commit 65c6de85ba
3 changed files with 34 additions and 7 deletions

View File

@ -63,6 +63,8 @@ namespace glerminal
unsigned int m_sprites_texture; unsigned int m_sprites_texture;
unsigned int m_framebuffer; unsigned int m_framebuffer;
unsigned int m_framebuffer_backing_texture; unsigned int m_framebuffer_backing_texture;
unsigned int m_screen_framebuffer;
unsigned int m_screen_framebuffer_backing_texture;
unsigned int m_layer_colors_buffer; unsigned int m_layer_colors_buffer;
unsigned int m_layer_scales_buffer; unsigned int m_layer_scales_buffer;
unsigned int m_screen_size_uniform_location; unsigned int m_screen_size_uniform_location;

View File

@ -267,11 +267,13 @@ namespace glerminal
glDrawArraysInstanced(GL_TRIANGLES, 0, 6, GRID_AREA * LAYER_COUNT); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, GRID_AREA * LAYER_COUNT);
glUseProgram(m_screen_program); glUseProgram(m_screen_program);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer);
glBindVertexArray(m_screen_vao); glBindVertexArray(m_screen_vao);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
glBlitNamedFramebuffer(m_screen_framebuffer, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glfwSwapBuffers(m_window); glfwSwapBuffers(m_window);
} }
@ -646,7 +648,10 @@ namespace glerminal
throw std::runtime_error(""); throw std::runtime_error("");
} }
// setup uniforms later // setup uniforms for screen shader
glUseProgram(m_screen_program);
glUniform1i(glGetUniformLocation(m_screen_program, LAYER_COUNT_UNIFORM_NAME), LAYER_COUNT);
// -- setup textures -- // -- setup textures --
glGenTextures(1, &m_sprites_texture); glGenTextures(1, &m_sprites_texture);
@ -686,10 +691,24 @@ namespace glerminal
glBindTextureUnit(1, m_framebuffer_backing_texture); glBindTextureUnit(1, m_framebuffer_backing_texture);
// setup uniforms for screen shader // -- setup screen framebuffer --
glUseProgram(m_screen_program); glGenFramebuffers(1, &m_screen_framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_screen_framebuffer);
glUniform1i(glGetUniformLocation(m_screen_program, LAYER_COUNT_UNIFORM_NAME), LAYER_COUNT); glGenTextures(1, &m_screen_framebuffer_backing_texture);
glBindTexture(GL_TEXTURE_2D, m_screen_framebuffer_backing_texture);
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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, SCREEN_WIDTH, SCREEN_HEIGHT);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_screen_framebuffer_backing_texture, 0);
} }
void glerminal::deinit_glfw() void glerminal::deinit_glfw()
@ -701,6 +720,8 @@ namespace glerminal
void glerminal::deinit_gl() void glerminal::deinit_gl()
{ {
glDeleteFramebuffers(1, &m_screen_framebuffer);
glDeleteTextures(1, &m_screen_framebuffer_backing_texture);
glDeleteFramebuffers(1, &m_framebuffer); glDeleteFramebuffers(1, &m_framebuffer);
glDeleteTextures(1, &m_framebuffer_backing_texture); glDeleteTextures(1, &m_framebuffer_backing_texture);
glDeleteTextures(1, &m_sprites_texture); glDeleteTextures(1, &m_sprites_texture);

View File

@ -13,8 +13,12 @@ namespace
void glerminal_test_save_image() void glerminal_test_save_image()
{ {
glReadBuffer(GL_LEFT); // -- DIRTY HACK --
glReadPixels(0, 0, GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, GL_RGB, GL_UNSIGNED_BYTE, pixels); //
// GL_TEXTURE_2D is not rebound after setting up screen framebuffer
// This code will break if this behavior changes
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
stbi_flip_vertically_on_write(true); stbi_flip_vertically_on_write(true);
stbi_write_png("image.png", GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, 3, pixels, 1280 * 3); stbi_write_png("image.png", GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, 3, pixels, 1280 * 3);