【文档说明】计算机图形学computer-graphics课件17.ppt,共(38)页,1.123 MB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-77497.html
以下为本文档部分文字说明:
BuffersSoftwareCollege,ShandongUniversityInstructor:ZhouYuanfengE-mail:yuanfeng.zhou@gmail.comReview2•Ras
terization:Polygonscanconversionalgorithm;•Hiddensurfaceremoval•Aliasing3Objectives•IntroduceadditionalOpenGLbuffers•Learntoreadandwritebuffers•L
earntouseblending4BufferDefineabufferbyitsspatialresolution(nxm)anditsdepth(orprecision)k,thenumberofbits/pixelpixel5OpenGLFrameBuffer6Open
GLBuffers•Colorbufferscanbedisplayed-Front-Back-Auxiliary-Overlay•Depth•Accumulation-Highresolutionbuffer•Stencil-Holdsmasks7Writingin
Buffers•Conceptually,wecanconsiderallofmemoryasalargetwo-dimensionalarrayofpixels•Wereadandwriterectangularblockofpixels-Bitblocktransfer(bitblt)o
perations•Theframebufferispartofthismemory(Doublebuffer)•LeftandrightbuffersforStereroimagesframebuffer(des
tination)writingintoframebuffersourcememoryBit-blockwriting•Ascan-linewritingwhilepolygonfilling•Characterdisplay•Clearallpixelbuffer
8BitBltcopy•BOOLBitBlt(intx,inty,intnWidth,intnHeight,CDC*pSrcDC,intxSrc,intySrc,DWORDdwRop);//SRCCOPY•Copiesabitmapfromthesourcedevicecontexttothis
currentdevicecontext.(FromaBitmapinmemorytocurrentCDC)910WritingModelReaddestinationpixelbeforewritingsour
ceSource:sDestination:d’d’=f(s,d)11BitWritingModes(16modes)•Sourceanddestinationbitsarecombinedbitwise•16possiblefunctions(onepercolumninta
ble)replaceORXORMode3&6Mode6Mode31213XORmode•RecallfromChapter3thatwecanuseXORbyenablinglogicoperationsandselectingtheXORwritemodeS(SM)M•XORisesp
eciallyusefulforswappingblocksofmemorysuchasmenusthatarestoredoffscreenIfSrepresentsscreenandMrepresentsamenuthesequ
enceSSMMSMSSMswapstheSandM14ThePixelPipeline•OpenGLhasaseparatepipelineforpixels-Writingpixelsinvolves•Movingpixelsfromprocessormemoryt
otheframebuffer•Formatconversions•Mapping,Lookups,Tests-Readingpixels•Formatconversion15RasterPosition•OpenGLmaintainsarasterpositionaspart
ofthestate•SetbyglRasterPos*()(worldcoord)-glRasterPos3f(x,y,z);•Therasterpositionisageometricentity-Passe
sthroughgeometricpipeline-Eventuallyyieldsa2Dpositioninscreencoordinates-Thispositionintheframebufferiswherethenextrasterprimitiv
eisdrawn16BufferSelection•OpenGLcandrawintoorreadfromanyofthecolorbuffers(front,back,auxiliary)•Defaulttothebackbuffer•
ChangewithglDrawBufferandglReadBuffer•Notethatformatofthepixelsintheframebufferisdifferentfromthatofprocessormemoryandthesetwotypes
ofmemoryresideindifferentplaces-Needpackingandunpacking-DrawingandreadingcanbeslowOpenGLcodeLib3dsMesh*mesh=f3d
s->meshes[i];//由3ds文件中读取出需要数据glGenBuffers(1,&m_Vertex[i]);glGenBuffers(1,&m_Index[i]);glBindBuffer(GL_ARRAY_BUFFER,m_Vertex[i]);glBufferData(GL
_ARRAY_BUFFER,(sizeof(float)*9*mesh->nfaces)+(sizeof(float)*3*mesh->nvertices),TotleV,GL_STATIC_DRAW);glVertex
Pointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));glNormalPointer(GL_FLOAT,0,BUFFER_OFFSET(V_size));glBindBuffer(GL_ARRAY_BUFFER,
m_Vertex[0]);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_Index[0]);glDrawElements(GL_TRIANGLES,DrawCount[0],GL_UNSIGNED_SHO
RT,NULL);1718Bitmaps•OpenGLtreats1-bitpixels(bitmaps)differentlyfrommulti-bitpixels(pixelmaps)•Bitmapsaremasksthatdetermineifthecorrespo
ndingpixelintheframebufferisdrawnwiththepresentrastercolor-0colorunchanged-1colorchangedbasedonwriti
ngmode•Bitmapsareusefulforrastertextcursor(XOR)-GLUTfont:GLUT_BIT_MAP_8_BY_1319RasterColor•SameasdrawingcolorsetbyglColor*()•Currentr
astercolorisfixedbylastcalltoglRasterPos*()•Geometrydrawninblue•OnesinbitmapuseadrawingcolorofredglColo
r3f(1.0,0.0,0.0);glRasterPos3f(x,y,z);glColor3f(0.0,0.0,1.0);glBitmap(…….glBegin(GL_LINES);glVertex3f(…..)20DrawingBitmapsglBit
map(width,height,x0,y0,xi,yi,bitmap)firstrasterpositionsecondrasterpositionoffsetfromrasterpositionincrementsinraster
positionafterbitmapdrawnTextdefinitioninOpenGL•Rastertextisfasterthanvectortext;•Times,Courier,ComputerModern;•Size:10pt,24pt(1
inch=72pt)21TextinOpenGL•8x13textisdefinedinGLUT_BITMAP_8_BY_13;•Thesizeoftextisrelatedwithresolutionofscreen;•Defi
ne8x13text:•GLubytemy_font[128][13];•base=glGenLists(128);•for(i=0;i<128;i++){•glNewList(base+i,GL_COMPILE);•glBitMap(8,13,0.0,0.0,10
.0,0.0,my_font[i]);•glEndList();•}2223Example:CheckerBoardGLubytewb[2]={0x00,0xFF};GLubytecheck[512];i
nti,j;for(i=0;i<64;i++)for(j=0;j<8,j++)check[i*8+j]=wb[(i/8+j)%2];glBitmap(64,64,0.0,0.0,0.0,0.0,check);24ImageFormats•W
eoftenworkwithimagesinastandardformat(JPEG,TIFF,GIF,PS,EPS)•Howdoweread/writesuchimageswithOpenGL?•NosupportinOpenGL-OpenGLknowsnothingofimagefor
mats-SomecodeavailableonWeb-Canwritereaders/writersforsomesimpleformatsinOpenGLPS&EPS•PostScript(PS)imageforcontrollingprinterRGBimageconvertsto7bi
tsASCII;soPSimagecanbeidentifiedbyprinter,butitislarge.•EncapsulatedPostScript(EPS)issimilartoPSimage.25GIFimage•CompuServeIncor
porated(1987)GraphicsInterchangeFormatColorIndexImagelow-resolutionsmallanimations(imageslices)26TIFF•Tagged(标签)ImageFileFormat•Highquality•First
forscannedcopy•Losslesscompression•Cansavevectorboundaryofimage27JPEG•lossycompression•Highcompressionratio•Discretecosinetransform28Sizecomparis
on•Image1200x1200•TIFF:1440198bytes(about1.37m)•JPEG:80109and38962bytes(about78kb)•EPS:abouttwiceofTIFF•CompressedTIFF
:abouthalfsize•ZIPcompression:TIFFandEPSaresimilar29TIFFJPEG-18JPEG-3730DisplayingaPPMImage(Linux)•PPMisaverysimpleformat(24
bits)•Eachimagefileconsistsofaheaderfollowedbyallthepixeldata•HeaderP3#comment1#comment2.#commentnrowscolumnsmaxvaluepixels31R
eadingtheHeaderFILE*fd;intk,nm;charc;inti;charb[100];floats;intred,green,blue;printf("enterfilename\n");scanf("%s",b);fd=fop
en(b,"r");fscanf(fd,"%[^\n]",b);if(b[0]!='P'||b[1]!='3'){printf("%sisnotaPPMfile!\n",b);exit(0);}printf("%sisaPPMfile\n",b);checkfor“P3”infirs
tline32ReadingtheHeader(cont)fscanf(fd,"%c",&c);while(c=='#'){fscanf(fd,"%[^\n]",b);printf("%s\n",b);fscanf(fd,"%c",&c);}ungetc(c,fd);skipovercomm
entsbylookingfor#infirstcolumn33ReadingtheDatafscanf(fd,"%d%d%d",&n,&m,&k);printf("%drows%dcolumnsmaxvalue=%d\n",n,m,k);nm=n*m;image=malloc
(3*sizeof(GLuint)*nm);s=255./k;for(i=0;i<nm;i++){fscanf(fd,"%d%d%d",&red,&green,&blue);image[3*nm-3*i-3]=red;image[3*nm-3*i-2]=green
;image[3*nm-3*i-1]=blue;}scalefactor34PixelMaps•OpenGLworkswithrectangulararraysofpixelscalledpixelmapsorimages•Pixelsareinone
byte(8bit)chunks-Luminance(grayscale)images1byte/pixel-RGB3bytes/pixel•Threefunctions-Drawpixels:proces
sormemorytoframebuffer-Readpixels:framebuffertoprocessormemory-Copypixels:framebuffertoframebufferPixelformat•Imageinmemory:RGB,RGBA,Index•Format
:intorfloat•glPixelMap3536OpenGLPixelFunctionsglReadPixels(x,y,width,height,format,type,myimage)startpixelinframebuffersizetypeofimag
etypeofpixelspointertoprocessormemoryGLubytemyimage[512][512][3];glReadPixels(0,0,512,512,GL_RGB,GL_UNSIGNED_BYTE,myimage);glDrawPixels
(width,height,format,type,myimage)startsatrasterposition37ScalingtheImageDataWecanscaletheimageinthe
pipelineglPixelTransferf(GL_RED_SCALE,s);glPixelTransferf(GL_GREEN_SCALE,s);glPixelTransferf(GL_BLUE_SCALE,s);Wemayhavetoswapbyteswhenwegofrompr
ocessormemorytotheframebufferdependingontheprocessor.Ifso,wecanuseglPixelStorei(GL_UNPACK_SWAP_BYTES,GL_TRUE);3
8Thedisplaycallbackvoiddisplay(){glClear(GL_COLOR_BUFFER_BIT);glRasterPos2i(0,0);glDrawPixels(n,m,GL_R
GB,GL_UNSIGNED_INT,image);glFlush();}