From: Michael H. <mh...@us...> - 2000-11-20 20:53:54
|
Update of /cvsroot/pythianproject/Prototypes/GLBitmapDemo In directory slayer.i.sourceforge.net:/tmp/cvs-serv13453 Modified Files: frmMain.pas gadgetcollage.bmp Added Files: glBitmapDemo.cfg Log Message: updated to use textured bitmaps -mike --- NEW FILE --- -$A+ -$B- -$C+ -$D+ -$E- -$F- -$G+ -$H+ -$I+ -$J+ -$K- -$L+ -$M- -$N+ -$O+ -$P+ -$Q- -$R- -$S- -$T- -$U- -$V+ -$W- -$X+ -$YD -$Z1 -cg -AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE; -H+ -W+ -M -$M16384,1048576 -K$00400000 -U"..\..\PythianProject\Source\Units" -O"..\..\PythianProject\Source\Units" -I"..\..\PythianProject\Source\Units" -R"..\..\PythianProject\Source\Units" Index: frmMain.pas =================================================================== RCS file: /cvsroot/pythianproject/Prototypes/GLBitmapDemo/frmMain.pas,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** frmMain.pas 2000/10/26 17:16:15 1.1 --- frmMain.pas 2000/11/20 20:53:51 1.2 *************** *** 9,14 **** uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ! Menus, ExtCtrls, GLPanel, OpenGL, Textures, Trace; type --- 9,17 ---- uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ! Menus, ExtCtrls, GLPanel, OpenGL, Textures, Trace, FastDIB, FastFiles; + const + MAXTEXIDS = 256; + type *************** *** 22,25 **** --- 25,35 ---- } + TTexBMPData = record + DisplayList :integer; + TexIDs :array[1..MAXTEXIDS] of Cardinal; + cellsWidth, cellsHeight :integer; + end; + + // we use this to store the pixel data from a bitmap TPixelData = TByteArray; *************** *** 51,61 **** --- 61,81 ---- Px :PPixelData; PxWidth, PxHeight :integer; + + FastBMP :TFastDIB; + td:TTexBMPData; + procedure SetProjection(Sender: TObject); function BmpToPixelData(filename:string):PPixelData; end; + var MainForm: TMainForm; + function MakeTexturesFromBmp(var aBMP:TFastDIB):TTexBMPData; // returns display list + procedure DeleteTexBMP(bmp:TTexBMPData); + procedure GenTexFromBMP(BMP:TFastDIB; tx:integer); + procedure DrawTexBMP(bmp:TTexBMPData); + implementation *************** *** 91,94 **** --- 111,115 ---- procedure TMainForm.GLPanelGLInit(Sender: TObject); + var otherbmp :TFastDIB; begin // Add GL init code here *************** *** 112,115 **** --- 133,145 ---- end; end; + + FastBMP := TFastDIB.Create; + LoadFromFile(FastBMP,'millennium_wallpaper.bmp'); + + otherBmp := TFastDIB.Create; + otherBMp.free; + + td := MakeTexturesFromBMP(FastBMP); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); *************** *** 132,141 **** glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); ! // reset the matrix to it's identity (original, unmodified) matrix glMatrixMode(GL_PROJECTION); glLoadIdentity; glMatrixMode(GL_MODELVIEW); glLoadIdentity; { ADD YOUR DRAWING CODE HERE } --- 162,195 ---- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); ! // add bitmap code here ! // px contains our data in RGB format 8bit per component, 24 bits pp ! glRasterPos2d(-1,-1); ! glClear(GL_DEPTH_BUFFER_BIT); ! glDisable(GL_DEPTH_TEST); ! ! {if px <> nil then ! glDrawPixels(pxWidth,pxHeight,GL_RGB,GL_UNSIGNED_BYTE,px);} ! glEnable(GL_TEXTURE_2D); ! glColor3f(1.0,1.0,1.0); ! glMatrixMode(GL_PROJECTION); glLoadIdentity; glMatrixMode(GL_MODELVIEW); glLoadIdentity; + // change co-ordinate system to 1:1 pixel mapping + glScalef(2.0 / Width, -2.0 / Height, 1.0); + glTranslatef(-(Width / 2), -(Height / 2), 0); + DrawTexBMP(td); + + glEnable(GL_DEPTH_TEST); + glClear(GL_DEPTH_BUFFER_BIT); + + glDisable(GL_TEXTURE_2D); + // reset the matrix to it's identity (original, unmodified) matrix + glMatrixMode(GL_PROJECTION); + glLoadIdentity; + glMatrixMode(GL_MODELVIEW); + glLoadIdentity; { ADD YOUR DRAWING CODE HERE } *************** *** 202,214 **** glEnd; ! // add bitmap code here ! // px contains our data in RGB format 8bit per component, 24 bits pp ! glLoadIdentity; ! glRasterPos2d(-1,-1); ! glClear(GL_DEPTH_BUFFER_BIT); ! glDisable(GL_DEPTH_TEST); ! if px <> nil then ! glDrawPixels(pxWidth,pxHeight,GL_RGB,GL_UNSIGNED_BYTE,px); ! glEnable(GL_DEPTH_TEST); end; --- 256,260 ---- glEnd; ! end; *************** *** 281,284 **** --- 327,484 ---- if assigned(px) then FreeMem(px); + FastBMP.Free; + DeleteTexBMP(td); + end; + + function MakeTexturesFromBmp(var aBMP:TFastDIB):TTexBMPData; // returns display list + var + dl :integer; + cellsX,cellsY :byte; // number of cells in each direction + buffer:TFastDIB; + x,y:integer; + r:TRect; + id:Cardinal; + begin + cellsX := (aBMP.Width div 256) + 1; + cellsY := (aBMP.Height div 256) + 1; + + buffer := nil; + + Result.cellsWidth := cellsX; + Result.cellsHeight := cellsY; + for x := 1 to MAXTEXIDS do + result.TexIDs[x] := 0; + for y := 1 to cellsY do + begin + for x := 1 to cellsX do + begin + // clear temp buffer + if assigned(buffer) then buffer.Free; + buffer := TFastDIB.Create; + + // work out rect we want to copy to a temporary buffer for + // texture generation + r.Left := (x-1)*256; + r.Top := (y-1)*256; + r.Right := r.Left+256; // now make sure they fit + if r.Right > aBMP.Width then r.Right := aBMP.Width; + r.Bottom := r.Top+256; + if r.Bottom > aBMP.Height then r.Bottom := aBMP.Height; + buffer.SetSize(256,256,24,0); + + aBMP.DrawRect(buffer.hDC,0,0,r.Right-r.Left,r.Bottom-r.Top,r.Left,r.Top); + // now tmpBMP has the part of the picture to generate in it + + // shrink textures here for maximum efficiency + // so go generate the texture from the fast DIB + glGenTextures(1,@id); + Result.TexIDS[x+((y-1)*result.cellsWidth)] := id; + GenTexFromBMP(buffer,id); + + end; + end; + + buffer.Free; + end; + + procedure GenTexFromBMP(BMP:TFastDIB; tx:integer); + var + pd :PPixelData; + x,y:integer; + c:TFColor; + red,green,blue:byte; + pxWidth, pxHeight:integer; + + function XYToOffset(ox,oy:integer):integer; + var pxoffset:integer; + begin + { + 0 1 2 3 4 5 6 7 8 9 PxWidth=10 + 0 X X X X X X X X X X PxHeight=3 + 1 X X X X X X X X X X + 2 X X X X X X X X X X } + pxoffset := 0; + oy := (PxHeight-1) - oy; // now y is OK + pxoffset := ox; + if oy > 0 then + begin + pxoffset := pxoffset + (oy*(PxWidth){-1}) {+ 1}; + end; + pxoffset := pxoffset * 3; // move into position for RGB data + Result := pxoffset; + end; + + begin + if bmp.Bpp <> 24 then raise Exception.Create('Don''t support non 24bit pixel formats! (image is '+IntToStr(bmp.bpp)+')'); + pxWidth := bmp.Width; + pxHeight := bmp.Height; + // allocate memory for it, assume RGB data (3 components) + pd := AllocMem( (bmp.Width*bmp.Height)*3 ); + // now for the inefficient bit - copying the data pixel by pixel + // from the Win32 GDI to our own data structure. + for y := 0 to pxHeight-1 do + begin + for x := 0 to pxWidth-1 do + begin + c := bmp.Pixels24[y,x]; + + pd^[ XYToOffset(x,y) + 0 ] := c.r; + pd^[ XYToOffset(x,y) + 1 ] := c.g; + pd^[ XYToOffset(x,y) + 2 ] := c.b; + end; + end; + + // now we have pixel data generate the texture + glBindTexture(GL_TEXTURE_2D, tx); // Start defining stored texture + + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); + + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + + glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND); + + glTexImage2D(GL_TEXTURE_2D, + 0, + 3, + pxWidth, + pxHeight, + 0, + GL_RGB, + GL_UNSIGNED_BYTE, + pd); + end; + + procedure DeleteTexBMP(bmp:TTexBMPData); + var a:integer; + begin + for a := 1 to bmp.cellsWidth*bmp.cellsHeight do + glDeleteTextures(1,@bmp.TexIDs[a]); + end; + + procedure DrawTexBMP(bmp:TTexBMPData); + var y,x:integer; + begin + glColor3f(1.0,1.0,1.0); + + glPushAttrib(GL_BLEND); + glDisable(GL_BLEND); + for y := 1 to bmp.cellsHeight do + begin + for x := 1 to bmp.cellsWidth do + begin + glBindTexture(GL_TEXTURE_2D,bmp.TexIDs[x+((y-1)*bmp.cellsWidth)]); + { the +1, -1 here is because there is a bug somewhere that mirrors + the last row to the first generating seams between the blocks } + glBegin(GL_QUADS); + glTexCoord2f(0,1); glVertex2f( 255*(x-1)-1 ,256*(y-1)); + glTexCoord2f(1,1); glVertex2f( 255*x ,256*(y-1)); + glTexCoord2f(1,0); glVertex2f( 255*x ,256*y+1); + glTexCoord2f(0,0); glVertex2f( 255*(x-1)-1 ,256*y+1); + glEnd; + end; + end; + glPopAttrib; end; Index: gadgetcollage.bmp =================================================================== RCS file: /cvsroot/pythianproject/Prototypes/GLBitmapDemo/gadgetcollage.bmp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 Binary files /tmp/cvsdQOYRR and /tmp/cvs2GYUVR differ |