Chapter 3 Viewing x y z w Vertex

  • Slides: 86
Download presentation
Chapter 3. 뷰잉(Viewing)

Chapter 3. 뷰잉(Viewing)

정점 변환의 단계 x y z w 오브젝트 좌표 Vertex 눈좌표 Modelview matrix 클립좌표

정점 변환의 단계 x y z w 오브젝트 좌표 Vertex 눈좌표 Modelview matrix 클립좌표 Projection matrix Perspective matrix 정규화장치좌표 Viewport transformation 윈도우좌표 2013 -2학기 3장 뷰잉

정육면체를 그리는 간단한 [예제 3 – 1]: cube. c #include <GL/glut. h> #include <stdlib.

정육면체를 그리는 간단한 [예제 3 – 1]: cube. c #include <GL/glut. h> #include <stdlib. h> void init(void) { gl. Clear. Color (0. 0, 0. 0); gl. Shade. Model (GL_FLAT); } void display(void){ gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Load. Identity (); /* 행렬을 클리어 */ glu. Look. At (0. 0, 5. 0, 0. 0, 1. 0, 0. 0); /*뷰잉변환*/ gl. Scalef (1. 0, 2. 0, 1. 0); /* 모델 변환 */ glut. Wire. Cube (1. 0); gl. Flush (); } 2013 -2학기 3장 뷰잉

void reshape (int w, int h){ gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h);

void reshape (int w, int h){ gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); gl. Frustum (-1. 0, 1. 5, 20. 0); gl. Matrix. Mode (GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); init (); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func(keyboard); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉

#include <GL/glut. h> #include <stdlib. h> void init(void) { gl. Clear. Color (0. 0,

#include <GL/glut. h> #include <stdlib. h> void init(void) { gl. Clear. Color (0. 0, 0. 0); gl. Shade. Model (GL_FLAT); } void draw_triangle(void){ gl. Begin (GL_LINE_LOOP); gl. Vertex 2 f(0. 0, 25. 0); gl. Vertex 2 f(25. 0, -25. 0); gl. Vertex 2 f(-25. 0, -25. 0); gl. End(); } 2013 -2학기 3장 뷰잉

뷰잉 및 모델링 변환 void display(void){ gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1.

뷰잉 및 모델링 변환 void display(void){ gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Load. Identity (); gl. Color 3 f (1. 0, 1. 0); draw_triangle (); gl. Enable (GL_LINE_STIPPLE); gl. Line. Stipple (1, 0 x. F 0 F 0); gl. Load. Identity (); gl. Translatef (-20. 0, 0. 0); draw_triangle (); 2013 -2학기 3장 뷰잉

gl. Line. Stipple (1, 0 x. F 00 F); gl. Load. Identity (); gl.

gl. Line. Stipple (1, 0 x. F 00 F); gl. Load. Identity (); gl. Scalef (1. 5, 0. 5, 1. 0); draw_triangle (); gl. Line. Stipple (1, 0 x 8888); gl. Load. Identity (); gl. Rotatef (90. 0, 1. 0); draw_triangle (); gl. Disable (GL_LINE_STIPPLE); gl. Flush (); } 2013 -2학기 3장 뷰잉

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei)

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); if (w <= h) gl. Ortho (-50. 0, -50. 0*(GLfloat)h/(GLfloat)w, -1. 0, 1. 0); else gl. Ortho (-50. 0*(GLfloat)w/(GLfloat)h, -50. 0, -1. 0, 1. 0); gl. Matrix. Mode(GL_MODELVIEW); } 2013 -2학기 3장 뷰잉

void keyboard(unsigned char key, int x, int y) { switch (key) { case 27:

void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); init (); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func (keyboard); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉

 Nate Robins 의 변환관련 튜토 리얼 http: //www. cs. utah. edu/~narobins/opengl. html transformation

Nate Robins 의 변환관련 튜토 리얼 http: //www. cs. utah. edu/~narobins/opengl. html transformation 프로그램 실행 2013 -2학기 3장 뷰잉

 void glu. Perspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); ◦ gl. Frustum()과

void glu. Perspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); ◦ gl. Frustum()과 같은 관측 공간 생성, 지정방식의 차이가 있다. ◦ fovy는 y축 방향의 FOV 각도를 나타냄 ◦ aspect 인자는 절두체의 종횡비(x/y, 정사각형의 경우 종횡비는 1. 0이다) fovy 라 메 카 near w h far Aspect = w/h 2013 -2학기 3장 뷰잉

예제 3 -4 행렬을 push 또는 pop 하기 draw_wheel_and_bolts() { long i; draw_wheel(); for

예제 3 -4 행렬을 push 또는 pop 하기 draw_wheel_and_bolts() { long i; draw_wheel(); for (i = 0; i < 5; i++) { gl. Push. Matrix(); gl. Rotatef(72. 0*i, 0. 0, 1. 0); gl. Translatef(3. 0, 0. 0); draw_bolt(); gl. Pop. Matrix(); } } 2013 -2학기 3장 뷰잉

draw_body_and_wheel_and_bolts() { draw_car_body(); gl. Push. Matrix(); gl. Translatef(40, 0, 30); /* 첫번째 바퀴의 위치로

draw_body_and_wheel_and_bolts() { draw_car_body(); gl. Push. Matrix(); gl. Translatef(40, 0, 30); /* 첫번째 바퀴의 위치로 이동 */ draw_wheel_and_bolts(); gl. Pop. Matrix(); gl. Push. Matrix(); gl. Translatef(40, 0, -30); /* 두 번째 바퀴의 위치로 이동 */ draw_wheel_and_bolts(); gl. Pop. Matrix(); … } 2013 -2학기 3장 뷰잉

클리핑 평면에 대한 코드 [예제 3 – 5 ]: clip. c #include <GL/glut. h>

클리핑 평면에 대한 코드 [예제 3 – 5 ]: clip. c #include <GL/glut. h> #include <stdlib. h> void init(void) { gl. Clear. Color (0. 0, 0. 0); gl. Shade. Model (GL_FLAT); } void display(void){ GLdouble eqn[4] = {0. 0, 1. 0, 0. 0}; GLdouble eqn 2[4] = {1. 0, 0. 0}; gl. Clear(GL_COLOR_BUFFER_BIT); 2013 -2학기 3장 뷰잉

gl. Color 3 f (1. 0, 1. 0); gl. Push. Matrix(); gl. Translatef (0.

gl. Color 3 f (1. 0, 1. 0); gl. Push. Matrix(); gl. Translatef (0. 0, -5. 0); /* 아래쪽 절반 클리핑 -- y < 0 */ gl. Clip. Plane (GL_CLIP_PLANE 0, eqn); gl. Enable (GL_CLIP_PLANE 0); /* 왼쪽 절반 클리핑 -- x < 0 */ gl. Clip. Plane (GL_CLIP_PLANE 1, eqn 2); gl. Enable (GL_CLIP_PLANE 1); gl. Rotatef (90. 0, 1. 0, 0. 0); glut. Wire. Sphere(1. 0, 20, 16); gl. Pop. Matrix(); gl. Flush (); } 2013 -2학기 3장 뷰잉

void reshape (int w, int h){ gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h);

void reshape (int w, int h){ gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); glu. Perspective(60. 0, (GLfloat) w/(GLfloat) h, 1. 0, 20. 0); gl. Matrix. Mode (GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); init (); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func(keyboard); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉

여러가지 변환 함께 사용하기 태양계 만들기 #include <GL/glut. h> #include <stdlib. h> static int

여러가지 변환 함께 사용하기 태양계 만들기 #include <GL/glut. h> #include <stdlib. h> static int year = 0, day = 0; void init(void) { gl. Clear. Color (0. 0, 0. 0); gl. Shade. Model (GL_FLAT); } 2013 -2학기 3장 뷰잉

void display(void){ gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl.

void display(void){ gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Push. Matrix(); glut. Wire. Sphere(1. 0, 20, 16); /* 태양을 그린다 */ gl. Rotatef ((GLfloat) year, 0. 0, 1. 0, 0. 0); gl. Translatef (2. 0, 0. 0); gl. Rotatef ((GLfloat) day, 0. 0, 1. 0, 0. 0); glut. Wire. Sphere(0. 2, 10, 8); /* 작은 행성을 그린다 */ gl. Pop. Matrix(); glut. Swap. Buffers(); } 2013 -2학기 3장 뷰잉

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei)

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); glu. Perspective(60. 0, (GLfloat) w/(GLfloat) h, 1. 0, 20. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); glu. Look. At (0. 0, 5. 0, 0. 0, 1. 0, 0. 0); } 2013 -2학기 3장 뷰잉

void keyboard (unsigned char key, int x, int y) { switch (key) { case

void keyboard (unsigned char key, int x, int y) { switch (key) { case 'd': day = (day + 10) % 360; glut. Post. Redisplay(); break; case 'D': day = (day - 10) % 360; glut. Post. Redisplay(); break; 2013 -2학기 3장 뷰잉

case 'y': year = (year + 5) % 360; glut. Post. Redisplay(); break; case

case 'y': year = (year + 5) % 360; glut. Post. Redisplay(); break; case 'Y': year = (year - 5) % 360; glut. Post. Redisplay(); break; case 27: exit(0); break; default: break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DOUBLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DOUBLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); init (); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func(keyboard); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉

로봇팔 만들기 #include <GL/glut. h> #include <stdlib. h> static int shoulder = 0, elbow

로봇팔 만들기 #include <GL/glut. h> #include <stdlib. h> static int shoulder = 0, elbow = 0; void init(void) { gl. Clear. Color (0. 0, 0. 0); gl. Shade. Model (GL_FLAT); } 2013 -2학기 3장 뷰잉

void display(void) { gl. Clear (GL_COLOR_BUFFER_BIT); gl. Push. Matrix(); gl. Translatef (-1. 0, 0.

void display(void) { gl. Clear (GL_COLOR_BUFFER_BIT); gl. Push. Matrix(); gl. Translatef (-1. 0, 0. 0); gl. Rotatef ((GLfloat) shoulder, 0. 0, 1. 0); gl. Translatef (1. 0, 0. 0); gl. Push. Matrix(); gl. Scalef (2. 0, 0. 4, 1. 0); glut. Wire. Cube (1. 0); gl. Pop. Matrix(); 2013 -2학기 3장 뷰잉

gl. Translatef (1. 0, 0. 0); gl. Rotatef ((GLfloat) elbow, 0. 0, 1. 0);

gl. Translatef (1. 0, 0. 0); gl. Rotatef ((GLfloat) elbow, 0. 0, 1. 0); gl. Translatef (1. 0, 0. 0); gl. Push. Matrix(); gl. Scalef (2. 0, 0. 4, 1. 0); glut. Wire. Cube (1. 0); gl. Pop. Matrix(); glut. Swap. Buffers(); } 2013 -2학기 3장 뷰잉

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei)

void reshape (int w, int h) { gl. Viewport (0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); glu. Perspective(65. 0, (GLfloat) w/(GLfloat) h, 1. 0, 20. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Translatef (0. 0, -5. 0); } 2013 -2학기 3장 뷰잉

void keyboard (unsigned char key, int x, int y){ switch (key) { case 's':

void keyboard (unsigned char key, int x, int y){ switch (key) { case 's': shoulder = (shoulder + 5) % 360; glut. Post. Redisplay(); break; case 'S': shoulder = (shoulder - 5) % 360; glut. Post. Redisplay(); break; case 'e': elbow = (elbow + 5) % 360; glut. Post. Redisplay(); break; 2013 -2학기 3장 뷰잉

case 'E': elbow = (elbow - 5) % 360; glut. Post. Redisplay(); break; case

case 'E': elbow = (elbow - 5) % 360; glut. Post. Redisplay(); break; case 27: exit(0); break; default: break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DOUBLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DOUBLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); init (); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func(keyboard); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉

 int glu. Un. Project(GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble model. Matrix[16],

int glu. Un. Project(GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble model. Matrix[16], const GLdouble proj. Matrix[16], const GLint viewport[4], GLdouble *objx, GLdouble *objy, GLdouble *objz); ◦ 모델뷰 행렬(model. Matrix)과 투영행렬(proj. Matrix), 뷰포트 (viewport)로 정의된 변환을 사용하여 주어진 윈도우 좌표 (winx, winy, winz)를 오브젝트 좌표(objx, objy, objz)에 매핑한 다. ◦ 함수가 성공적이면 GL_TRUE, 에러가 발생하면 GL_FALSE를 리턴한다. 2013 -2학기 3장 뷰잉

 Int glu. Un. Project 4(GLdouble winx, GLdouble winy, GLdouble winz, gldouble clipw, const

Int glu. Un. Project 4(GLdouble winx, GLdouble winy, GLdouble winz, gldouble clipw, const GLdouble model. Matrix[16], const GLdouble proj. Matrix[16], const GLint viewport[4], Glclampd z. Near, Glclampd z. Far, GLdouble *objx, GLdouble *objy, GLdouble *objz); ◦ glu. Un. Project()와 비슷. GLU 1. 3에서 변경되어 glu. Un. Project 4() 는 표준값이 아닌 gl. Depth. Range 값뿐만 아니라 1보다 큰 w 좌 표값도 처리할 수 있다. 2013 -2학기 3장 뷰잉

 Int glu. Project(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble model. Matrix[16], const

Int glu. Project(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble model. Matrix[16], const GLdouble proj. Matrix[16], const GLint viewport[4], GLdouble *winx, GLdouble *winy, GLdouble *winz); ◦ 모델뷰 행렬(model. Matrix), 투영행렬 (proj. Matrix), 뷰포트(viewport) 로 정의된 변환을 통해 주어진 오브젝트 좌표(objx, objy, objz)를 윈도우 좌표에 2013 -2학기 3장 뷰잉 매핑한다.

기하 프로세싱 파이프라인을 반대로 실행하기 [예제 3 – 8 ]: unproject. c #include <GL/glut.

기하 프로세싱 파이프라인을 반대로 실행하기 [예제 3 – 8 ]: unproject. c #include <GL/glut. h> #include <stdlib. h> #include <stdio. h> void display(void){ gl. Clear(GL_COLOR_BUFFER_BIT); gl. Flush(); } void reshape(int w, int h){ 2013 -2학기 3장 뷰잉

glu. Perspective (45. 0, (GLfloat) w/(GLfloat) h, 1. 0, 100. 0); gl. Matrix. Mode(GL_MODELVIEW);

glu. Perspective (45. 0, (GLfloat) w/(GLfloat) h, 1. 0, 100. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); } void mouse(int button, int state, int x, int y) { GLint viewport[4]; GLdouble mvmatrix[16], projmatrix[16]; GLint realy; /* Open. GL y 좌표 위치 */ GLdouble wx, wy, wz; /* 변환된 x, y , z 월드 좌표 */ 2013 -2학기 3장 뷰잉

switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { gl. Get. Integerv (GL_VIEWPORT,

switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { gl. Get. Integerv (GL_VIEWPORT, viewport); gl. Get. Doublev (GL_MODELVIEW_MATRIX, mvmatrix); gl. Get. Doublev (GL_PROJECTION_MATRIX, projmatrix); /* viewport[3]은 윈도우의 높이를 픽셀 단위로 나타낸다는 것 주의 */ realy = viewport[3] - (GLint) y - 1; printf ("Coordinates at cursor are (%4 d, %4 d)n", x, realy); glu. Un. Project ((GLdouble) x, (GLdouble) realy, 0. 0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz); printf ("World coords at z=0. 0 are (%f, %f)n", wx, wy, wz); 2013 -2학기 3장 뷰잉

glu. Un. Project ((GLdouble) x, (GLdouble) realy, 1. 0, mvmatrix, projmatrix, viewport, &wx, &wy,

glu. Un. Project ((GLdouble) x, (GLdouble) realy, 1. 0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz); printf ("World coords at z=1. 0 are (%f, %f)n", wx, wy, wz); } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) exit(0); break; default: break; } } 2013 -2학기 3장 뷰잉

void keyboard(unsigned char key, int x, int y) { switch (key) { case 27:

void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } 2013 -2학기 3장 뷰잉

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (500, 500); glut. Init. Window. Position (100, 100); glut. Create. Window (argv[0]); glut. Display. Func(display); glut. Reshape. Func(reshape); glut. Keyboard. Func (keyboard); glut. Mouse. Func(mouse); glut. Main. Loop(); return 0; } 2013 -2학기 3장 뷰잉