OpenGL Basic Drawing

28
OpenGL Basic Drawing Jian-Liang Lin 2002

description

OpenGL Basic Drawing. Jian-Liang Lin 2002. A Smidgen of OpenGL Code. # include 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); - PowerPoint PPT Presentation

Transcript of OpenGL Basic Drawing

Page 1: OpenGL Basic Drawing

OpenGL Basic Drawing

Jian-Liang Lin2002

Page 2: OpenGL Basic Drawing

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();

}

Page 3: OpenGL Basic Drawing

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

Page 4: OpenGL Basic Drawing

OpenGL Command Syntax -2

Page 5: OpenGL Basic Drawing

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 variableMany state variables refer to modes that are enabled or disabled with the command glEnable() or glDisable().

Page 6: OpenGL Basic Drawing

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().

Page 7: OpenGL Basic Drawing

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)– …

Page 8: OpenGL Basic Drawing

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 color-

indexed mode.• GLUT_RGBA, GLUT_INDEX…

– Single or double buffering. • GLUT_SINGLE, GLUT_DOUBLE…

– Other buffers.• GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM…

Page 9: OpenGL Basic Drawing

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.

Page 10: OpenGL Basic Drawing

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

Page 11: OpenGL Basic Drawing

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…

Page 12: OpenGL Basic Drawing

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 );

Page 13: OpenGL Basic Drawing

Start GLUT framework

glutMainLoop();– Like Win32’s message loop.– Start all GLUT operations.

Page 14: OpenGL Basic Drawing

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 ();

}

Page 15: OpenGL Basic Drawing

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. */

Page 16: OpenGL Basic Drawing

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. */

}

Page 17: OpenGL Basic Drawing

GLUT: Complete sample result

Page 18: OpenGL Basic Drawing

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.

Page 19: OpenGL Basic Drawing

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();

}

Page 20: OpenGL Basic Drawing

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.

Page 21: OpenGL Basic Drawing

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.

Page 22: OpenGL Basic Drawing

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();

Page 23: OpenGL Basic Drawing

A Drawing Survival Kit -4

Page 24: OpenGL Basic Drawing

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)

Page 25: OpenGL Basic Drawing

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 );

Page 26: OpenGL Basic Drawing

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 );

Page 27: OpenGL Basic Drawing

A Drawing Survival Kit -8

Specifying a Shading Model– glShadeModel( GLenum mode );

• mode: GL_SMOOTH(default), GL_FLAT

Page 28: OpenGL Basic Drawing

Any Question?

?