diff --git a/include/Buffers.h b/include/Buffers.h index d7bae95..36e72a8 100644 --- a/include/Buffers.h +++ b/include/Buffers.h @@ -80,6 +80,8 @@ class RBO RBO(int w, int h, GLuint component = GL_DEPTH_COMPONENT); // Создает буфер рендера с заданными параметрами размеров и используемых компонент ~RBO(); // Уничтожение буфера + void reallocate(int w, int h, GLuint component = GL_DEPTH_COMPONENT); // Изменяет размеры буфера рендера + GLuint getHandler(); // Возвращает дескриптор буфера рендера protected: GLuint handler; // Дескриптор diff --git a/include/Texture.h b/include/Texture.h index e64599c..ed521f6 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -23,6 +23,8 @@ class Texture Texture& operator=(const Texture& other); // Оператор присваивания + void reallocate(GLuint width, GLuint height, GLuint texType = TEX_DIFFUSE, GLint internalformat = GL_RGBA, GLint format = GL_RGBA, GLenum dataType = GL_FLOAT); // Пересоздает текстуру для имеющегося дескриптора + void use(); // Привязка текстуры static void disable(GLuint type); // Отвязка текстуры по типу GLuint getType(); // Возвращает тип текстуры diff --git a/src/Buffers.cpp b/src/Buffers.cpp index c0acea7..2b2d448 100644 --- a/src/Buffers.cpp +++ b/src/Buffers.cpp @@ -189,6 +189,13 @@ RBO::~RBO() glDeleteRenderbuffers(1, &handler); } +// Изменяет размеры буфера рендера +void RBO::reallocate(int w, int h, GLuint component) +{ + glBindRenderbuffer(GL_RENDERBUFFER, handler); // Привязка элементного буфера + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h); +} + // Возвращает дескриптор буфера рендера GLuint RBO::getHandler() { diff --git a/src/Texture.cpp b/src/Texture.cpp index e08ecb8..311f7f3 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -115,6 +115,13 @@ Texture::~Texture() } } +// Пересоздает текстуру для имеющегося дескриптора +void Texture::reallocate(GLuint width, GLuint height, GLuint texType, GLint internalformat, GLint format, GLenum dataType) +{ + use(); + glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, dataType, NULL); +} + // Привязка текстуры void Texture::use() { diff --git a/src/main.cpp b/src/main.cpp index 540d463..71a05fc 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,14 +9,42 @@ #include "Shader.h" #include "Lights.h" -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 #define WINDOW_CAPTION "OPENGL notes on rekovalev.site" +// Указатели на текстуры для изменения размеров окна +Texture* pgPosition = NULL; +Texture* pgNormal = NULL; +Texture* pgDiffuseP = NULL; +Texture* pgAmbientSpecular = NULL; +RBO* pgrbo = NULL; +// Размеры окна +int WINDOW_WIDTH = 800; +int WINDOW_HEIGHT = 600; + // Функция-callback для изменения размеров буфера кадра в случае изменения размеров поверхности окна void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); + + // Изменение размеров текстур для G-буфера + if (pgPosition) + pgPosition->reallocate(width, height, 0, GL_RGB32F, GL_RGB); + if (pgNormal) + pgNormal->reallocate(width, height, 1, GL_RGB16F, GL_RGB); + if (pgDiffuseP) + pgDiffuseP->reallocate(width, height, 2, GL_RGBA16F); + if (pgAmbientSpecular) + pgAmbientSpecular->reallocate(width, height, 3); + // И буфера глубины + if (pgrbo) + pgrbo->reallocate(width, height); + + // Запомним новые размеры окна + WINDOW_WIDTH = width; + WINDOW_HEIGHT = height; + + // Изменим параметры перспективной матрицы проекции для камеры + Camera::current().setPerspective(CAMERA_FOVy, (float)width/height); } bool firstMouse = true; @@ -133,6 +161,13 @@ int main(void) // Активируем базовый буфер кадра FBO::useDefault(); + // Сохраним указатели на текстуры для изменения размеров окна + pgPosition = &gPosition; + pgNormal = &gNormal; + pgDiffuseP = &gDiffuseP; + pgAmbientSpecular = &gAmbientSpecular; + pgrbo = &grbo; + // Шейдер для расчета освещенности ShaderProgram lightShader; // Загрузка и компиляция шейдеров