3D Rotation using OpenGL in C++


3D Roataion in OpenGL in C++
Code Updated : 25/07/2014 @ 06:16 AM
There are small changes occur...



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath> 

typedef struct{float x;float y;}Point2D;
typedef struct{float x;float y;float z;}Point3D;


Point3D A,B,C,D,E,F,G,H; 
Point2D Projected_A,Projected_B,Projected_C,Projected_D,Projected_E,Projected_F,Projected_G,Projected_H;

GLfloat v_list[8][3];  
GLint f_list[6][4]; 

void init(void)
{
 
 v_list[0][0] = 100;
 v_list[0][1] = 100;
 v_list[0][2] = 100;

 v_list[1][0] = 300;
 v_list[1][1] = 100;
 v_list[1][2] = 100;

 v_list[2][0] = 300;
 v_list[2][1] = 300;
 v_list[2][2] = 100;

 v_list[3][0] = 100;
 v_list[3][1] = 300;
 v_list[3][2] = 100;

 v_list[4][0] = 100;
 v_list[4][1] = 100;
 v_list[4][2] = 300;

 v_list[5][0] = 300;
 v_list[5][1] = 100;
 v_list[5][2] = 300;

 v_list[6][0] = 300;
 v_list[6][1] = 300;
 v_list[6][2] = 300;

 v_list[7][0] = 100;
 v_list[7][1] = 300;
 v_list[7][2] = 300;







}

void rotatebox_x(float deg_x){  
 float ang = deg_x * 3.14159265359 / 180.0;  

 for (int i = 0; i <= 7; i++) {  
  v_list[i][0] = v_list[i][0];
  v_list[i][2] = v_list[i][2]*cos(ang) - v_list[i][1]*sin(ang);
  v_list[i][1] = v_list[i][2]*sin(ang) + v_list[i][1]*cos(ang);
 }
 glutPostRedisplay(); 
}

void rotatebox_y(float deg_y){  
 float ang = deg_y * 3.14159265359 / 180.0;

 for (int i = 0; i <= 7; i++) {  
  v_list[i][1] = v_list[i][1];
  v_list[i][0] = v_list[i][0]*cos(ang) - v_list[i][2]*sin(ang);
  v_list[i][2] = v_list[i][0]*sin(ang) + v_list[i][2]*cos(ang);
 }
 glutPostRedisplay(); 
}

Point2D Project_ortho(float x, float y, float z){ 
 Point2D p0;
 p0.x = x ;
 p0.y = y ;
 return p0;
}

void drawBox(Point2D A, Point2D B, Point2D C, Point2D D, Point2D E, Point2D F, Point2D G, Point2D H)
{
 
 glPointSize(1.0);

 glBegin(GL_LINE_LOOP);
 glVertex2i(A.x, A.y); glVertex2i(B.x, B.y); glVertex2i(C.x, C.y); glVertex2i(D.x, D.y);
 glEnd();

 glBegin(GL_LINE_LOOP);
 
  glVertex2i(E.x, E.y); glVertex2i(F.x, F.y); glVertex2i(G.x, G.y); glVertex2i(H.x, H.y);
 glEnd();

 glBegin(GL_LINES);
 
  glVertex2i(A.x, A.y); glVertex2i(E.x, E.y);

  glVertex2i(B.x, B.y); glVertex2i(F.x, F.y);

  glVertex2i(C.x, C.y); glVertex2i(G.x, G.y);

  glVertex2i(D.x, D.y); glVertex2i(H.x, H.y);
 glEnd();

 glFlush();
}

void draw_Cube(){
 Projected_A = Project_ortho(v_list[0][0],v_list[0][1],v_list[0][1]);
 Projected_B = Project_ortho(v_list[1][0],v_list[1][1],v_list[1][1]);
 Projected_C = Project_ortho(v_list[2][0],v_list[2][1],v_list[2][1]);
 Projected_D = Project_ortho(v_list[3][0],v_list[3][1],v_list[3][1]);
 Projected_E = Project_ortho(v_list[4][0],v_list[4][1],v_list[4][1]);
 Projected_F = Project_ortho(v_list[5][0],v_list[5][1],v_list[5][1]);
 Projected_G = Project_ortho(v_list[6][0],v_list[6][1],v_list[6][1]);
 Projected_H = Project_ortho(v_list[7][0],v_list[7][1],v_list[7][1]);

 drawBox(Projected_A,Projected_B,Projected_C,Projected_D,Projected_E,Projected_F,Projected_G,Projected_H);

}

void myDisplay()
{
 glClearColor(1.0f, 1.0f, 1.0f, 0.0f); 
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1.0f, 0.0f, 0.0f);
 draw_Cube();
}


void keyboard(unsigned char key, int x, int y)
{
 if (key == 'q' || key == 'Q')
  exit(EXIT_SUCCESS);
}

void special(int key, int, int) {
 switch (key) {
 case GLUT_KEY_LEFT: rotatebox_y(-1); break; 
 case GLUT_KEY_RIGHT: rotatebox_y(1);break; 
 case GLUT_KEY_UP: rotatebox_x(-1);break; 
 case GLUT_KEY_DOWN: rotatebox_x(1);break; 
 default: return;
 }
}

int main( int argc, char ** argv ) {
 glutInit( &argc, argv );
 glutInitWindowPosition( 0, 0 );
 glutInitWindowSize( 800, 600 );
 glutCreateWindow( "3D Transformations" );

 glMatrixMode( GL_PROJECTION ); 
 glLoadIdentity();
 gluOrtho2D( 0, 800, 0, 600 );
 glViewport(0, 0, 800, 600);

 init();

 glutKeyboardFunc(keyboard); 
 glutSpecialFunc(special);
 glutDisplayFunc( myDisplay );
 glutMainLoop();

 return( 0 );
}

Comments

Popular posts from this blog

Missionaries & Canibal Problem in AI using Pro Log

Hide the navigation bar in jHipster Angular 2

Spring Boot - No Need to Restart the Development Server for Each Time