Introduction to OpenGL Programming - Korea...

36
Introduction to OpenGL Programming Jung Lee

Transcript of Introduction to OpenGL Programming - Korea...

Page 1: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Introduction to

OpenGL Programming

Jung Lee

Page 2: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 2 KUCG |

OpenGL Reference Sites

• OpenGL Official Site

http://www.opengl.org/

• Khronos Group

http://www.khronos.org/

• Nehe Productions

http://nehe.gamedev.net/

Page 3: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 3 KUCG |

OpenGL-Related Files

• Header File • gl.h, glaux.h, glu.h, glut.h

To the ‘include’ directory

• Static Library • opengl32.lib, glaux.lib, glu32.lib, glut32.lib

To the ‘lib’ directory

• Dynamic Library • opengl32.dll, glu32.dll, glut.dll, glut32.dll

C:\WINDOWS\system32

Page 4: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 4 KUCG |

Project Creation [Visual Studio 6.0] (1/3)

• [File] [New] (Ctrl+N)

[Win32 Console Application]

Enter the project name [OK]

[An empty project] [Finish]

Page 5: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 5 KUCG |

Project Creation [Visual Studio 6.0] (2/3)

• [Project] [Settings] (Alt+F7)

[Link] Object/library modules

• Type ‘opengl32.lib glu32.lib glut32.lib glaux.lib’

Page 6: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 6 KUCG |

Project Creation [Visual Studio 6.0] (3/3)

• [File] [New] (Ctrl+N)

[C++ Source File]

Enter the main file name

Page 7: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 7 KUCG |

Project Creation [Visual Studio 8.0] (1/3)

• [File] [New] [Project] (Ctrl+Shift+N)

[Visual C++] [Win32] [Win32 Console App]

Enter the project name

[빈 프로젝트]

Page 8: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 8 KUCG |

Project Creation [Visual Studio 8.0] (2/3)

• [Project] [속성] (Alt+F7)

[구성 속성] [링커] [입력]

[추가 종속성]

• Type ‘opengl32.lib glu32.lib glut32.lib glaux.lib’

Page 9: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 9 KUCG |

Project Creation [Visual Studio 8.0] (3/3)

• [소스 파일] 우클릭 [새 항목 추가]

[코드] [C++파일(.cpp)]

Enter the main file name [추가(A)]

Page 10: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 10 KUCG |

Project Creation [Visual Studio 2008] (1/2)

• [File] [New] (Ctrl+Shift+N)

[Project types] [General]

[Templates] [Empty Project]

Enter the project file name

Page 11: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 11 KUCG |

Project Creation [Visual Studio 2008] (2/2)

• [Project] [Settings…] (Alt+F7)

[Link] [Input] [Additional Dependencies]

• Type ‘opengl32.lib glu32.lib glut32.lib glaux.lib’

Page 12: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 12 KUCG |

Project Creation [Visual Studio 2010] (1/2)

• [File] [New] [Project] (Ctrl+Shift+N)

[Visual C++] [Win32] [Win32 Console App]

Enter the project name

[Empty project]

Page 13: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 13 KUCG |

Project Creation [Visual Studio 2010] (2/2)

• [Project] [Properties] (Alt+F7)

[Configuration Properties] [Linker] [Input]

[Additional Dependencies]

• Type ‘opengl32.lib glu32.lib glut32.lib glaux.lib’

Page 14: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 14 KUCG |

Example

Page 15: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 15 KUCG |

OpenGL Primitives

• Point GL_POINTS

• Line GL_LINES

GL_LINE_STRIP

GL_LINE_LOOP

• Polygon GL_POLYGON

GL_TRIANGLES

GL_TRIANGLE_STRIP

GL_TRIANGLE_FAN

GL_QUAD_STRIP

• Miscellaneous

Cube

Tetrahedron

Icosahedron

Sphere

Torus

Cone

Page 16: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 16 KUCG |

Point

• GL_POINTS

glBegin(GL_POINTS);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glEnd();

v1 v2

v3 v4

Page 17: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 17 KUCG |

Line (1/3)

• GL_LINES

glBegin(GL_LINES);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glEnd();

v1 v2

v3 v4

Page 18: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 18 KUCG |

Line (2/3)

• GL_LINE_STRIP

glBegin(GL_LINE_STRIP);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glEnd();

v1 v2

v3 v4

Page 19: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 19 KUCG |

Line (3/3)

• GL_LINE_LOOP

glBegin(GL_LINE_LOOP);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glEnd();

v1 v2

v3 v4

Page 20: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 20 KUCG |

Triangle (1/3)

• GL_TRIANGLES

glBegin(GL_TRIANGLES);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glEnd();

v1 v3

v2

Page 21: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 21 KUCG |

Triangle (2/3)

• GL_TRIANGLE_STRIP

glBegin(GL_TRIANGLE_STRIP);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glVertex3f(v5x, v5y, v5z);

glEnd();

v1

v2

v3

v4

v5

Page 22: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 22 KUCG |

Triangle (3/3)

• GL_TRIANGLE_FAN

glBegin(GL_TRIANGLE_FAN);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glVertex3f(v5x, v5y, v5z);

glEnd(); v1

v2

v3

v4 v5

Page 23: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 23 KUCG |

Quadrilateral (1/2)

• GL_QUADS

glBegin(GL_QUADS);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glEnd();

v1 v4

v3 v2

Page 24: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 24 KUCG |

Quadrilateral (2/2)

• GL_QUAD_STRIP

glBegin(GL_QUAD_STRIP);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glVertex3f(v5x, v5y, v5z);

glVertex3f(v6x, v6y, v6z);

glEnd();

v1

v2

v3

v4

v5

v6

Page 25: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 25 KUCG |

Polygon

• GL_POLYGON

glBegin(GL_POLYGON);

glVertex3f(v1x, v1y, v1z);

glVertex3f(v2x, v2y, v2z);

glVertex3f(v3x, v3y, v3z);

glVertex3f(v4x, v4y, v4z);

glVertex3f(v5x, v5y, v5z);

glVertex3f(v6x, v6y, v6z);

glEnd();

v1

v2

v3 v4

v6

v5

Page 26: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 26 KUCG |

Miscellaneous (1/3)

• Cube

void glutSolidCube(GLdouble size)

void glutWireCube(GLdouble size)

• Tetrahedron

void glutSolidTetrahedron(void)

void glutWireTetrahedron(void)

• Icosahedron

void glutSolidIcosahedron(void)

void glutWireIcosahedron(void)

Page 27: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 27 KUCG |

Miscellaneous (2/3)

• Sphere

void glutSolidSphere(GLdouble radius, GLint

slices, GLint stacks)

void glutWireSphere(GLdouble radius, GLint

slices, GLint stacks)

• Torus

void glutSolidTorus(GLdouble innerRadius,

GLdouble outerRadius, GLint nsides, GLint rings)

void glutWireTorus(GLdouble innerRadius,

GLdouble outerRadius, GLint nsides, GLint rings)

Page 28: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 28 KUCG |

Miscellaneous (3/3)

• Cone

void glutSolidCone(GLdouble base_radius,

GLdouble height, GLint slices, GLint stacks)

void glutWireCone(GLdouble base_radius,

GLdouble height, GLint slices, GLint stacks)

• Teapot

void glutSolidTeapot(GLdouble size)

void glutWireTeapot(GLdouble size)

Page 29: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 29 KUCG |

Teapot Example

Page 30: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 30 KUCG |

Basic Functions (1/2)

• glPointSize(GLfloat size)

glGetFloatv(GL_POINT_SIZE_RANGE)

• Returns the range of the point size that the hardware supports

• glLineWidth(GLfloat width)

glGetFloatv(GL_LINE_WIDTH_RANGE)

• Returns the range of the line width that the hardware supports

Page 31: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 31 KUCG |

Point Example

Page 32: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 32 KUCG |

Basic Functions (2/2)

• glShadeModel(mode)

Sets the polygon filling method

mode

• GL_FLAT

– By only one color

• GL_SMOOTH

– By the weighted averaging the colors of member vertices (gradation)

– Default value

Page 33: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 33 KUCG |

Line Example

Page 34: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 34 KUCG |

Triangle Example

Page 35: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 35 KUCG |

Rectangle Example : Revisited

Page 36: Introduction to OpenGL Programming - Korea Universitykucg.korea.ac.kr/.../CNCE340/tutor/04opengl_introduction.pdf · 2002-01-16 · Introduction to OpenGL Programming Jung Lee . Korea

Korea University Computer Graphics Lab.

Jung Lee | September 26, 2012 | # 36 KUCG |

‘Single vs. Double Buffering’ glFlush() vs. glutSwapBuffers()

• Single Buffering (GLUT_SINGLE, Default)

• Double Buffering (GLUT_DOUBLE)

cf.) triple buffering

Graphic Processor

Frame Buffer

Video Controller

Display Device

Graphic Processor

Front Buffer

Video Controller

Display Device

Back Buffer

Slow Very Fast