2015春计算机图形学空间信息introduction programming in opengl_第1页
2015春计算机图形学空间信息introduction programming in opengl_第2页
2015春计算机图形学空间信息introduction programming in opengl_第3页
2015春计算机图形学空间信息introduction programming in opengl_第4页
2015春计算机图形学空间信息introduction programming in opengl_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1Programming in OpenGL中国地质大学2History of OpenGLnSilicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982)nTo access the system, application programmers used a library called GLnWith GL, it was relatively simple to program three dimensional interact

2、ive applications 中国地质大学3OpenGL: What is It?nThe success of GL lead to OpenGL (1992), a platform-independent API that was nEasy to usenClose enough to the hardware to get excellent performancenFocus on renderingnOmitted windowing and input to avoid window system dependencies 中国地质大学4OpenGL: What is It

3、?nGL (Graphics Library): Library of 2-D, 3-D drawing primitives and operationsnAPI for 3-D hardware acceleration nGLU (GL Utilities): Miscellaneous functions dealing with camera set-up and higher-level shape descriptionsnGLUT (GL Utility Toolkit): Window-system independent toolkit with numerous util

4、ity functions, mostly dealing with user interface中国地质大学5OpenGL ArchitectureDisplayListPolynomialEvaluatorPer VertexOperations &PrimitiveAssemblyRasterizationPer FragmentOperationsFrameBufferTextureMemoryCPUPixelOperations中国地质大学6OpenGL as a RenderernGeometric primitivesnpoints, lines and polygons

5、nImage Primitivesnimages and bitmapsnseparate pipeline for images and geometryolinked through texture mappingnRendering depends on statencolors, materials, light sources, etc.中国地质大学7OpenGL Geometric Primitives中国地质大学8Specifying Geometric PrimitivesnPrimitives are specified usingglBegin(primType);.glE

6、nd();nprimType determines how vertices are combinedGLfloat red, green, blue;GLfloat x, y;glBegin(primType);for (i = 0; i nVerts; i+) glColor3f(red, green, blue); glVertex2f(x, y); . / change coord. valuesglEnd();中国地质大学9OpenGL Vertex/Color Command FormatsglVertex3fv( v )Number ofcomponents2 - (x,y) 3

7、 - (x,y,z), (r,g,b)4 - (x,y,z,w), (r,g,b,a)Data Typeb - byteub - unsigned bytes - shortus - unsigned shorti - intui - unsigned intf - floatd - doubleVectoromit “v” forscalar form e.g.,glVertex2f(x, y)glColor3f(r, g, b)glColor3fv( v )中国地质大学10Software OrganizationGLUTGLUGLGLX, AGLor WGLX, Win32, Mac O

8、/Ssoftware and/or hardwareapplication programOpenGL Motifwidget or similar中国地质大学11Lack of Object OrientationnOpenGL is not object oriented so that there are multiple functions for a given logical functionnglVertex3f nglVertex2i nglVertex3dvnUnderlying storage mode is the samenEasy to create overload

9、ed functions in C+ but issue is efficiency中国地质大学12OpenGL function formatglVertex3f(x,y,z)belongs to GL libraryfunction namex,y,z are floatsglVertex3fv(p)p is a pointer to an arraydimensions中国地质大学OpenGLWindow management13n初始化( glutInit ) glutInit function must be called before other function use glut

10、Init(&argc, argv);n设定窗口的显示模式( glutInitDisplayMode) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); GLUT_DOUBLE, GLUT_INDEXnthe size and position of the window glutInitWindowPosition设置窗口在屏幕中的位置 glutInitWindowPosition(100,120) glutInitWindowSize设置窗口的大小 glutInitWindowSize(400,300)nCreate Window( glutC

11、reateWindow) glutCreateWindow(矩形); glutMainLoop( )OpenGLWindow managementOpenGLWindow managementn指定窗口的显示内容函数( glutDisplayFunc) glutDisplayFunc(Display)n运行框架( glutMainLoop) 启动主GLUT事件处理循环OpenGLGraphics rendering16n指定窗口背景色( glClearColor)nglClearColor(1.0f, 1.0f, 1.0f, 1.0f);OpenGLGraphics rendering17混合

12、色红色成分(R)绿色成分(G)蓝色成分(B)黑0.00.00.0红1.00.00.0绿0.01.00.0黄1.01.00.0蓝0.00.01.0紫1.00.01.0青0.01.01.0深灰5浅灰0.750.750.75棕0.600.400.12南瓜橙0.980.6250.12粉红0.980.040.70紫红0.600.400.70白1.01.01.0OpenGLGraphics rendering18n刷新窗口的缓冲区( glClear)n设定投影参数 glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,150.0)

13、;n绘制图形 glRectf(50.0f, 100.0f, 150.0f, 50.0f);OpenGL程序实例1绘制矩形#include void Initial(void)glClearColor(1.0f, 1.0f, 1.0f, 1.0f); /设置窗口背景颜色为白色glMatrixMode(GL_PROJECTION); /设置投影参数gluOrtho2D(0.0,200.0,0.0,150.0);void Display(void)glClear(GL_COLOR_BUFFER_BIT); /用当前背景色填充窗口glColor3f(1.0f, 0.0f, 0.0f); /设置当前的绘

14、图颜色为红色glRectf(50.0f, 100.0f, 150.0f, 50.0f); /绘制一个矩形 glFlush(); /处理所有的OpenGL程序OpenGL程序实例1绘制矩形int main(int argc, char* argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /初始化窗口的显示模式glutInitWindowSize(400,300); /设置窗口的尺寸glutInitWindowPosition(100,120); /设置窗口的位置glutCreateWindow(

15、矩形); /创建一个名为矩形的窗口glutDisplayFunc(Display); /设置当前窗口的显示回调函数Initial(); /完成窗口初始化glutMainLoop(); /启动主GLUT事件处理循环return 0;OpenGL程序实例2绘制奥运五环#include GLuint OlympicRings;void Initial(void)glClearColor(1.0f, 1.0f, 1.0f, 1.0f); OlympicRings = glGenLists(1);glNewList(OlympicRings, GL_COMPILE);glColor3f(1.0, 1.0

16、, 0.0);glTranslatef(-22.0, 0.0, 0.0);glutSolidTorus(0.5, 20.0, 15, 50); /绘制黄色环 glColor3f(0.0, 1.0, 0.0);glTranslatef(44.0, 0.0, 0.0);glutSolidTorus(0.5, 20.0, 15, 50); /绘制绿色环glColor3f(0.0, 0.0, 0.0);glTranslatef(-22.0, 30.0, 0.0);glutSolidTorus(0.5, 20.0, 15, 50); /绘制黑色环glColor3f(0.0, 0.0, 1.0);glTr

17、anslatef(-42.0, 0.0, 0.0);glutSolidTorus(0.5, 20.0, 15, 50); /绘制蓝色环glColor3f(1.0, 0.0, 0.0);glTranslatef(84.0, 0.0, 0.0);glutSolidTorus(0.5, 20.0, 15, 50); /绘制红色环glEndList();OpenGL程序实例2绘制奥运五环void ChangeSize(int w, int h)glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D (

18、-70.0f, 70.0f, -70.0f, 70.0f);OpenGL程序实例2绘制奥运五环void Display(void)glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(OlympicRings); /调用显示列表glFlush(); OpenGL程序实例2绘制奥运五环int main(int argc, char* argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

19、 glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow(OpenGL模型绘制函数示例); glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);Initial(); glutMainLoop(); return 0;OpenGL程序实例2绘制奥运五环Coordinate transformation sequence中国地质大学26nOpenGL Matrix OperationsnGLU Clipping windownOpenGL Vi

20、ewport2d Viewing in OpenGL中国地质大学27nglMatrixMode(GL_PROJECTION)nglLoadIdentity();OpenGL Matrix Operations中国地质大学28ngluOtho2D(xwmin, xwmax, ywmin, ywmax); Coordinate positions for the clipping window boundaries are given as double precision numbers.nThe default clipping window: wxl=-1.0,wxr=1.0, wyt=-1

21、.0, wyb=1.0GLU Clipping window中国地质大学29nglViewPort(xvmin,yvmin,vpWidth,vpHeighht) All parameter values are given in integer screen coordinates relative to the display window.OpenGL Viewport中国地质大学3031OpenGL CameranRight-handed systemnFrom point of view of camera looking out into scene:nOpenGL places a

22、 camera at the origin in object space pointing in the negative z directionnPositive rotations are counterclockwise around axis of rotation中国地质大学32Coordinate SystemsnThe units in glVertex are determined by the application and are called object or problem coordinatesnThe viewing specifications are als

23、o in object coordinates and it is the size of the viewing volume that determines what will appear in the imagenInternally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates中国地质大学33Transformations in OpenGlnModeling transformationnRefer to the transformation of models (i

24、.e., the scenes, or objects)nViewing transformationnRefer to the transformation on the cameranProjection transformationnRefer to the transformation from scene to image中国地质大学34Model/View TransformationsnModel-view transformations are usually visualized as a single entitynBefore applying modeling or v

25、iewing transformations, need to set glMatrixMode(GL_MODELVIEW)nModeling transforms the objectoTranslation:glTranslate(x,y,z)oScale: glScale(sx,sy,sz)oRotation: glRotate(theta, x,y,z)nViewing transfers the object into camera coordinates(定义观察坐标系)ogluLookAt (eyeX, eyeY, eyeZ, centerX, centerY, centerZ,

26、 upX, upY, upZ)中国地质大学nViewing transfers the object into camera coordinates(定义观察坐标系)ogluLookAt (eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)oP0( eyeX, eyeY, eyeZ )观察坐标系原点oPref( , centerX, centerY, centerZ )reference positiono upX, upY, upZ 定义观察正向矢量Model/View Transformations中国地质大学3536Mo

27、del/View transformationCourtesy: Neider, Davis and Woo, “The OpenGL Programming Guide”中国地质大学37Projection TransformationnTransformation of the 3D scene into the 2D rendered image planenBefore applying projection transformations, need to setglMatrixMode(GL_PROJECTION)nOrthographic projectionoglOrtho(l

28、eft, right, bottom, top, near, far)nPerspective projectionoglFrustum (left, right, bottom, top, near, far)中国地质大学38Projection TransformationF.S.Hill, “Computer Graphics using OpenGL”Orthographic projectionPerspective projection中国地质大学nvoid gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear

29、, GLdouble zFar);OpenGL symmetric perspective projection function.Projection Transformation中国地质大学3940GLUT event loopnLast line in main.c for a program using GLUT is the infinite event loopglutMainLoop();nIn each pass through the event loop, GLUT nlooks at the events in the queuenfor each event in th

30、e queue, GLUT executes the appropriate callback function if one is definednif no callback is defined for the event, the event is ignorednIn glutDisplayFunc(mydisplay) identifies the function to be executednEvery GLUT program must have a display callback中国地质大学41Posting redisplaysnMany events may invoke the display callback functionnCan lead to multiple executions of the display callback on a single pass through the event loopnWe can avoid this problem by instead usingglutPostRedisplay(); which sets a flag. nGLUT checks to see if the flag is set at the e

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论