Download presentation
Presentation is loading. Please wait.
1
3D Graphics with OpenGL CSCE 458 Fall 2004
Chang-Hun Kim Department of Computer Science Korea University
2
Reference Books Practical Algorithms for 3D Computer Graphics
R. Stuart Ferguson Introduction of 3D Graphics with OpenGL/DirectX OpenGL Programming Guide 4th Edition Dave Shreiner, Mason Woo, Jackie Neider, Tom Davis OpenGL bible OpenGL Reference Manual 3rd Edition Dave Shreiner
3
Reference Websites OpenGL website Basic GLUT information/downloads
Basic GLUT information/downloads
4
Computer Graphics System
Lighting Projection Transformation Shading, Color, Texture mapping Synthetic Camera Viewport Transformation Image Modeling Transformation Coordinate System Viewing Transformation
5
What is OpenGL? Software interface to graphics hardware Basic features
Drawing primitives Transformations Shading: Color, lighting Advanced features Texture mapping Double buffering Frame buffer manipulation Feedback: Picking, selection
6
Creating OpenGL Programs in MFC
7
How to Use It? Include the header files Link the libraries
#include <GL/gl.h> #include <GL/glu.h> #include <GL/glaux.h> Link the libraries ‘opengl32.lib’, ‘glu32.lib’, ‘glaux.lib’ : in MFC -laux -ltk -lGLU -lGL -lXext -lXll -lm : in UNIX
8
Getting Started – MFC OpenGL Programming Visual C++ 6.0
9
New – MFC AppWizard (exe)
10
New – MFC AppWizard (1/7)
11
New – MFC AppWizard (2/7)
12
New – MFC AppWizard (3/7)
13
New – MFC AppWizard (4/7)
14
New – MFC AppWizard (5/7)
15
New – MFC AppWizard (6/7)
16
New – MFC AppWizard (7/7)
17
Workspace
18
Build
19
Execute
20
Project Settings (1/2)
21
Project Settings (2/2)
22
Include Header Files
23
Member Variables and Functions
24
Class Wizard (1/2)
25
Class Wizard (2/2)
26
OnCreate( ) (1/2)
27
OnCreate( ) (2/2) int CPracticeView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here m_pDC = new CClientDC(this); // get device context if( m_pDC == NULL ) ::AfxMessageBox( "fail to get device context" ); return FALSE; } if( !SetupPixelFormat() ) // setup pixel format ::AfxMessageBox( "SetupPixelFormat failed" ); // get rendering context if( ( m_hRC = wglCreateContext(m_pDC->GetSafeHdc()) ) == 0 ) ::AfxMessageBox( "wglCreateContext failed" ); // make current rendering context if( wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE ) ::AfxMessageBox( "wglMakeCurrent failed" ); return 0;
28
OnDestory( )
29
OnEraseBkgnd( ), OnSize( )
30
PreCreateWindow( ), OnDraw( )
31
SetupPixelFormat( ) (1/2)
32
SetupPixelFormat( ) (2/2)
BOOL CPracticeView::SetupPixelFormat(PIXELFORMATDESCRIPTOR* pPFD) { PIXELFORMATDESCRIPTOR pfd = { sizeof( PIXELFORMATDESCRIPTOR ), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; int pixelformat; PIXELFORMATDESCRIPTOR* pPFDtoUse; pPFDtoUse = (0 == pPFD)? &pfd : pPFD; if ( 0 == ( pixelformat = ::ChoosePixelFormat( m_pDC->GetSafeHdc(), pPFDtoUse ) ) ) ::AfxMessageBox( "ChoosePixelFormat failed" ); return FALSE; } if ( FALSE == ::SetPixelFormat( m_pDC->GetSafeHdc(), pixelformat, pPFDtoUse ) ) ::AfxMessageBox( "SetPixelFormat failed" ); return TRUE;
33
All the drawing commands must be in this function!!!
DrawScene( ) // set the background color // clear screen (with the background color) All the drawing commands must be in this function!!!
34
Result – Simple Program
35
Drawing OpenGL Primitives
36
OpenGL Primitives Points Lines Triangles Quadrilaterals Polygons
37
DrawScene( ) – 2D Points // A // B // C // D
38
Result – 2D Points B C A D
39
DrawScene( ) – 2D Lines (1/3)
// line AB // line CD
40
Result – 2D Lines (1/3) B C A D
41
DrawScene( ) – 2D Lines (2/3)
// line AB // line BC // line CD
42
Result – 2D Lines (2/3) B C A D
43
DrawScene( ) – 2D Lines (3/3)
// line AB // line BC // line CD // line DA
44
Result – 2D Lines (3/3) B C A D
45
DrawScene( ) – Line Pattern
Pair
46
Result – Line Pattern
47
DrawScene( ) – Triangles (1/3)
// triangle ABC // discarded
48
Result – Triangles (1/3) B C A D
49
DrawScene( ) – Triangles (2/3)
// triangle ABC // triangle BCD
50
Result – Triangles (2/3) v3 v1 v5 v4 v2 B C A D
51
DrawScene( ) – Triangles (3/3)
// triangle ABC // triangle ACD
52
Result – Triangles (3/3) v3 v2 v4 v1 B C v5 A D
53
DrawScene( ) – Polygon Pattern
54
Result – Polygon Pattern
55
DrawScene( ) – Quadrilaterals
// quad ABCD // quad EFGH
56
Result – Quadrilaterals (1)
B C F G A D E H
57
DrawScene( ) – Quadrilaterals
// quad ABDC // quad CDFE // quad EFHG
58
Result – Quadrilaterals (2)
B D F H A C E G
59
DrawScene( ) – Polygons
// polygon ABCDEFGH
60
Result – Polygons B C D E A H G F
61
Applications - Interaction by Mouse Event
62
Mouse Event Handling
63
New Member Variables
64
Constructor
65
Mouse Button Up // call OnDraw()
66
Mouse Button Down
67
Mouse Button Move
68
DrawScene( )
69
Result – Mouse Dragging
70
Result – After Dragging
71
Applications - Animation by Timer
72
Rotating a Square Four vertices of the square
Four equally spaced points on a circle (-sinθ, cosθ) (cosθ, sinθ) (-cosθ, -sinθ) (sinθ, -cosθ)
73
Class Wizard
74
New Variables
75
Constructor
76
OnCreate( )
77
OnSize( )
78
DrawScene( )
79
Math Header File
80
OnTimer( )
81
Result – Rotating a Square
82
Double Buffering (1/2)
83
Double Buffering (2/2)
84
Interaction by the Keyboard
85
Key Event Handling
86
Interaction by the Menu
87
Resources – Menu (1/2)
88
Resources – Menu (2/2)
89
Class Wizard
90
Reset Function
91
Basic 3D Programming
92
Contents Camera and Objects Orthographic Projection Drawing a Cube
Locating the Camera Building Objects Hidden Surface Removal Rotating a Color Cube Exercises
93
Cameras and Objects (1/3)
Projection Synthetic camera model Projectors, center of projection, projection plane
94
Cameras and Objects (2/3)
Projection plane In front of the camera
95
Cameras and Objects (3/3)
View frustum 6 parameters Truncated pyramid Clipping volume
96
Orthographic Projection (1/2)
y void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) x z
97
Orthographic Projection (2/2)
98
Drawing a Cube (1/3) GLUT Library
glut.h (include), glut32.lib (lib), glut32.dll (system32) void glutWireCube(GLdouble size) void glutSoildCube(GLdouble size)
99
Drawing a Cube (2/3)
100
Drawing a Cube (3/3)
101
GLUT Objects Sphere, cone, Utah teapot, etc.
void glutWireSphere(GLdouble radius, GLint slices, GLint stacks) void glutSolidSphere(GLdouble radius, GLint slices, void glutWireTorus(GLdouble inner, GLdouble outer, GLint sides, GLint slices) void glutSolidTorus(GLdouble inner, GLdouble outer, void glutWireTeapot(GLdouble size) void glutSolidTeapot(GLdouble size)
102
Locating Camera (1/3) Position and orientation of the camera
Eye position, target point, up vector void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble atx, GLdouble aty, GLdouble atz, GLdouble upx, GLdouble upy, GLdouble upz)
103
Locating Camera (2/3)
104
Locating Camera (3/3)
105
Building Objects (1/6)
106
Building Objects (2/6) GLfloat vertices[8][3] = { { -1, -1, 1 }, { -1, 1, 1 }, { 1, 1, 1 }, { 1, -1, 1 }, { -1, -1, -1 }, { -1, 1, -1 }, { 1, 1, -1 }, { 1, -1, -1 } }; GLfloat colors[8][3] = { { 0, 0, 1 }, { 0, 1, 1 }, { 1, 1, 1 }, { 1, 0, 1 }, { 0, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 }, { 1, 0, 0 } }; glBegin(GL_QUADS); // quad #1 glColor3fv(colors[0]); glVertex3fv(vertices[0]); glColor3fv(colors[3]); glVertex3fv(vertices[3]); glColor3fv(colors[2]); glVertex3fv(vertices[2]); glColor3fv(colors[1]); glVertex3fv(vertices[1]); // quad #2 glColor3fv(colors[7]); glVertex3fv(vertices[7]); glColor3fv(colors[6]); glVertex3fv(vertices[6]);
107
Building Objects (3/6) // quad #3
glColor3fv(colors[3]); glVertex3fv(vertices[3]); glColor3fv(colors[0]); glVertex3fv(vertices[0]); glColor3fv(colors[4]); glVertex3fv(vertices[4]); glColor3fv(colors[7]); glVertex3fv(vertices[7]); // quad #4 glColor3fv(colors[1]); glVertex3fv(vertices[1]); glColor3fv(colors[2]); glVertex3fv(vertices[2]); glColor3fv(colors[6]); glVertex3fv(vertices[6]); glColor3fv(colors[5]); glVertex3fv(vertices[5]); // quad #5 // quad #6 glEnd();
108
Building Objects (4/6)
109
Building Objects (5/6)
110
Building Objects (6/6)
111
Hidden Surface Removal (1/2)
Z-Buffer (depth buffer) Extra storage to store depth information
112
Hidden Surface Removal (2/2)
113
Rotating a Cube (1/3)
114
Rotating a Cube (2/3)
115
Rotating a Cube (3/3)
116
Transformation
117
Contents OpenGL Transformation Matrices Translation Rotation Scaling
118
OpenGL Transformation Matrices
Matrix modes Model-view and projection matrices Three basic functions Translation, rotation, scaling 3D vertices 2D vertices model-view projection CTM (Current Transformation Matrix) glTranslatef(dx, dy, dz); glRotatef(angle, vx, vy, vz); glScalef(sx, sy, sz);
119
void glTranslate{fd} (TYPE dx, TYPE dy, TYPE dz)
Translation (1/5) Add a displacement to every vertex on the object void glTranslate{fd} (TYPE dx, TYPE dy, TYPE dz) object centered at origin object moved away from the camera
120
Translation (2/5)
121
Translation (3/5)
122
Translation (4/5)
123
Translation (5/5)
124
glPushMatrix, glPopMatrix (1/2)
perform a transformation and then return to the same state as before its execution glPushMatrix(); glTranslatef(.....); glRotatef(.....); glScalef(.....); /* draw object here */ glPopMatrix();
125
glPushMatrix, glPopMatrix (2/2)
126
void glRotate{fd} (TYPE angle, TYPE ax, TYPE ay, TYPE az)
Rotation Rotate in counterclockwise direction About the given axis with fixed point of the origin void glRotate{fd} (TYPE angle, TYPE ax, TYPE ay, TYPE az) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(xf, yf, zf); glRotatef(angle, ax, ay, az); glTranslatef(-xf, -yf, -zf) general rotation
127
void glScale{fd} (TYPE sx, TYPE sy, TYPE sz)
Scaling Make an object bigger or smaller With scale factors and fixed point of the origin void glScale{fd} (TYPE sx, TYPE sy, TYPE sz) scaling with nonuniform scale factors
128
Viewing
129
Contents Projections in OpenGL Parallel viewing Perspective viewing
Orthographic projection Perspective viewing Perspective projection Walk through a scene Look-at function Shadow projection
130
Definition of Eye Position
131
Initialization of Eye Position
132
Modification – DrawScene( )
133
OnKeyDown( )
134
Result – Walking Though a Scene
135
Light & Material
136
Contents Phong reflection model OpenGL lighting
Diffuse, specular, and ambient reflections OpenGL lighting Specifying a light source Point, distant, and spot lights Specifying a material Exercise
137
DrawScene( ) – WireTeapot
138
Wired Utah Teapot
139
Shade Model glShadeModel(GLenum mode) mode Change the shading model
Flat or smooth (Gouraud) Can be called in ... OnSize(), DrawScene() mode GL_FLAT GL_SMOOTH: Default value
140
Working with Normals (1/2)
Flat shading p0 p1 p2 glShadeModel(GL_FLAT); glBegin(GL_TRIANGLES); glNormal3fv(n); glVertex3fv(p1); glVertex3fv(p2); glVertex3fv(p3); glEnd();
141
Working with Normals (2/2)
Smooth shading glShadeModel(GL_SMOOTH); glBegin(GL_TRIANGLES); glNormal3fv(n1); glVertex3fv(p1); glNormal3fv(n2); glVertex3fv(p2); glNormal3fv(n3); glVertex3fv(p3); glEnd();
142
Flat-Shaded Sphere
143
Smooth-Shaded Sphere
144
Phong Reflection Model
Simple analytic model: Diffuse reflection + Specular reflection + Emission + “Ambient”
145
Diffuse Reflection (1/2)
Surface reflects equally in all directions Examples: chalk, clay
146
Diffuse Reflection (2/2)
How much light is reflected? Depends on angle of incident light dL = dA cos Q Cosine law (dot product)
147
Specular Reflection (1/3)
Strongest reflection near mirror angle Examples: mirrors, metals
148
Specular Reflection (2/3)
How much light is seen? Depends on ... Angle of incident light and Angle to viewer
149
Specular Reflection (3/3)
Phong model {cos(a)}n
150
Emission Light emitting directly from polygon Emission ≠ 0
151
Total Illumination Result
Ambient Reflection of all indirect illumination Total Illumination Result
152
OpenGL Lighting Target to enable All colors will be assigned based on
“Lighting calculation” mode Each “light source” All colors will be assigned based on Light sources and Material properties not by glColor*( ) glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); . glEnable(GL_LIGHTn);
153
DrawScene( ) – SolidTeapot
154
Solid Utah Teapot
155
Enable Lighting (1/3)
156
Enable Lighting (2/3)
157
Specifying a Light Source (1/2)
Defaults for “glEnable(GL_LIGHT0)” No ambient light Diffuse and specular component ex) White (1.0, 1.0, 1.0, 1.0) Position ex) (0.0, 0.0, 1.0, 0.0) distant light
158
Specifying a Light Source (2/2)
Create a light source light GL_LIGHT0, GL_LIGHT1, … param GL_POSITION, GL_DIFFUSE, GL_SPECLAR, GL_AMBIENT glLight{if}(GLenum light, GLenum param, TYPE value); glLight{if}v(GLenum light, GLenum param, TYPE *value);
159
Light Sources Distant light Point light Spot light
GL_POSITION, GL_DIFFUSE, GL_SPECULAR, GL_AMBIENT Point light GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION Spot light GL_SPOT_DIRECTION, GL_SPOT_CUTOFF, GL_SPOT_EXPONENT
160
Point Light Source (1/2)
161
Point Light Source (2/2)
162
Control of Light Position (1/4)
163
Control of Light Position (2/4)
164
Control of Light Position (3/4)
165
Control of Light Position (4/4)
166
Specifying All Terms (1/2)
167
Specifying All Terms (2/2)
168
Point Light – Attenuation (1/2)
169
Point Light – Attenuation (2/2)
170
Spot Light (1/4)
171
Spot Light (2/4)
172
Spot Light (3/4)
173
Spot Light (4/4)
174
Multiple Lights (1/2)
175
Multiple Lights (2/2)
176
Specifying a Material (1/5)
Reflectivity properties of a material face GL_FRONT, GL_BACK, GL_FRONT_AND_BACK name GL_DIFFUSE, GL_SPECLAR, GL_AMBIENT, GL_AMBIENT_AND_DIFFUSE, GL_EMISSION, GL_SHININESS glMaterial{if}(GLenum face, GLenum name, TYPE value); glMaterial{if}v(GLenum face, GLenum name, TYPE *value);
177
Specifying a Material (2/5)
178
Specifying a Material (3/5)
179
Specifying a Material (4/5)
180
Specifying a Material (5/5)
181
White Shiny Material (1/2)
182
White Shiny Material (2/2)
183
Brass Material (1/2)
184
Brass Material (2/2)
185
Red Plastic Material (1/2)
186
Red Plastic Material (2/2)
187
Multiple Objects (1/2)
188
Multiple Objects (2/2)
189
Exercise Create yours!! color plate 25
190
Texture Mapping
191
Contents Texels and textures Constructing a texture map
Texture coordinates Texture parameters A rotating cube with texture Texture mapping with images Exercise
192
Texels and Textures Texels – texture elements
Texture coordinates (s, t)
193
Constructing a Texture Map
Steps: Read an image for the texture map Define parameters that determine how the texture should be applied Define texture coordinates for the vertices
194
1D, 2D, & 3D Texture Maps void glTexImage1D(GLenum target, GLint level, GLint iformat, GLsizei width, GLint border, GLenum format, GLenum type, Glvoid *texels); void glTexImage2D(GLenum target, GLint level, GLint iformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, Glvoid *texels); void glTexImage3D(GLenum target, GLint level, GLint iformat, GLsizei width, GLsizei height, Glsizei depth, GLint border, GLenum format, GLenum type, Glvoid *texels);
195
2D Texture Map A continuous image in (s, t) space Texture Map
Texture Image
196
Texture Coordinates (1/2)
Mapping between points on geometric objects and texels ex) Mapping the texture to a quadrilateral void glTexCoord{1234}{sifd}(TYPE scoord, ...); void glTexCoord{1234}{sifd}v(TYPE *coord); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3fv(vertex[0]); glTexCoord2f(0.0, 1.0); glVertex3fv(vertex[1]); glTexCoord2f(1.0, 1.0); glVertex3fv(vertex[2]); glTexCoord2f(1.0, 0.0); glVertex3fv(vertex[3]); glEnd();
197
Texture Coordinates (2/2)
A checkerboard texture to a quadrilateral Texture Map Quadrilateral
198
Texture Parameters (1/3)
Control over how the texture is applied to the surface Deciding what to do if values are out of (0, 1) GL_CLAMP void glTexParameter{if}(GLenum target, GLenum name, TYPE value); void glTexParameter{if}v(GLenum target, GLenum name, TYPE *value); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
199
Texture Parameters (2/3)
Pre-image of the pixel Texture mapping is not really applied to the surface but rather a pixel in screen coordinates Magnification Each texel covers multiple pixels Minification Each pixel covers multiple texels Magnification Minification
200
Texture Parameters (3/3)
Point sampling OpenGL uses to average a 2x2 array of neighboring texels GL_LINEAR glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
201
A Rotating Cube with Texture
202
Constructor
203
DrawQuad( )
204
DrawScene( )
205
A Color Cube
206
DrawQuad( )
207
Texture Images Load a BMP file for a texture image
208
OpenGL Auxiliary Library
209
Project Settings
210
DrawScene( )
211
LoadTexture( )
212
LoadTexture( )
213
Result – Texture Mapping (1)
214
Multiple Texture Images
215
LoadTexture( )
216
DrawScene( )
217
Result – Texture Mapping (2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.