1 / 43

3D Game Programming OpenGL

3D Game Programming OpenGL. Ming-Te Chi Department of Computer Science,  National Chengchi University. Outline. Coordinate system Basic OpenGL Program GLUT event loop IO. Cartesian Plane. +y. (4, 3). (0, 0). +x. (-3, -2). +z. Coordinate Clipping. +y. (150, 100). (75, 50).

kylar
Download Presentation

3D Game Programming OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3D Game ProgrammingOpenGL Ming-Te Chi Department of Computer Science,  National Chengchi University

  2. Outline • Coordinate system • Basic OpenGL Program • GLUT • event loop • IO

  3. Cartesian Plane +y (4, 3) (0, 0) +x (-3, -2) +z

  4. Coordinate Clipping +y (150, 100) (75, 50) (0, 0) +x (-75, -50)

  5. Viewport • Mapping drawing coordinates to windows coordinates +y (0, 0) +x clipping space Window space

  6. Projection • Getting 3D to 2D • Orthographic projections • Perspective projections

  7. Representing Visuals • 3D objects • Mesh: geometry • Materials • Texture maps • Lighting • Shader

  8. OpenGL and GLUT Overview • What is OpenGL ? • What can it do for me? • OpenGL in windowing systems • Why GLUT • A GLUT program template

  9. What Is OpenGL? • Graphics rendering API • high-quality color images composed of geometric and image primitives • window system independent • operating system independent

  10. SGI and GL • Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) • To access the system, application programmers used a library called GL • With GL, it was relatively simple to program three dimensional interactive applications

  11. OpenGL The success of GL lead to OpenGL (1992), a platform-independent API that was • Easy to use • Close enough to the hardware to get excellent performance • Focus on rendering • Omitted windowing and input to avoid window system dependencies

  12. OpenGL Evolution • Originally controlled by an Architectural Review Board (ARB) • Members included SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,……. • Relatively stable (present version 4.2) • Evolution reflects new hardware capabilities • 3D texture mapping and texture objects • Vertex programs • Allows for platform specific features through extensions • ARB replaced by Khronos

  13. Revolution of GPU • Graphics Process Unit (GPU)

  14. OpenGL generation

  15. Timeline of OpenGL Aug OpenGL 4.2

  16. OpenGL Libraries • OpenGL core library • OpenGL32 on Windows • GL on most unix/linux systems (libGL.a) • OpenGL Utility Library (GLU) • Provides functionality in OpenGL core but avoids having to rewrite code • Links with window system • GLX for X window systems • WGL for Windows • AGL for Macintosh

  17. Per Vertex Operations & Primitive Assembly Polynomial Evaluator DisplayList Per Fragment Operations Frame Buffer CPU Rasterization Texture Memory Pixel Operations OpenGL Architecture

  18. 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.

  19. Related APIs • AGL, GLX, WGL • glue between OpenGL and windowing systems • GLU (OpenGL Utility Library) • part of OpenGL • NURBS, tessellators, quadric shapes, etc. • GLUT (OpenGL Utility Toolkit) • portable windowing API • not officially part of OpenGL

  20. application program OpenGL Motif widget or similar GLUT GLX, AGLor WGL GLU GL X, Win32, Mac O/S software and/or hardware OpenGL and Related APIs

  21. OpenGL-related ecosystem

  22. Preliminaries • Headers Files • #ifdef WIN32 • #include <windows.h> // Must have for Windows platform builds • #include "glee.h" // OpenGL Extension "autoloader" • #include <gl\gl.h> // Microsoft OpenGL headers (version 1.1 by themselves) • #include <gl\glu.h> // OpenGL Utilities • #include <gl\glut.h> // Glut (or freeglut). Depend on the install dictionary • #endif • Libraries (win32) • opengl32.lib • glu32.lib • glut32.lib

  23. Enumerated Types • OpenGL defines numerous types for compatibility • GLfloat, GLint, GLenum, etc.

  24. Notes on compilation • See website and ftp for examples • Unix/linux • Include files usually in …/include/GL • Compile with –lglut –lglu –lgl loader flags • May have to add –L flag for X libraries • Mesa implementation included with most linux distributions • Check web for latest versions of Mesa and glut

  25. Compilation on Windows • Visual C++ • Get glut.h, glut32.lib and glut32.dll from web • Create a console application • Add opengl32.lib, glu32.lib, glut32.lib to project settings (under link tab) • Cygwin (Linux under Windows) • Can use gcc and similar makefile to linux • Use –lopengl32 –lglu32 –lglut32 flags

  26. Compilation on Mac OSX • OpenGL and GLUT come with the OS and Xcode installations. • To verify, check for: /Library/Frameworks/{OpenGL,GLUT}.framework • Use -framework GLUT -framework OpenGLflags

  27. GLUT (OpenGL Utility Toolkit) • GLUT was originally written by Mark Kilgard to support the sample programs in the second edition OpenGL 'RedBook‘ • Latest version • glut-3.7.6-bin.zip (117 KB) since 2001! • Strict license • Sweet alternative • freeglut • http://freeglut.sourceforge.net/docs/api.php

  28. 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

  29. GLUT Sample ProgramEvent OpenGL and Windows Initialization Int 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(); return 0; } Callback function Registration Event Loop Get Event Process Event Idle

  30. OpenGL Initialization • Set up whatever state you’re 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 ); }

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

  32. OpenGL Command Formats glVertex3fv( v ) Data Type Vector Number of components b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double omit “v” for scalar form glVertex2f( x, y ) 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w)

  33. 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(); }

  34. Idle Callbacks • Use for animation and continuous update glutIdleFunc( idle ); void idle( void ) { t += dt; glutPostRedisplay(); }

  35. Animation in GLUT alternative • glutIdleFunc • Sets the global idle callback. • Only one idle function • Can be easily stopped by • Call glutPostRedisplay to refresh the screen void glutIdleFunc ( void (*func)()); glutIdleFunc ( NULL );

  36. 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; } }

  37. The mouse callback glutMouseFunc(mymouse) • void mymouse(GLint button, GLint state, GLint x, GLint y) • Returns • which button (GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON) caused event • state of that button (GLUT_UP, GLUT_DOWN) • Position in window

  38. Positioning • The position in the screen window is usually measured in pixels with the origin at the top-left corner • Consequence of refresh done from top to bottom • OpenGL uses a world coordinate system with origin at the bottom left • Must invert y coordinate returned by callback by height of window • y = h – y; (0,0) h w

  39. Orthographic Projection //myReshape void myReshape(int w, int h) { /* Save the new width and height */ screenWidth = w; screenHeight = h; /* Reset the viewport... */ glViewport(0, 0, screenWidth, screenHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLfloat)screenWidth, 0.0, (GLfloat)screenHeight, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); }

  40. Rect Drawing void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red // R G B glColor3f(1.0f, 0.0f, 0.0f); // Draw a filled rectangle with current color glRectf(x, y, x + rsize, y - rsize); // Flush drawing commands and swap glutSwapBuffers(); }

  41. Time function glutTimerFunc(unsigned int millis, void (GLUTCALLBACK *func)(int value), int value); • Registers a timer callback to be triggered in a specified number of milliseconds.

  42. Bounce void TimerFunction(int value) { // Reverse direction when you reach left or right edge if(x > windowWidth-rsize || x < -windowWidth) xstep = -xstep; // Reverse direction when you reach top or bottom edge if(y > windowHeight || y < -windowHeight + rsize) ystep = -ystep; // Actually move the square x += xstep; y += ystep;

  43. // Check bounds. This is in case the window is made // smaller while the rectangle is bouncing and the // rectangle suddenly finds itself outside the new // clipping volume if(x > (windowWidth-rsize + xstep)) x = windowWidth-rsize-1; else if(x < -(windowWidth + xstep)) x = -windowWidth -1; if(y > (windowHeight + ystep)) y = windowHeight-1; else if(y < -(windowHeight - rsize + ystep)) y = -windowHeight + rsize - 1; // Redraw the scene with new coordinates glutPostRedisplay(); glutTimerFunc(33,TimerFunction, 1); }

More Related