Introduction to OpenGL Programming

Download Report

Transcript Introduction to OpenGL Programming

OpenGL Basic Drawing
Jian-Liang Lin
2002
A Smidgen of OpenGL Code
#include <whateverYouNeed.h>
main()
{
InitializeAWindowPlease();
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush();
UpdateTheWindowAndCheckForEvents();
}
OpenGL Command Syntax -1
OpenGL constants prefix by GL_.
OpenGL routines prefixed by gl and
suffiexed by the number of arguments
and type.
– glVertex2i, glNormal3f,
glTexCoord2fv…etc
OpenGL Command Syntax -2
OpenGL as a State Machine -1
OpenGL is a state machine.
You put it into various states (or modes) that
then remain in effect until you change them.
As you've already seen, the current color is a
state variable
Many state variables refer to modes that are
enabled or disabled with the command
glEnable() or glDisable().
OpenGL as a State Machine -2
Each state variable or mode has a default
value, and at any point you can query the
system for each variable's current value.
Typically, you use one of the six following
commands to do this: glGetBooleanv(),
glGetDoublev(), glGetFloatv(),
glGetIntegerv(), glGetPointerv(), or
glIsEnabled().
GLUT: Overview
A very useful framework for rapid OpenGL
program development.
– Portable through lots of platforms.
– Provide basic window, input, and event
management.
– Callback event handler.
– Idle routine and timer.
– A simple, cascading popup menu facility.
– Offers lots of basic object. (wireframe/solid)
– …
GLUT: Initialize and
creating a window -1
glutInit( int* argc, char** argv );
– Initialize GLUT and process any command line
arguments.
– This should be called before any GLUT routines.
glutInitDisplayMode( unsigned int mode );
– Specify whether to apply RGBA mode or colorindexed mode.
• GLUT_RGBA, GLUT_INDEX…
– Single or double buffering.
• GLUT_SINGLE, GLUT_DOUBLE…
– Other buffers.
• GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM…
GLUT: Initialize and
creating a window -2
glutInitWindowPosition( int x, int y );
– Specify the screen location of your window
glutInitWindowSize( int w, int h );
– Specify the size of your window in pixels.
glutCreateWindow( char* pTitle );
– Create a window with OpenGL context for
rendering.
– The window will appear until glutMainLoop() is
called.
GLUT: Callback function -1
Callback functions are not called by you.
They are called by GLUT.
You can register some functions to GLUT,
and they can be called when meeting the
situation.
Some types of callback
– Window event handler
– IO event handler
– Global handler
GLUT: Callback function -2
glutDisplayFunc( void (*pfn)(void) );
– Display callback; GLUT called it when the content
of the window needed to be updated.
– You call manually call glutPostRedisplay() to
arbitrarily ask GLUT to call that callback function.
glutReshapeFunc( void (*pfn)( int w, int h ) );
– GLUT called it when the size of window is
changed.
– Recalculate view frustum & viewport…
GLUT: Callback function -3
glutKeyboardFunc( void(*pfn)(unsigned char key, int
x, int y )); and glutMouseFunc( void(*pfn)( int button,
int state, int x, int y ));
– User Input event handler.
glutMotionFunc( void(*pfn)( int x, int y ));
– GLUT call this registered callback function when the mouse
is moved while a mouse button is also pressed.
glutIdleFunc( void(*pfn)(void));
– GLUT call this registered callback function when idling.
– You can simply call:
• glutIdleFunc( glutPostRedisplay );
Start GLUT framework
glutMainLoop();
– Like Win32’s message loop.
– Start all GLUT operations.
GLUT: Complete sample -1
#include <GL/glut.h>
void display(void) {
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
/* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and
(0.75, 0.75, 0.0) */
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd(); /* don't wait! * start processing buffered OpenGL routines */
glFlush ();
}
GLUT: Complete sample -2
void init (void) {
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/* * Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
GLUT: Complete sample -3
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}
GLUT: Complete sample result
GLUT: Simple 3D objects
drawing support -1
GLUT includes several 3D objects
drawing:
– Cone, cube, sphere, dodecahedron,
icosahedron, octahedron, teapot,
tetrahedron, torus.
– Both wireframe and solid.
• glutWireCube, glutSolidSphere … etc.
– These objects are drawn centered at the
origin of the world coordinate system.
GLUT: Simple 3D objects
drawing support -2
You can try this simple display callback
function and see the result.
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT );
glutWireCube( 1.0 );
glFlush();
}
A Drawing Survival Kit -1
Clearing the window
– glClear( GLbitfield mask );
• GL_COLOR_BUFFER_BIT -> glClearColor( … );
• GL_DEPTH_BUFFER_BIT -> glClearDepth( … );
Specifying a color
– glColor3f( GLfloat r, GLfloat g, GLfloat b );
– glIndexi( int i );
• glutSetColor( int i, GLfloat r, GLfloat g, GLfloat b ); for
setting indexed color.
A Drawing Survival Kit -2
Specifying Vertices
– glVertex{2,3,4}{sifd}[v]( … );
• Ex: glVertex3f( 1.0f, 0.5f, 2.0f );
– Vertex array
• Not introduced here.
A Drawing Survival Kit -3
OpenGL Geometric Drawing Primitives
– glBegin( GLenum mode );
• mode is GL_POLYGON, GL_LINES … etc
– glEnd();
– All primitives should be placed between these two
OpenGL routines
• Ex
glBegin(GL_POLYGON);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 3.0);
glVertex2f(4.0, 3.0);
glVertex2f(6.0, 1.5);
glVertex2f(4.0, 0.0);
glEnd();
A Drawing Survival Kit -4
A Drawing Survival Kit -5
Valid calls between glBegin(…); & glEnd();
– glVertex*(); glNormal*(); glColor*(); glIndex*();
glTexCoord*(); glMaterial*(); …
Forcing Completion of Drawing
– glFlush(void);
• Asynchronous
– glFinish(void);
• Synchronous
– glutSwapBuffers(void);
• Swap back buffer and front buffer. (Double buffering for
animation)
A Drawing Survival Kit -6
Basic State Management
– glEnable( … );
• Enable some state. Ex: glEnable( GL_LIGHTING );
– glDisable( … );
• Disable some state. Ex. glDisable( GL_TEXTURE_2D );
– glIsEnabled( … );
• Query if the specific state is enabled. Ex:
glIsEnabled( GL_BLEND );
– glGetBooleanv(…); glGetIntegerv(…);
glGetFloatv(…); glGetDoublev( … );
glGetPointerv(…);
• Query the specific state value.
– Ex: glGetIntegerv( GL_VIEWPORT, vp );
A Drawing Survival Kit -7
Polygon Detail
– glPolygonMode( GLenum face, GLenum mode );
• face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
• mode: GL_POINT, GL_LINE, GL_FILL
– glFrontFace( GLenum mode );
• mode: GL_CCW(default), GL_CW
– glCullFace( GLenum mode );
• mode: GL_FRONT, GL_BACK,
GL_FRONT_AND_BACK
• glEnable( GL_CULL_FACE );
A Drawing Survival Kit -8
Specifying a Shading Model
– glShadeModel( GLenum mode );
• mode: GL_SMOOTH(default), GL_FLAT
Any Question?