1. Philip Rideout
  2. pez-viewer

Commits

Philip Rideout  committed 64e0fe4

fix up aspect ratio

  • Participants
  • Parent commits b820ebf
  • Branches default

Comments (0)

Files changed (2)

File shaders.glsl

View file
 -- VS
 
 in vec2 Position;
+in vec2 TexCoord;
 out vec2 vTexCoord;
 
 void main()
 {
-    vTexCoord = 0.5 + Position.xy * 0.5;
-    vTexCoord.y = 1 - vTexCoord.y;
+    vTexCoord = TexCoord;
     gl_Position = vec4(Position, 0, 1);
 }
 

File viewer.cpp

View file
 
 GLuint QuadProgram;
 GLuint QuadVao;
-const GLuint PositionSlot = 0;
 
 GLuint LoadProgram(const char* vs, const char* gs, const char* fs);
 GLuint CurrentProgram();
 
 #define u(x) glGetUniformLocation(CurrentProgram(), x)
 #define a(x) glGetAttribLocation(CurrentProgram(), x)
+#define offset(x) ((const GLvoid*)x)
 
 PezConfig PezGetConfig()
 {
     return config;
 }
 
-GLuint CreateQuad()
+GLuint CreateQuad(int sourceWidth, int sourceHeight)
 {
+    // Stretch to fit:
+    float q[] = {
+        -1, -1, 0, 1,
+        +1, -1, 1, 1,
+        -1, +1, 0, 0,
+        +1, +1, 1, 0 };
+        
+    PezConfig cfg = PezGetConfig();
+    float sourceRatio = (float) sourceWidth / sourceHeight;
+    float destRatio = (float) cfg.Width  / cfg.Height;
+    
+    // Horizontal fit:
+    if (sourceRatio > destRatio) {
+        q[1] = q[5] = -0.5f / sourceRatio;
+        q[9] = q[13] = 0.5f / sourceRatio;
+
+    // Vertical fit:    
+    } else {
+        q[0] = q[8] = -0.5f * sourceRatio;
+        q[4] = q[12] = 0.5f * sourceRatio;
+    }
+
     GLuint vbo, vao;
-    const short q[] = { -1, -1, 1, -1, -1, 1, 1, 1 };
-
+    
+    glUseProgram(QuadProgram);
     glGenVertexArrays(1, &vao);
     glBindVertexArray(vao);
     glGenBuffers(1, &vbo);
     glBindBuffer(GL_ARRAY_BUFFER, vbo);
     glBufferData(GL_ARRAY_BUFFER, sizeof(q), q, GL_STATIC_DRAW);
-    glVertexAttribPointer(PositionSlot, 2, GL_SHORT, GL_FALSE, 4, 0);
-    glEnableVertexAttribArray(PositionSlot);
+    glVertexAttribPointer(a("Position"), 2, GL_FLOAT, GL_FALSE, 16, 0);
+    glVertexAttribPointer(a("TexCoord"), 2, GL_FLOAT, GL_FALSE, 16, offset(8));
+    glEnableVertexAttribArray(a("Position"));
+    glEnableVertexAttribArray(a("TexCoord"));
     
     return vao;
 }
 
 void PezInitialize()
 {
-    glClearColor(1, 0, 0, 1);
+    glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
     QuadProgram = LoadProgram("shaders.VS", 0, "shaders.FS");
-    QuadVao = CreateQuad();
 }
 
 void PezRender()
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glGenerateMipmap(GL_TEXTURE_2D);
     
+    QuadVao = CreateQuad(width, height);
     Mode = HasTexture;
 }