Sie sind auf Seite 1von 26

Basics of OpenGL and GLUT

Basics of OpenGL and GLUT


#include <windows.h> #include <GL/glut.h> int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize( 500,500 ); glutInitWindowPosition(0,0 ); glutCreateWindow( example1 ); init(); glutDisplayFunc( display ); glutMainLoop(); return 0; }

Basics of OpenGL and GLUT


glutInit(int argc, char *argv) initializes GLUT, and processes any command line arguments glutInitWindowPosition(int x, int y) specifies the location of the created window with respect to the screen glutInitWindowSize(int width, int height) specified the size of the created window in terms of pixels

Basics of OpenGL and GLUT


glutInitDisplayMode(unsigned int mode) sets the display mode, determined by bitwise ORing of GLUT display mode bit masks:
Display Mode Meaning Use RGB colors Use RGB plus alpha (for transparency) Use colormapped colors Use double buffering (recommended) Use single buffering (not recommended) Use depth buffer (needed for hidden surface removal)

GLUT _RGB GLUT _RGBA GLUT _INDEX GLUT _DOUBLE GLUT _SINGLE
GLUT_ DEPTH

Basics of OpenGL and GLUT


glutCreateWindow(char *string) Creates a window with OpenGL context. It returns a unique identifier for the new window. init(): Set up whatever state youre going to use glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (xmin, xmax, ymin, ymax);

Basics of OpenGL and GLUT


glutDisplayFunc(void (*func)(void)) Whenever GLUT determines that the contents of the window should be redisplayed, the callback function specified is executed. glutMainLoop(void) Enters the GLUT event-Processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.

Example1-a 50x50 rectangle in OpenGL


#include <windows.h> #include <GL/glut.h> int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowSize( 800,800 ); glutInitWindowPosition(0,0 ); glutCreateWindow( rectangle ); init(); glutDisplayFunc( display ); glutMainLoop(); return 0; }

Example1-a rectangle in OpenGL


void init(void) // Specify 2D WCS { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,100,0,100); }

Example1-a rectangle in OpenGL


void display() { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glBegin(GL_POLYGON); glVertex2f(0.0,0.0); glVertex2f(50.0,0.0); glVertex2f(50.0,50.0); glVertex2f(0.0,50.0); glEnd(); glFlush(); }

Example1-a rectangle in OpenGL

Example2-a rectangle centered at (50,50) in WCS


void display() { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glBegin(GL_POLYGON); glVertex2f(25.0,25.0); glVertex2f(75.0,25.0); glVertex2f(75.0,75.0); glVertex2f(25.0,75.0); glEnd(); glFlush(); }

Example2-a rectangle centered at (50,50) in WCS

Example3- Rotate the rectangle in example2 by 200 CCW about (0,0)


void display() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glRotatef(20, 0.0,0.0, 1.0); glBegin(GL_POLYGON); -----glEnd(); glPopMatrix(); glFlush(); }

Example3- Rotate the rectangle in example2 by 200 CCW about (0,0)

Example4- Rotate the rectangle in example2 by 200 CCW about (50,50)


void display() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(50.0,50.0,0); glRotatef(20, 0.0,0.0, 1.0); glTranslatef(-50.0,-50.0,0); glBegin(GL_POLYGON); ----glEnd(); glPopMatrix(); glFlush(); }

Example4- Rotate the rectangle in example2 by 200 CCW about (50,50)

GLUT Callback Functions


Callback function
A programmer specified routine that can be registered to be called in response to a specific type of event (when an event happens)
Window resize or redraw User input (mouse, keyboard) Animation (render many frames)

Register callbacks with GLUT


glutDisplayFunc( my_display ); glutIdleFunc( my_idle_func ); glutKeyboardFunc( my_key_events ); glutMouseFunc ( my_mouse );

Rendering Callback
It is invoked when the system senses that the contents of the window need to be redisplayed, either because:
graphics window has completed its initial creation the program explicitly requests redrawing, by calling glutPostRedisplay()
void my_display (void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE ); glEnd(); }

Idle Callback
This callback is called when an idle event is generated An idle event is generated every time the system has nothing better to do. Use for animation and continuous update void idle( void ) { .. glutPostRedisplay(); }

Keyboard Callbacs
Called if a keyboard key is pressed void my_key_events (char key, int x, int y ) { switch ( key ) { case q : case Q : .. break; case r : case R : . break; } }

Mouse Callback
Called when mouse is clicked/pressed
void myMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { } }

Example: rotate the rectangle continuously


float theta=0.0; int main(int argc, char *argv[]) { glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; }

Example: rotate the rectangle continuously


void display() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(0.5,0.5,0); glRotatef(theta, 0.0,0.0, 1.0); glTranslatef(-0.5,-0.5,0); myDrawing(); glPopMatrix(); glutSwapBuffers(); glFlush(); }

Example: rotate the rectangle continuously


void idle() { if(theta>360.0) theta=0.0; else theta+=2.0; glutPostRedisplay(); }

Common Callback Functions

Special Key Constants


GLUT_KEY_F1 GLUT_KEY_F2 GLUT_KEY_F3 GLUT_KEY_F4 GLUT_KEY_F5 GLUT_KEY_F6 GLUT_KEY_F7 GLUT_KEY_F8 GLUT_KEY_F9 GLUT_KEY_F10

GLUT_KEY_F11 GLUT_KEY_F12 GLUT_KEY_LEFT GLUT_KEY_UP GLUT_KEY_RIGHT GLUT_KEY_DOWN GLUT_KEY_PAGE GLUT_KEY_PAGE GLUT_KEY_HOME GLUT_KEY_END GLUT_KEY_INSERT

Das könnte Ihnen auch gefallen