Experiment 3 OpenGL Geometric transformations

Source: Internet
Author: User
Tags translate function

Transferred from: http://www.cnblogs.com/opengl/archive/2012/10/30/2747130.html

1 . Experimental Purpose:

Understand how an OpenGL program translates, rotates, and scales transformations.

2 . Experimental content:

(1) Reading the experiment principle, running the demonstration experiment code, mastering the OpenGL program translation, rotation, zoom transformation method;

(2) Try to complete the experiment work according to the model Code;

3 . Experimental principle:

(1) The geometric transformation under OpenGL

In the core library of OpenGL, each geometric transformation has a separate function, and all transformations are defined in three-dimensional space.

The translation matrix constructor is gltranslate<f,d> (TX, Ty, TZ), which multiplies the current matrix and a matrix representing the moving object. TX, Ty,tz Specifies the matrix of the moving object, which can be any real value, suffix f (single-precision floating-point float), or D (double-precision floating-point double), tz=0.0 for two-dimensional applications.

The rotation matrix constructor is glrotate<f,d> (Theta, VX, VY, VZ), which multiplies the current matrix and a matrix that represents a rotating object. Theta, VX, VY, VZ specifies the matrix of the rotating object, and the object will rotate counterclockwise around (0,0,0) to (x, y, z) lines, and the parameter theta represents the angle of rotation. The component of the vector v= (VX, VY,VZ) can be any real value that defines the direction of the axis of rotation through the origin of the coordinates, the suffix F (single-precision floating-point float), or D (double-precision floating-point double), vx=0.0,vy=0.0,vz=1.0 for two-dimensional rotation.

The scaling matrix constructor is glscale<f,d> (SX, SY, SZ), which multiplies the current matrix and a matrix that represents the scaling object. SX, SY,SZ specifies the matrix of the scaled object, representing the scale in the x, Y, z direction, which can be any real value, and when the scaling parameter is negative, the function is the reflection matrix and the scaling is relative to the origin. The suffix is f (single-precision floating-point float) or D (double-precision floating-point double).

Note that this is all about "multiplying the current matrix with a matrix that represents moving < rotating, scaling > objects", rather than saying "This function is spinning" or "This function is moving," for a reason, and it will be immediately mentioned.

Suppose the current matrix is a unit matrix, then multiply by a matrix R representing rotation, multiplied by a matrix t representing the move, and the resulting matrix is multiplied by the coordinate matrix V of each vertex. So, the vertex coordinates that have been transformed are ((RT) v). Because matrix multiplication satisfies the binding rate ((RT) v) = R (Tv)), in other words, it is actually moved first and then rotated. That is, the order of the actual transformations is reversed from the order in which the code is written. Since the results of "move after first" and "move after first" are likely to be different, you need to pay special attention to this when you are beginners.

(2) Introduction to various transformations under OpenGL

We live in a three-dimensional world-if we want to observe an object, we can:

1, from the different positions to observe it (People movement, select a location to see). (View transform)

2, move or rotate it, of course, if it is only the object inside the computer, we can also enlarge or reduce it (object movement, let people look at its different parts). (Model Transformation)

3, if the object is drawn down, we can choose: whether a "near-large far small" perspective effect is required. In addition, we may only want to see part of the object, not all (the range specified). (Projection transformation)

4. We may want to draw the entire picture, but it occupies only a portion of the paper, not all of it (specified in the display window's location). (Viewport transform)

All of this can be achieved in OpenGL.

From the point of view of "relative movement", it is equivalent to change the position and direction of the observer and change the position and direction of the object itself. In OpenGL, these two functions are implemented even using the same functions.

Since both the model and the view transform are implemented by matrix operations, the matrix of the current operation should be set as the Model view matrix before the transformation. The method to set is to call the Glmatrixmode function as a gl_modelview parameter, like this:

Glmatrixmode (Gl_modelview);

The statement specifies a 4x4 modeling matrix as the current matrix.

In general, we need to set the current matrix to the unit matrix before making the transformation. The function that sets the current matrix to the unit matrix is:

Glloadidentity ();

When we are doing a matrix operation, it is possible to save a matrix before recovering it over a period of time. When we need to save, call the Glpushmatrix () function, which is equivalent to pressing the current matrix onto the stack. When you need to restore the last save, call the Glpopmatrix () function, which is equivalent to ejecting a matrix from the top of the stack stack to the current matrix. OpenGL specifies that the stack capacity can hold at least 32 matrices, and in some OpenGL implementations, the stack capacity actually exceeds 32. So don't worry too much about the capacity of the matrix.

Usually, it is more convenient and quicker to use this kind of recovery after saving than to change and reverse the transformation first.

Note: Both the Model View matrix and the projection matrix have a corresponding stack. Use Glmatrixmode to specify whether the current operation is a model view matrix or a projection matrix.

4. Demo Code:

(1) , Translate Example

#include <GL/glut.h>

void init (void)

{

Glclearcolor (1.0, 1.0, 1.0, 0.0);

Glmatrixmode (gl_projection);

GLUORTHO2D (-5.0, 5.0,-5.0, 5.0); The set display range is x:-5.0~5.0, y:-5.0~5.0

Glmatrixmode (Gl_modelview);

}

void Drawsquare (void)//Draw Center at Origin, square with side length 2

{

Glbegin (Gl_polygon); Vertex specifies that a counterclockwise direction is required

GLVERTEX2F ( -1.0f,-1.0f);//lower left point

GLVERTEX2F (1.0f,-1.0f);//Lower Right point

GLVERTEX2F (1.0f, 1.0f);//upper Right Point

GLVERTEX2F ( -1.0f,1.0f);//upper left point

Glend ();

}

void MyDraw1 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Gltranslatef (0.0,-3.0,0.0); Then move down 3 units

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void myDraw2 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Glpushmatrix ();

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Glpopmatrix ();

Gltranslatef (2.0,0.0,0.0); Then move right 2 units

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void Main (int argc, char** argv)

{

Glutinit (&ARGC, argv);

Glutinitdisplaymode (Glut_single | GLUT_RGB);

Glutinitwindowposition (0, 0);

Glutinitwindowsize (600, 600);

Glutcreatewindow ("Translate function example");

Init ();

Glutdisplayfunc (MYDRAW1);

Glutmainloop ();

}

To generate a graph:

Note: MyDraw1 () and MYDRAW2 () produce exactly the same graphics, why?

(2) , Rotate Example

#include <GL/glut.h>

void init (void)

{

Glclearcolor (1.0, 1.0, 1.0, 0.0);

Glmatrixmode (gl_projection);

GLUORTHO2D (-5.0, 5.0,-5.0, 5.0); The set display range is x:-5.0~5.0, y:-5.0~5.0

Glmatrixmode (Gl_modelview);

}

void Drawsquare (void)//Draw Center at Origin, square with side length 2

{

Glbegin (Gl_polygon); Vertex specifies that a counterclockwise direction is required

GLVERTEX2F ( -1.0f,-1.0f);//lower left point

GLVERTEX2F (1.0f,-1.0f);//Lower Right point

GLVERTEX2F (1.0f, 1.0f);//upper Right Point

GLVERTEX2F ( -1.0f,1.0f);//upper left point

Glend ();

}

void MyDraw1 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

Glrotatef (30,0.0,0.0,1.0); Rotate Clockwise 30 angle

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Glloadidentity (); Set the current matrix as the unit matrix

Gltranslatef ( -2.0,-3.0,0.0); Move 2 units to the left and move down 3 units

Glrotatef ( -30,0.0,0.0,1.0); Rotate Counterclockwise 30 angle

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void myDraw2 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Glpushmatrix (); Press the current matrix into the stack

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

Glrotatef (30,0.0,0.0,1.0); Rotate Clockwise 30 angle

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Glpopmatrix (); POPs a matrix from the top of the stack stack to the current matrix

Gltranslatef ( -2.0,-3.0,0.0); Move 2 units to the left and move down 3 units

Glrotatef ( -30,0.0,0.0,1.0); Rotate Counterclockwise 30 angle

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void Main (int argc, char** argv)

{

Glutinit (&ARGC, argv);

Glutinitdisplaymode (Glut_single | GLUT_RGB);

Glutinitwindowposition (0, 0);

Glutinitwindowsize (600, 600);

Glutcreatewindow ("Rotate function example");

Init ();

Glutdisplayfunc (MYDRAW1);

Glutmainloop ();

}

To generate a graph:

Note: MyDraw1 () and MYDRAW2 () produce exactly the same graphics, why?

(3) , Scale Example

#include <GL/glut.h>

void init (void)

{

Glclearcolor (1.0, 1.0, 1.0, 0.0);

Glmatrixmode (gl_projection);

GLUORTHO2D (-5.0, 5.0,-5.0, 5.0); The set display range is x:-5.0~5.0, y:-5.0~5.0

Glmatrixmode (Gl_modelview);

}

void Drawsquare (void)//Draw Center at Origin, square with side length 2

{

Glbegin (Gl_polygon); Vertex specifies that a counterclockwise direction is required

GLVERTEX2F ( -1.0f,-1.0f);//lower left point

GLVERTEX2F (1.0f,-1.0f);//Lower Right point

GLVERTEX2F (1.0f, 1.0f);//upper Right Point

GLVERTEX2F ( -1.0f,1.0f);//upper left point

Glend ();

}

void MyDraw1 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

Glscalef (1.0,1.5,1.0); The x and z directions remain the same, and the y direction is 1.5 times times greater.

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Glloadidentity (); Set the current matrix as the unit matrix

Gltranslatef ( -2.0,-3.0,0.0); Move 2 units to the left and move down 3 units

Glscalef (0.5,1.5,1.0); The z-direction remains the same, the X-direction is reduced to 0.5 times times the original, and the Y-direction is 1.5 times times the original.

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void myDraw2 (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Draw an edge length of 2 red squares at the origin point

Glpushmatrix (); Press the current matrix into the stack

Gltranslatef (2.0,3.0,0.0); Move 2 units to the right and move up 3 units

Glscalef (1.0,1.5,1.0); The x and z directions remain the same, and the y direction is 1.5 times times greater.

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Draw an edge length of 2 green squares

Glpopmatrix (); POPs a matrix from the top of the stack stack to the current matrix

Gltranslatef ( -2.0,-3.0,0.0); Move 2 units to the left and move down 3 units

Glscalef (0.5,1.5,1.0); The z-direction remains the same, the X-direction is reduced to 0.5 times times the original, and the Y-direction is 1.5 times times the original.

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Draw an edge length of 2 blue squares

Glflush ();

}

void Main (int argc, char** argv)

{

Glutinit (&ARGC, argv);

Glutinitdisplaymode (Glut_single | GLUT_RGB);

Glutinitwindowposition (0, 0);

Glutinitwindowsize (600, 600);

Glutcreatewindow ("scale function example");

Init ();

Glutdisplayfunc (MYDRAW1);

Glutmainloop ();

}

To generate a graph:

Note: MyDraw1 () and MYDRAW2 () produce exactly the same graphics, why?

(4) , synthetic examples

#include <GL/glut.h>

void init (void)

{

Glclearcolor (1.0, 1.0, 1.0, 0.0);

Glmatrixmode (gl_projection);

GLUORTHO2D (-5.0, 5.0,-5.0, 5.0); The set display range is x:-5.0~5.0, y:-5.0~5.0

Glmatrixmode (Gl_modelview);

}

void Drawsquare (void)//Draw Center at Origin, square with side length 2

{

Glbegin (Gl_polygon); Vertex specifies that a counterclockwise direction is required

GLVERTEX2F ( -1.0f,-1.0f);//lower left point

GLVERTEX2F (1.0f,-1.0f);//Lower Right point

GLVERTEX2F (1.0f, 1.0f);//upper Right Point

GLVERTEX2F ( -1.0f,1.0f);//upper left point

Glend ();

}

void Mydraw (void)

{

Glclear (Gl_color_buffer_bit); Empty

Glloadidentity (); Set the current matrix as the unit matrix

Glpushmatrix ();

Gltranslatef (0.0f,2.0f,0.0f);

Glscalef (3.0,0.5,1.0);

GLCOLOR3F (1.0, 0.0, 0.0);

Drawsquare (); Upper Red Rectangle

Glpopmatrix ();

Glpushmatrix ();

Gltranslatef ( -3.0,0.0,0.0);

Glpushmatrix ();

Glrotatef (45.0,0.0,0.0,1.0);

glcolor3f (0.0, 1.0, 0.0);

Drawsquare (); Middle left Diamond

Glpopmatrix ();

Gltranslatef (3.0,0.0,0.0);

Glpushmatrix ();

Glrotatef (45.0,0.0,0.0,1.0);

glcolor3f (0.0, 0.7, 0.0);

Drawsquare (); Middle Diamond

Glpopmatrix ();

Gltranslatef (3.0,0.0,0.0);

Glpushmatrix ();

Glrotatef (45.0,0.0,0.0,1.0);

glcolor3f (0.0, 0.4, 0.0);

Drawsquare (); Middle Right Diamond

Glpopmatrix ();

Glpopmatrix ();

Gltranslatef (0.0,-3.0,0.0);

Glscalef (4.0,1.5,1.0);

glcolor3f (0.0, 0.0, 1.0);

Drawsquare (); Below the blue rectangle

Glflush ();

}

void Main (int argc, char** argv)

{

Glutinit (&ARGC, argv);

Glutinitdisplaymode (Glut_single | GLUT_RGB);

Glutinitwindowposition (0, 0);

Glutinitwindowsize (600, 600);

Glutcreatewindow ("Geometric transformation function synthesis example");

Init ();

Glutdisplayfunc (Mydraw);

Glutmainloop ();

}

To generate a graph:

5. Lab work:

Draw as Shape:

Tips:

(1) write a diamond-drawn function drawdiamond (void);

void Drawdiamond (void)//Draw Center diamond at Origin

{

Glbegin (Gl_polygon); Vertex specifies that a counterclockwise direction is required

GLVERTEX2F (0.0f,-1.0f);//Next point

GLVERTEX2F (2.0f,0.0f);//Right Point

GLVERTEX2F (0.0f, 1.0f);//Upper Point

GLVERTEX2F ( -2.0f,0.0f);//Left Point

Glend ();

}

(2) A geometric transformation to draw three different positions, rotation angle, color diamond.

Attached: Glut installation package with batch installation: Http://files.cnblogs.com/opengl/glut-install.rar

Experiment 3 OpenGL Geometric transformations

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.