OpenGL (3) Basics of 3D Programming

Source: Internet
Author: User

Orthogonal projection

Viod glortho (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far)

// Set an orthogonal projection matrix and define a horizon body in the shape of straight parallel cubes.


Void gluwirecube (gldouble size) // generate a wirecube with the center at the origin and side length of size

Void maid (gldouble size) // generate a cube


An example of a simple 3D Cube:

# Include <Gl/glut. h> # include <math. h >#include <vector >#include <iostream> using namespace STD; void Init () {glclearcolor (0.0, 0.0, 0.0, 0.0 ); // specify the screen background as black glcolor3f (1.0, 1.0, 1.0); // set the painting color to white glshademodel (gl_flat ); // set the color interpolation to the flat mode} void display () {glclear (gl_color_buffer_bit); // clear the screen color as the color specified by us (); // force the preceding Drawing operation to execute} void reshape (int w, int h) {glmatrixmode (gl_projection); // set it to the projection mode glloadidentity (); glortho (-1.0, 1.0, -1.0, 1.0,-1.0, 1.0); glviewport (, (glsizei) W, (glsizei) H);} int main (INT argc, char ** argv) {gluinit (& argc, argv); // initialize the maid mode (glu_single | glu_rgb); // set the window mode to single-buffer and RGB mode, and the gluinitwindowsize (500,500 ); // set the window size Init (); arg createwindow ("test"); // set the window title fig (Display); // set the drawing callback function fig (reshape ); // set the window callback function (); // start the loop and wait for the response to return 0 ;}

Run the following command:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/D5/wKioL1QAM_aSZIEiAAHTj3OmQyE308.gif "Title =" if the water gif_August 29, 2014 15:57:21 .gif "alt =" wkiol1qam_aszieiaahtj3omqye308.gif "/>

Camera Positioning

The above example has a problem: the camera only faces one plane of the cube, so we can only see one plane of the cube. If you want to see other faces, you must move the camera or the object to be observed. In OpenGL, these two operations are equivalent. Implemented by the view matrix.

To get the expected view, we can set the camera position and orientation in the object coordinate system. We can determine the camera position and specify it through the point pointed by the camera. You also need to specify an upward vector. For convenience, the vector is usually set to (0, 1, 0) or the Y axis in the coordinate system.

Void glulookat (gldouble eyex, gldouble eyey, gldouble Eyez,

Gldouble ATX, gldouble Aty, gldouble atz,

Gldouble UPX, gldouble upy, gldouble upz)

// Determine a matrix that can be used for camera positioning and targeting. The parameters involved include the camera position (viewpoint eye), the observed point (point at), and the desired up direction.

Modify the above Code to obtain

# Include <Gl/glut. h> # include <math. h >#include <vector >#include <iostream> using namespace STD; void Init () {glclearcolor (0.0, 0.0, 0.0, 0.0 ); // specify the screen background as black glcolor3f (1.0, 1.0, 1.0); // set the painting color to white glshademodel (gl_flat ); // set the color interpolation to the flat mode} void display () {glclear (gl_color_buffer_bit); // clear the glmatrixmode (gl_modelview) and glloadidentity () of the screen color (); glulookat (1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gluwirecube (0.5); glflush (); // force the preceding Drawing operation to execute} void reshape (int w, int h) {glmatrixmode (gl_projection); // set it to the projection mode glloadidentity (); glortho (-2.0, 2.0, -2.0, 2.0,-2.0, 2.0); glviewport (, (glsizei) W, (glsizei) H);} int main (INT argc, char ** argv) {gluinit (& argc, argv); // initialize the maid mode (glu_single | glu_rgb); // set the window mode to single-buffer and RGB mode, and the gluinitwindowsize (500,500 ); // set the window size Init (); arg createwindow ("test"); // set the window title fig (Display); // set the drawing callback function fig (reshape ); // set the window callback function (); // start the loop and wait for the response to return 0 ;}

Run the following command:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/D3/wKiom1QAOM7hZ2YNAACvmPTtob8421.jpg "Title =" qq20140829162302.png "alt =" wkiom1qaom7hz2ynaacvmpttob8421.jpg "/>

Because the camera is placed at (, 1), this image is obtained.


OpenGL (3) Basics of 3D Programming

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.