Ignore:
Timestamp:
Mar 20, 2011, 12:23:08 PM (13 years ago)
Author:
george
Message:
  • Added: OpenGL method to display bitmap as texture on top of 2D rectangle.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GraphicTest/UDrawMethod.pas

    r210 r211  
    88  Classes, SysUtils, StdCtrls, ExtCtrls, UPlatform, UFastBitmap, Graphics,
    99  LCLType, IntfGraphics, fpImage, GraphType, BGRABitmap, BGRABitmapTypes,
    10   LclIntf;
     10  LclIntf, GL, OpenGLContext;
    1111
    1212type
     
    2727    FrameDuration: TDateTime;
    2828    PaintObject: TPaintObject;
     29    OpenGLBitmap: Pointer;
     30    OpenGLControl: TOpenGLControl;
     31    TextureId: GLuint;
    2932    constructor Create; virtual;
    3033    destructor Destroy; override;
     
    9497  end;
    9598
     99  { TOpenGLMethod }
     100
     101  TOpenGLMethod = class(TDrawMethod)
     102    procedure SetBitmap(const AValue: TBitmap); override;
     103    constructor Create; override;
     104    destructor Destroy; override;
     105    procedure DrawFrame(FastBitmap: TFastBitmap); override;
     106  end;
     107
     108
    96109const
    97   DrawMethodClasses: array[0..6] of TDrawMethodClass = (
     110  DrawMethodClasses: array[0..7] of TDrawMethodClass = (
    98111    TCanvasPixels, TCanvasPixelsUpdateLock, TLazIntfImageColorsCopy,
    99112    TLazIntfImageColorsNoCopy, TBitmapRawImageData, TBitmapRawImageDataPaintBox,
    100     TBGRABitmapPaintBox);
     113    TBGRABitmapPaintBox, TOpenGLMethod);
    101114
    102115implementation
     116
     117{ TOpenGLMethod }
     118
     119procedure TOpenGLMethod.SetBitmap(const AValue: TBitmap);
     120begin
     121  inherited SetBitmap(AValue);
     122end;
     123
     124constructor TOpenGLMethod.Create;
     125begin
     126  inherited Create;
     127  Caption := 'OpenGL';
     128  PaintObject := poOpenGL;
     129end;
     130
     131destructor TOpenGLMethod.Destroy;
     132begin
     133  inherited Destroy;
     134end;
     135
     136procedure TOpenGLMethod.DrawFrame(FastBitmap: TFastBitmap);
     137var
     138  X, Y: Integer;
     139  P: PInteger;
     140  R: PInteger;
     141const
     142  GL_CLAMP_TO_EDGE = $812F;
     143begin
     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
     156  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
     157  //glLoadIdentity;             { clear the matrix }
     158  //glTranslatef(0.0, 0.0, -3.0);  // -2.5); { viewing transformation }
     159
     160  //glLoadIdentity;             { clear the matrix }
     161
     162  P := OpenGLBitmap;
     163  with FastBitmap do
     164  for Y := 0 to Size.Y - 1 do begin
     165    R := P;
     166    for X := 0 to Size.X - 1 do begin
     167      //R^ := Round($ff * (Y / Size.Y)) or $ff000000;
     168      R^  := NoSwapBRComponent(Pixels[X, Y]) or $ff000000;
     169      Inc(R);
     170    end;
     171    Inc(P, Size.X);
     172  end;
     173
     174    glLoadIdentity;
     175    //glRotatef(30.0, 0, 0, 1.0);
     176    glTranslatef(-OpenGLControl.Width div 2, -OpenGLControl.Height div 2, 0.0);
     177
     178    glEnable(GL_TEXTURE_2D);
     179    glBindTexture(GL_TEXTURE_2D, TextureId);
     180      //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     181      //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     182      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     183      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     184      glTexImage2D(GL_TEXTURE_2D, 0, 4, OpenGLControl.Width, OpenGLControl.Height,
     185        0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap);
     186      //glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 256,
     187      //0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap);
     188
     189    //Define how alpha blending will work and enable alpha blending.
     190    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     191    //glEnable(GL_BLEND);
     192
     193    glBegin(GL_QUADS);
     194    //glBegin(GL_POLYGON);
     195      glColor3ub(255, 255, 255);
     196      glTexCoord2f(0, 0);
     197      glVertex3f(0, 0, 0);
     198      glTexCoord2f(OpenGLControl.Width div 2, 0);
     199      glVertex3f(OpenGLControl.Width, 0, 0);
     200      glTexCoord2f(OpenGLControl.Width div 2, OpenGLControl.Height div 2);
     201      glVertex3f(OpenGLControl.Width, OpenGLControl.Height, 0);
     202      glTexCoord2f(0, OpenGLControl.Height div 2);
     203      glVertex3f(0, OpenGLControl.Height, 0);
     204    glEnd();
     205
     206  OpenGLControl.SwapBuffers;
     207end;
    103208
    104209{ TBGRABitmapPaintBox }
Note: See TracChangeset for help on using the changeset viewer.