Thursday, May 15, 2014

Opengl,C++ : Display Various glu Objects

1:  /* displays various glu objects */  
2:  #include <stdlib.h>  
3:  #include <GL/glut.h>  
4:  GLUquadricObj *obj;  
5:  static GLfloat theta[] = {0.0,0.0,0.0};  
6:  static GLint axis = 2;  
7:  void init()  
8:  {  
9:       glClearColor(1.0, 1.0, 1.0, 1.0);  
10:       glColor3f(1.0, 0.0, 0.0);  
11:       obj = gluNewQuadric(); //Create quadrics to draw simple geometric shapes  
12:       gluQuadricDrawStyle(obj, GLU_LINE); //Draw the shapes using different OpenGL primitives  
13:       //GLU_LINE     Quadrics are drawn “wireframe,” using line primitives.  
14:  }  
15:  void display()  
16:  {  
17:       /* display callback, clear frame buffer and z buffer,  
18:       rotate object and draw, swap buffers */  
19:       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
20:       glLoadIdentity();//Reset the drawing perspective  
21:       glRotatef(theta[0], 1.0, 0.0, 0.0);  
22:       glRotatef(theta[1], 0.0, 1.0, 0.0);  
23:       glRotatef(theta[2], 0.0, 0.0, 1.0);  
24:       gluDisk(obj, 0.5, 1.0, 10, 10);  
25:    // GLUquadric*      quad, GLdouble      inner,GLdouble      outer,GLint slices, GLint loops)  
26:  //quad== Specifies the quadrics object (created with gluNewQuadric).  
27:  //inner== Specifies the inner radius of the disk (may be 0).  
28:  //outer== Specifies the outer radius of the disk.  
29:  //slices== Specifies the number of subdivisions around the z axis.  
30:  //loops== Specifies the number of concentric rings about the origin into which the disk is subdivided.  
31:       //glutWirelcosahedron();  
32:       //glutWireDodecahedron();  
33:       //gluSphere(obj, 1.0, 12, 12);  
34:       //gluCylinder(obj,1.0,0.5,1.0,12,12);  
35:       //gluPartialDisk(obj,0.5,1.0,10,10,0.0,45.0);  
36:       //glutWireTeapot(1.0);  
37:       //glutWireTorus(0.5,1.0,10,10);  
38:       //glutWireCone(1.0,1.0,10,10);  
39:       glutSwapBuffers();  
40:  }  
41:  void spinObject()  
42:  {  
43:       /* Idle callback, spin cube 2 degrees about selected axis */  
44:       theta[axis] += 0.01;  
45:       if( theta[axis] > 360.0 ) theta[axis] -= 360.0;  
46:       glutPostRedisplay();  
47:  }  
48:  void mouse(int btn, int state, int x, int y)  
49:  {  
50:       /* mouse callback, selects an axis about which to rotate */  
51:       if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;  
52:       if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;  
53:       if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;  
54:  }  
55:  void myReshape(int w, int h)  
56:  {  
57:    glViewport(0, 0, w, h);  
58:    glMatrixMode(GL_PROJECTION);  
59:    glLoadIdentity();  
60:    if (w <= h)  
61:      glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,  
62:        2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);  
63:    else  
64:      glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,  
65:        2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);  
66:    glMatrixMode(GL_MODELVIEW);  
67:  }  
68:  void key(unsigned char key, int x, int y)  
69:  {  
70:       if(key=='x') glutIdleFunc(NULL);  
71:       if(key=='s') glutIdleFunc(spinObject);  
72:  }  
73:  void main(int argc, char **argv)  
74:  {  
75:    glutInit(&argc, argv);  
76:    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
77:    glutInitWindowSize(500, 500);  
78:    glutCreateWindow("object");  
79:       init();  
80:    glutReshapeFunc(myReshape);  
81:    glutDisplayFunc(display);  
82:       glutIdleFunc(NULL);  
83:       glutMouseFunc(mouse);  
84:       glutKeyboardFunc(key);  
85:       glutMainLoop();  
86:  }  

No comments:

Post a Comment