Flipping after glReadPixels lazarus / 70-OpenGL ---------------------------------------------------------------------- gilsonalencar · 2012-07-19 12:18 After many efforts, i can to put a OpenGLControl graphics in a bitmap image. But it is inverted from original source, when glReadPixels is used. After weeks searching in the web,  i can´t to find any solution to flip it using gl functions. Anybody knows how to do it? Technics using Canvas.Pixels[X,Y] are very slow. ---------------------------------------------------------------------- Leledumbo · 2012-07-19 12:39 What format parameter (5th position) do you give? ---------------------------------------------------------------------- gilsonalencar · 2012-07-19 13:31 The complet code is here. Code: [Select] function BitmapImage(c:TOpenGLControl):TBitmap; var   data,rdata: pbyte;   w,h,s: integer;   bitMap: TBitmap; begin   c.Repaint;   w := c.Width;   h := c.Height;   s := w*h*4;   result := TBitmap.Create;   bitMap:=result;   try     bitMap.SetSize(w,h);     bitMap.PixelFormat:=pf32bit;     getMem(data,s);     try       glPixelStorei(GL_PACK_ALIGNMENT, 4);       pixel}       glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);            rdata := bitMap.RawImage.Data;       Move(data^,rdata^,bitMap.RawImage.DataSize);     finally       freemem(data);     end;   except     bitMap.free;   end; end; ---------------------------------------------------------------------- KpjComp · 2012-07-19 13:39 When you say inverted, do you mean the R & B are swapped. I wrote a routine here that might help -> http://80.123.225.56/index.php/topic,17383.msg95733.html#msg95733 ---------------------------------------------------------------------- User137 · 2012-07-19 13:55 If it's about colors, then this code: Code: [Select]glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);you can switch to GL_BGR. If it's graphics upside down, then you need to draw then in OpenGL differently. If you use ortho (2D) mode, you should set the screen so that origo (0, 0) is in top left. Or that may not help at all, who knows. I haven't used glReadPixels, i hear it's slow operation. Even slower than canvas operations. ---------------------------------------------------------------------- KpjComp · 2012-07-19 14:09 QuoteGL_BGR I think GL_BGR is an extension, so some video cards might not support it.  Maybe most video cards will have this option now, so it might not be an issue.  Quotei hear it's slow operation I suppose it depends on what your doing, I use it for doing image analysis and for this it seems to work pretty well.  Also if you do use it, one idea is to only grab the area of interest. ---------------------------------------------------------------------- gilsonalencar · 2012-07-19 14:15 QuoteWhen you say inverted, do you mean the R & B are swapped. I wrote a routine here that might help -> http://80.123.225.56/index.php/topic,17383.msg95733.html#msg95733 A 180 degrees inversion in position, not color. ---------------------------------------------------------------------- KpjComp · 2012-07-19 14:36 QuoteA 180 degrees inversion in position, not color. One idea is maybe try changing the HandleType to bmDIB, as I think there always top to bottom.  Not sure if this has an effect on Linux. Another idea is copy Scanlines at a time. ---------------------------------------------------------------------- gilsonalencar · 2012-07-19 14:37 The image file attached, shows the problem. On the left, the original graphics from OpenGLControl. On the right, the result of bitmap image (3D axis). ---------------------------------------------------------------------- KpjComp · 2012-07-19 15:02 That is strange, it's not just top to bottom, but left to right. Are you calling this during your render while the context is still valid, and straight after you do the swapbuffers? ---------------------------------------------------------------------- gilsonalencar · 2012-07-19 15:27 QuoteThat is strange, it's not just top to bottom, but left to right. Are you calling this during your render while the context is still valid, and straight after you do the swapbuffers? I'm calling this after do swapbuffers. ---------------------------------------------------------------------- KpjComp · 2012-07-19 15:50 QuoteI'm calling this after do swapbuffers. Oh, in that case is there any reason your doing -> Code: [Select]c.Repaint; If your doing this during the Paint Event, then your telling your control to repaint during your paint event, doesn't really make sense.  The way I captured the screen is set a flag to say then next render will be captured, and after the render I would reset.   Not sure if this is your problem though, I'm not an OpenGL expert. ---------------------------------------------------------------------- User137 · 2012-07-19 18:19 Some options: 1) You could try using Canvas.Copyrect() to flip the image to another TBitmap. It should support negative width and height in TRect. 2) Draw them 180 degree rotated when taking screenshot. 3) Same as 1), but copy pixel by pixel to inverse coordinates.