Transformations in the four OpenGL

Source: Internet
Author: User

A Viewpoint transformation
Transformation of Camera position (viewpoint)
void Glulookat (gldouble eyex,gldouble eyey,gldouble Eyez,
Gldouble centerx,gldouble centery, gldouble Centerz,
Gldouble upx,gldouble upy,gldouble Upz)


Two-model transformation
Changes in the location of the model


1 panning
GLTRANSLATE{FD} (TYPE x,type y,type z);


2 rotation
GLROTATE{FD} (TYPE angle,type x,type,y,type z);


3 Scaling
GLSCALE{FD} (TYPE x,type y,type z);


Three-projection transformation
The three-dimensional model is projected onto the screen for display.
1 Perspective projection
From the point of view near the object is large, far from the viewpoint of the object is small, far to the pole is to disappear, become the extinction.
void Glfrustum (gldouble left,gldouble right,gldouble bottom,gldouble top,gldouble near,gldouble FAR);
void Gluperspective (gldouble fovy,gldouble aspect,gldouble znear, gldouble Zfar);


2 Face Projection
No matter how far the object is from the camera, the size of the object after projection is constant, and the positive projection corresponds to the result observed at infinity.
void Glortho (gldouble left,gldouble right,gldouble bottom,gldouble top, gldouble near,gldouble far);
void gluortho2d (gldouble left,gldouble right,gldouble bottom,gldouble top);


Four-viewport transformation
The objects projected in the scene are displayed on a two-dimensional viewport plane.
Glviewport (Glint x,glint y,glsizei width, glsizei height);


Five cropping transformations
In addition to the six clipping planes defined by the scene body, you can customize one or more additional clipping planes to remove extraneous targets in the scenario.
void Glclipplane (Glenum plane,const gldouble *equation);


Six Sun Earth Moon example

#include <glut.h> #include <stdio.h> #include <time.h> static int day = 200;double Getfps () {static int co Unt;static Double save;static clock_t last, current;double timegap;++count;if (Count <=) {return save;} Count = 0;last = Current;current = Clock (); timegap = (current-last)/(double) clk_tck;save = 50.0f/timegap;return Save;} void display () {//print fpsdouble fps = getfps ();p rintf ("fps =%f\n", fps), glenable (gl_depth_test);//Turn on the update depth buffer function, Blocked pixels are not drawn glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);//Clear Color Buffer | Depth buffer Glmatrixmode (gl_projection);//Set current matrix mode: Apply subsequent matrix operations to the projection matrix glloadidentity (); The previous matrix transformation leads to a changed stack top matrix, which is set to the unit matrix, that is, the effect of the previous matrix transformation Gluperspective (75, 1, 1, 400000);//setting Perspective projection Matrix Glmatrixmode (gl_ Modelview);//Set the current matrix mode: Applies the subsequent matrix operation to the Model view matrix stack. glloadidentity ();//resets the stack top matrix before the change of matrix to the unit matrix, That is, the effect of the previous matrix transformation ends Glulookat (0,-200000, 200000, 0, 0, 0, 0, 0, 1);//Set the viewpoint//sunglcolor3f (1, 0, 0);//set drawing color to red glutsolidspher E (69600, 20, 20);//render a sphere//earthglcolor3f (0, 0, 1);//set drawing color to blue glrotateF (Day, 0, 0,-1);//rotate Gltranslatef (150000, 0, 0);//Translate Glutsolidsphere (15945, 20, 20);//Render a sphere//moonglcolor3f (1, 1, 0); GlR Otatef (day/30.0 * 360-day, 0, 0,-1);//Rotate Gltranslatef (38000, 0, 0);//Translate Glutsolidsphere (4345, 20, 20);//Render a sphere Glflus h ();//empties the buffer, immediately executes the draw command Glutswapbuffers ();//double buffering when swapping two buffer pointers}void idle () {++day;if (Day > Total) {day = 0;} Display ();//Call the Display method to force flush}int Main (int argc, char **argv) {glutinit (&AMP;ARGC, argv);// Initialize Glutglutinitdisplaymode (Glut_rgb | glut_double)///display mode (RGB color | double buffering) glutcreatewindow ("Earth,moon,sun");//Set Window caption Glutinitwindowsize (400, 400);// Set Window size glutdisplayfunc (display);//Set Drawing function Glutidlefunc (&idle); Glutmainloop ();//Turn on message loop return 0;}




Transformations in the four OpenGL

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.