Computer Graphics 3 Lecture 3: OpenGL. Pr. Min Chen Dr. Benjamin Mora. University of Wales Swansea. 1. Benjamin Mora. Content. Introduction to OpenGL-NVidia Dawn Demo. Different “drawable” primitives in OpenGL. OpenGL Pipeline and Matrix Transformations. Lighting. Textures.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Computer Graphics 3Lecture 3:OpenGL
Pr. Min Chen
Dr. Benjamin Mora
University of Wales Swansea
1
Benjamin Mora
University of Wales Swansea
2
Benjamin Mora
University of Wales Swansea
3
Benjamin Mora
University of Wales Swansea
4
Benjamin Mora
University of Wales Swansea
5
Benjamin Mora
University of Wales Swansea
6
Benjamin Mora
University of Wales Swansea
7
Benjamin Mora
University of Wales Swansea
8
Benjamin Mora
University of Wales Swansea
9
Benjamin Mora
University of Wales Swansea
10
Benjamin Mora
Projection
Camera System
Primitive
University of Wales Swansea
11
Benjamin Mora
Initial Frame-Buffer
After the second primitive rasterization
After the first primitive rasterization
University of Wales Swansea
12
Benjamin Mora
University of Wales Swansea
13
Benjamin Mora
5
5
4
4
1
1
3
3
2
2
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
GL_POLYGON
1
1
1
2
1
2
3
2
3
2
3
1
2
3
4
4
4
4
4
5
5
5
3
5
6
6
GL_TRIANGLE_STRIP
GL_TRIANGLES
GL_TRIANGLE_FAN
GL_QUADS
GL_QUAD_STRIP
University of Wales Swansea
14
Benjamin Mora
University of Wales Swansea
15
Benjamin Mora
Rasterization
Linear Interpolation
University of Wales Swansea
16
Benjamin Mora
University of Wales Swansea
17
Benjamin Mora
University of Wales Swansea
18
Benjamin Mora
Eye/Camera coordinates
World coordinates
Modelview Matrix
Projection Matrix
Normalized device coordinates
Clip coordinates
Viewport Transformation
Perspective Division
Window Coordinates
University of Wales Swansea
19
Benjamin Mora
Object as stored in memory
z
World coordinate system
x
o
University of Wales Swansea
20
Benjamin Mora
z
x
o
1-Modelview transform: Moving objects in space
2-Projection transform: Take into consideration the camera. Coordinates are now expressed in the camera coordinate system
3-w division followed by the viewport transform then occur to find the vertex projections. Once this done for a sufficient number vertices (e.g., 3 for a triangle), object rasterization can happen
University of Wales Swansea
21
Benjamin Mora
University of Wales Swansea
22
Benjamin Mora
University of Wales Swansea
23
Benjamin Mora
Rotation matrix created from an angle and a line in the direction x,y,z that cross the origin
Translation Matrix
University of Wales Swansea
24
Benjamin Mora
University of Wales Swansea
25
Benjamin Mora
University of Wales Swansea
26
Benjamin Mora
Projection on x [-1..1]
Fragment Depth
University of Wales Swansea
27
Benjamin Mora
x+width,y+height
image
x,y
0,0
University of Wales Swansea
28
Benjamin Mora
x+width,y+height
image
x,y
0,0
University of Wales Swansea
29
Benjamin Mora
Final image
Final z-buffer
University of Wales Swansea
30
Benjamin Mora
If the rasterized z-value is less than the current z-value
Then replace the previous color and z-value by the new ones
University of Wales Swansea
31
Benjamin Mora
University of Wales Swansea
32
Benjamin Mora
http://www.opengl.org/resources/tutorials/advanced/advanced97/notes/node196.html
University of Wales Swansea
33
Benjamin Mora
University of Wales Swansea
34
Benjamin Mora
the vertices, and then interpolated
for every fragment.
University of Wales Swansea
35
Benjamin Mora
source.
in all direction.
University of Wales Swansea
36
Benjamin Mora
http://en.wikipedia.org/wiki/Phong_shading
University of Wales Swansea
37
Benjamin Mora
University of Wales Swansea
38
Benjamin Mora
University of Wales Swansea
39
Benjamin Mora
University of Wales Swansea
40
Benjamin Mora
University of Wales Swansea
41
Benjamin Mora
University of Wales Swansea
42
Benjamin Mora
University of Wales Swansea
43
Benjamin Mora
University of Wales Swansea
44
Benjamin Mora
University of Wales Swansea
45
Benjamin Mora
GL_DEPTH_BUFFER_BIT |
GL_ACCUM_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT );
University of Wales Swansea
46
Benjamin Mora
glBegin(GL_LINES); //Specify lines. Could be GL_TRIANGLES, etc…
//First vertex
glColor3f(1.0,0.,0.);//3 float colors for the first vertex
glNormal3f(0.707,0.707,0); //first normal
glTexcoord2f(0,0); //First texture coordinate
glVertex3f(500,100,2); //first vertex
//Second vertex
glColor4f(1.0,0.,0.,1.);//4 float colors (last value: opacity)
glNormal3fv(v); //gives a vector of float as parameters
glTexcoord2f(1,1); //Second texture coordinate
glVertex3d(500,100,2);//double instead of float
glEnd(); // End of the vertex flow
University of Wales Swansea
47
Benjamin Mora
void GLRenderer::InitLighting()
{
float ambiant[4]= {0.2,0.2,0.2,1.};
float diffuse[4]= {0.7,0.7,0.7,1.};
float specular[4]= {1,1,1,1.};
float exponent=8;
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//Be careful here: the lights go through the OpenGL transform pipeline
float lightDir[4] = {0,0,1,0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambiant);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,exponent);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDir);
}
University of Wales Swansea
48
Benjamin Mora
unsigned int *textureId=new unsigned int[nbOfTextures];
glGenTextures(nbOfTextures,textureId);
for (i=0;i<nbOfTextures;i++)
{
glBindTexture(GL_TEXTURE_2D, textureId[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, textureDimensionsX[i], textureDimensionsY[i], 0,GL_RGB,GL_UNSIGNED_BYTE, texture[i]);
}
University of Wales Swansea
49
Benjamin Mora
University of Wales Swansea
50
Benjamin Mora
University of Wales Swansea
51
Benjamin Mora
University of Wales Swansea
52
Benjamin Mora
void VertexPointer ( int size, enum type, sizei stride, void *pointer ) ;
void ColorPointer ( int size, enum type, sizei stride, void *pointer ) ;
void TexCoordPointer ( int size, enum type, sizei stride, void *pointer ) ;
void IndexPointer ( enum type, sizei stride, void *pointer ) ;
void NormalPointer ( enum type, sizei stride, void *pointer ) ;
void EdgeFlagPointer ( sizei stride, void *pointer ) ;
www.opengl.org
University of Wales Swansea
53
Benjamin Mora
void EnableClientState ( enum array ) ;
void DisableClientState ( enum array ) ; void ArrayElement ( int i ) ; void DrawArrays ( enum mode, int first, sizei count ) ;
void DrawElements ( enum mode, sizei count, enum type, void *indices ) ;
University of Wales Swansea
54
Benjamin Mora
University of Wales Swansea
55
Benjamin Mora
char* extensionsList = (char*) glGetString(GL_EXTENSIONS);
// All the supported extensions are inside the string
// extensionsList.
//Example for getting the 3D texture functionality.
//First step: Declare a function type
typedef void (APIENTRY * PFNGLTEXIMAGE3DPROC)
(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
PFNGLTEXIMAGE3DPROC glTexImage3D; // Declare a function pointer
glTexImage3D= (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
//Get the pointer address;
University of Wales Swansea
56
Benjamin Mora
University of Wales Swansea
57
Benjamin Mora
University of Wales Swansea
58
Benjamin Mora
Image created by Steve Leach using OpenOffice Draw (v1.9.95), 30 May 2005 for use in the Shadow Volumes article.
http://en.wikipedia.org/wiki/Shadow_volume
University of Wales Swansea
59
Benjamin Mora
University of Wales Swansea
60
Benjamin Mora