SharpGL learning notes (5) view transform, sharpgl learning notes

Source: Internet
Author: User

SharpGL learning notes (5) view transform, sharpgl learning notes

The main object in the visual view is to display the projected objects in the Visual View to a two-dimensional view plane. in computer graphics, it is defined to display the objects after geometric transformation, projection transformation, and cropping transformation in the specified area of the screen.

The Perspective Projection and normal projection discussed earlier generate a visual object using the Viewport () function, the projected objects in the visual view can be displayed in the area specified by the screen.

By default, the viewport is the entire rectangular area you use to draw 3D images.

The prototype of Viewport is: Viewport (int x, int y, int width, int height)

 

Let's describe the role of this function:

(1) Viewport defines the area of the view in the window and specifies the ing between the two-dimensional pixel plane and the area of the view.

(2) If Viewport (, w/2, h) exists, the following results are displayed:

 

(3) You can also use Viewport () to generate multi-window effects:

Viewport (0, 0, w/2, h/2); drawpic (); // lower left corner

Viewport (w/2, 0, w/2, h/2); drawpic (); // lower right corner

Viewport (0, h/2, w/2, h/2); drawpic (); // upper left corner

Viewport (w/2, h/2, w/2, h/2); drawpic (); // upper right corner

 

 

The following is the source code of the Multi-Window effect example:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using SharpGL; 10 11 namespace SharpGLWinformsApplication1 12 {13 public partial class SharpGLForm: Form 14 {15 public SharpGLForm () 16 {17 InitializeComponent (); 18} 19 20 private void openGLControl_OpenGLDraw (object sender, PaintEventArgs e) 21 {22 OpenGL gl = openGLControl. openGL; 23 gl. clear (OpenGL. GL_COLOR_BUFFER_BIT | OpenGL. GL_DEPTH_BUFFER_BIT); 24 gl. loadIdentity (); 25 display (gl); 26} 27 28 void display (OpenGL gl) 29 {30 gl. color (1.0, 0.0, 0.0); 31 // draw split line, divided into four visible area 32 gl. viewport (0, 0,400,400); 33 gl. begin (OpenGL. GL_LINES); 34 {35 gl. vertex (-1.0, 0); 36 gl. vertex (1.0, 0); 37 gl. vertex (0.0,-1.0); 38 gl. vertex (0.0, 1.0); 39} 40 gl. end (); 41 42 43 // defines the Area 44 gl in the lower left corner. color (0.0, 1.0, 0.0); 45 gl. viewport (0, 0,200,200); 46 gl. begin (OpenGL. GL_POLYGON); 47 {48 gl. vertex (-0.5,-0.5); 49 gl. vertex (-0.5, 0.5); 50 gl. vertex (0.5, 0.5); 51 gl. vertex (0.5,-0.5); 52} 53 gl. end (); 54 55 // defines 56 gl in the upper right corner. color (0.0, 0.0, 1.0); 57 gl. viewport (200,200,200,200); 58 gl. begin (OpenGL. GL_POLYGON); 59 {60 gl. vertex (-0.5,-0.5); 61 gl. vertex (-0.5, 0.5); 62 gl. vertex (0.5, 0.5); 63 gl. vertex (0.5,-0.5); 64} 65 gl. end (); 66 67 // defines the region 68 gl in the upper left corner. color (1.0, 0.0, 0.0); 69 gl. viewport (0,200,200,200); 70 gl. begin (OpenGL. GL_POLYGON); 71 {72 gl. vertex (-0.5,-0.5); 73 gl. vertex (-0.5, 0.5); 74 gl. vertex (0.5, 0.5); 75 gl. vertex (0.5,-0.5); 76} 77 gl. end (); 78 79 // defines 80 gl in the lower right corner. color (1.0, 1.0, 1.0); 81 gl. viewport (200, 0,200,200); 82 gl. begin (OpenGL. GL_POLYGON); 83 {84 gl. vertex (-0.5,-0.5); 85 gl. vertex (-0.5, 0.5); 86 gl. vertex (0.5, 0.5); 87 gl. vertex (0.5,-0.5); 88} 89 gl. end (); 90 91 gl. flush (); 92} 93 94 private void openGLControl_OpenGLInitialized (object sender, EventArgs e) 95 {96 OpenGL gl = openGLControl. openGL; 97 gl. clearColor (0, 0, 0, 0); 98} 99 100 101 private void openGLControl_Resized (object sender, EventArgs e) 102 {103 OpenGL gl = openGLControl. openGL; 104 gl. matrixMode (OpenGL. GL_PROJECTION); 105 gl. loadIdentity (); 106 107 gl. ortho2D (-1.0, 1.0,-1.0, 1.0); 108 gl. matrixMode (OpenGL. GL_MODELVIEW); 109} 110} 111}

 

The effect is as follows:

 

Download the source code in this section

 

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.