introduction to opengl 1. 2 opengl a graphics rendering api introduced in 1992 by silicon graphics...
TRANSCRIPT
OpenGL
• A Graphics rendering API introduced in 1992 by Silicon Graphics Inc
• Provide the low-level functions to access graphics hardware directly
• Cross-platform
3
Related API
• GLU (OpenGL Utility Library)– Part of OpenGL– Use the prefix of glu (ex: gluLookAt())
• GLUT (OpenGL Utility Toolkit)– Not officially part of OpenGL– hide the complexities of differing window system
APIs. – Use the prefix of glut (ex: glutDisplayFunc())
4
OpenGL Utility Toolkit (GLUT)
• Visual C++– http://www.xmission.com/~nate/glut.html– glut32.dll to %WinDir%\System, – glut32.lib to $(MSDevDir)\..\..\VC\lib– glut.h to $(MSDevDir)\..\..\VC\include\GL.
5
A Simple Example
#include <GL/glut.h>
void GL_display();
void GL_reshape(GLsizei w, GLsizei h);
void main(void)
{
glutCreateWindow("Sample");
glutDisplayFunc(GL_display);
glutReshapeFunc(GL_reshape);
glutMainLoop();
}
6
A Simple Example
void GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}7
A Simple Example
void GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}Color Buffer Depth Buffer 8
A Simple Example
void GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}9
A Simple Example
void GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}11
OpenGL as a State Machine
• Put a value into various states, then it will remain in effect until being changed.– e.g. glColor*()
• Many state variables are enabled or disabled with glEnable(), glDisable()– e.g. glEnable(GL_LIGHT0)
12
A Simple Examplevoid GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}13
Command Syntax
glVertex3f( 1.0, 1.0, 0.0 );
OpenGL API Name The number and types of arguments
Data Type C-Language Type OpenGL Type Definition
b 8-bit integer signed char GLbyte
s 16-bit integer short GLshort
i 32-bit integer long GLint, GLsizei
f 32-bit floating-point float GLfloat, GLclampf
d 64-bit floating-point double GLdouble, GLclampd
ub 8-bit unsigned integer unsigned char GLubyte, GLboolean
us 16-bit unsigned integer unsigned short GLushort
ui 32-bit unsigned integer unsigned long GLuint, GLenum, GLbitfield14
A Simple Examplevoid GL_display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_LINES );
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
glFlush();
}15
A Simple Example
void GL_reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
16
A Simple Example
void GL_reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
17
A Simple Example
void GL_reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
18
Matrix in OpenGL
• There are two matrix stacks.– ModelView matrix (GL_MODELVIEW)– Projection matrix (GL_PROJECTION)
• When we call functions of transformation, we should change to the appropriate matrix stack first.
glMatrixMode(GL_MODELVIEW);//now we are in modelview matrix stack!//do modelview transformation here…..
glMatrixMode(GL_PROJECTION);//now we are in projection matrix stack!//do projection transformation here….
19
ModelView Matrix
• Modeling Transformation• Perform rotate, translate, scale and
combinations of these transformations to the object.
• Viewing Transformation• To positioning and aiming the camera
20
Modeling Transformations
• glTranslate{fd}(x, y, z)– Multiplies current matrix by a matrix that moves
an object by x,y,z
glTranslatef( 0, 0, -1 )
21
Modeling Transformations
• glRotate{fd}(angle, x, y, z )– Multiplies current matrix by a matrix that rotates
an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees
glRotatef( 45.0, 0, 0, 1)
22
Modeling Transformations
• glScale{fd} (x, y, z)– Multiplies current matrix by a matrix that scales
an object along axes.
glScalef( 2.0, -0.5, 1.0 )
23
Matrix in OpenGL
• Matrix multiplications always apply to the top of matrix stack.
Top matrixIn the stack
Translation matrix(glTranslatef)
X
25
Matrix in OpenGL
• The order of transformations is critical.
26
glTranslatef( 1,0,0 );
glRotatef(45.0, 0,0,1 );
drawObject();
glRotatef(45.0, 0,0,1 );
glTranslatef( 1,0,0 );
drawObject();
Projection Transformations
• Orthographic Projection• glOrtho( GLdouble left, GLdouble right, GLdouble
bottom, GLdouble top, GLdouble near, GLdouble far )• gluOrtho2D( GLdouble left, GLdouble right, GLdouble
bottom, GLdouble top)
27
Projection Transformations
• Perspective Projection– gluPerspective( GLdouble fovy, GLdouble
aspect, GLdouble near, GLdouble far );
28
Projection Transformations
• Perspective Projection– glFrustum( GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );
29
A Simple Example
void GL_reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
30
OpenGL Transformations
31
ModelviewMatrix
ModelviewMatrix
ViewportTransformation
ViewportTransformation
ProjectionMatrix
ProjectionMatrix
PerspectiveDivision
eye coordina
tes
clip coordina
tes
normalized device
coordinates
window coordina
tes
XYZW
VertexVertex
XY
glTranslatefglRotatefglScalfgluLookAt
gluPerspectivegluOrtho2DglFrustumglOrtho glViewport()
Matrix in OpenGL
• Mantain matrix stack– glPushMatrix() : used to save current stack– glPopMatrix() : used to restore previous
stack
glPushMatirx()
x
glScalef glPopMatrix()
32
Matrix in OpenGLglPushMatrix();
glTranslatef (-1.0, 0.0, 0.0); glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix();
glScalef (2.0, 0.4, 1.0); glutWireCube (1.0);
glPopMatrix();
glPushMatrix(); glTranslatef (1.0, 0.0, 0.0); glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix();
glScalef (2.0, 0.4, 1.0); glutWireCube (1.0);
glPopMatrix(); glPopMatrix();
glPopMatrix();
33
Matrix in OpenGL
• Reference: Object Hierarchy– http://www.gamedev.net/reference/articles/
article1267.asp
34