OpenGL starts from 1.0 - 3D viewing

Our article extends the two-dimensional world to three-dimensional. We have already understood from the previous article that geometric transformation and observation transformation are essentially the same, so ignore the three-dimensional geometric transformation for the time being and directly enter the world of three-dimensional observation.
The 3D viewing pipeline consists of five steps: modeling transformation, viewing transformation, projection transformation, normalization transformation and clipping, and viewport transformation. The process of transforming the object from the model coordinate system to the world coordinate system is called modeling transformation. The observation transformation is to convert the scene model built in the world coordinate system to the selected observation coordinate system. The viewing coordinate system defines viewing parameters, including the location and orientation of the projection plane. Then define a 2D cropping window corresponding to the camera lens on the projection plane, and establish a 3D cropping area. This clipping region is called the viewing volume, and the projection transformation transforms the viewing coordinate description of the scene into the coordinate position of the projection plane. Afterwards, the object is mapped to the normalized coordinate system, and all parts outside the viewing body are clipped. Clipping operations can be performed on all device-independent coordinate transformations. As in 2D viewing, viewport boundaries can be specified in normalized or device coordinates. The final step is the viewport transformation, which maps the viewing coordinate system to the specified display window in the device coordinate system.
Without further ado, let's go straight to the most intuitive example.

#include <GL/glut.h>
GLint winWidth = 600, winHeight = 600;
GLfloat x0 = 100.0, y0 = 50.0, z0 = 50.0;//观察点位置
GLfloat xref = 50.0, yref = 50.0, zref = 0.0;//注视点位置
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;//向上向量
GLfloat xwMin = -40.0, ywMin = -60.0, xwMax = 40.0, ywMax = 60.0;//裁剪窗口坐标
GLfloat dnear = 25.0, dfar = 125.0;//裁剪平面
void  init(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);//观察函数
    glMatrixMode(GL_PROJECTION);
    glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);//视锥体
}
void displayFcn(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 1.0, 0.0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_LINE);
    glBegin(GL_QUADS);//绘制正方形
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(100.0, 0.0, 0.0);
    glVertex3f(100.0, 100.0, 0.0);
    glVertex3f(0.0, 100.0, 0.0);
    glEnd();
    glFlush();

}
void reshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0, 0, newWidth, newHeight);//活动视口
    winWidth = newWidth;
    winHeight = newHeight;
}
void main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RED);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Perspective view of A Square");
    init();
    glutDisplayFunc(displayFcn);
    glutReshapeFunc(reshapeFcn);
    glutMainLoop();
}

write picture description here
In the example we draw a square in the 3D world, but since we are using perspective projection mode, we end up showing a green trapezoid. Well, let's analyze the function to achieve this effect.
OpenGL maintains a matrix stack for each of the four modes (model viewing, projection, texture, and color) that can be selected with glMatrixMode. Each stack initially contains only the identity matrix, and the matrix at the top of the stack is called the "current matrix" for that mode. After specifying the observation and geometric transformations, the top of the modeling observation stack is a 4*4 composite matrix of the observation transformations and various geometric transformations applied to the scene.
OpenGL has two functions for working with matrices on the stack:

glPushMatrix();//复制活动栈顶的当前矩阵并将其存入第二个栈位置
glPopMatrix();//破坏栈顶矩阵,栈的第二个矩阵成为当前矩阵

The example first specifies the modeling observation mode and calls the function gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz) to calculate the modeling observation matrix. The first three parameters of this function specify the origin of the observation reference system (observation point position), the next three parameters specify the reference point position (viewpoint position), and the last three parameters specify the upward vector (the direction of the human head).
After that, the code specifies the projection mode and calls glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar) to calculate the projection matrix. This function specifies a symmetrical frustum observer or an oblique frustum observer. The first four parameters of this function set the coordinates of the clipping window on the near plane, while the last two parameters specify the distance from the origin of the coordinate system along the negative z-axis to the near and far clipping planes.
In addition to this function, the orthographic projection parameters are selected with the following parameters:

glOrtho(xwmin,xwmax,ywmin,ywmax,dnear,dfar);

This function produces a parallel projection perpendicular to the viewing plane (near clipping plane).
The symmetric perspective projection prism viewing volume is built with the following GLU functions:

gluPerspective(theta,aspect,dnear,dfar);

The first two parameters of this function define the size and position of the clipping window on the near clipping plane, and the last two parameters specify the distance from the observer point to the near and far clipping planes. The parameter theta represents the field of view, that is, the angle between the upper and lower clipping planes, and the parameter aspect assigns the aspect ratio of the clipping window.
Both the glFrustum and gluPerspective functions are used to generate perspective projection views. For perspective projection, the perspective reference point is the origin of the viewing coordinate system and the near clipping plane is the viewing plane.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525993&siteId=291194637