Changeset 212 for GraphicTest/UDrawMethod.pas
- Timestamp:
- Mar 20, 2011, 7:18:27 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest/UDrawMethod.pas
r211 r212 8 8 Classes, SysUtils, StdCtrls, ExtCtrls, UPlatform, UFastBitmap, Graphics, 9 9 LCLType, IntfGraphics, fpImage, GraphType, BGRABitmap, BGRABitmapTypes, 10 LclIntf, GL, OpenGLContext;10 LclIntf, GL, GLExt, OpenGLContext; 11 11 12 12 type … … 30 30 OpenGLControl: TOpenGLControl; 31 31 TextureId: GLuint; 32 procedure Init; virtual; 32 33 constructor Create; virtual; 33 34 destructor Destroy; override; … … 102 103 procedure SetBitmap(const AValue: TBitmap); override; 103 104 constructor Create; override; 105 procedure Init; override; 104 106 destructor Destroy; override; 105 107 procedure DrawFrame(FastBitmap: TFastBitmap); override; 106 108 end; 107 109 110 { TOpenGLPBOMethod } 111 112 TOpenGLPBOMethod = class(TDrawMethod) 113 pboIds: array[0..1] of GLuint; 114 Index, NextIndex: Integer; 115 procedure SetBitmap(const AValue: TBitmap); override; 116 procedure Init; override; 117 constructor Create; override; 118 destructor Destroy; override; 119 procedure DrawFrame(FastBitmap: TFastBitmap); override; 120 end; 108 121 109 122 const 110 DrawMethodClasses: array[0.. 7] of TDrawMethodClass = (123 DrawMethodClasses: array[0..8] of TDrawMethodClass = ( 111 124 TCanvasPixels, TCanvasPixelsUpdateLock, TLazIntfImageColorsCopy, 112 125 TLazIntfImageColorsNoCopy, TBitmapRawImageData, TBitmapRawImageDataPaintBox, 113 TBGRABitmapPaintBox, TOpenGLMethod );126 TBGRABitmapPaintBox, TOpenGLMethod, TOpenGLPBOMethod); 114 127 115 128 implementation 129 130 { TOpenGLPBOMethod } 131 132 procedure TOpenGLPBOMethod.SetBitmap(const AValue: TBitmap); 133 begin 134 inherited SetBitmap(AValue); 135 end; 136 137 //procedure glGenBuffersARB2 : procedure(n : GLsizei; buffers : PGLuint); extdecl; 138 139 procedure TOpenGLPBOMethod.Init; 140 var 141 DataSize: Integer; 142 glExtensions: string; 143 begin 144 OpenGLControl.MakeCurrent; 145 DataSize := OpenGLControl.Width * OpenGLControl.Height * SizeOf(Integer); 146 // glGenBuffersARB(Length(pboIds), PGLuint(pboIds)); 147 //if glext_LoadExtension('GL_ARB_pixel_buffer_object') then 148 if Load_GL_ARB_vertex_buffer_object then begin 149 glGenBuffersARB(2, @pboIds); 150 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]); 151 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DataSize, Pointer(0), GL_STREAM_READ_ARB); 152 glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]); 153 glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DataSize, Pointer(0), GL_STREAM_READ_ARB); 154 155 end else raise Exception.Create('GL_ARB_pixel_buffer_object not supported'); 156 157 glEnable(GL_TEXTURE_2D); 158 glBindTexture(GL_TEXTURE_2D, TextureId); 159 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 160 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 163 glTexImage2D(GL_TEXTURE_2D, 0, 4, OpenGLControl.Width, OpenGLControl.Height, 164 0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap); 165 end; 166 167 constructor TOpenGLPBOMethod.Create; 168 begin 169 inherited Create; 170 Caption := 'OpenGL PBO'; 171 PaintObject := poOpenGL; 172 // SetLength(pboIds, 2); 173 Index := 0; 174 NextIndex := 1; 175 end; 176 177 destructor TOpenGLPBOMethod.Destroy; 178 begin 179 inherited Destroy; 180 end; 181 182 procedure TOpenGLPBOMethod.DrawFrame(FastBitmap: TFastBitmap); 183 var 184 X, Y: Integer; 185 P: PInteger; 186 R: PInteger; 187 Ptr: ^GLubyte; 188 TextureShift: TPoint; 189 TextureShift2: TPoint; 190 const 191 GL_CLAMP_TO_EDGE = $812F; 192 begin 193 // "index" is used to read pixels from framebuffer to a PBO 194 // "nextIndex" is used to update pixels in the other PBO 195 Index := (Index + 1) mod 2; 196 NextIndex := (Index + 1) mod 2; 197 198 glLoadIdentity; 199 200 glBindTexture(GL_TEXTURE_2D, TextureId); 201 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 202 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 203 //glTexImage2D(GL_TEXTURE_2D, 0, 4, OpenGLControl.Width, OpenGLControl.Height, 204 // 0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap); 205 //glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 256, 206 //0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap); 207 208 // bind the texture and PBO 209 //glBindTexture(GL_TEXTURE_2D, textureId); 210 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[index]); 211 212 // copy pixels from PBO to texture object 213 // Use offset instead of ponter. 214 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, OpenGLControl.Width, OpenGLControl.Height, 215 GL_BGRA, GL_UNSIGNED_BYTE, Pointer(0)); 216 217 218 // bind PBO to update texture source 219 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[nextIndex]); 220 221 // Note that glMapBufferARB() causes sync issue. 222 // If GPU is working with this buffer, glMapBufferARB() will wait(stall) 223 // until GPU to finish its job. To avoid waiting (idle), you can call 224 // first glBufferDataARB() with NULL pointer before glMapBufferARB(). 225 // If you do that, the previous data in PBO will be discarded and 226 // glMapBufferARB() returns a new allocated pointer immediately 227 // even if GPU is still working with the previous data. 228 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, OpenGLControl.Width * OpenGLControl.Height * SizeOf(Integer), Pointer(0), GL_STREAM_DRAW_ARB); 229 230 // map the buffer object into client's memory 231 ptr := glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB); 232 if Assigned(ptr) then begin 233 // update data directly on the mapped buffer 234 P := PInteger(Ptr); 235 with FastBitmap do 236 for Y := 0 to Size.Y - 2 do begin 237 R := P; 238 for X := 0 to Size.X - 1 do begin 239 R^ := NoSwapBRComponent(Pixels[X, Y]) or $ff000000; 240 Inc(R); 241 end; 242 Inc(P, Size.X); 243 end; 244 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); 245 end; 246 247 // it is good idea to release PBOs with ID 0 after use. 248 // Once bound with 0, all pixel operations are back to normal ways. 249 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); 250 251 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); 252 //glRotatef(30.0, 0, 0, 1.0); 253 glTranslatef(-OpenGLControl.Width / 2, -OpenGLControl.Height / 2, 0.0); 254 glBindTexture(GL_TEXTURE_2D, TextureId); 255 256 TextureShift := Point(0, 0); 257 TextureShift2 := Point(0, 0); 258 glBegin(GL_QUADS); 259 glColor3ub(255, 255, 255); 260 glTexCoord2f(TextureShift.X, TextureShift.Y); 261 glVertex3f(TextureShift2.X, TextureShift2.Y, 0); 262 glTexCoord2f(TextureShift.X + OpenGLControl.Width div 2, TextureShift.Y); 263 glVertex3f(TextureShift2.X + OpenGLControl.Width, TextureShift2.Y, 0); 264 glTexCoord2f(TextureShift.X + OpenGLControl.Width div 2, TextureShift.Y + OpenGLControl.Height div 2); 265 glVertex3f(TextureShift2.X + OpenGLControl.Width, TextureShift2.Y + OpenGLControl.Height, 0); 266 glTexCoord2f(TextureShift.X, TextureShift.Y + OpenGLControl.Height div 2); 267 glVertex3f(TextureShift2.X, TextureShift2.Y + OpenGLControl.Height, 0); 268 glEnd(); 269 270 OpenGLControl.SwapBuffers; 271 end; 116 272 117 273 { TOpenGLMethod } … … 127 283 Caption := 'OpenGL'; 128 284 PaintObject := poOpenGL; 285 end; 286 287 procedure TOpenGLMethod.Init; 288 begin 289 inherited Init; 290 //OpenGLControl.MakeCurrent; 129 291 end; 130 292 … … 142 304 GL_CLAMP_TO_EDGE = $812F; 143 305 begin 144 (* glEnable(GL_TEXTURE_2D); // enables 2d textures 145 glClearColor(0.0,0.0,0.0,1.0); // sets background color 146 glClearDepth(1.0); 147 glDepthFunc(GL_LEQUAL); // the type of depth test to do 148 glEnable(GL_DEPTH_TEST); // enables depth testing 149 glShadeModel(GL_SMOOTH); // enables smooth color shading 150 {blending} 151 glColor4f(1.0,1.0,1.0,0.5); // Full Brightness, 50% Alpha ( NEW ) 152 glBlendFunc(GL_SRC_ALPHA, GL_ONE); 153 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); 154 *) 155 306 glLoadIdentity; 156 307 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); 157 308 //glLoadIdentity; { clear the matrix } 158 309 //glTranslatef(0.0, 0.0, -3.0); // -2.5); { viewing transformation } 159 160 //glLoadIdentity; { clear the matrix }161 310 162 311 P := OpenGLBitmap; … … 172 321 end; 173 322 174 glLoadIdentity;175 323 //glRotatef(30.0, 0, 0, 1.0); 176 324 glTranslatef(-OpenGLControl.Width div 2, -OpenGLControl.Height div 2, 0.0); … … 459 607 end; 460 608 609 procedure TDrawMethod.Init; 610 begin 611 612 end; 613 461 614 constructor TDrawMethod.Create; 462 615 begin
Note:
See TracChangeset
for help on using the changeset viewer.