You are on page 1of 25

OpenGL Basics

OpenGL is a massive basic


powerful, flexible standard
platform and windowing
independent rendering system
Mel Slater, Anthony Steed 1997-1999

1
What Is OpenGL?

Graphics rendering API


high-quality color images composed of
geometric and image primitives
window system independent
operating system independent
An application programming interface
(API)
A (low-level) Graphics rendering API

2
2
Philosophy of OpenGL
Platform independent
Window system independent
Rendering only
Aims to be real-time
Takes advantage of graphics hardware
where it exists
Client-server system
Standard supported by major companies

3
OpenGL as a Renderer
Geometric primitives
points, lines and polygons
Image Primitives
images and bitmaps
separate pipeline for images and
geometry
linked through texture mapping
Rendering depends on state
colors, materials, light sources, etc.
4
4
OpenGL Basics

OpenGLs primary function Rendering

Rendering? converting geometric/mathematical


object descriptions into frame buffer values

OpenGL can render:


Geometric primitives
Bitmaps and Images (Raster primitives)

5
Code Example

void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1,1,0,1);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
.
6
Sample Example

Void DrawQuad(GLfloat color[])


{
glColor3f(0,0,1);
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(1.0, 0,0);
glVertex2f(1.0, 1.0);
glVertex2f(0.0, 1.0);
glEnd();
}

7
Generating Output
Output generated within a glBegin(),
glEnd() block:
glBegin(GL_POINTS);
glVertex2d(1.0,1.0);
glVertex2d(2.0,1.0);
glVertex2d(2.0,2.0);
glEnd();

8
Drawing Mode
glBegin(GLenum glBegin(GL_POLYGON);
mode) glVertex2d(1.0,1.0);
mode includes glVertex2d(2.0,1.0);
GL_POINTS glVertex2d(2.0,2.0);
GL_LINES glEnd();
GLINE_STRIP
GL_LINE_LOOP
GL_POLYGON
convex only
triangles
quadrilaterals
9
glVertexnt
glVertex2d(GLdouble x, GLdouble y);
glVertex3f(GLfloat x, GLfloat y, GLfloat z);
glVertex2i(GLint x, GLint y);
glVertex3d(GLdouble x,GLdouble y,
GLdouble z);
n = 2,3,4
t = d, f, i, s
glVertex4f(GLdouble x, GLdouble y,
GLdouble z, GLdouble w);

10
Modelview Matrix
glMatrixMode(GL_MODELVIEW)
making changes to modelview

11
Matrix Operations
glLoadMatrix{f}{d}(const GLfloat *m);
replaces current matrix
glMultMatrix{f}{d} (const GLfloat *m);
if t is current matrix then tm is the new one
glPushMatrix{f}{d} ();
pushes copy of current matrix down on stack;
glPopMatrix();
restores top of stack to be current matrix.

12
Example: Object Hierarchy
GObject *object; //pointer to graphics object
glMatrixModel(GL_MODELVIEW);
/*push and duplicate current matrix*/
glPushMatrix();
/*premultiply M by CTM*/
glMultMatrix(object->CTM);
/*now draw all faces in object*/
glPopMatrix(); //restore original M

13
Transformations
glTranslate{d}{f}(x,y,z);
translation matrix T(x,y,z)
glScale{d}{f}(x,y,z);
scaling matrix S(x,y,z)
glRotate{d}{f}(angle, x, y, z);
matrix for positive (anti-clockwise) rotation
of angle degrees about vector (x,y,z)
If M is current matrix, and Q is transformation
matrix, then new current matrix is QM

14
GLUT (OpenGL Utility Toolkit)
portable windowing API
not officially part of OpenGL

15
Preliminaries

Headers Files
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Libraries
Enumerated Types
OpenGL defines numerous types for
compatibility
GLfloat, GLint, GLenum, etc.

16
1
GLUT Basics
Application Structure
Configure and open window
Initialize OpenGL state
Register input callback functions
render
resize
input: keyboard, mouse, etc.
Enter event processing loop

17
1
Sample Program
void main( int argc, char** argv )
{
int mode = GLUT_RGB|GLUT_DOUBLE;
glutInitDisplayMode( mode );
glutCreateWindow( argv[0] );
init();
glutDisplayFunc( display );
glutReshapeFunc( resize );
glutKeyboardFunc( key );
glutIdleFunc( idle );
glutMainLoop();
}
18
1
OpenGL Initialization
Set up whatever state youre going to use
void init( void )
{
glClearColor( 0.0, 0.0, 0.0, 1.0
);
glClearDepth( 1.0 );

glEnable( GL_LIGHT0 );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );
}
19
1
GLUT Basics GLUT

Program Structure
1. Configure and open window (GLUT)
2. Initialize OpenGL (Optional)
3. Register input callback functions (GLUT)
Render
Resize
Input: keyboard, mouse, etc
4. Enter event processing loop (GLUT)
20
GLUT Callback Functions
Routine to call when something
happens
window resize or redraw
user input
animation
Register callbacks with GLUT
glutDisplayFunc( display );
glutIdleFunc( idle );
glutKeyboardFunc( keyboard );

21
2
Rendering Callback
Do all of your drawing here
glutDisplayFunc( display );
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT );
glBegin( GL_TRIANGLE_STRIP );
glVertex3fv( v[0] );
glVertex3fv( v[1] );
glVertex3fv( v[2] );
glVertex3fv( v[3] );
glEnd();
glutSwapBuffers();
}

22
2
Idle Callbacks

Use for animation and continuous


update
glutIdleFunc( idle );
void idle( void )
{
t += dt;
glutPostRedisplay();
}

23
2
User Input Callbacks

Process user input


glutKeyboardFunc( keyboard );
void keyboard( unsigned char key, int x,
int y )
{
switch( key ) {
case q : case Q :
exit( EXIT_SUCCESS );
break;
case r : case R :
rotate = GL_TRUE;
glutPostRedisplay();
break;
}
}

24
2
OpenGL Geometric Primitives

All geometric primitives are specified


by vertices
GL_LINES
GL_POINTS GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP

GL_TRIANGLES
GL_TRIANGLE_STRIP GL_QUADS GL_QUAD_STRIP
GL_TRIANGLE_FAN

25
2

You might also like