Read an image file for OpenGL texture lazarus / 70-OpenGL ---------------------------------------------------------------------- LemonParty · 2024-12-29 13:36 How to read an image for calling procedure glTexImage2D? I mean the last artgument "pixels". ---------------------------------------------------------------------- Lulu · 2024-12-30 08:46 Hi, you can study from existing libraries that use OpenGL. here some links: https://github.com/neurolabusc/OpenGLCoreTutorials  see file gltex.pas ,procedure TGLTex.LoadTex(fnm: string). There is also https://github.com/Lulu04/OGLCScene this one use TBGRABitmap library to load image file from disk. Go to folder source, in file "oglcTexture.inc" you will found the code to create an OpenGL texture from an image file. (see TTextureManager.Add(...) functions) ---------------------------------------------------------------------- marcov · 2024-12-30 10:44 You can use any image type, as long as you can get the address of the first pixel in memory. That is what you need to pass to that parameter. This is not necessarily the (0,0) pixel. If the image is stored bottom up (like BMPs by default), it may be (0,height-1). You can  compensate that by the y-mirroring the points used to draw the texture. Note that some image types have padding bytes to make the size of a row of pixels divisable by some value (most notably again BMP where it is "4"). You can set that with e.g. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   glPixelStorei( GL_UNPACK_ALIGNMENT, 4); ---------------------------------------------------------------------- LemonParty · 2024-12-30 14:22 I modified code from https://github.com/neurolabusc/OpenGLCoreTutorials Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure FlipVertical (var px: TPicture);var  p: array of byte;  i, half, b: integer;  LoPtr, HiPtr: PInteger;begin  if px.Height < 3 then exit;  half := (px.Height div 2);  b := px.Bitmap.RawImage.Description.BytesPerLine;  LoPtr := PInteger(px.Bitmap.RawImage.Data);  HiPtr := PInteger(px.Bitmap.RawImage.Data+ ((px.Height -1) * b));  setlength(p, b);  for i := 1 to half do begin    System.Move(LoPtr^,p[0],b); //(src, dst,sz)    System.Move(HiPtr^,LoPtr^,b);    System.Move(p[0],HiPtr^,b);    Inc(PByte(LoPtr), b );    Dec(PByte(HiPtr), b);  end;end; procedure LoadTex(fnm: string; var Texture: GLuint; var Width, Height: GLuint);var  px: TPicture;  internalformat: GLint;begin  px := TPicture.Create;  try    px.LoadFromFile(fnm);  except    px.Bitmap.Width:=0;  end;  if (px.Bitmap.PixelFormat <> pf32bit ) or (px.Bitmap.Width < 1) or (px.Bitmap.Height < 1) then begin    //showmessage('Error loading 32-bit power-of-two bitmap '+fnm);    exit;  end;  FlipVertical(px); //flip vertical: pngs stored so first line is top of screen, OpenGL usually uses cartesian coordinates where increasing Y moves upwards  Width:= px.Bitmap.Width;  Height:= px.Bitmap.Height;  glGenTextures(1, @Texture);  glBindTexture(GL_TEXTURE_2D,  Texture);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  {$IFDEF UNIX}  if px.Bitmap.PixelFormat = pf32bit    then internalformat := GL_BGRA    else internalformat := GL_BGR;  {$ELSE}  if px.Bitmap.PixelFormat = pf32bit    then internalformat := GL_RGBA    else internalformat := GL_RGB;  {$ENDIF}  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, px.Width, px.Height, 0, internalformat, GL_UNSIGNED_BYTE, PInteger(px.Bitmap.RawImage.Data));  glGenerateMipmap(GL_TEXTURE_2D);  px.Free;end;   And it works. Thank you.