Assignment 2 using OpenGL to draw Android robots

Source: Internet
Author: User
Tags cos set background sin

I. Implementation BRIEF

Assignment 2 Report

Goal: Draw an Android robot.

Code structure: The parameter Drawrobot function in Glutdisplayfunc (Drawrobot) is the outermost function of the robot that implements the painting, including drawing faces, drawing bodies, convey, and drawing feet. Each function continues to subdivide the steps and methods of the drawing. As shown in the following:

Two. Experience
This mission gave me an initial understanding of OpenGl. By reading textbooks and online information, master

Use the basic features of OpenGl, such as how to draw basic points, lines, and polygons. and understand some important concepts that have not been understood before, such as the difference between glloadidentity and Glpushmatrix and Glpopmatrix. According to my understanding, glloadidentity is to set the matrix in the stack top of the matrix to the unit matrix, which is equivalent to the initial setting of the origin of the coordinates, and ignores the change of the position of the coordinates origin, such as translation and rotation. Glpushmaxtrix and Glpopmatrix are used to press the current matrix into the stack, and then perform some transformations, based on the previously transformed coordinates, and finally out of the stack, back to the coordinates before the stack.

Next I will continue to learn more about OpenGL's powerful features.

The code is as follows:
#include <gl/glut.h>#include<math.h>inti;ConstGlfloat PI =3.1415926536f;Const intn = +;ConstGlfloat Eyer =0.025f;//radius of eyeConstGlfloat Facer =0.3f;//radius of FaceConstGlfloat Bodyr =0.05f;//radius of the corner of bodyConstGlfloat LIMBR =0.06f;//radius of limb in both endsvoidDRAWLIMB (void) {    //draw a half circle at one endGlbegin (Gl_polygon);  for(i =0; I < n; i++) {glvertex2f (limbr* COS (2* pi/n * i), limbr * sin (2* pi/n *i));        } glend (); //Draw a rectangleGltranslatef (-0.06f,0.0f,0.0f); GLRECTF (0.0f,0.0f,0.12f, -0.3f);        Glend (); //draw a half circle at the other endGltranslatef (0.06f, -0.3f,0.0f);    Glbegin (Gl_polygon);  for(i =0; I < n; i++) {glvertex2f (limbr* COS (2* pi/n * i), limbr * sin (2* pi/n *i));    } glend (); Glflush ();}voidDrawhands (void) {glloadidentity (); Gltranslatef (-0.38f,0.2f,0.0f);    Drawlimb ();    Glloadidentity (); Gltranslatef (0.38f,0.2f,0.0f); Drawlimb ();}voidDrawbody (void) {glloadidentity (); //draw a rectangle firstGltranslatef (-0.3f,0.25f,0.0f); GLRECTF (0.0f,0.0f,0.6f, -0.45f);        Glend (); //draw a half circle at the left bottomGltranslatef (0.05f, -0.45f,0.0f);    Glbegin (Gl_polygon);  for(i =0; I < n; i++) {        if(Sin (2* pi/n * i) <0) {glvertex2f (Bodyr* COS (2* pi/n * i), Bodyr * sin (2* pi/n *i));        }} glend (); //draw a half circle at the right bottomGltranslatef (0.5f,0.0f,0.0f);    Glbegin (Gl_polygon);  for(i =0; I < n; i++) {        if(Sin (2* pi/n * i) <0) {glvertex2f (Bodyr* COS (2* pi/n * i), Bodyr * sin (2* pi/n *i));        }} glend (); //draw the bottom rectangleGLRECTF (0.0f,0.0f, -0.5f, -0.05f);        Glend (); Glflush ();}voidDrawlegs (void) {glloadidentity (); Gltranslatef (-0.11f, -0.08f,0.0f);        Drawlimb ();    Glloadidentity (); Gltranslatef (0.11f, -0.08f,0.0f);    Drawlimb (); }voidDrawLine (void) {gllinewidth (5.0f);    Glbegin (Gl_lines); GLVERTEX2F (0.0f,0.0f); GLVERTEX2F (0.0f,0.15f);        Glend (); Glflush ();}voidDrawEye (void) {glcolor3f (1.0f,1.0f,1.0f);    Glbegin (Gl_polygon);  for(i =0; I < n; i++) {glvertex2f (Eyer* COS (2* pi/n * i), Eyer * sin (2* pi/n *i));    } glend (); Glcolor3ub (164,202, $); Glflush ();}voidDrawfaceshape (void) {glbegin (Gl_polygon);  for(i =0; I < n; i++) {        if(Sin (2* pi/n * i) >=0) {glvertex2f (Facer* COS (2* pi/n * i), facer * sin (2* pi/n *i));    }} glend (); Glflush ();}voidDrawface (void) {glloadidentity (); Gltranslatef (0.0f,0.27f,0.0f); Drawfaceshape ();//draw the overall shape of the face//Draw Left eyeglloadidentity (); Gltranslatef (-0.13f,0.42f,0.0f);        DrawEye (); //Draw Right eyeglloadidentity (); Gltranslatef (0.13f,0.42f,0.0f);        DrawEye (); //Draw Left lineglloadidentity (); Gltranslatef (-0.13f,0.5f,0.0f); Glrotated (30.0f,0.0f,0.0f,1.0f);        DrawLine (); //Draw Right Lineglloadidentity (); Gltranslatef (0.13f,0.5f,0.0f); Glrotated (-30.0f,0.0f,0.0f,1.0f);        DrawLine (); }voidDrawrobot (void) {Glclearcolor (1.0f,1.0f,1.0f,0.0f);//Set Background color to whiteglclear (gl_color_buffer_bit);    Glloadidentity (); Glcolor3ub (164,202, $);//Color of RobotDrawface ();    Drawbody ();    Drawhands (); Drawlegs ();}intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_rgb|glut_single); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Android Robot");    Glutdisplayfunc (Drawrobot);        Glutmainloop (); return 0;}

Assignment 2 using OpenGL to draw Android robots

Related Article

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.