Commits

Philip Rideout committed f2e0ef3

progress on the resize feature, not quite working (ignore aspect and don't flip)

Comments (0)

Files changed (2)

 GLuint MeshVao;
 GLsizei MeshIndexCount;
 GLsizei MeshVertexCount;
-TexturePod CurrentTexture;
+GLuint CurrentTexture;
 Matrix4 ModelviewMatrix;
 Matrix4 ProjectionMatrix;
 Matrix3 NormalMatrix;
 
 GLuint LoadProgram(const char* vs, const char* gs, const char* fs);
 GLuint CurrentProgram();
+GLuint CreateQuad(int sourceWidth, int sourceHeight, int destWidth, int destHeight);
 
 #define u(x) glGetUniformLocation(CurrentProgram(), x)
 #define a(x) glGetAttribLocation(CurrentProgram(), x)
     GLenum redType;
     int redSize, alpSize;
     GLint lod = 0;
+    
+    GLboolean performResize = GL_TRUE;
+    if (performResize) {
+    
+        // Configure the new dimensions:
+        const int newWidth = 2000;
+        const int newHeight = 1200;
+        
+        // Create the FBO:
+        GLuint fboHandle, fboTexture, colorBuffer;
+        glGenFramebuffers(1, &fboHandle);
+        glBindFramebuffer(GL_FRAMEBUFFER, fboHandle);
+        glGenTextures(1, &fboTexture);
+        glBindTexture(GL_TEXTURE_2D, fboTexture);
+        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_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, newWidth, newHeight, 0, GL_RGB, GL_HALF_FLOAT, 0);
+        glGenRenderbuffers(1, &colorBuffer);
+        glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
+        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTexture, 0);
+        pezCheck(GL_NO_ERROR == glGetError(), "Unable to attach color buffer");
+        pezCheck(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER), "Unable to create FBO.");
+        
+        // Obtain some sizing information:
+        PezConfig cfg = PezGetConfig();
+        glBindTexture(GL_TEXTURE_2D, CurrentTexture);
+        glGetTexLevelParameteriv(GL_TEXTURE_2D, lod, GL_TEXTURE_WIDTH, &pixels.Width);
+        glGetTexLevelParameteriv(GL_TEXTURE_2D, lod, GL_TEXTURE_HEIGHT, &pixels.Height);
 
-    glBindTexture(GL_TEXTURE_2D, CurrentTexture.Handle);
+        // Blit the texture onto the FBO:
+        glViewport(0, 0, newWidth, newHeight);
+        QuadVao = CreateQuad(pixels.Width, pixels.Height, newWidth, newHeight);
+        glBindTexture(GL_TEXTURE_2D, CurrentTexture);
+        glUseProgram(QuadProgram);
+        glBindVertexArray(QuadVao);
+        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+        
+        // Swap the FBO texture with the loaded texture:
+        CurrentTexture = fboTexture;
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
+        glViewport(0, 0, cfg.Width, cfg.Height);
+        QuadVao = CreateQuad(newWidth, newHeight, cfg.Width, cfg.Height);
+        glViewport(0, 0, cfg.Width, cfg.Height);
+    }
+
+    glBindTexture(GL_TEXTURE_2D, CurrentTexture);
     glGetTexLevelParameteriv(GL_TEXTURE_2D, lod, GL_TEXTURE_WIDTH, &pixels.Width);
     glGetTexLevelParameteriv(GL_TEXTURE_2D, lod, GL_TEXTURE_HEIGHT, &pixels.Height);
     glGetTexLevelParameteriv(GL_TEXTURE_2D, lod, GL_TEXTURE_DEPTH, &pixels.Depth);
 
     } else {
         pezFatal("Texture %d (%d x %d) has unsupported format 0x%04.4x (%d bits per component)",
-            CurrentTexture.Handle, pixels.Width, pixels.Height, redType, redSize);
+            CurrentTexture, pixels.Width, pixels.Height, redType, redSize);
     }
     
     pixels.FrameCount = 1;
     glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
 }
 
-GLuint CreateQuad(int sourceWidth, int sourceHeight)
+GLuint CreateQuad(int sourceWidth, int sourceHeight, int destWidth, int destHeight)
 {
     // Stretch to fit:
     float q[] = {
         -1, +1, 0, 0,
         +1, +1, 1, 0 };
 
-    PezConfig cfg = PezGetConfig();
     float sourceRatio = (float) sourceWidth / sourceHeight;
-    float destRatio = (float) cfg.Width  / cfg.Height;
+    float destRatio = (float) destWidth  / destHeight;
     
     // Horizontal fit:
     if (sourceRatio > destRatio) {
     glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
     QuadProgram = LoadProgram("shaders.Quad.VS", 0, "shaders.Quad.FS");
     MeshProgram = LoadProgram("shaders.Mesh.VS", 0, "shaders.Mesh.FS");
-    glGenTextures(1, &CurrentTexture.Handle);
+    glGenTextures(1, &CurrentTexture);
     
     PezConfig cfg = PezGetConfig();
-    ScreenVao = CreateQuad(cfg.Width, cfg.Height);
+    ScreenVao = CreateQuad(cfg.Width, cfg.Height, cfg.Width, cfg.Height);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 }
 
     
         glUseProgram(QuadProgram);
         glBindVertexArray(QuadVao);
-        glBindTexture(GL_TEXTURE_2D, CurrentTexture.Handle);
+        glBindTexture(GL_TEXTURE_2D, CurrentTexture);
         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
     } else if (Mode == HasMesh) {
     
     pezPrintString("Image size is %d x %d\n", result.width, result.height);
 
-    glBindTexture(GL_TEXTURE_2D, CurrentTexture.Handle);
+    glBindTexture(GL_TEXTURE_2D, CurrentTexture);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, result.width, result.height, 0,
                  GL_RGB, GL_FLOAT, result.cols);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glGenerateMipmap(GL_TEXTURE_2D);
     
-    QuadVao = CreateQuad(result.width, result.height);
+    PezConfig cfg = PezGetConfig();
+    QuadVao = CreateQuad(result.width, result.height, cfg.Width, cfg.Height);
     Mode = HasTexture;
 }
 
     
     pezPrintString("Image size is %d x %d\n", pixels.Width, pixels.Height);
 
-    glBindTexture(GL_TEXTURE_2D, CurrentTexture.Handle);
+    glBindTexture(GL_TEXTURE_2D, CurrentTexture);
     glTexImage2D(GL_TEXTURE_2D, 0, pixels.InternalFormat, pixels.Width, pixels.Height, 0,
                  pixels.Format, pixels.Type, pixels.Frames);
     pezFreePixels(pixels);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glGenerateMipmap(GL_TEXTURE_2D);
 
-    QuadVao = CreateQuad(pixels.Width, pixels.Height);
+    PezConfig cfg = PezGetConfig();
+    QuadVao = CreateQuad(pixels.Width, pixels.Height, cfg.Width, cfg.Height);
     Mode = HasTexture;
 }
 
     file.setFrameBuffer (rawPixels - dw.min.x - dw.min.y * width, 1, width);
     file.readPixels (dw.min.y, dw.max.y);
 
-    glBindTexture(GL_TEXTURE_2D, CurrentTexture.Handle);
+    glBindTexture(GL_TEXTURE_2D, CurrentTexture);
     
     glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glGenerateMipmap(GL_TEXTURE_2D);
-    
-    QuadVao = CreateQuad(width, height);
+
+    PezConfig cfg = PezGetConfig();
+    QuadVao = CreateQuad(width, height, cfg.Width, cfg.Height);
     Mode = HasTexture;
 }
 
     float x;
     float y;
     float z;
-#ifndef __GNUC__
-    float d;
-#endif
 }
 #ifdef __GNUC__
 __attribute__ ((aligned(16)))