1 / 8

CG Programming (Modeling)

CG Programming (Modeling). Fall, 2008. GLUT Main(). int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); // 더블 버퍼 사용 ( 화면 버퍼와 백버퍼 ), RGB 칼라모델 , 깊이버퍼 사용 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

tana
Télécharger la présentation

CG Programming (Modeling)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CG Programming(Modeling) Fall, 2008

  2. GLUT Main() int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); // 더블 버퍼 사용(화면 버퍼와 백버퍼), RGB칼라모델, 깊이버퍼 사용 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Simple Modeling"); // display(): Rendering Loop 에서 계속 불림 glutDisplayFunc(display); // keyboard(): 키보드 이벤트 처리 glutKeyboardFunc(keyboard); // mouse(): 마우스 이벤트 처리 glutMouseFunc(mouse); // motion(): 포인트 위치(x, y) 움직임 이벤트 처리 (절대 좌표) glutMotionFunc(motion); // ReadModel(): 파일로부터 물체를 읽어드리는 함수 ReadModel(); // GLSetupRC(): OpenGL를 사용하여 빛설정, 시각좌표계설정, 프로젝션 설정 GLSetupRC(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }

  3. GLUT 이벤트 처리 • 키보드 이벤트 • void keyboard(unsigned char key, int x, int y) • key 변수: 눌려진 문자 값 • x, y 변수: 포인트 위치 • 마우스 이벤트 • void mouse(int button, int state, int x, int y) • button 변수: GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON • state 변수: GLUT_UP, GLUT_DOWN • x, y 변수: window-relative mouse position • 포인트 움직임 이벤트 • void motion(int x, int y) • x, y 변수: window-relative mouse position

  4. OpenGL 환경 설정 관련 함수 void GLSetupRC(void) { /* Enable a single OpenGL light. */ // LIGHT0 : 설정한 조명의 이름, GL_LIGHT1, GL_LIGHT2, … 로 조명 추가 가능 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); // 난반사 설정 glLightfv(GL_LIGHT0, GL_POSITION, light_specular); // 정반사 설정 glLightfv(GL_LIGHT0, GL_POSITION, light_ambient); // 주변광 설정 glLightfv(GL_LIGHT0, GL_POSITION, light_position); // 조명의 위치 설정 glEnable(GL_LIGHT0); // 설정한 LIGHT0를 사용 가능 glEnable(GL_LIGHTING); // 설정한 조명들을 켬  glDisable(…): 조명들을 끔 /* Use depth buffering for hidden surface elimination. */ glEnable(GL_DEPTH_TEST); // 깊이 테스트 켬 /* Setup the view */ glMatrixMode(GL_PROJECTION); // 프로젝션 설정 glLoadIdentity(); // 프로젝션 행렬 초기화  단위 행렬로 대치 gluPerspective( 40.0, // field of view in degree 1.0, // aspect ratio 1.0, // Z near 2000.0); // Z far glMatrixMode(GL_MODELVIEW); // 시각좌표계 UVN 설정 glLoadIdentity(); gluLookAt(400.0, 400.0, 400.0, // eye is at (400,400,400) 0.0, 0.0, 0.0, // center is at (0,0,0) 0.0, 1.0, 0.); // up is in positive Y direction }

  5. GLUT Display 함수 void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); MakeGL_Model(); // 물체 모델링 if (status==WIRE) DrawWire(); // 물체 렌더링 (선구조형상) else DrawShade(); // 물체 렌더링 (쉐이딩) }

  6. Cube.dat VERTEX = 8 -100.0 -100.0 -100.0 100.0 -100.0 -100.0 100.0 100.0 -100.0 -100.0 100.0 -100.0 -100.0 -100.0 100.0 100.0 -100.0 100.0 100.0 100.0 100.0 -100.0 100.0 100.0 FACE = 12 7 5 6 7 4 5 3 2 1 3 1 0 2 6 5 2 5 1 3 7 6 3 6 2 0 4 7 0 7 3 1 5 4 1 4 0 Read File(모델링 파일) void ReadModel() { FILE *f1; char s[81]; int i; if ((f1 = fopen(fname, "rt"))== NULL) { printf("No file\n"); exit(0); } fscanf(f1,"%s",s); printf( "%s", s ); // 문자열 “VERTEX”읽음 fscanf(f1,"%s",s); printf( "%s", s ); // 문자열 “=“ 읽음 fscanf(f1,"%d",&pnum); printf( "%d\n", pnum); // 버텍스의 개수 읽음 mpoint = (Point*)malloc(sizeof(Point)*pnum); // 버텍스 개수 만큼 POINT배열 설정 for (i=0; i<pnum; i++){ fscanf(f1,"%f",&mpoint[i].x); // 버텍스 읽음 (x좌표) fscanf(f1,"%f",&mpoint[i].y); // 버텍스 읽음 (y좌표) fscanf(f1,"%f",&mpoint[i].z); // 버텍스 읽음 (z좌표) printf( "%f %f %f\n", mpoint[i].x, mpoint[i].y,mpoint[i].z); } fscanf(f1,"%s",s); printf( "%s", s ); // 문자열 “FACE”읽음 fscanf(f1,"%s",s); printf( "%s", s ); // 문자열 “=”읽음 fscanf(f1,"%d",&fnum); printf( "%d\n", fnum); // 면의 개수 읽음 mface = (Face*)malloc(sizeof(Face)*fnum); // 면 개수 만큼 FACE배열 설정 for (i=0; i<fnum; i++){ fscanf(f1,"%d",&mface[i].ip[0]); // 1번째 점의 인덱스 읽음 fscanf(f1,"%d",&mface[i].ip[1]); // 2번째 점의 인덱스 읽음 fscanf(f1,"%d",&mface[i].ip[2]); // 3번째 점의 인덱스 읽음 printf("%d %d %d\n", mface[i].ip[0], mface[i].ip[1], mface[i].ip[2]); } fclose(f1); }

  7. PushMatrix PopMatrix OpenGL Modeling void MakeGL_Model(void) { int i; Point norm; // 폴리건의 노말 벡터 glShadeModel(GL_SMOOTH); // Gouraud 쉐이딩 // GL_FLAT  Constant 쉐이딩 if (glIsList(1)) glDeleteLists(1,1); // 모델 리스트 설정 glNewList(1, GL_COMPILE); glPushMatrix(); // 행렬 스택에 푸쉬 glTranslatef(xloc, yloc, zloc); // 물체 이동 변환 행렬 glRotatef(angle, 0.0, 1.0, 0.0); // 물체 회전 변환 행렬 glScalef(scalefactor, scalefactor, scalefactor); // 물체 크기 변환 행렬 for (i = 0; i < fnum; i++) { norm = cnormal(mp[mf[i].ip[2]], mp[mf[i].ip[1]], mp[mf[i].ip[0]]); // 면 노말 계산 glBegin(GL_TRIANGLES); // 삼각형으로 면 생성 glNormal3f(norm.x, norm.y, norm.z); // 면 노말 설정 glVertex3f(mp[mf[i].ip[0]].x, mp[mf[i].ip[0]].y, mp[mf[i].ip[0]].z); // 1번째 점 설정 glVertex3f(mp[mf[i].ip[1]].x, mp[mf[i].ip[1]].y, mp[mf[i].ip[1]].z); // 2번째 점 설정 glVertex3f(mp[mf[i].ip[2]].x, mp[mf[i].ip[2]].y, mp[mf[i].ip[2]].z); // 3번째 점 설정 glEnd(); } glPopMatrix(); // 행렬 스택에서 팝 glEndList(); }

  8. OpenGL Shading void Draw……(void) { // glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 화면 배경 색 흰색으로 설정 // 칼라 버퍼와 깊이 버퍼 초기화 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable (GL_DEPTH_TEST); // 깊이 테스트 켬 glEnable (GL_LIGHT0); // 조명 켬 glEnable (GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); // 물체의 재질 패러미터 사용 if (cull) glEnable(GL_CULL_FACE); // Culling 사용 else glDisable(GL_CULL_FACE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // 선구조형상으로 표현 // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // 쉐이딩으로 표현 // 모델링단계에서 설정한 모델링 리스트의 1번을 부름 glCallList(1); // 더블 버퍼 사용: 화면 버퍼와 백버퍼를 교환  속도향상 glutSwapBuffers(); }

More Related