Upload
others
View
28
Download
0
Embed Size (px)
Citation preview
Introduction to
OpenGL Programming
Jung Lee
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/
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
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]
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’
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
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
[빈 프로젝트]
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’
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)]
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
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’
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]
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’
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 14 KUCG |
Example
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
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
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
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
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
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
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
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
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
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
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
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)
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)
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)
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 29 KUCG |
Teapot Example
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
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 31 KUCG |
Point Example
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
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 33 KUCG |
Line Example
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 34 KUCG |
Triangle Example
Korea University Computer Graphics Lab.
Jung Lee | September 26, 2012 | # 35 KUCG |
Rectangle Example : Revisited
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