티스토리 뷰
곡면 텍스처 매핑
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | #pragma warning(disable:4996) #include <iostream> #include <vector> #include <conio.h> #include <gl/glut.h> #include <gl/gl.h> #include <gl/glu.h> using namespace std; GLfloat circle[13][3] = { { -1.0, 0.0, 0.0 }, { -1.0, 0.5, 0.0 }, { -0.5, 1.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.5, 1.0, 0.0 }, { 1.0, 0.5, 0.0 }, { 1.0, 0.0, 0.0 }, { 1.0, -0.5, 0.0 }, { 0.5, -1.0, 0.0 }, { 0.0, -1.0, 0.0 }, { -0.5, -1.0, 0.0 }, { -1.0, -0.5, 0.0 }, { -1.0, 0.0, 0.0 } }; #define N 10 #define SIZE 3*N+1 GLfloat controlPoints[SIZE][13][3]; //이미지를 요렇게 만들기 GLfloat textureMap[64][64][3]; GLuint tex[2]; GLfloat xRot = 0.0f; GLfloat yRot = 0.0f; void assignColor(GLfloat col[3], GLfloat r, GLfloat g, GLfloat b) { col[0] = r; col[1] = g; col[2] = b; } int isEven(int x) { if (x % 2 == 0) return 1; else return 0; } void makeTextureMap() { for (int i = 0; i < 64; i++) for (int j = 0; j < 64; j++) { if (isEven(i / 8 + j / 8)) assignColor(textureMap[i][j], 1.0, 1.0, 1.0); else assignColor(textureMap[i][j], 1.0, 0.0, 0.0); } } void makeTextures() { glGenTextures(2, tex); glBindTexture(GL_TEXTURE_2D, tex[0]); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_FLOAT, textureMap); /************************************ */ glBindTexture(GL_TEXTURE_2D, tex[1]); GLfloat planes[] = { 0.0, 0.3, 0.0, 0.0 }; GLfloat planet[] = { 0.0, 0.0, 0.3, 0.0 }; glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv(GL_S, GL_OBJECT_PLANE, planes); glTexGenfv(GL_T, GL_OBJECT_PLANE, planet); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_FLOAT, textureMap); } void buildCircle(float radius, float height, GLfloat data[13][3]) { int i; for (i = 0; i < 13; i++) { data[i][0] = circle[i][0] * radius; data[i][1] = circle[i][1] * radius; data[i][2] = height; } } void buildSurfaceOfRotation() { for (int i = 0; i < 7; i++){ float r = sqrt(36.0f - pow((6-i), 2)); cout << r << endl; buildCircle(r, 6-i, controlPoints[i]); } //buildCircle(2.0, 7.0, controlPoints[1]); //buildCircle(1.5, 6.0, controlPoints[2]); //buildCircle(1.5, 5.0, controlPoints[3]); //buildCircle(2.0, 4.0, controlPoints[4]); //buildCircle(2.5, 3.0, controlPoints[5]); //buildCircle(2.5, 2.0, controlPoints[6]); //buildCircle(1.0, 8.0, controlPoints[0]); //buildCircle(8.0, 7.0, controlPoints[1]); } void display(void) { int row, col; GLfloat patch[4][4][3]; float texel[2][2][2] = { { { 0.0, 0.0 }, { 1.0, 0.0 } }, { { 0.0, 1.0 }, { 1.0, 1.0 } } }; glEnable(GL_MAP2_TEXTURE_COORD_2); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); buildSurfaceOfRotation(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); //glRotatef(zRot, 0.0f, 0.0f, 1.0f); //텍스처를 불러온다? glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); //glBindTexture(GL_TEXTURE_2D, tex[0]); glPushMatrix(); glTranslatef(0.0, 2.0, 0.0); for (row = 0; row < 2; row++) { for (col = 0; col < 4; col++) { glMap2f( GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 39, 4, &controlPoints[3 * row][3 * col][0] ); /* glMap2f( GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, 2, 2, 0.0, 1.0, 4, 2, &texel[0][0][0] ); */ /* This second call to glMap2f builds the texture spline. We replace the control point grid with the "texel" grid at the top of this function. This way the same parameters (say u and v) that produce points on the geometry spline can also produce texture coordinates. The arguments to glMap2 are target, s_min, s_max, s_stride, s_order, t_min, t_max, t_stride, t_order The target is "GL_MAP2_TEXTURE_COORD_2. Both parameter mins are 0, both maxes are 1. Since we have 2 floats per point, s_stride is 2; since there are two columns to texel we have 2*2 or 4 for t_stride. Finally, since the texture spline values all lie in a plane order 2 is fine for the texture spline. All that we are doing here is constructing a mesh over the square 0 <= s, t <= 1. */ glMapGrid2f(5, 0.0, 1.0, 20, 0.0, 1.0); glEvalMesh2(GL_FILL, 0, 5, 0, 20); } } glPopMatrix(); /* glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glBindTexture(GL_TEXTURE_2D, tex[1]); glPushMatrix(); glTranslatef(0.0, 4.0, 0.0); for (row = 0; row < 2; row++) { for (col = 0; col < 4; col++) { glMap2f(GL_MAP2_VERTEX_3, 0.0, 1.0, 3, 4, 0.0, 1.0, 39, 4, &controlPoints[3 * row][3 * col][0]); glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0); glEvalMesh2(GL_FILL, 0, 20, 0, 20); } } glPopMatrix(); */ glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glutSwapBuffers(); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, w*1.0 / h, 0.5, 200); glMatrixMode(GL_MODELVIEW); } void myInit() { GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 30.0, 15.0, 10.0, 1.0 }; GLfloat mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 }; GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 }; GLfloat mat_specular[] = { 0.9, 0.9, 0.9, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(20.0, 5.0, 10.0, 0.0, 2.0, 5.0, 0.0, 0.0, 1.0); makeTextureMap(); makeTextures(); glEnable(GL_TEXTURE_2D); glEnable(GL_AUTO_NORMAL); glEnable(GL_MAP2_VERTEX_3); //glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); //glEnable(GL_LIGHTING); //glEnable(GL_LIGHT0); glClearColor(0.0, 0.0, 0.0, 1.0); //glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); //glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); //glLightfv(GL_LIGHT0, GL_SPECULAR, specular); //glLightfv(GL_LIGHT0, GL_POSITION, position); //glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); //glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); } void SpecialKeys(int key, int x, int y) { if (key == GLUT_KEY_UP) xRot = +1.0f; if (key == GLUT_KEY_DOWN) xRot = -1.0f; if (key == GLUT_KEY_LEFT) yRot = -1.0f; if (key == GLUT_KEY_RIGHT) yRot = +1.0f; if (key > 356.0f) xRot = 0.0f; if (key < -1.0f) xRot = 355.0f; if (key > 356.0f) yRot = 0.0f; if (key < -1.0f) yRot = 355.0f; cout << xRot << " " << yRot << endl; // Refresh the Window glutPostRedisplay(); } int main(int argc, char *argv[]) { int i, j, k, m, n; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("test"); glutReshapeFunc(myReshape); glutSpecialFunc(SpecialKeys); myInit(); glutDisplayFunc(display); glutMainLoop(); } | cs |
'영상처리 > OpenGL' 카테고리의 다른 글
쭉 긁으면 되는 소스 (5) (0) | 2016.07.13 |
---|---|
쭉 긁으면 되는 소스 (3) (0) | 2016.06.07 |
쭉 긁으면 되는 소스 (2) (0) | 2016.06.07 |
쭉 긁으면 되는 소스 (1) (0) | 2016.06.07 |
댓글