3D游戏编程OpenGL入门.pdf

上传人:asd****56 文档编号:70321512 上传时间:2023-01-19 格式:PDF 页数:67 大小:7.01MB
返回 下载 相关 举报
3D游戏编程OpenGL入门.pdf_第1页
第1页 / 共67页
3D游戏编程OpenGL入门.pdf_第2页
第2页 / 共67页
点击查看更多>>
资源描述

《3D游戏编程OpenGL入门.pdf》由会员分享,可在线阅读,更多相关《3D游戏编程OpenGL入门.pdf(67页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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

26、gramming IIGame Programming II OpenGL Pipeline OpenGL Pipeline Main task:transformation and lighting Transformation:Model-View transformation:translation,rotation,scaling Projection transformation:perspective,parallel Lighting Fixed-pipeline implements per-vertex lighting Lighting is normally delaye

27、d to fragment processing in programmable pipeline Game Programming IIGame Programming II Vertex Processing Assemble vertices into primitives Lines/Curves Triangles/Polygons/Surfaces Game Programming IIGame Programming II Primitive Assembly Dont render invisible objects Clipping Remove primitives out

28、side of the cameras view frustum Backface culling Remove triangles facing away from camera Usually cuts down$of triangles by about 50%Game Programming IIGame Programming II Clipping and Culling Convert a primitive into a set of fragments Each pixel has both RGB color and depth Interpolate vertex col

29、or over fragments Fragment might not correspond to pixels on screen:Occluded fragments Game Programming IIGame Programming II Rasterization Assemble fragments into final framebuffer Hidden-surface removal:Some fragments may occlude parts of others Z-buffer sorts pixels by distance Handle transparenc

30、y Other operations Game Programming IIGame Programming II Fragment Processing Using vertex list to represent a set rendering primitives 14 primitives supported by OpenGL Point:GL_POINTS Line:GL_LINES,GL_LINE_STRIP,GL_LINE_LOOP Triangle:GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN Polygon:GL_QUADS,

31、GL_QUAD_STRIP,GL_POLYGON Game Programming IIGame Programming II Rendering Primitives Polygons must obey following rules Simple polygon Convex polygon Coplanar vertices Triangle satisfies all the above rules Most common rendering primitive Game Programming IIGame Programming II Restriction on Polygon

32、s Game Programming IIGame Programming II Specifying Vertex 1/2 Type of rendering primitive and its vertices have to be specified Vertices specification must be inside a pair of glBegin()/glEnd()Specify rendering primitive when glBegin()is called Game Programming IIGame Programming II Specifying Vert

33、ex 2/2 void drawParallelogram(GLfloat color)glBegin(GL_QUADS);glColor3fv(color);glVertex2f(0.0,0.0);glVertex2f(1.0,0.0);glVertex2f(1.5,1.118);glVertex2f(0.5,1.118);glEnd();GL_POINTS Point size can be modified by glPointSize(float size)Game Programming IIGame Programming II Rendering Primitive:Point

34、glBegin(GL_POINTS);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();GL_LINES Represented by a pair of vertices Width of line can be specified by gl

35、LineWidth(float width)Game Programming IIGame Programming II Rendering Primitive:Line glBegin(GL_LINES);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);gl

36、End();GL_LINE_STRIP Open line segment represented by a set of connected lines No line from P7 to P0 Game Programming IIGame Programming II Rendering Primitive:Line glBegin(GL_LINE_STRIP);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVerte

37、x2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();GL_LINE_LOOP Closed line segments represented by a set of connected lines Note the line from P7 to P0 Game Programming IIGame Programming II Rendering Primitive:Line glBegin(GL_LINE_LOOP);glColor3fv(color);glVer

38、tex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();GL_POLYGON Simple convex polygon Polygon is filled with the specified color Game Programming IIGame Programming II Render

39、ing Primitive:Polygon glBegin(GL_POLYGON);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();GL_TRIANGLES Every three vertices form a triangle 3*N ve

40、rtices represent N triangles Redundant vertices are ignored Game Programming IIGame Programming II Rendering Primitive:Triangle glBegin(GL_TRIANGLES);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);

41、glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();GL_TRIANGLE_STRIP N+2 vertices represent N triangles Odd triangle i formed by vertex i,i+1,i+2 Even triangle i formed by vertex i+1,i,i+2 Game Programming IIGame Programming II Rendering Primitive:Triangle glBegin(GL_TRIANGLE_STRIP);glColor3fv(colo

42、r);glVertex2f(v0.x,v0.y);glVertex2f(v1.x,v1.y);glVertex2f(v2.x,v2.y);glVertex2f(v3.x,v3.y);glVertex2f(v4.x,v4.y);glVertex2f(v5.x,v5.y);glVertex2f(v6.x,v6.y);glVertex2f(v7.x,v7.y);glEnd();v0 v2 v1 v3 v4 v5 v6 v7 GL_TRIANGLE_FAN N+2 vertices represent N triangles Triangle i formed by vertex 1,i+1,i+2

43、Game Programming IIGame Programming II Rendering Primitive:Triangle glBegin(GL_TRIANGLE_FAN);glColor3fv(color);glVertex2f(v0.x,v0.y);glVertex2f(v1.x,v1.y);glVertex2f(v2.x,v2.y);glVertex2f(v3.x,v3.y);glVertex2f(v4.x,v4.y);glVertex2f(v5.x,v5.y)glEnd();v0 v1 v2 v3 v4 v5 GL_QUADS Every four vertices for

44、m a quad Game Programming IIGame Programming II Rendering Primitive:Quad glBegin(GL_QUADS);glColor3fv(color);glVertex2f(P0.x,P0.y);glVertex2f(P1.x,P1.y);glVertex2f(P2.x,P2.y);glVertex2f(P3.x,P3.y);glVertex2f(P4.x,P4.y);glVertex2f(P5.x,P5.y);glVertex2f(P6.x,P6.y);glVertex2f(P7.x,P7.y);glEnd();The bas

45、ic rendering command in OpenGL Only a few OpenGL functions can be called inside a glBegin()/glEnd()pair Describing vertex properties glVertex,glColor,glTexCoord,glNormal Game Programming IIGame Programming II glBegin()/glEnd()How to convert 3D objects to 2D image?Just like taking a photograph!Game P

46、rogramming IIGame Programming II Camera Analogy 1/2 camera tripod model viewing volume Projection transformations Adjust the lens of the camera Viewing transformations Tripoddefine position and orientation of the viewing volume in the world Modeling transformations Moving the model Viewport transfor

47、mations Enlarge or reduce the physical photograph Game Programming IIGame Programming II Camera Analogy 2/2 Steps in Forming an Image Specify geometry(world coordinates)Specify camera(camera coordinates)Project(clip coordinates)Map to viewport(screen coordinates)Each step uses transformations Every

48、transformation is equivalent to a change in coordinate systems Game Programming IIGame Programming II Coord.Sys.and Transformations Game Programming IIGame Programming II Coord.Sys.and Transformations Object Coordinates World Coordinates Camera Coordinates Clip Coordinates Screen Coordinates Model T

49、ransformation World Transformation Project Transformation Viewport Transformation Game Programming IIGame Programming II Affine Transformations 1/2 Definition Using a single matrix multiplication to represent affine transformation by using augmented matrix and augmented vector Preserving geometry li

50、nes,polygons,quadrics Affine=line preserving Rotation,translation,scaling Projection Concatenation(composition)Game Programming IIGame Programming II Affine Transformations 2/2 Each vertex is a column vector w is usually 1.0 (x,y,z,w)=(ax,ay,az,aw)If w is not 1.0,we can recover x,y,z by division by

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁