CS380 LAB II OpenGL

36
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [http://nehe.gamedev.net/ ]

description

CS380 LAB II OpenGL. Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [ http://nehe.gamedev.net/ ]. Goal. Introduce OpenGL programming Help you do CS380 homework by yourself. Notice. Use Noah board for your questions ( http://noah.kaist.ac.kr/course/CS380 ). - PowerPoint PPT Presentation

Transcript of CS380 LAB II OpenGL

Page 1: CS380 LAB II OpenGL

CS380 LAB IIOpenGL

Donghyuk Kim

Reference1. [OpenGL course slides by Rasmus Stenholt]

Reference2. [http://nehe.gamedev.net/]

Page 2: CS380 LAB II OpenGL

Goal

Introduce OpenGL programming Help you do CS380 homework by yourself

2

Page 3: CS380 LAB II OpenGL

Notice

Use Noah board for your questions (http://noah.kaist.ac.kr/course/CS380)

3

Page 4: CS380 LAB II OpenGL

Outline

Transformations Modeling Transformations

Draw 3D objects Translate Rotate Scale

4

Page 5: CS380 LAB II OpenGL

5

Transformations

All transformations in OpenGL are carried out by matrices

There are different matrices for different purposes The modelview matrix

GL_MODELVIEW The projection matrix

GL_PROJECTION The texture matrix

GL_TEXTURE All matrices are post-multiplied

I.e. the current matrix M becomes MN when N is performed Post-multiplication is closely linked to matrix stacks

Page 6: CS380 LAB II OpenGL

6

Transformations

MatricesAll matrices in OpenGL are 4x4Why use 4-D matrices for 3-D graphics?

A 4x4 matrix can rotate and translate the same vector in one operation

http://gamedev.stackexchange.com/questions/72044/why-do-we-use-4x4-matrices-to-transform-things-in-3d

Page 7: CS380 LAB II OpenGL

Transformations

Specify which matrix is the current matrix

Mode GL_MODELVIEW

Applies subsequent matrix operations to the model view matrix stack

GL_PROJECTION Applies subsequent matrix operations to the projection matrix stack

etc.

7

void glMatrixMode( GLenum   mode);

Page 8: CS380 LAB II OpenGL

Skeleton Code #include <GL/glut.h> #include <GL/glu.h> void display();

int main( int argc, char* argv[] ) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(512, 512); glutCreateWindow("CS380 LAB"); glutDisplayFunc(display); glutMainLoop(); return 0; }

void display(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3f(1.0,0.0,0.0); glutWireTeapot(0.5); glFlush(); }

8

Page 9: CS380 LAB II OpenGL

Set a view

9

int main( int argc, char* argv[] ){ … glutReshapeFunc( reshape ); …}

void reshape(int width, int height){ glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double aspect = width/double(height); gluPerspective(45, aspect, 1, 1024);}

Page 10: CS380 LAB II OpenGL

Set a view

fovy View angle, in degrees, in the y direction

aspect Ratio of x (width) to y (height)

zNear Distance from the viewer to the near clipping plane (positive)

zFar Distance from the viewer to the far clipping plane (positive)

10

glMatrixMode(GL_PROJECTION); glLoadIdentity(); double aspect = width/double(height); gluPerspective(45, aspect, 1, 1024);

void gluPerspective( GLdouble   fovy,

  GLdouble   aspect,

  GLdouble   zNear,

  GLdouble   zFar);

Page 11: CS380 LAB II OpenGL

Set a view

Detailed explanations about Viewing Transformation will be provided in the next week.

11

Page 12: CS380 LAB II OpenGL

Draw 3D objects

Use a set of OpenGL primitives e.g.,

glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); glBegin(GL_TRIANGLES); … glEnd(); …

12

Page 13: CS380 LAB II OpenGL

Draw 3D objects

GLUT provide simple 3D objects e.g., Teapot object

void glutSolidTeapot(GLdouble size); void glutWireTeapot(GLdouble size);

13

Page 14: CS380 LAB II OpenGL

Draw 3D objects

void display(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0); glColor3f(r,g,b); glutWireTeapot(0.5); glFlush(); }

14

Page 15: CS380 LAB II OpenGL

Translations

Multiply the current matrix by a translation matrix

x, y, zSpecify the x, y, and z coordinates of a

translation vector

15

void glTranslatef( GLfloat   x,  GLfloat   y,  GLfloat   z);

Page 16: CS380 LAB II OpenGL

Translations

Multiply the current matrix by a translation matrix

16

void glTranslatef( GLfloat   x,  GLfloat   y,  GLfloat   z);

1 0 0 X

0 1 0 Y

0 0 1 Z

0 0 0 1

Translation matrix (4x4)

Page 17: CS380 LAB II OpenGL

Review of Translations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0);

17

Page 18: CS380 LAB II OpenGL

Review of Translations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0);

18

Model view matrix

Page 19: CS380 LAB II OpenGL

Review of Translations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0);

19

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Model view matrix

Page 20: CS380 LAB II OpenGL

Review of Translations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0);

20

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Model view matrix

1 0 0 0

0 1 0 0

0 0 1 -1

0 0 0 1

1 0 0 0

0 1 0 0

0 0 1 -1

0 0 0 1

Page 21: CS380 LAB II OpenGL

Review of Translations

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0); glTranslatef(0,0,-1.0);

21

Model view matrix

1 0 0 0

0 1 0 0

0 0 1 -1

0 0 0 1

1 0 0 0

0 1 0 0

0 0 1 -2

0 0 0 1

Again??

1 0 0 0

0 1 0 0

0 0 1 -1

0 0 0 1

Page 22: CS380 LAB II OpenGL

Review of Translations

void display(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0,0,-1.0); glTranslatef(0,0,-1.0); glColor3f(r,g,b); glutWireTeapot(0.5); glFlush(); }

22

Page 23: CS380 LAB II OpenGL

Tutorials

Move your teapot based on key inputs ‘l’: left ‘r’: right ‘u’: up ‘d’: down ‘f’: move out ‘n’: move in

23

‘l’ ‘f’

Page 24: CS380 LAB II OpenGL

Tutorials

24

void keyboard(unsigned char key, int x, int y){ if (key == 'l') xpos -= 0.1; else if (key == 'r') xpos += 0.1; else if (key == 'u') ypos += 0.1; else if (key == 'd') ypos -= 0.1; else if (key == 'f') zpos -= 0.1; else if (key == 'n') zpos += 0.1; glutPostRedisplay();}

float xpos = 0.0;float ypos = 0.0;float zpos = -2.0;

void main(){…glutKeyboardFunc(keyboard);…}

void display(){…glTranslatef(xpos,ypos,zpos); … }

Page 25: CS380 LAB II OpenGL

Rotations

angle angle of rotation, in degrees

x, y, z x, y, and z coordinates of a vector, respectively Normalized vector (If not, GL will normalize it)

25

void glRotatef( GLfloat   angle,  GLfloat   x,  GLfloat   y,  GLfloat   z);

Page 26: CS380 LAB II OpenGL

Rotations

26

void glRotatef( GLfloat   angle,  GLfloat   x,  GLfloat   y,  GLfloat   z);

? ? ? 0

? ? ? 0

? ? ? 0

0 0 0 1

The questions will be resolved in the CS380 lecture “Modeling Transformations“.

Page 27: CS380 LAB II OpenGL

Rotations

void display(){ … glTranslatef(xpos,ypos,zpos); glRotatef(30,0,0,1); … }

27

30 degrees rotation based on a normalized vector (0, 0, 1)

Page 28: CS380 LAB II OpenGL

Tutorials

Rotate your teapot on X axis e.g., 30, 60, and 90 degrees

28

30 degrees 60 degrees 90 degrees

Page 29: CS380 LAB II OpenGL

Tutorials

30 degrees on X axis glRotatef(30,1,0,0);

60 degrees on X axis glRotatef(60,1,0,0);

90 degrees on X axis glRotatef(90,1,0,0);

29

Page 30: CS380 LAB II OpenGL

Scaling

x, y, zscale factors along the x, y, and z axes,

respectively

30

void glScalef( GLfloat   x,  GLfloat   y,  GLfloat   z);

Page 31: CS380 LAB II OpenGL

Scaling

31

void glScalef( GLfloat   x,  GLfloat   y,  GLfloat   z);

X 0 0 0

0 Y 0 0

0 0 Z 0

0 0 0 1

Page 32: CS380 LAB II OpenGL

Scaling

32

void display(){… glLoadIdentity(); glTranslatef(0,0,-3); glScalef(2,1,1);…}

Page 33: CS380 LAB II OpenGL

Review of Scaling

33

void display(){… glLoadIdentity(); glTranslatef(0,0,-3); glScalef(2,1,1);…}

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Model view matrix

Page 34: CS380 LAB II OpenGL

Review of Scaling

34

void display(){… glLoadIdentity(); glTranslatef(0,0,-3); glScalef(2,1,1);…}

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Model view matrix

1 0 0 0

0 1 0 0

0 0 1 -3

0 0 0 1

1 0 0 0

0 1 0 0

0 0 1 -3

0 0 0 1

Page 35: CS380 LAB II OpenGL

Review of Scaling

35

void display(){… glLoadIdentity(); glTranslatef(0,0,-3); glScalef(2,1,1);…}

Model view matrix

2 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

2 0 0 0

0 1 0 0

0 0 1 -3

0 0 0 1

1 0 0 0

0 1 0 0

0 0 1 -3

0 0 0 1

Page 36: CS380 LAB II OpenGL

36

Next time

Viewing Transformation in OpenGL