【问题标题】:orthographic scaling opengl viewport正交缩放opengl视口
【发布时间】:2016-06-19 22:12:00
【问题描述】:

我发现 this code sample 可以满足我对现代 opengl 的需求。

我正在尝试弄清楚如何使用我的代码执行此操作,但我遇到了非常困难的时期。

/*
 * GL03Viewport.cpp: Clipping-area and Viewport
 * Implementing reshape to ensure same aspect ratio between the
 * clipping-area and the viewport.
 */
#include <GL/glut.h>  // 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 >= height) {
     // aspect >= 1, set the height from -1 to 1, with larger width
      gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
   } else {
      // aspect < 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(&argc, argv);          // Initialize GLUT
   //glutInitWindowSize(640, 480);   // Set the window's initial width & height - non-square
   glutInitWindowSize(1024, 720);   // Set the window's initial width & 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;
}

出于两个原因,我对我的代码进行了几次尝试。 1)我希望左下角为 0,0,右上角为宽度、高度。

2) 第二个也是更令人困惑的部分是如何维护我的像素坐标。示例代码使用 -1.0 到 1.0 范围内的点,但我想说我的四边形是 50 像素乘 50 像素。

目前我像这样创建我的视口和投影。

    point3 eye, center;
    vec3 up;
    vmathP3MakeFromElems(&eye, 0, 0, 0);
    vmathP3MakeFromElems(&center, 0, 0, -1);
    vmathV3MakeFromElems(&up, 0, 1, 0);
    vmathM4MakeLookAt(&v_mat, &eye, &center, &up);

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

    glViewport(0, 0, width, height);
    vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1, 100);

我目前正在绘制 1 个这样定义的四边形:

//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};

我设置它的模型矩阵是这样的:

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

vmathM4SetElem(&out->model_mat, 3, 0, (out->scale.x) - (x));
vmathM4SetElem(&out->model_mat, 3, 1, (out->scale.y) + (y));

那么我怎样才能像示例代码中那样实现视口缩放但能够使用像素[这可能是错误的方法,我不确定]。

顶部的示例代码可以复制粘贴并使用此命令构建。

gcc -lglut -lGLU -lGL -o glsample glsample.c && ./glsample

【问题讨论】:

    标签: c opengl orthographic


    【解决方案1】:
    vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1, 100);
    

    比方说,在p_mat 这一行之后将包含一个这样的矩阵:

    |1/width                 | -> p_mat[0][0]
    |         1/height       | -> p_mat[1][1]
    |                   .... |
    |                        |
    

    这应该确保,如果宽度和高度以像素为单位,您将保持像素完美的比例。 但是,您提供的第一个示例使用

    gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
    

    不考虑屏幕宽度和高度(仅考虑它们之间的纵横比),您将拥有统一的单位比例。

    基本上,通过将投影矩阵 (p_mat[0][0] &amp; p_mat[1][1]) 中的这两个分量(除以相同的数字以保持纵横比)相乘或相乘,您可以缩放视口。要实现像素级完美缩放,只需将它们设置为 1/width 和 1/height。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多