루빅스 큐브 만들기2
2014. 12. 8. 20:42
개발/Open GL
반응형
개요
cube 객체를 생성하고 27개의 작은 정육면체로 큰 정육면체를 만들기
간단한 설명
클래스를 작성하여 반복문으로 각 위치를 설정해주고 정육면체를 그린다.
생각나는데로 만들어서 노가다성 코드가 많음.
좌표설정 및 칼라 설정
가장 간단한 노가다로 처리
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 | static GLfloat vertices[3][9][8][3] = { //Front { //Top { { -0.5, 0.5, 0.2 }, { -0.5, 0.5, 0.5 }, { -0.2, 0.5, 0.5 }, { -0.2, 0.5, 0.2 }, { -0.5, 0.2, 0.2 }, { -0.5, 0.2, 0.5 }, { -0.2, 0.2, 0.5 }, { -0.2, 0.2, 0.2 } }, { { -0.15, 0.5, 0.2 }, { -0.15, 0.5, 0.5 }, { 0.15, 0.5, 0.5 }, { 0.15, 0.5, 0.2 }, { -0.15, 0.2, 0.2 }, { -0.15, 0.2, 0.5 }, { 0.15, 0.2, 0.5 }, { 0.15, 0.2, 0.2 } }, { { 0.2, 0.5, 0.2 }, { 0.2, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.2 }, { 0.2, 0.2, 0.2 }, { 0.2, 0.2, 0.5 }, { 0.5, 0.2, 0.5 }, { 0.5, 0.2, 0.2 } }, //Mid { { -0.5, 0.5, -0.15 }, { -0.5, 0.5, 0.15 }, { -0.2, 0.5, 0.15 }, { -0.2, 0.5, -0.15 }, { -0.5, 0.2, -0.15 }, { -0.5, 0.2, 0.15 }, { -0.2, 0.2, 0.15 }, { -0.2, 0.2, -0.15 } }, { { -0.15, 0.5, -0.15 }, { -0.15, 0.5, 0.15 }, { 0.15, 0.5, 0.15 }, { 0.15, 0.5, -0.15 }, { -0.15, 0.2, -0.15 }, { -0.15, 0.2, 0.15 }, { 0.15, 0.2, 0.15 }, { 0.15, 0.2, -0.15 } }, { { 0.2, 0.5, -0.15 }, { 0.2, 0.5, 0.15 }, { 0.5, 0.5, 0.15 }, { 0.5, 0.5, -0.15 }, { 0.2, 0.2, -0.15 }, { 0.2, 0.2, 0.15 }, { 0.5, 0.2, 0.15 }, { 0.5, 0.2, -0.15 } }, //Bot { { -0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.2 }, { -0.2, 0.5, -0.2 }, { -0.2, 0.5, -0.5 }, { -0.5, 0.2, -0.5 }, { -0.5, 0.2, -0.2 }, { -0.2, 0.2, -0.2 }, { -0.2, 0.2, -0.5 } }, { { -0.15, 0.5, -0.5 }, { -0.15, 0.5, -0.2 }, { 0.15, 0.5, -0.2 }, { 0.15, 0.5, -0.5 }, { -0.15, 0.2, -0.5 }, { -0.15, 0.2, -0.2 }, { 0.15, 0.2, -0.2 }, { 0.15, 0.2, -0.5 } }, { { 0.2, 0.5, -0.5 }, { 0.2, 0.5, -0.2 }, { 0.5, 0.5, -0.2 }, { 0.5, 0.5, -0.5 }, { 0.2, 0.2, -0.5 }, { 0.2, 0.2, -0.2 }, { 0.5, 0.2, -0.2 }, { 0.5, 0.2, -0.5 } } }, //Middle { //Top { { -0.5, 0.15, 0.2 }, { -0.5, 0.15, 0.5 }, { -0.2, 0.15, 0.5 }, { -0.2, 0.15, 0.2 }, { -0.5, -0.15, 0.2 }, { -0.5, -0.15, 0.5 }, { -0.2, -0.15, 0.5 }, { -0.2, -0.15, 0.2 } }, { { -0.15, 0.15, 0.2 }, { -0.15, 0.15, 0.5 }, { 0.15, 0.15, 0.5 }, { 0.15, 0.15, 0.2 }, { -0.15, -0.15, 0.2 }, { -0.15, -0.15, 0.5 }, { 0.15, -0.15, 0.5 }, { 0.15, -0.15, 0.2 } }, { { 0.2, 0.15, 0.2 }, { 0.2, 0.15, 0.5 }, { 0.5, 0.15, 0.5 }, { 0.5, 0.15, 0.2 }, { 0.2, -0.15, 0.2 }, { 0.2, -0.15, 0.5 }, { 0.5, -0.15, 0.5 }, { 0.5, -0.15, 0.2 } }, //Mid { { -0.5, 0.15, -0.15 }, { -0.5, 0.15, 0.15 }, { -0.2, 0.15, 0.15 }, { -0.2, 0.15, -0.15 }, { -0.5, -0.15, -0.15 }, { -0.5, -0.15, 0.15 }, { -0.2, -0.15, 0.15 }, { -0.2, -0.15, -0.15 } }, { //Black One - Center Cube { -0.35, 0.35, -0.35 }, { -0.35, 0.35, 0.35 }, { 0.35, 0.35, 0.35 }, { 0.35, 0.35, -0.35 }, { -0.35, -0.35, -0.35 }, { -0.35, -0.35, 0.35 }, { 0.35, -0.35, 0.35 }, { 0.35, -0.35, -0.35 } }, { { 0.2, 0.15, -0.15 }, { 0.2, 0.15, 0.15 }, { 0.5, 0.15, 0.15 }, { 0.5, 0.15, -0.15 }, { 0.2, -0.15, -0.15 }, { 0.2, -0.15, 0.15 }, { 0.5, -0.15, 0.15 }, { 0.5, -0.15, -0.15 } }, //Bot { { -0.5, 0.15, -0.5 }, { -0.5, 0.15, -0.2 }, { -0.2, 0.15, -0.2 }, { -0.2, 0.15, -0.5 }, { -0.5, -0.15, -0.5 }, { -0.5, -0.15, -0.2 }, { -0.2, -0.15, -0.2 }, { -0.2, -0.15, -0.5 } }, { { -0.15, 0.15, -0.5 }, { -0.15, 0.15, -0.2 }, { 0.15, 0.15, -0.2 }, { 0.15, 0.15, -0.5 }, { -0.15, -0.15, -0.5 }, { -0.15, -0.15, -0.2 }, { 0.15, -0.15, -0.2 }, { 0.15, -0.15, -0.5 } }, { { 0.2, 0.15, -0.5 }, { 0.2, 0.15, -0.2 }, { 0.5, 0.15, -0.2 }, { 0.5, 0.15, -0.5 }, { 0.2, -0.15, -0.5 }, { 0.2, -0.15, -0.2 }, { 0.5, -0.15, -0.2 }, { 0.5, -0.15, -0.5 } } }, //Back { //Top { { -0.5, -0.2, 0.2 }, { -0.5, -0.2, 0.5 }, { -0.2, -0.2, 0.5 }, { -0.2, -0.2, 0.2 }, { -0.5, -0.5, 0.2 }, { -0.5, -0.5, 0.5 }, { -0.2, -0.5, 0.5 }, { -0.2, -0.5, 0.2 } }, { { -0.15, -0.2, 0.2 }, { -0.15, -0.2, 0.5 }, { 0.15, -0.2, 0.5 }, { 0.15, -0.2, 0.2 }, { -0.15, -0.5, 0.2 }, { -0.15, -0.5, 0.5 }, { 0.15, -0.5, 0.5 }, { 0.15, -0.5, 0.2 } }, { { 0.2, -0.2, 0.2 }, { 0.2, -0.2, 0.5 }, { 0.5, -0.2, 0.5 }, { 0.5, -0.2, 0.2 }, { 0.2, -0.5, 0.2 }, { 0.2, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.2 } }, //Mid { { -0.5, -0.2, -0.15 }, { -0.5, -0.2, 0.15 }, { -0.2, -0.2, 0.15 }, { -0.2, -0.2, -0.15 }, { -0.5, -0.5, -0.15 }, { -0.5, -0.5, 0.15 }, { -0.2, -0.5, 0.15 }, { -0.2, -0.5, -0.15 } }, { { -0.15, -0.2, -0.15 }, { -0.15, -0.2, 0.15 }, { 0.15, -0.2, 0.15 }, { 0.15, -0.2, -0.15 }, { -0.15, -0.5, -0.15 }, { -0.15, -0.5, 0.15 }, { 0.15, -0.5, 0.15 }, { 0.15, -0.5, -0.15 } }, { { 0.2, -0.2, -0.15 }, { 0.2, -0.2, 0.15 }, { 0.5, -0.2, 0.15 }, { 0.5, -0.2, -0.15 }, { 0.2, -0.5, -0.15 }, { 0.2, -0.5, 0.15 }, { 0.5, -0.5, 0.15 }, { 0.5, -0.5, -0.15 } }, //Bot { { -0.5, -0.2, -0.5 }, { -0.5, -0.2, -0.2 }, { -0.2, -0.2, -0.2 }, { -0.2, -0.2, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.2 }, { -0.2, -0.5, -0.2 }, { -0.2, -0.5, -0.5 } }, { { -0.15, -0.2, -0.5 }, { -0.15, -0.2, -0.2 }, { 0.15, -0.2, -0.2 }, { 0.15, -0.2, -0.5 }, { -0.15, -0.5, -0.5 }, { -0.15, -0.5, -0.2 }, { 0.15, -0.5, -0.2 }, { 0.15, -0.5, -0.5 } }, { { 0.2, -0.2, -0.5 }, { 0.2, -0.2, -0.2 }, { 0.5, -0.2, -0.2 }, { 0.5, -0.2, -0.5 }, { 0.2, -0.5, -0.5 }, { 0.2, -0.5, -0.2 }, { 0.5, -0.5, -0.2 }, { 0.5, -0.5, -0.5 } } } }; GLfloat primary_colors[][3] = { { 1.0, 0.0, 0.0 }, // red { 1.0, 1.0, 0.0 }, // yellow { 0.0, 1.0, 0.0 }, // green { 0.0, 0.0, 1.0 }, // blue { 1.0, 1.0, 1.0 }, // gold { 1.0, 0.0, 1.0 } // magenta }; |
클래스 생성
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 | class Cube{ private: GLfloat vertices[8][3]; GLfloat colors[6][3]; int location[3]; public: void setPosition(GLfloat set[8][3]); void setLocation(int loc[3]); void getLocation(int *ptr); void setColor(GLfloat set[6][3]); void getColor(GLfloat get[6][3]); void setAngle(int new_angleX, int new_angleY, int new_angleZ); void drawCube(); void drawBlackCube(); }; void Cube::setColor(GLfloat set[6][3]){ for (int i = 0; i<6; i++){ for (int j = 0; j<3; j++){ colors[i][j] = set[i][j]; } } } void Cube::getColor(GLfloat get[6][3]){ for (int i = 0; i < 6; i++){ for (int j = 0; j < 3; j++){ get[i][j] = colors[i][j]; } } } /** * Cube 위치 설정 */ void Cube::setLocation(int loc[3]){ for (int i = 0; i < 3; i++){ location[i] = loc[i]; } } /** * Cube 위치 반환 */ void Cube::getLocation(int *ptr){ for (int i = 0; i < 3; i++){ ptr[i] = location[i]; } } /** * Cube 위치 좌표 설정 */ void Cube::setPosition(GLfloat set[8][3]){ for (int i = 0; i < 8; i++){ for (int j = 0; j < 3; j++){ vertices[i][j] = set[i][j]; } } } /** * Cube 그리기 */ void Cube::drawCube(){ glBegin(GL_QUADS); glColor3fv(colors[0]); glNormal3f(0.0f, 1.0f, 0.0f);//top glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[2]); glVertex3fv(vertices[1]); glColor3fv(colors[1]); glNormal3f(0.0f, 0.0f, 1.0f);//front glVertex3fv(vertices[1]); glVertex3fv(vertices[2]); glVertex3fv(vertices[6]); glVertex3fv(vertices[5]); glColor3fv(colors[2]); glNormal3f(1.0f, 0.0f, 0.0f);//right glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]); glColor3fv(colors[3]); glNormal3f(0.0f, 0.0f, -1.0f);//back glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[4]); glColor3fv(colors[4]); glNormal3f(0.0f, -1.0f, 0.0f);//buttom glVertex3fv(vertices[7]); glVertex3fv(vertices[4]); glVertex3fv(vertices[5]); glVertex3fv(vertices[6]); glColor3fv(colors[5]); glNormal3f(-1.0f, 0.0f, 0.0f);//left glVertex3fv(vertices[4]); glVertex3fv(vertices[0]); glVertex3fv(vertices[1]); glVertex3fv(vertices[5]); glEnd(); // End Drawing The Cube } /** * Center Cube 그리기 */ void Cube::drawBlackCube(){ glBegin(GL_QUADS); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[2]); glVertex3fv(vertices[1]); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[1]); glVertex3fv(vertices[2]); glVertex3fv(vertices[6]); glVertex3fv(vertices[5]); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[0]); glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[4]); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[7]); glVertex3fv(vertices[4]); glVertex3fv(vertices[5]); glVertex3fv(vertices[6]); glColor3f(0.5f, 0.5f, 0.5f); glVertex3fv(vertices[4]); glVertex3fv(vertices[0]); glVertex3fv(vertices[1]); glVertex3fv(vertices[5]); glEnd(); // End Drawing The Cube } |
Display Func
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 | void setAllCube(){ /**************** 큐브 초기 좌표 설정 *****************/ for (int i = 0; i < 3; i++){ for (int j = 0; j < 9; j++){ cube[i][j].setPosition(vertices[i][j]); cube[i][j].setLocation(primaryLocation[i][j]); cube[i][j].setColor(primary_colors); } } } void drawAllCube(){ for (int a = 0; a < 3; a++){ for (int b = 0; b < 9; b++){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 9; j++){ int save[3]; int cnt = 0; cube[i][j].getLocation(save); //위치에 맞는 큐브를 선택 for (int k = 0; k < 3; k++){ if (primaryLocation[a][b][k] == save[k]){ cnt++; } } if (cnt == 3){ glPushMatrix(); if (i == 1 && j == 4) cube[i][j].drawBlackCube(); else cube[i][j].drawCube(); glPopMatrix(); } } } } } } void Init(){ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(700, 700); glutInitWindowPosition(0, 0); glutCreateWindow("Rubik\'s Cube"); // glClearColor(1.0, 1.0, 1.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glEnable(GL_DEPTH_TEST); setAllCube(); } void MyDisplay(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPopMatrix(); glRotatef(g_fSpinY, 1.0f, 0.0f, 0.0f); glRotatef(g_fSpinX, 0.0f, 0.0f, 1.0f); drawAllCube(); glPopMatrix(); glutSwapBuffers(); glFlush(); } |
반응형
'개발 > Open GL' 카테고리의 다른 글
루빅스 큐브 만들기3 (3) | 2014.12.24 |
---|---|
루빅스 큐브 만들기1 (0) | 2014.11.22 |
이미지 불러오기 (0) | 2014.11.21 |
정육면체 그리기 (0) | 2014.11.21 |
도형 그리기 및 이동 (0) | 2014.11.03 |