OpenGL에서 제공하는 glRotationf() 함수를 사용하지 않고 물체를 회전시켜라!
Computer_Graphics :
2007. 3. 22. 21:37
반응형
001: #include "GL/glut.h" 002: #include <stdio.h> 003: #include <math.h> 004: #define PI 3.14159265f 005: GLfloat point[8][3]={{50.0,50.0,50.0},//우상앞 0 006: {-50.0,50.0,50.0},//좌상앞 1 007: {-50.0,-50.0,50.0},//좌하앞 2 008: {50.0,-50.0,50.0},//우하앞 3 009: {50.0,50.0,-50.0},//우상뒤 4 010: {-50.0,50.0,-50.0},//좌상뒤 5 011: {-50.0,-50.0,-50.0},//좌하뒤 6 012: {50.0,-50.0,-50.0}};//우하뒤 7 013: const GLfloat rot = .1f; 014: 015: void myRotatef(char what) 016: { 017: int i; 018: 019: if (what=='x') //X축 회전 020: { 021: for(i=0;i<8;i++) 022: { 023: point[i][1]=point[i][1]*cos(rot)-point[i][2]*sin(rot); 024: point[i][2]=point[i][1]*sin(rot)+point[i][2]*cos(rot); 025: 026: } 027: }else if(what=='y') //Y축 회전 028: { 029: for(i=0;i<8;i++) 030: { 031: point[i][0]=point[i][0]*cos(rot)+point[i][2]*sin(rot); 032: point[i][2]=point[i][0]*-sin(rot)+point[i][2]*cos(rot); 033: } 034: }else if(what=='z') //Z 축 회전 035: { 036: for(i=0;i<8;i++) 037: { 038: point[i][0]=point[i][0]*cos(rot)+point[i][1]*-sin(rot); 039: point[i][1]=point[i][0]*sin(rot)+point[i][1]*cos(rot); 040: } 041: } 042: glutPostRedisplay(); 043: } 044: 045: void RenderScene(void) 046: { 047: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 048: glPushMatrix(); 049: 050: glFrontFace(GL_CCW); 051: glBegin(GL_QUADS); 052: // Front Face 053: // Cyan 054: glColor3ub((GLubyte) 0, (GLubyte)255, (GLubyte)255); 055: glVertex3f(point[0][0],point[0][1],point[0][2]); 056: glVertex3f(point[1][0],point[1][1],point[1][2]); 057: glVertex3f(point[2][0],point[2][1],point[2][2]); 058: glVertex3f(point[3][0],point[3][1],point[3][2]); 059: // Back Face 060: // Yellow 061: glColor3f(1.0f, 1.0f, 0.0f); 062: glVertex3f(point[4][0],point[4][1],point[4][2]); 063: glVertex3f(point[7][0],point[7][1],point[7][2]); 064: glVertex3f(point[6][0],point[6][1],point[6][2]); 065: glVertex3f(point[5][0],point[5][1],point[5][2]); 066: // Top Face 067: glColor3f(1.0f, 1.0f, 1.0f); 068: glVertex3f(point[0][0],point[0][1],point[0][2]); 069: glVertex3f(point[4][0],point[4][1],point[4][2]); 070: glVertex3f(point[5][0],point[5][1],point[5][2]); 071: glVertex3f(point[1][0],point[1][1],point[1][2]); 072: // Bottom Face 073: // Magenta 074: glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255); 075: glVertex3f(point[3][0],point[3][1],point[3][2]); 076: glVertex3f(point[2][0],point[2][1],point[2][2]); 077: glVertex3f(point[6][0],point[6][1],point[6][2]); 078: glVertex3f(point[7][0],point[7][1],point[7][2]); 079: // Left face 080: // Green 081: glColor3f(0.0f, 1.0f, 0.0f); 082: glVertex3f(point[1][0],point[1][1],point[1][2]); 083: glVertex3f(point[5][0],point[5][1],point[5][2]); 084: glVertex3f(point[6][0],point[6][1],point[6][2]); 085: glVertex3f(point[2][0],point[2][1],point[2][2]); 086: // Right face 087: glColor3f(0.0f, 0.0f, 1.0f); 088: glVertex3f(point[0][0],point[0][1],point[0][2]); 089: glVertex3f(point[3][0],point[3][1],point[3][2]); 090: glVertex3f(point[7][0],point[7][1],point[7][2]); 091: glVertex3f(point[4][0],point[4][1],point[4][2]); 092: 093: glEnd(); 094: glPopMatrix(); 095: glutSwapBuffers(); 096: } 097: void SetupRC() 098: { 099: glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); 100: glEnable(GL_DEPTH_TEST); 101: glShadeModel(GL_SMOOTH); 102: glEnable(GL_CULL_FACE); 103: } 104: void SpecialKeys(int key, int x, int y) //조작 105: { 106: if(key == GLUT_KEY_F1) 107: myRotatef('x'); 108: if(key == GLUT_KEY_F2) 109: myRotatef('y'); 110: if(key == GLUT_KEY_F3) 111: myRotatef('z'); 112: if(key == GLUT_KEY_F4) 113: exit(1); 114: } 115: void ChangeSize(int w, int h) 116: { 117: GLfloat fAspect; 118: if(h == 0) 119: h = 1; 120: glViewport(0, 0, w, h); 121: glMatrixMode(GL_PROJECTION); 122: glLoadIdentity(); 123: fAspect = (GLfloat)w / (GLfloat)h; 124: gluPerspective(35.0f, fAspect, 1.0f, 1000.0f); 125: glMatrixMode(GL_MODELVIEW); 126: glLoadIdentity(); 127: glTranslatef(0.0f, 0.0f, -400.0f); 128: } 129: int main(int argc, char* argv[]) 130: { 131: glutInit(&argc, argv); 132: glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 133: glutInitWindowSize(800,600); 134: glutCreateWindow("RGB Cube"); 135: glutReshapeFunc(ChangeSize); 136: glutSpecialFunc(SpecialKeys); 137: glutDisplayFunc(RenderScene); 138: SetupRC(); 139: glutMainLoop(); 140: 141: return 0; 142: }
왜 크기가 변할까.... 생각해보시오....
반응형
'Computer_Graphics' 카테고리의 다른 글
Jack In The Box (0) | 2007.04.14 |
---|---|
쿼터니언의 능력 (0) | 2007.03.22 |
쿼터니온을 사용해 오브젝트 회전시키기 (0) | 2007.03.22 |