3D游戏编程OpenGL入门.pdf
《3D游戏编程OpenGL入门.pdf》由会员分享,可在线阅读,更多相关《3D游戏编程OpenGL入门.pdf(67页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、OpenGL:Introduction Yanci Zhang Game Programming IIGame Programming II Overview of OpenGL OpenGL vs.Direct X OpenGL“Hello World”GLUT library Rendering pipeline Coordinate system Game Programming IIGame Programming II Outline OpenGL=Open Graphics Library Graphics rendering API Produce high-quality co
2、lor images composed of 3D geometric objects and images Hardware independent Cross platform Game Programming IIGame Programming II What is OpenGL?Bases for many advanced data structures in game programming Typical applications Scene graph State graph Decision tree Kd-tree,quad tree Game Programming I
3、IGame Programming II Basic Function Rendering basic primitives,like points,lines,triangles Matrix operations Local illumination Texture mapping Pixel operations Game Programming IIGame Programming II What Can OpenGL Do?Create windows Handle window events Response to user input Scene management Game
4、Programming IIGame Programming II What Can Not OpenGL Do?OpenGL is only graphics library DirectX handles graphics,audio,user input Use OpenGL improperly,system does nothing Use DirectX improperly,system does something beyond expectation Game Programming IIGame Programming II OpenGL vs.DirectX 1/2 Op
5、enGL Industry standard maintained by OpenGL Architectural Review Board(ARB)Stable function interface Cross platform Very clean,easy to learn DirectX Microsofts product Instable function interface Only support Windows Game Programming IIGame Programming II OpenGL vs.DirectX 2/2 Naming With prefix gl
6、Capital letter for each new word Examples glBegin,glEnd glEnable,glDisable glClear Game Programming IIGame Programming II OpenGL Functions Basic data type With prefix GL All small letters Similar to C+data type Example:GLdouble=double,GLfloat=float,GLint=int,GLuint=unsigned int Constant With prefix
7、GL_ All capital letters Examples:GL_POINTS,GL_CCW Game Programming IIGame Programming II Data Type&Constant Game Programming IIGame Programming II Hello World 1/3#include#include main()initWindow();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,
8、1.0,-1.0,1.0);All opengl programs should include gl.h.Almost all opengl programs include glu.h Initialize window which is a platform-dependent operation Clear windows with black color Use white color to start rendering something on the screen Specify projection matrix Game Programming IIGame Program
9、ming II Hello World 2/3 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();Specify the rendering content inside a pair of glBegin/glEnd Execute rendering What we are supposed to see on the screen Does the abov
10、e program really work?At least the window creation is missing Game Programming IIGame Programming II Hello World 3/3 Who is responsible for windows creation and management?GLUT Not a part of OpenGL Perform system-level I/O with the host operating system Allow creation of cross-platform code Make lea
11、rning OpenGL easier Game Programming IIGame Programming II OpenGL Utility Toolkit 1/2 Main Functions Window definition Window control Monitoring keyboard and mouse input Drawing a number of geometric primitives Limitation:glutMainLoop()A function which never returns Hard to integrate GLUT into a pro
12、gram which wants to control its own event loop Process is terminated when window is closed Game Programming IIGame Programming II OpenGL Utility Toolkit 2/2 1.Create window 2.Initialize OpenGL 3.Register callback functions Rendering callback function Window resize callback function User input callba
13、ck function 4.Enter main loop Infinite loop Game Programming IIGame Programming II GLUT Framework Game Programming IIGame Programming II Example:GLUT 1/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow(
14、Simple);init();glutDisplayFunc(display);glutKeyboardFunc(key);glutMainLoop();Game Programming IIGame Programming II Example:GLUT 2/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow(Simple);init();glutDi
15、splayFunc(display);glutKeyboardFunc(key);glutMainLoop();Configure the display mode Game Programming IIGame Programming II Example:GLUT 3/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow(Simple);init();
16、glutDisplayFunc(display);glutKeyboardFunc(key);glutMainLoop();Create a(500,500)window named“Simple”Game Programming IIGame Programming II Example:GLUT 4/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow
17、(Simple);init();glutDisplayFunc(display);glutKeyboardFunc(key);glutMainLoop();Initialization(optional)Game Programming IIGame Programming II Example:GLUT 5/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWin
18、dow(Simple);init();glutDisplayFunc(display);glutKeyboardFunc(key);glutMainLoop();Register user-defined callback functions Game Programming IIGame Programming II Example:GLUT 6/6#include#include void main(int argc,char*argv)int mode=GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode(mode);glutInitWindowSize(50
19、0,500);glutCreateWindow(Simple);init();glutDisplayFunc(display);glutKeyboardFunc(key);glutMainLoop();Enter infinite loop of OpenGL Game Programming IIGame Programming II Events Queue Event queue Keyboard Mouse Window .Mouse_callback().Keypress_callback().window_callback().MainLoop()glutDisplayFunc(m
20、yDisplayFunc);/register callback void myDisplayFunc(void)/implementation of callback glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLE);glEnd();glFlush();Rendering callback void glutDisplayFunc(void(*func)(void);Implement rendering task in this function A must in GLUT Game Programming IIGame Programm
21、ing II Important GLUT Callbacks 1/5 Window resize callback void glutReshapeFunc(void(*func)(int width,int height);Called when window size is changed Common operations:modify OpenGL matrix,viewport width,height is the new width and height of window respectively Game Programming IIGame Programming II
22、Important GLUT Callbacks 2/5 Keyboard input callback void glutKeyboardFunc(void(*func)(unsigned char key,int x,int y);key is the ascii character of pressed key;(x,y)indicates mouse position when key is pressed Game Programming IIGame Programming II Important GLUT Callbacks 3/5 void keyboard(unsigned
23、 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;Mouse input callback void glutMouseFunc(void(*func)(int button,int state,int x,int y);Called when mouse button is pressed or released button is one of GLUT_LEFT_BUTTON,GLUT_
24、RIGHT_BUTTON,GLUT+MIDDLE_BUTTON;state is either GLUT_UP or GLUT_DOWN;(x,y)indicates mouse position when mouse button state changed Game Programming IIGame Programming II Important GLUT Callbacks 4/5 Idle callback void glutIdleFunc(void(*func)(void);Offer a mechanism for doing animation Game Programm
25、ing IIGame Programming II Important GLUT Callbacks 5/5 void idle(void)t+=dt;glutPostRedisplay();Input:scene objects,lighting,camera Most of the data is vertex list Output:pixels stored in framebuffer Question:how to convert 3D vertex list to 2D pixels?OpenGL is designed to fulfill this task Game Pro
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 游戏 编程 OpenGL 入门
限制150内