orthographic scaling opengl viewport
<p>I found <a href="https://www3.ntu.edu.sg/home/ehchua/programming/opengl/CG_Introduction.html" rel="nofollow">this code sample</a> that does what I would like to do with modern opengl.</p>

<p>I am trying to figure out how to do this with my code but I am having a really difficult time.</p>

<pre><code>/*
 * GL03Viewport.cpp: Clipping-area and Viewport
 * Implementing reshape to ensure same aspect ratio between the
 * clipping-area and the viewport.
 */
#include &lt;GL/glut.h&gt;  // GLUT, include glu.h and gl.h

/* Initialize OpenGL Graphics */
void initGL() {
   // Set "clearing" or background color
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}

void display() {
   glClear(GL_COLOR_BUFFER_BIT);   // Clear the color buffer with current clearing color

   // Define shapes enclosed within a pair of glBegin and glEnd
   glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
      glColor3f(1.0f, 0.0f, 0.0f); // Red
      glVertex2f(-0.8f, 0.1f);     // Define vertices in counter-clockwise (CCW) order
      glVertex2f(-0.2f, 0.1f);     //  so that the normal (front-face) is facing you
      glVertex2f(-0.2f, 0.7f);
      glVertex2f(-0.8f, 0.7f);

      glColor3f(0.0f, 1.0f, 0.0f); // Green
      glVertex2f(-0.7f, -0.6f);
      glVertex2f(-0.1f, -0.6f);
      glVertex2f(-0.1f,  0.0f);
      glVertex2f(-0.7f,  0.0f);

      glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
      glVertex2f(-0.9f, -0.7f);
      glColor3f(1.0f, 1.0f, 1.0f); // White
      glVertex2f(-0.5f, -0.7f);
      glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
      glVertex2f(-0.5f, -0.3f);
      glColor3f(1.0f, 1.0f, 1.0f); // White
      glVertex2f(-0.9f, -0.3f);
   glEnd();

   glBegin(GL_TRIANGLES);          // Each set of 3 vertices form a triangle
      glColor3f(0.0f, 0.0f, 1.0f); // Blue
      glVertex2f(0.1f, -0.6f);
      glVertex2f(0.7f, -0.6f);
      glVertex2f(0.4f, -0.1f);

      glColor3f(1.0f, 0.0f, 0.0f); // Red
      glVertex2f(0.3f, -0.4f);
      glColor3f(0.0f, 1.0f, 0.0f); // Green
      glVertex2f(0.9f, -0.4f);
      glColor3f(0.0f, 0.0f, 1.0f); // Blue
      glVertex2f(0.6f, -0.9f);
   glEnd();

   glBegin(GL_POLYGON);            // These vertices form a closed polygon
      glColor3f(1.0f, 1.0f, 0.0f); // Yellow
      glVertex2f(0.4f, 0.2f);
      glVertex2f(0.6f, 0.2f);
      glVertex2f(0.7f, 0.4f);
      glVertex2f(0.6f, 0.6f);
      glVertex2f(0.4f, 0.6f);
      glVertex2f(0.3f, 0.4f);
   glEnd();

   glFlush();  // Render now
}

/* Handler for window re-size event. Called back when the window first appears and
   whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
   // Compute aspect ratio of the new window
   if (height == 0) height = 1;                // To prevent divide by 0
   GLfloat aspect = (GLfloat)width / (GLfloat)height;

   // Set the viewport to cover the new window
   glViewport(0, 0, width, height);

   // Set the aspect ratio of the clipping area to match the viewport
   glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
   glLoadIdentity();             // Reset the projection matrix
   if (width &gt;= height) {
     // aspect &gt;= 1, set the height from -1 to 1, with larger width
      gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
   } else {
      // aspect &lt; 1, set the width to -1 to 1, with larger height
     gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
   }
}

/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
   glutInit(&amp;argc, argv);          // Initialize GLUT
   //glutInitWindowSize(640, 480);   // Set the window's initial width &amp; height - non-square
   glutInitWindowSize(1024, 720);   // Set the window's initial width &amp; height - non-square
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
   glutCreateWindow("Viewport Transform");  // Create window with the given title
   glutDisplayFunc(display);       // Register callback handler for window re-paint event
   glutReshapeFunc(reshape);       // Register callback handler for window re-size event
   initGL();                       // Our own OpenGL initialization
   glutMainLoop();                 // Enter the infinite event-processing loop
   return 0;
}
</code></pre>

<p>There are a few chances that I made to my code for two reasons.
1) I'd like to have 0,0 be the lower left corner and width, height be the upper right hand corner.</p>

<p>2) The second and more confusing part is how do I maintain my pixel coordinates. The example code uses points in the range of -1.0 to 1.0 but I would like to be able to say my quad is 50px by 50px.</p>

<p>currently I create my viewport and projections like this.</p>

<pre><code>    point3 eye, center;
    vec3 up;
    vmathP3MakeFromElems(&amp;eye, 0, 0, 0);
    vmathP3MakeFromElems(&amp;center, 0, 0, -1);
    vmathV3MakeFromElems(&amp;up, 0, 1, 0);
    vmathM4MakeLookAt(&amp;v_mat, &amp;eye, &amp;center, &amp;up);

    vec3 trans;
    vmathV3MakeFromElems(&amp;trans, 0, 0, -20);
    vmathM4MakeTranslation(&amp;v_mat, &amp;trans);

    glViewport(0, 0, width, height);
    vmathM4MakeOrthographic(&amp;p_mat, 0, width, 0, height, 1, 100);
</code></pre>

<p>I am currently drawing 1 quad that's defined like this:</p>

<pre><code>//3 position, 4 color, 2 texture coordinates
float v_data[] = {-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
          -1.0f, 1.0f,  0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
          1.0f,  1.0f,  0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
          1.0f,  -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f};

short i_data[] = {0, 1, 2, 0, 2, 3};
</code></pre>

<p>and I setup it's model matrix like this:</p>

<pre><code>vmathV3MakeFromElems(&amp;out-&gt;scale, texture-&gt;width * 0.5f, texture-&gt;height * 0.5f, 1);
vmathM4SetElem(&amp;out-&gt;model_mat, 0, 0, out-&gt;scale.x);
vmathM4SetElem(&amp;out-&gt;model_mat, 1, 1, out-&gt;scale.y);
vmathM4SetElem(&amp;out-&gt;model_mat, 2, 2, 1);
vmathM4SetElem(&amp;out-&gt;model_mat, 3, 3, 1);

vmathM4SetElem(&amp;out-&gt;model_mat, 3, 0, (out-&gt;scale.x) - (x));
vmathM4SetElem(&amp;out-&gt;model_mat, 3, 1, (out-&gt;scale.y) + (y));
</code></pre>

<p>so how can I achieve viewport scaling like in the sample code but being able to use pixels [this might be the wrong approach, i am unsure].</p>

<p>this sample code at the top can be copy pasted and built with this command.</p>

<pre><code>gcc -lglut -lGLU -lGL -o glsample glsample.c &amp;&amp; ./glsample
</code></pre>