Open GL Programming - National Chiao Tung University

Download Report

Transcript Open GL Programming - National Chiao Tung University

Open GL Programming
Speaker: 彭任右
Date: 2005/10/3
Coordinate System
y
y
• World
z+
left handed
right handed
x
z+
• Window
View Port
(0,0)
(w,h)
(w,h)
(0,0)
x
OpenGL Color Models
• RGBA or Color Index
color index mode
Red Green
1
2
4
8
ww
16
ww
Blue
0
1
2
3
24
25
26
Display
123
www
219
74
www
RGBA mode
OpenGL Command Formats
glVertex3fv( v )
Number of
components
2 - (x,y)
3 - (x,y,z)
4 - (x,y,z,w)
Data Type
b
ub
s
us
i
ui
f
d
-
byte
unsigned byte
short
unsigned short
int
unsigned int
float
double
Vector
omit “v” for
scalar form
glVertex2f( x, y )
Buffers
•
•
•
•
•
Color Buffer
Depth Buffer
Stencil Buffer
Accumulation Buffer
Selection Buffer
State Machine
• Set State
–
–
–
–
glPointSize(size);
glLineWidth(width);
glLineStipple(repeat, pattern);
glShadeModel(GL_SMOOTH);
• Get State
– glGet*();
• Enable Features
– glEnable(GL_LIGHTING);
– glDisable(GL_TEXTURE_2D);
– glIsEnable(GL_DEPTH_TEST);
Framework
• Clear the window
• Set the Viewing, Projection, and Viewport
transformation
• Loop for each primitive
– Modeling Transformation
– Specify the primitive type, attributes, and
rendering states
• Flush or Swap buffer
Clearing the Window
• glClearColor(red, green, blue,
alpha);
– From 0.0f to 1.0f
• glClearDepth(1.0f);
• glClear();
– glClear(GL_COLOR_BUFFER_BIT);
– glClear(GL_DEPTH_BUFFER_BIT);
– Use the logical or ( | ) to combine
Specifying a Color
• glColor3f()
–
–
–
–
–
–
–
–
glColor3f(0.0,
glColor3f(1.0,
glColor3f(0.0,
glColor3f(0.0,
glColor3f(1.0,
glColor3f(0.0,
glColor3f(0.0,
glColor3f(1.0,
0.0,
0.0,
1.0,
0.0,
1.0,
0.0,
1.0,
1.0,
0.0) – black
0.0) – red
0.0) – green
1.0) – blue
0.0) – yellow
0.0) – magenta
1.0) – cyan
1.0) – White
MVPV
Matrix Operation
• glMatrixMode()
– GL_PROJECTION
– GL_MODELVIEW
– GL_TEXTURE
• glLoadIdentity()
• glLoadMatrix()
• glMultMatrix()
• glPushMatrix()
• glPopMatrix()
Matrix Multiplication
•
•
•
•
•
•
•
•
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrix(N);
glMultMatrix(M);
glMultMatrix(L);
glBegin(GL_POINTS);
glVertex3f(v);
glEnd();
• The vertex transformation is N(M(Lv))
Modeling Transformation
• Move object
glTranslate{234}{fd}( x, y, z )
• Rotate object around arbitrary axis
glRotate{234}{fd}( angle, x, y,
z )
– angle is in degrees
• Stretch, shrink, or mirror object
glScale{234}{fd}( x, y, z )
Transformation Tutorial
Viewing Transformation
• Position the camera/eye
in the scene
– gluLookAt(
posx, posy, posz,
viewx, viewy, viewz,
upx,
upy,
upz);
Projection Transformation
• Orthogonal Projection
• Perspective Projection
Orthogonal Projection
• glOrtho(left, right, bottom, top,
near far);
• gluOrtho2D( left, right, bottom,
top )
Perspective Projection
• glFrustum(left,
right, bottom,
top, zNear,
zFar);
• gluPerspective
(fovy, aspect,
zNear, zFar);
Projection Tutorial
Viewport Transformation
• View Port
– usually same as window size
– viewport aspect ratio should be same as projection
transformation or resulting image may be distorted
– glViewport(x, y, width, height);
height
(x,y)
(0,0)
width
OpenGL Geometric Primitives
• All geometric primitives are specified by
vertices
GL_POINTS
GL_LINES
GL_LINE_STRIP
GL_LINE_LOOP
GL_POLYGON
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
GL_QUADS GL_QUAD_STRIP
Shapes Tutorial
Drawing Primitives
•
•
•
•
glBegin(GLenum mode);
glColor*();
glVertex*();
glEnd();
glShadeModel()
• glShadeModel(GLenum mode);
– Flat Shading
• GL_FLAT
– Smooth Shading
• GL_SMOOTH
glPolygonMode()
• glPolygonMode(face, mode);
– GLenum face
• GL_FRONT
• GL_BACK
– GLenum mode
• GL_POINT
• GL_LINE (hint: wireframe)
• GL_FILL
Depth
• Depth
– think as distance from view point
• Depthe Test
y
left handed
x
– glEnable(GL_DEPTH_TEST);
– If we draw rectangle first, the ellipse will cove the rectangle when
the depth test is closed.
Z = 1.0
Z = 1.0
Z = 2.0
Without Depth Test
z+
Z = 2.0
With Depth Test
Culling
• glFrontFace(GLenum mode);
– GL_CW
– GL_CCW
• glCullFace(GLenum mode);
– GL_FRONT
– GL_BACK
– GL_FRONT_AND_BACK
• glEnable(GL_CULL_FACE);
Counterclockwise winding
(Front facing)
Force Completion of Drawing
• Win32
– SwapBuffers(hDC);
• GLUT
– glutSwapBuffer();
glRenderMode()
• GLint glRenderMode(GLenum
mode);
– GL_RENDER
– GL_SELECTION
– GL_FEEDBACK
Selection Mode
• Method to determine which primitives are
inside the viewing volume
• Select selection mode for rendering
– glRenderMode(GL_SELECT);
Selection Buffer
• glSelectBuffer(GLsizei size,
GLuint *buffer);
• 4 elements per hit
–
–
–
–
# of names on names stack
ZMin
ZMax
Bottom of names stack
Names Stack
• To identify a primitive, give it a name
– “names” are just integer values, not strings
• Names are stack based
– allows for hierarchies of primitives
– glInitNames();
– glPushName();
– glPopName();
– glLoadName();
Sample
void RenderScene()
{
// “set the rendering states
glInitNames();
glLoadName(1);
“draw something”
glLoadName(2);
// draw something else … continue
}
Picking
• gluPickMatrix(x, y, width, height, viewport);
– x, y should be set as xMouse and yMouse
• glGetIntegerv(GL_VIEWPORT, viewport);
Sample
void pick(xPos, yPos)
{
glSelectBuffer(LENGTH, selectBuff);
glMatrixMode(GL_PROJECTION);
glRenderMode(GL_SELECTION);
glLoadIdentity();
gluPickMatrix(x, y, 2, 2, viewport);
gluPerspective(fovy, aspect, near, far);
RenderScene();
hits = glRenderMode(GL_RENDER);
Cout << we pick << selectBuff[3] << object;
}