Delphi中的OpenGL - 16位纹理显示

时间:2014-08-22 22:40:15

标签: delphi opengl

我有一个问题,我不知道打败这个。 我将一个16位单色纹理从TIFF图像加载到内存中,然后将此纹理上传到OpenGL纹理中。它工作正常。但我希望有两个参数 - 一个范围为0..65535的X1和X2整数,其中:低于X1的值显示为黑色,高于X2的值为白色,X1和X2之间的值缩放为一个监视器的深度(通常为8位)。 我怎么用OpenGL做到这一点?我是通过GDI制作的,但对高分辨率图像来说效果很慢。

Delphi中有代码,我在其中初始化OpenGL并加载纹理。

创建表单(开头):

procedure TForm1.FormCreate(Sender: TObject);
var
  DC:HDC;
  RC:HGLRC;
begin
  OsX:=0;
  OsY:=0;
  OsZ:=-5;
  Obrot:=0;

  // for Open GL
  DC:=GetDC(Panel1.Handle);
  SetupPixelFormat(DC);  //pixel format setting up
  RC:=wglCreateContext(DC);  //makes OpenGL window out of DC
  wglMakeCurrent(DC, RC);    //makes OpenGL window active
  GLInit; //calling opengl init
end;

设置像素格式:

procedure TForm1.setupPixelFormat(DC:HDC);
const
    pfd:TPIXELFORMATDESCRIPTOR = (
        nSize:sizeof(TPIXELFORMATDESCRIPTOR); 
        nVersion:1; 
        dwFlags:PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER;
        iPixelType:PFD_TYPE_RGBA; 
        cColorBits:24; 
        cRedBits:0;
        cRedShift:0;
        cGreenBits:0; 
        cGreenShift:0; 
        cBlueBits:0;
        cBlueShift:0;
        cAlphaBits:0;
        cAlphaShift:0;
        cAccumBits: 0;
        cAccumRedBits: 0; 
        cAccumGreenBits: 0;
        cAccumBlueBits: 0;
        cAccumAlphaBits: 0; 
        cDepthBits:16; 
        cStencilBits:0; 
        cAuxBuffers:0; 
        iLayerType:PFD_MAIN_PLANE; 
        bReserved: 0;
        dwLayerMask: 0; 
        dwVisibleMask: 0;
        dwDamageMask: 0; 
   );
var
  pixelFormat : integer;
begin
  pixelFormat := ChoosePixelFormat(DC, @pfd);
  if (pixelFormat = 0) then exit;
  if (SetPixelFormat(DC, pixelFormat, @pfd) <> TRUE) then exit;
end;

OpenGL初始化:

procedure TForm1.GLInit();
var
  Tablica:PTablica2Word; //pointer to an array of Word of Word
  Tab:PTablica1Word; //pointer to an array of Word
  Wys,Szer:Cardinal; //Width and Height of loaded image
  pData:Pointer;
  p:Pointer;
  i,j:Integer;
  Krok:Cardinal;
begin
  glMatrixMode(GL_PROJECTION);
  glFrustum(-0.1, 0.1, -0.1, 0.1, 0.3, 25.0);
  glMatrixMode(GL_MODELVIEW);
  glEnable(GL_DEPTH_TEST);

  //loading a 16-bit texture from 3.tiff file
  New(Tablica);
  TIFFForm.LoadFile('3.tiff',Tablica,True); //loading an image
  TablicaRozmiar(Tablica,Szer,Wys); //getting an Width and Height of loaded image

  New(Tab);
  SetLength(Tab.Dane,Szer*Wys);
  Krok:=0;
  //dropping loaded data to buffer Tab (pointer)
  For i:=0 to Wys-1 do
  Begin
    For j:=0 to Szer-1 do
    Begin
      Tab.Dane[Krok]:=Tablica.Dane[j,i];
      Inc(Krok);
    End;
  End;
  //assigning pointer
  pData:=Pointer(@Tab.Dane[0]);
  CreateTexture(Szer,Wys,pData); //creating an texture from loaded image
  Dispose(Tab);
  Dispose(Tablica);    

  Draw();
end;

创建纹理:

function TForm1.CreateTexture(Width,Height:Word;pData:Pointer):Cardinal;
Begin
  glGenTextures(1, Texture);
  glBindTexture(GL_TEXTURE_2D, Texture);
  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  Result:=gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE16, Width, Height, GL_LUMINANCE, GL_UNSIGNED_SHORT, pData); //putting an loaded 16-bit image into opengl texture

end;

图:

procedure TForm1.Draw();
begin
  Application.ProcessMessages;

  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glTranslatef(OsX,0,0);
  glTranslatef(0,OsY,0);
  glTranslatef(0,0,OsZ);

  glRotate(Obrot,0,1,0);

  glEnable(GL_CULL_FACE);
  glEnable(GL_TEXTURE_2D);

  glTexGenf(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  glTexGenf(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);


  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);


  glBindTexture(GL_TEXTURE_2D, Texture);

  glBegin(GL_QUADS);
    // Front Face
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  0.0);
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  0.0);
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  0.0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  0.0);
  glEnd();

  SwapBuffers(wglGetCurrentDC);
end;

感谢任何建议:)

使用Graphics32和Scanline有一部分GDI绘图:

TablicaRozmiar(ai_TablicaDraw,Szer,Wys); //getting Width and Height of data
Bitmapa.Width:=Szer; //setting up virtual TBitmap32 Width
Bitmapa.Height:=Wys; //setting up virtual TBitmap32 Height
KolorMakeTable; //making a table of colour - scalling from 16bit to 8 bit display, using X1 and X2 values
For i:=0 to Wys-1 do //making a scanline
Begin
  abi_Punkty:=Bitmapa.ScanLine[i]; //abi_Punkty:PColor32Array
  For j:=0 to Szer-1 do
  Begin
    abi_Punkty[j]:=((KolorTableR[ai_TablicaDraw.Dane[j,i]] shl 8) + KolorTableG[ai_TablicaDraw.Dane[j,i]]) shl 8 + KolorTableB[ai_TablicaDraw.Dane[j,i]]; //putting a grayscale color
  End;
End;

0 个答案:

没有答案