Win32 OpenGL programming (10) view Transformation

Source: Internet
Author: User
Tags mercurial version control system

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie


Discuss newsgroups and documents

Abstract

Four 3D transformations are mentioned in the camera metaphor in the previous article (Series Article (7), hereinafter referred to as xo7, similar to other articles in the series), as follows:

1. The process of determining the camera Location corresponds to "view transformation" (viewing transformations)

2. The process of determining the Object Location corresponds to "modeling transformations)

3. The process of determining the camera magnification corresponds to projection transformations)

4. The process of determining the Photo size corresponds to "viewport transformations)

In xo7, we are talking about the first type of view transformation, that is, changing the position of the observer and the effect of the perspective. In xo8, we are talking about the second type of transformation model transformation, in xo9, we are talking about projection transformation. This article begins to explain the last transformation, namely the viewport transformation.

 

View Transform

This transformation should be the simplest of the four types. In the camera metaphor, I said that he confirmed the Photo size. In reality, he confirmed the area to be drawn. Of course, we didn't set the client area of the window to occupy the whole window by default. There is only one key function glviewport, which is easy to understand:

OpenGL programming guide
":

Glviewport-set the viewport
C Specification
Void glviewport (glint X,
Glint y,
Glsizei width,
Glsizei height );
Parameters

X, Y

Specify the lower left corner of the viewport rectangle,
In pixels. The initial value is (0, 0 ).
Width, height

Specify the width and height
Of the viewport.
When a GL context is first attached to a window,
Width and height are set to the dimensions of that
Window.

 

No matter how much processing is done, the final image will be mapped to this rectangle, which occupies the customer area of the entire window by default, except in all the previous examples (except for the example of the jigsaw puzzle) we didn't touch the viewport transform, so the default value is the size of the window at the moment of creation. We can try to change the size of the window and find that the fact shape does not grow, and the position is no longer centered, in this case, we need to transform the view and adjust the view. Here, I will not provide new examples for this purpose because of the previous example and examples that were not used in the past. Let's just look at the differences between the two examples.

 

When the window is not changed, re-set the View:

Normal situation:

 


When the window is reduced: The image is offset.


 

When the window is enlarged: The image is not centered.

 

Let's look at the example in the jigsaw puzzle:

The following code is available:

void ReShape(unsigned auWidth, unsigned auHeight){    glViewport(0, 0, auWidth, auHeight);}

// FUNCTIONS //////////////////////////////////////////////LRESULT CALLBACK WindowProc(HWND hwnd,                             UINT msg,                             WPARAM wparam,                             LPARAM lparam){    // this is the main message handler of the system    PAINTSTRUCT        ps;        // used in WM_PAINT    HDC                hdc;    // handle to a device context    // what is the message     switch(msg)    {        case WM_CREATE:         {            // do initialization stuff here            // return success            return(0);        } break;    case WM_PAINT:         {            // simply validate the window             hdc = BeginPaint(hwnd,&ps);                 // end painting            EndPaint(hwnd,&ps);            // return success            return(0);        } break;    case WM_DESTROY:         {            // kill the application, this sends a WM_QUIT message             PostQuitMessage(0);            // return success            return(0);        } break;    case WM_SIZE:        {            ReShape(LOWORD(lparam), HIWORD(lparam));        }    default:break;    } // end switch    // process any messages that we didn't take care of     return (DefWindowProc(hwnd, msg, wparam, lparam));} // end WinProc

 

Note the function of reshape. When the window size changes, it will reset the view. In this way, the image will change as the window size changes (this is what we need in most cases)

Under normal circumstances:


Window shrinking: The image is still centered, because the aspect ratio changes, resulting in a change in the aspect ratio.


When the window is enlarged: The image is still centered, and the aspect ratio is also changed due to the change of the aspect ratio.


The above example demonstrates the function of glviewport. The source code has been provided before. However, we will find a problem, that is, when the window aspect ratio is changed, the image actually changes the aspect ratio, resulting in deformation, which is not in line with our ideas in most cases, we can control this by controlling the aspect ratio of the window (in most cases) or directly controlling the aspect ratio of the glviewport parameter to ensure that the image is not distorted. (But the image may be displaced)

Screen Segmentation

Players who have played the "Three Kingdoms" series do not know whether they have fought with their comrades on the same machine. I have. When I show that a player is on the top and one is on the bottom, it is really interesting that you can enjoy online on the same machine without having to go through the network. In fact, we can achieve this through two consecutive screen views (I don't know if the Three Kingdoms are also achieved through this technology). For example, let's look at the example of the above-mentioned jigsaw puzzle, we want to draw four times on the screen so that four people can play and fight at the same time, so we can do this:

void ReShape(unsigned auWidth, unsigned auHeight){    WindowWidth = auWidth;    WindowHeight = auHeight;}// All Scene Show code void SceneShow(GLvoid)        {    glClear(GL_COLOR_BUFFER_BIT);        // left bottom    glViewport(0, 0, WindowWidth/2, WindowHeight/2);    gTriBTop.Draw();    gTriBRight.Draw();    gTriSLeft.Draw();    gRectangle.Draw();    gTriSMid.Draw();    gTriMLeft.Draw();    gParal.Draw();    // right bottom    glViewport(WindowWidth/2, 0, WindowWidth/2, WindowHeight/2);    gTriBTop.Draw();    gTriBRight.Draw();    gTriSLeft.Draw();    gRectangle.Draw();    gTriSMid.Draw();    gTriMLeft.Draw();    gParal.Draw();    // left top     glViewport(0 , WindowHeight/2, WindowWidth/2, WindowHeight/2);    gTriBTop.Draw();    gTriBRight.Draw();    gTriSLeft.Draw();    gRectangle.Draw();    gTriSMid.Draw();    gTriMLeft.Draw();    gParal.Draw();    // right top     glViewport(WindowWidth/2 , WindowHeight/2, WindowWidth/2, WindowHeight/2);    gTriBTop.Draw();    gTriBRight.Draw();    gTriSLeft.Draw();    gRectangle.Draw();    gTriSMid.Draw();    gTriMLeft.Draw();    gParal.Draw();    glFlush();}  

 

Display Effect:


Note that in the above Code, we only have one specific display code. The display code does not know how many copies we have drawn and where they are drawn. This is the benefit of OpenGL design, the drawing itself is very simple. Just draw a standard drawing near the origin. After one type of conversion, it can be generated from a different graph, this is a bit like the decorator mode in the design mode.

To save space, only key clips are posted. For the complete source code, see the 2009-10-29/jtfourtangram directory of the source code of my blog. For more information, see the description of getting the complete source code of the blog.

Haha, it's quite happy. Four people playing the same machine at the same time (in fact, any game can be used for reference). What? Is there any way to operate on a single mouse? Where are you confused? Is it useless to insert four mouse clicks? Well, we recommend that you look at what I previously wrote about multi-Mouse. Four people play with the four mouse at the same time, it's not impossible ^ in the past, World of Warcraft seemed to have a game map of the 4-nation war very popular. If it was an extra design, we could play on the same machine ^ let's use everyone's creativity.

References

1. OpenGL Reference Manual
, OpenGL Reference Manual

2. OpenGL
OpenGL programming guide
), Dave shreiner, Mason Woo, Jackie neider, Tom Davis
Xu Bo, Mechanical Industry Press

3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.

4. OpenGL getting started, by eastcowboy, this is a good tutorial I have found on the Internet. It is quite complete and popular. This is the first address: http://bbs.pfan.cn/post-184355.html

 

Other articles in this OpenGL Series

1.
Win32 OpenGL programming (1) steps required for OpenGL programming under Win32
"

2. Win32 OpenGL programming (2) searching for missing OpenGL functions
"

3. Win32 OpenGL programming (3) Basic Elements (points, straight lines, polygon)
"

4. Win32 OpenGL programming (4) 2D graphics BASICS (advanced knowledge of color and coordinate systems)
"

5. Win32 OpenGL programming (5) vertices Array
"

6. Win32 OpenGL programming (6) entering the 3D world
"

7. Win32 OpenGL programming (7) 3D view transformation-the key to true 3D
"

8. Win32 OpenGL programming (8) 3D model transformation and its Combined Application
"

9. Win32 OpenGL programming (9) Projection Transformation
"

Application Example: Win32 OpenGL 2D programming series -- drawing of jigsaw puzzle
"

Complete source code retrieval instructions

Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:

Https://blog-sample-code.jtianling.googlecode.com/hg/

For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"

If you only want to browse all the code, you can go to Google Code to view it. the following URL:

Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code

 

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

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.