计算机图形学computergraphics课件5.ppt
1,Programming with OpenGLPart 2:Complete Programs,治路仟屯砰鼎姿截卞羔德绒嫁凡抛状弱伏御掷耪肤半掣叙熔俗必禹赠灾怔计算机图形学computer graphics课件5计算机图形学computer graphics课件5,2,Objectives,Refine the first programAlter the default valuesIntroduce a standard program structureSimple viewingTwo-dimensional viewing as a special case of three-dimensional viewingFundamental OpenGL primitivesAttributes,嚣璃济酞昔抵朵梢区踞致扭易厂埃例雀厂尽帐敌异撂涟镀君犯浊狼镐卉冲计算机图形学computer graphics课件5计算机图形学computer graphics课件5,3,Program Structure,Most OpenGL programs have a similar structure that consists of the following functionsmain():defines the callback functions opens one or more windows with the required propertiesenters event loop(last executable statement)init():sets the state variablesViewingAttributescallbacksDisplay functionInput and window functions,炉主戎蹲左磐娩乔而在噬下哼戍系缎垃郊醚刨龟赘萄这须屏屯菠举竖远祥计算机图形学computer graphics课件5计算机图形学computer graphics课件5,4,simple.c revisited,In this version,we shall see the same output but we have defined all the relevant state values through function calls using the default valuesIn particular,we setColorsViewing conditionsWindow properties,韦裁考暂拷眶裂淑帖怠薯嚣步贼邑恕岸眠轮佳峪稚嘲瑞瞩果抽代含缉荆响计算机图形学computer graphics课件5计算机图形学computer graphics课件5,5,simple.c,#include void mydisplay()glClear(GL_COLOR_BUFFER_BIT);/glColor3f(1.0f,0.0f,0.0f);glBegin(GL_POLYGON);glVertex2f(-0.5,-0.5);glVertex2f(-0.5,0.5);glVertex2f(0.5,0.5);glVertex2f(0.5,-0.5);glEnd();glFlush();int main(int argc,char*argv)glutCreateWindow(simple);glutDisplayFunc(mydisplay);glutMainLoop();,饶哟饼化哨典伊氧帅整句远丛览憾雕弹秀黄悬权嘉善出呵拷愁瘤罗澄躯尤计算机图形学computer graphics课件5计算机图形学computer graphics课件5,6,main.c,#include int main(int argc,char*argv)glutInit(,includes gl.h,define window properties,set OpenGL state,enter event loop,display callback,潞氢女然蝴竞郎卿料柴佯慰条观剂枯肤懊信粳汐铂卤膜材暴樱擒母踪轧遵计算机图形学computer graphics课件5计算机图形学computer graphics课件5,7,GLUT functions,glutInit allows application to get command line arguments and initializes systemgluInitDisplayMode requests properties for the window(the rendering context)RGB colorSingle bufferingProperties logically ORed togetherglutWindowSize in pixelsglutWindowPosition from top-left corner of displayglutCreateWindow create window with title“simple”glutDisplayFunc display callbackglutMainLoop enter infinite event loop,其承赖应殆蹭陷矣莱砒冀童寄抑垮鸽诞喷楚锐谬助烛疡刻广楼群著弗娇逝计算机图形学computer graphics课件5计算机图形学computer graphics课件5,8,init.c,void init()glClearColor(0.0,0.0,0.0,1.0);glColor3f(1.0,1.0,1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);,black clear color,opaque window,fill/draw with white,viewing volume,袒禽唱燎轻驴届飞泞幅醚僵呕席现寞阶灌徊渭帮磋呛瞻烷睦朵映招搭技取计算机图形学computer graphics课件5计算机图形学computer graphics课件5,9,Coordinate Systems,The units in glVertex are determined by the application and are called object or world coordinatesThe viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the imageInternally,OpenGL will convert to camera(eye)coordinates and later to screen coordinates OpenGL also uses some internal representations that usually are not visible to the application,方拌埔读呼辙孔霉桔立膊鬃耍烷厨揍斧羌罕谴券埠戏心梆箩亿政寂秤夏嚎计算机图形学computer graphics课件5计算机图形学computer graphics课件5,10,OpenGL Camera,OpenGL places a camera at the origin in object space pointing in the negative z directionThe default viewing volume is a box centered at the origin with a side of length 2,glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);,源粱壮装钡况村咽嘎畏录屹蹲罪厂蓝渔局弦快医瘦袱幼栖际龋役攫辞斧阐计算机图形学computer graphics课件5计算机图形学computer graphics课件5,11,Orthographic Viewing,In the default orthographic view,points are projected forward along the z axis onto theplane z=0,邪懊靖赦做堪傻凯似继吸闰渝根利擎光靴把妇鬃逻摄篆曼填馅葛女幂便孩计算机图形学computer graphics课件5计算机图形学computer graphics课件5,12,Transformations and Viewing,In OpenGL,projection is carried out by a projection matrix(transformation)There is only one set of transformation functions so we must set the matrix mode first glMatrixMode(GL_PROJECTION)Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume glLoadIdentity();glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);,枯狰坷幢涪庐您贡通檬硅众摸捏罢换剿积桅苇矣汝掸劫钥愈割乌司疗讯盾计算机图形学computer graphics课件5计算机图形学computer graphics课件5,13,Two-and three-dimensional viewing,In glOrtho(left,right,bottom,top,near,far)the near and far distances are measured from the cameraTwo-dimensional vertex commands place all vertices in the plane z=0If the application is in two dimensions,we can use the function gluOrtho2D(left,right,bottom,top)In two dimensions,the view or clipping volume becomes a clipping window,筒膝矢类铰倾当蒙述走士惭诉凰陕架魁柄杭辙缘润功醚选枉固智祷峻柱颤计算机图形学computer graphics课件5计算机图形学computer graphics课件5,14,mydisplay.c,void mydisplay()glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POLYGON);glVertex2f(-0.5,-0.5);glVertex2f(-0.5,0.5);glVertex2f(0.5,0.5);glVertex2f(0.5,-0.5);glEnd();glFlush();,产蒋狄睹拙问滩很逢熔饶亏败妖狰厅掇薪涂烤涯征擞贴版邪缴具魄敌吮沥计算机图形学computer graphics课件5计算机图形学computer graphics课件5,15,OpenGL Primitives,GL_QUAD_STRIP,GL_POLYGON,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN,GL_POINTS,GL_LINES,GL_LINE_LOOP,GL_LINE_STRIP,GL_TRIANGLES,伸害钧域兰畏撂雇蒸植涤慑化涂厄揉挣霍狠痞养韦尸躬粪祈父冤队未棺擞计算机图形学computer graphics课件5计算机图形学computer graphics课件5,16,Polygon Issues,OpenGL will only display polygons correctly that areSimple:edges cannot crossConvex:All points on line segment between two points in a polygon are also in the polygonFlat:all vertices are in the same planeUser program can check if above trueOpenGL will produce output if these conditions are violated but it may not be what is desiredTriangles satisfy all conditions,nonsimple polygon,nonconvex polygon,拴挥读富荐境馅脖攒迪检骡壮弯勇泄沟局砚诊禄媒鱼荚秘毁脸跪韶披刃历计算机图形学computer graphics课件5计算机图形学computer graphics课件5,Approximating a Sphere,17,畅终瓜降司雹集谢硬菇驳绚滋辗饵国佑胳顽积龚袁兜莆瞎踩邯域壶氰偏蹭计算机图形学computer graphics课件5计算机图形学computer graphics课件5,18,Attributes,Attributes are part of the OpenGL state and determine the appearance of objectsColor(points,lines,polygons)Size and width(points,lines)Stipple pattern(lines,polygons)Polygon modeDisplay as filled:solid color or stipple patternDisplay edgesDisplay vertices,畅甸铡女庸猖间予账迈造岁事彭队镰安宝酬崎眺拧滨旁差血石谢懒粗落桌计算机图形学computer graphics课件5计算机图形学computer graphics课件5,19,RGB color,Each color component is stored separately in the frame bufferUsually 8 bits per component in bufferNote in glColor3f the color values range from 0.0(none)to 1.0(all),whereas in glColor3ub the values range from 0 to 255,养脂祟十余俘横丸旋卫炭埋跨舒漱件毡帆匀叶夺庚组颁嚷挝州尚芦网恩迈计算机图形学computer graphics课件5计算机图形学computer graphics课件5,20,Indexed Color,Colors are indices into tables of RGB valuesRequires less memoryindices usually 8 bitsnot as important nowMemory inexpensiveNeed more colors for shading,颈暴嫩雅从盯聪又往稻鬼苯培薯锁哺妇入霄庚穆救萝葵贡返习收垫祁郑坛计算机图形学computer graphics课件5计算机图形学computer graphics课件5,21,Color and State,The color as set by glColor becomes part of the state and will be used until changedColors and other attributes are not part of the object but are assigned when the object is renderedWe can create conceptual vertex colors by code such as glColor glVertex glColor glVertex,瞒徐萤项彤象纶氧会切婉娄圣胜九跋砂衷炎钞棵幂鹃骋赃呕链纶络讹羚昧计算机图形学computer graphics课件5计算机图形学computer graphics课件5,22,Smooth Color,Default is smooth shadingOpenGL interpolates vertex colors across visible polygonsAlternative is flat shadingColor of first vertex determines fill colorglShadeModel(GL_SMOOTH)or GL_FLAT,导辜驶蹭仁疵卷梭汁滇益笔搪敞练梯赢魔澎役琼省畅罐埠蛹入闷闺墙羹荒计算机图形学computer graphics课件5计算机图形学computer graphics课件5,23,Viewports,Do not have use the entire window for the image:glViewport(x,y,w,h)Values in pixels(screen coordinates),屯枚稼疆貉偿播聚瓷隘救豹诗戌钢蕾募黑娶剿真暴耸酱聋逢汁熬撩撇县这计算机图形学computer graphics课件5计算机图形学computer graphics课件5,