Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:31:35+00:00 2026-05-26T14:31:35+00:00

I’ve got some code that initializes OpenGL to render to a java.awt.Canvas. The problem

  • 0

I’ve got some code that initializes OpenGL to render to a java.awt.Canvas.
The problem is, I can’t figure out how I can get the buffer of the canvas and turn it into a BufferedImage.

I’ve tried overriding getGraphics(), cloning the Raster, and replacing the CanvasPeer with a custom one.

I’m guessing OpenGL doesn’t use java graphics in any way then, so how can I get OpenGL’s buffer and convert it into a BufferedImage?

I am using LWJGL’s code for setting parent:

Display.setParent(display_parent);
Display.create();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:31:36+00:00Added an answer on May 26, 2026 at 2:31 pm

    You need to copy data from OpenGL buffer. I was using this method:

    FloatBuffer grabScreen(GL gl) 
    {       
        int w = SCREENWITDH;
        int h = SCREENHEIGHT;
        FloatBuffer bufor = FloatBuffer.allocate(w*h*4); // 4 = rgba
    
        gl.glReadBuffer(GL.GL_FRONT);
        gl.glReadPixels(0, 0, w, h, GL.GL_RGBA, GL.GL_FLOAT, bufor); //Copy the image to the array imageData
    
        return bufor;
    }
    

    You need to use something similar according to your OpenGL wrapper. This is JOGL example.

    And here for LWJGL wrapper:

    private static synchronized byte[] grabScreen()
    {
        int w = screenWidth;
        int h = screenHeight;
        ByteBuffer bufor = BufferUtils.createByteBuffer(w * h * 3);
    
        GL11.glReadPixels(0, 0, w, h, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, bufor); //Copy the image to the array imageData
    
        byte[] byteimg = new byte[w * h * 3];
        bufor.get(byteimg, 0, byteimg.length);
        return byteimg;
    }
    

    EDIT

    This may be useful also (it’s not fully mine, should be tuned too):

    BufferedImage toImage(byte[] data, int w, int h)
    {
        if (data.length == 0)
            return null;
    
        DataBuffer buffer = new DataBufferByte(data, w * h);
    
        int pixelStride = 3; //assuming r, g, b, skip, r, g, b, skip...
        int scanlineStride = 3 * w; //no extra padding   
        int[] bandOffsets = { 0, 1, 2 }; //r, g, b
        WritableRaster raster = Raster.createInterleavedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets,
                null);
    
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        boolean hasAlpha = false;
        boolean isAlphaPremultiplied = true;
        int transparency = Transparency.TRANSLUCENT;
        int transferType = DataBuffer.TYPE_BYTE;
        ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency,
                transferType);
    
        BufferedImage image = new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
    
        AffineTransform flip;
        AffineTransformOp op;
        flip = AffineTransform.getScaleInstance(1, -1);
        flip.translate(0, -image.getHeight());
        op = new AffineTransformOp(flip, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);
    
        return image;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.