17
OpenGL Tutorial 이선민 [email protected]

OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

OpenGL Tutorial이선민[email protected]

Page 2: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

What is OpenGL?

- 2D and 3D graphics API

- primitive rendering commands

- GLU (OpenGL Utility Library): provides functionality in OpenGL core. part of OpenGL

- GLUT (OpenGL Utility Toolkit): provides functionality common to all window systems.

http://www.opengl-tutorial.org/

https://www.opengl.org/resources/libraries/glut/spec3/node1.html

Page 3: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Install & build

- Installsudo apt-get install freeglut3-dev (Ubuntu 16.04 LTS)

- Buildg++ –o foo foo.c –lm –lGL –lGLU -lglut

Page 4: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

How to create window

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(400, 400);

glutInitWindowPosition( 50, 0 );

glutCreateWindow("Example");

Page 5: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

OpenGL main loop

do{

if (There Is an Event on the Event Queue) switch (Event Type) { case Keyboard Event:

Get Event Record, Run Keyboard Callback case ... } else Do Background Process} while (User Does Not Request Escape)

Page 6: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Callback functions

- glutReshapeFunc(resize)

- glutDisplayFunc(display)

- glutTimerFunc(timeStep, Timer, 0)

- glutKeyboardFunc(keyboard)

- glutMouseFunc(glutMouse)

- glutMotionFunc(glutMotion)

Page 7: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using
Page 8: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Modeling

glColor3f(1.0f, 1.0f, 1.0f);

glBegin(GL_POLYGON);

glVertex3f(10.0, 10.0, 0.0);

glVertex3f(-10.0, 10.0, 0.0);

glVertex3f(-10.0, -10.0, 0.0);

glVertex3f(10.0, -10.0, 0.0);

glEnd();

Page 9: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Modeling

Page 10: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Modeling

- transformations

glTranslatef(x, y, z):

glRotatef(angle, x, y, z):

glScalef(x, y, z):

1 0 0 x0 1 0 y0 0 1 z0 0 0 1

x 0 0 00 y 0 00 0 z 00 0 0 1

x^2(1-c)+c xy(1-c)-zx xz(1-c)+ys 0yx(1-c)+zs y^2(1-c)+c yz(1-c)-xs 0xz(1-c)-ys yz(1-c)+xs z^2(1-c)+c 0 0 0 0 1

Page 11: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Modeling

- Matrix stack

glPushMatrix()

glPopMatrix()

Page 12: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Viewing

gluLookAt(eye[0], eye[1], eye[2], ori[0], ori[1], ori[2], up[0], up[1], up[2]);

-z

ori

eye

up = y

Page 13: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Viewing

gluLookAt(eye[0], eye[1], eye[2], ori[0], ori[1], ori[2], up[0], up[1], up[2]);

-z

ori

eye

up = y

Page 14: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Projection

gluPerspective(fov, aspect, zNear, zFar)

Page 15: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Viewport

glViewport(x, y, w, h)

window

image h

w

y

x

Page 16: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

Programming Assignment #1

● Create a hierarchical model using matrix stacks● The model should consists of three-dimensional primitives such as polygons, boxes, cylinders,

spheres and quadrics.● The model should have a hierarchy of at least three levels● Animate the model to show the hierarchical structure

– Eg) a car driving with rotating wheels– Eg) a runner with arms and legs swing

● Make it aesthetically pleasing or technically illustrative

Page 17: OpenGL Tutorial - Seoul National Universitymrl.snu.ac.kr/courses/CourseGraphics/2019_OpenGLTutorial.pdf · 2019. 3. 20. · Programming Assignment #1 Create a hierarchical model using

과제 유의 사항 아래 사항을 지키지 않는 과제는 0 점 처리 될 수 있습니다 .

● 과제 제출 : 보고서와 코드를 압축해서 [email protected] 로 제출해주시기바랍니다 . 압축파일명은 본인학번 (****-*****.zip, ****-*****.tar 등 ) 입니다 . 보고서에

는 컴파일방법과 조작법 , 구현방법이 간략하게포함되어있어야합니다 . 과제에서 요구된 기본 , 추가 구현사항 중 구현한 것과 구현하지 못한것을 명확히 명시하 여야 합니다 .

– 중요 : 구현 언어는 자유롭게 선택하시면 되고 , 다만 아래와 같은 채점환경의 터미널에서 테스트할 것이므로 커맨드라인 실행 명령어를 꼭 명시해주세요 .

● 채점환경 : ubuntu 16.04 에서 커맨드라인 인풋으로 테스트합니다 . gcc 의 경우 버전 5.4.0 을 사용합니다 . 과제 제출전 듀얼부팅으로 우분투를 설치하시거나 VirtualBox

나 VMware 와같은 가상머신에 우분투를 설치하셔서 테스트해 주시기 바랍니다 .

● 라이브러리 : OpenGL 이나 Eigen 라이브러리 , mrl 홈페이지에있는 math library 를 제외한 외부라이브러리를 사용할경우 파일에포함해서 제출해주시기 바랍니다 . 채점환경

에서의 OpenGL version 은 4.6.0 이며 Eigen version 은 3.3.4 stable 입니다 . 위의 라이브러리이외의 외부라이브러리를쓰시거나 unstable version 을 이용해서 문제가 발생하는 경우 조교가 해결해드리지않습니다 .

– 다만 , 듀전날 자정이내로 제출하실경우 조교에게 채점환경에서 컴파일이 잘 되는지 시도해 볼 것을 요청하실 수 있습니다● 딜레이 :

최대 일주일까지 허용하며 , 하루당 해당 과제 득점의 10% 의 감점이 있습니다