Sunday, July 27, 2014

OpenGl - Displays a BARN shape on the XY plane.



Source Code:
1:  #include <windows.h>  
2:  #include <gl/Gl.h>  
3:  #include <gl/glut.h>  
4:  #include <cmath>   
5:    
6:  int WinWidth = 500, WinHeight = 500;  
7:    
8:  typedef struct{float x;float y;}Point2D;  
9:  typedef struct{float x;float y;float z;}Point3D;  
10:    
11:    
12:  Point3D COP = {100,100,-200};  
13:    
14:  Point3D A,B,C,D,E,F,G,H,I,J;     // Vertices of the BARN  
15:    
16:  void InitBarn(){  
17:       A.x = 200; A.y = 100; A.z = 200;  
18:       B.x = 200; B.y = 00; B.z = 200;  
19:       C.x = 400; C.y = 00; C.z = 200;  
20:       D.x = 400; D.y = 100; D.z = 200;  
21:       E.x = 300; E.y = 200; E.z = 200;  
22:    
23:       F.x = 200; F.y = 100; F.z = 400;  
24:       G.x = 200; G.y = 00; G.z = 400;  
25:       H.x = 400; H.y = 00; H.z = 400;  
26:       I.x = 400; I.y = 100; I.z = 400;  
27:       J.x = 300; J.y = 200; J.z = 400;  
28:  }  
29:    
30:  void constructBarn(Point2D A, Point2D B, Point2D C, Point2D D, Point2D E, Point2D F, Point2D G, Point2D H,Point2D I,Point2D J){  
31:       glPointSize(1.0);  
32:    
33:       glBegin(GL_LINE_LOOP);       
34:       //DRAWING FRONT FACE  
35:       glVertex2i(A.x, A.y);  
36:       glVertex2i(B.x, B.y);  
37:       glVertex2i(C.x, C.y);  
38:       glVertex2i(D.x, D.y);  
39:       glVertex2i(E.x, E.y);  
40:    
41:       glEnd();  
42:    
43:       glBegin(GL_LINE_LOOP);  
44:       //DRAWING BACK FACE  
45:       glVertex2i(F.x, F.y);  
46:       glVertex2i(G.x, G.y);  
47:       glVertex2i(H.x, H.y);  
48:       glVertex2i(I.x, I.y);  
49:       glVertex2i(J.x, J.y);  
50:    
51:       glEnd();  
52:         
53:       glBegin(GL_LINES);  
54:    
55:       //DRAWING OTHER LINES  
56:       glVertex2i(A.x, A.y);  
57:       glVertex2i(F.x, F.y);  
58:    
59:       glVertex2i(B.x, B.y);  
60:       glVertex2i(G.x, G.y);  
61:    
62:       glVertex2i(C.x, C.y);  
63:       glVertex2i(H.x, H.y);  
64:    
65:       glVertex2i(D.x, D.y);  
66:       glVertex2i(I.x, I.y);  
67:    
68:       glVertex2i(E.x, E.y);  
69:       glVertex2i(J.x, J.y);  
70:    
71:       glEnd();  
72:    
73:       glFlush();  
74:    
75:  }  
76:    
77:  Point2D Project_Perspective(Point3D p, Point3D CoP){  
78:       Point2D p2;  
79:       float d = abs(CoP.z);     // The distance between the COP and orign  
80:         
81:        p2.x=(d*p.x)/(p.z+d);   
82:     p2.y=(d*p.y)/(p.z+d);  
83:         
84:       return p2;  
85:  }  
86:    
87:  Point2D translate2D(Point2D p, float tx, float ty){  
88:    Point2D tp2;  
89:       tp2.x=p.x+tx;  
90:       tp2.y=p.y+ty;  
91:         
92:       return tp2;  
93:  }   
94:    
95:  Point3D translate3D(Point3D p, float tx, float ty, float tz){  
96:    Point3D tp3;  
97:       tp3.x=p.x+tx;  
98:       tp3.y=p.y+ty;  
99:       tp3.z=p.z+tz;  
100:    
101:       return tp3;  
102:  }   
103:    
104:  void keyboard(unsigned char key, int x, int y)  
105:  {  
106:    if (key == 'q' || key == 'Q')  
107:      exit(EXIT_SUCCESS);  
108:  }  
109:    
110:  void myMouse(int button, int state, int x, int y) {  
111:       if(state == GLUT_DOWN)   
112:       {  
113:            if(button == GLUT_LEFT_BUTTON)   
114:            {  
115:                    
116:            }  
117:            else if (button == GLUT_RIGHT_BUTTON)   
118:            {  
119:                    
120:            }  
121:       }  
122:  }  
123:    
124:  void myDisplay()  
125:  {  
126:       glClearColor(0.2f, 0.3f, 0.1f, 0.0f);   
127:       glClear(GL_COLOR_BUFFER_BIT);  
128:       glColor3f(1.0f, 0.5f, 0.8f);  
129:    
130:             Point2D Projected_A=Project_Perspective(A,COP);       
131:            Point2D Projected_B=Project_Perspective(B,COP);  
132:            Point2D Projected_C=Project_Perspective(C,COP);  
133:            Point2D Projected_D=Project_Perspective(D,COP);  
134:            Point2D Projected_E=Project_Perspective(E,COP);  
135:            Point2D Projected_F=Project_Perspective(F,COP);  
136:            Point2D Projected_G=Project_Perspective(G,COP);  
137:            Point2D Projected_H=Project_Perspective(H,COP);  
138:            Point2D Projected_I=Project_Perspective(I,COP);  
139:            Point2D Projected_J=Project_Perspective(J,COP);  
140:         
141:       constructBarn(  
142:            Projected_A,  
143:            Projected_B,  
144:            Projected_C,  
145:            Projected_D,  
146:            Projected_E,  
147:            Projected_F,  
148:            Projected_G,  
149:            Projected_H,  
150:            Projected_I,  
151:            Projected_J  
152:       );  
153:  }  
154:    
155:    
156:  int main( int argc, char ** argv ) {  
157:       glutInit( &argc, argv );  
158:       glutInitWindowPosition( 0, 0 );  
159:       glutInitWindowSize( WinWidth, WinHeight );  
160:       glutCreateWindow( "Projection of a BARN" );  
161:    
162:       glMatrixMode( GL_PROJECTION );   
163:       glLoadIdentity();  
164:       gluOrtho2D( 0, WinWidth, 0, WinHeight );  
165:       glViewport(0, 0, WinWidth, WinHeight);  
166:    
167:       InitBarn();  
168:    
169:       glutKeyboardFunc(keyboard);   
170:       glutMouseFunc( myMouse );  
171:       glutDisplayFunc( myDisplay );  
172:       glutMainLoop();  
173:    
174:       return( 0 );  
175:  }