OpenGL learning notes (2): View

Source: Internet
Author: User
Document directory
  • 1.1 3D geometric Assembly Line
  • 1.2 conversion Introduction
  • 2.1 transformation order
  • 2.2 model transformation
  • 2.3 view Transformation
  • 2.4 Projection Transformation
  • 2.5 view Transform

By yurunsun@gmail.com
Sina Weibo @ sun yurun Sina Blog
Csdn blog

Date: 2013-6-12

Content of this chapter:

  1. Pipeline and Transformation
  2. Model Transformation
  3. View Transformation
  4. Projection Transformation
  5. Operation Matrix
  6. Inverse Transformation and analog Transformation
1. assembly line and transformation 1.1 Three-dimensional Geometric Assembly Line

A Three-dimensional Geometric assembly line is a series of processes that convert a three-dimensional point into a two-dimensional point:

3D model coordinate ---> 3D world coordinate ---> 3D eye coordinate ---> 3D eye coordinate ---> 2D eye coordinate ---> 2D screen coordinate ||| | model transformation view transformation crop projection window map to the viewport
  1. Scenario

    A scenario is a combination of 3D space objects displayed in an image, including all geometric objects and illumination in the space.

  2. View

    A view is an information set used to create images, including scene space, scene coordinate system, coordinate system eye viewpoint, and projection operations.

  3. Modeling

    Modeling is the process of creating and defining a ry.

  4. 3D model coordinate system vs 3D world coordinate system

    The modeling process defines each object in its convenient and meaningful coordinate system, and then transforms the object to the coordinate system of the unified space through a series of model transformations. The former is the model coordinate system, and the latter is the world coordinate system.

  5. 3D eye Coordinate System

    Coordinate system starting from observation point

1.2 conversion Introduction

OpenGL combines model transformation and view transformation to share a matrix (Model View matrix)GL_MODELVIEW). This is feasible because the two are relatively speaking. Moving the camera five units along the Z axis is equivalent to moving all the objects in the reversed direction of the world coordinate system five units.

  1. Model Transformation

    The purpose of model transformation is to set the model's position and direction: rotation, movement, and scaling.

  2. View Transformation

    View transformation is equivalent to fixing the camera on a tripod and aligning it with the scene

  3. Projection Transformation

    Projection Transformation is equivalent to selecting the camera lens, that is, determining the field of view

  4. View Transform

    Projection Transformation specifies the ing mechanism, and the viewport transformation determines the shape of the valid screen area.

  5. General functions

    void glMatrixMode(GLenum mode); // GL_MODELVIEW/GL_PROJECTION/GL_TEXTUREvoid glLoadIdentity(void);void glLoadMatrixf(const float* m);void glMultMatrixf(const float* m);void glLoadTransposeMatrixf(const float* m);void glMultTransposeMatrixf(const float* m);

    Matrix Multiplication in OpenGL: assume that the current matrix is C, and the matrix specified by functions such as glmultmatrixf is M. After multiplication, the result is cm and stored in the previous C.

    The matrix in OpenGL is opposite to the column and column in C language. M [I] [J] indicates row J of column I.

2. View and model transformation sequence of 2.1 Transformations
glMatrixMode(GL_MODELVIEW);glLoadIdentity();glMultMatrixf(N); /* apply transformation N */glMultMatrixf(M); /* apply transformation M */glMultMatrixf(L); /* apply transformation L */glBegin(GL_POINTS);    glVertex3f(v); /* draw transformed vertex v */glEnd();

The modulo matrix containsI, N, NM, NML, The fixed point transformation isN(M(Lv))That is, V is first multiplied by L, LV is multiplied by m, and mlv is multiplied by N.

For example, if you want to change the source object to 30 ° Skewed and located at 5.0 on the X axis, you must first rotate and then pan along the X axis. To do this, follow these steps:

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glMultMatrixf(T); /* translation */glMultMatrixf(R); /* rotation */draw_the_object();
2.2 model transformation

First, make sure that it is in the mode matrix:

glMatrixMode(GL_MODELVIEW);

Then use the following transform function:

void glTranslatef(float x, float y,float z);void glRotatef(float angle, float x,float y,float z);void glScalef(float x, float y, float z);
2.3 view Transformation

View transformation is used to modify the position and direction of an observation point. According to the pipeline, the first thing to do is the object model transformation. Therefore, the view transformation function must be called before any model transformation function is called to ensure that the model transformation (not confusing) is applied to the object first)

The first method is to still use the functions of the above model transformation, but to perform the inverse operation of eye coordinates. More common and convenient to usegluLookAt().

2.4 Projection Transformation

First, make sure you are in the projection matrix:

glMatrixMode(GL_PROJECTION);
  1. Perspective Projection

    The obvious feature of perspective projection is that it is near big and small, because the visual object of the perspective projection is the pyramid's horizontal part.

    void glFrustum(GLdouble left, GLdouble right,                 GLdouble bottom, GLdouble top,                GLdouble near, GLdouble far);

    Create a matrix that represents the pivoting point and multiply it with the current matrix. More common and convenient to usegluPerspective().

  2. Forward projection

    Under normal projection, a visual object is a parallel cube. The distance between an object and a camera does not affect the size of the image. It is often used in CAD.

    void glOrtho(GLdouble left, GLdouble right,                 GLdouble bottom, GLdouble top,                GLdouble near, GLdouble far);

    AndglFrustumSimilar. You can usegluOrtho2D.

2.5 view Transform

UseglViewPort()The function can select a drawing area. For example, to divide a window, the split screen effect is displayed in the same window to display multiple views.

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);   
3. Matrix Stack

Let's draw the car body and wheel: Draw the car body, remember the position, move to the right front wheel, draw the wheel, discard the operation to move to the right front wheel back to the car body, move to the left front wheel ......

Transformation is stored in a matrix, so using a matrix stack can solve this problem ideally.

void glPushMatrix();void glPopMatrix();
  1. Visual matrix Stack

    For example, the vehicle body and wheel

  2. Projection Matrix Stack

    Generally, you do not need to combine the projection matrix. Therefore, you need to callglLoadIdentity(). If you want to display a Help window containing text, because the text is easy to locate in positive projection mode, you can temporarily set the projection matrix to a positive projection when displaying the Help window, then return to the original perspective projection.

    glMatrixMode(GL_PROJECTION);glPushMatrix(); /*save the current projection*/glLoadIdentity();glOrtho(...); /*set up for displaying help*/display_the_help();glPopMatrix();
4. Inverse Transformation and analog Transformation

The assembly line uses the view and projection matrix to convert the world coordinates of vertices into screen coordinates, but sometimes this process needs to be reversed, for example, returning 3D coordinates based on the mouse position.

int gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz,        const GLdouble modelMatrix[16],         const GLdouble projMatrix[16],         const GLint viewport[4],         GLdouble *objx, GLdouble *objy, GLdouble *objz);
  • Window coordinates: winx, winy, WINZ
  • Model matrix: modelmatrix
  • Projection Matrix: projmatrix
  • Viewport
  • World coordinates: objx, objy, objz

WINZ indicates the Z value of the window coordinate, that is, znear ~ of the camera pyramid ~ Zfar, with the standard depth ranging from 0 ~ 1. Use another function in non-standard depthgluUnProject4

int gluProject(GLdouble objx, GLdouble objy, GLdouble objz,        const GLdouble modelMatrix[16],         const GLdouble projMatrix[16],         const GLint viewport[4],         GLdouble *winx, GLdouble *winy, GLdouble *winz);

AndgluUnProjectThe screen coordinates are calculated based on the world coordinates.

  • If this article is helpful to you, please go to the csdn blog to leave a message;
  • Reprinted Please note: Technical blog from yurun

    Http://blog.csdn.net/sunyurun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.