OpenGL: flip image before glDrawPixels using Jogl

OpenGL images put (0,0) at the bottom left. Java images have (0,0) at the upper left, so as the y coordinates increase and move further from the axis, the row order makes the image look like it's upside down in the OpenGL world.

So, to provide glDrawPixels() with a suitable array, I need to flip the image vertically so that the row order puts the bottom of the image on the first row.

how do I do the flip?

public void display(GLDrawable drawable) {
     GL gl = drawable.getGL();
     gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
     gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);                    
     gWorld.getPixMap().getPixelData().copyToArray(0, pixelData, 0,
               WIDTH * HEIGHT);

     // i need to flip the pixelData here before it's drawn - how????
     
     gl.glDrawPixels(WIDTH, HEIGHT, gl.GL_BGRA,
               gl.GL_UNSIGNED_INT_8_8_8_8_REV, pixelData);
     frameCount++;
}
amitzoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
glPixelZoom(1.0,-1.0)


0
amitzoAuthor Commented:
I tried it but all I get is a black screen (note that it does work before I enter the line), there are no Exceptions in the log and no image is displayed.

could this somehow be messing up the color channel order? I think BGRA stands for blue,green, red, alpha
0
amitzoAuthor Commented:
Note that I don't need to reverse the entire pixelData, I just to flip the order of the rows. Each row needs to preserve the order within it.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

InteractiveMindCommented:
public static int [] flipVertically( int [] pixelData )
{
    int [] newPixelData = new int [pixelData.length] ;
   
    for ( int i=0; i<HEIGHT; i++ )
    {
        newPixelData [i] = pixelData [HEIGHT-i-1] ;
    }
   
    return newPixelData ;
}
0
InteractiveMindCommented:
Sorry, I'm not sure what I was thinking there ...  <slaps head>
0
InteractiveMindCommented:
Try something more like:

public static int [] flipVertically( int [] pixelData )
{
    int [] newPixelData = new int [pixelData.length] ;
   
    for ( int j=0; j<HEIGHT; j++ )
    {
        for ( int i=0; i<WIDTH; i++ )
        {
            newPixelData[j*WIDTH+i] = pixelData[HEIGHT*WIDTH-j*WIDTH-1+i] ;
        }
    }
   
    return newPixelData ;
}
0
amitzoAuthor Commented:
getting warmer, but now getting an ArrayIndexOutOfBounds for the second half of this statement

newPixelData[j*WIDTH+i] = pixelData[HEIGHT*WIDTH-j*WIDTH-1+i] ;
0
InteractiveMindCommented:
try

newPixelData[j*WIDTH+i] = pixelData[HEIGHT*WIDTH-(j+1)*WIDTH+i] ;
0
amitzoAuthor Commented:
even warmer, still one problem, the image is flipped left to right
0
InteractiveMindCommented:
This example program seems to be flipping the example array vertically using that code:


public class T
{
   
    static int WIDTH = 3 ;
    static int HEIGHT = 3 ;
   
    public static void main(String[]a)
    {
       
        // Declare array
        int [] arr = new int [] { 1, 2, 3,
                                4, 5, 6,
                                7, 8, 9 } ;
       
        // Output the numbers before the flip
        System.out.println( "Prior:\n" ) ;
        output( arr ) ;
       
        // Flip the array
        arr = flipVertically( arr ) ;
       
        // Output the numbers after the flip
        System.out.println( "\n\nPost:\n" ) ;
        output( arr ) ;
    }
   
    public static int [] flipVertically( int [] pixelData )
    {
        int [] newPixelData = new int [pixelData.length] ;
       
        for ( int j=0; j<HEIGHT; j++ )
        {
            for ( int i=0; i<WIDTH; i++ )
            {
                newPixelData[j*WIDTH+i] = pixelData[HEIGHT*WIDTH-(j+1)*WIDTH+i] ;
            }
        }
       
        return newPixelData ;
    }
   
    public static void output( int [] arr )
    {
        for ( int j=0; j<HEIGHT; j++ )
        {
            for ( int i=0; i<WIDTH; i++ )
            {
                System.out.print( arr[j*WIDTH+i] + "\t" ) ;
            }
            System.out.println() ;
        }
    }
   
}
0
amitzoAuthor Commented:
Another concern is the speed, video is 30 fps and doing this flip in Java slows down the preview substantially because it's not using OpenGL's hardware accelarated methods.

Can this be done by mapping the image data to an OpenGL canvas of some sort that can be rotated 180 degrees using hardware acceleration?
0
InteractiveMindCommented:
I bloody well hope so.

Will have a flick through my books ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amitzoAuthor Commented:
I figured out a quicker way to do it in Java.

public void flipVertically( int [] pixelData ) {      
    for ( int row=0; row<HEIGHT; row++ ) {
        System.arraycopy(pixelData, row*WIDTH, newPixelData, (HEIGHT-row-1)*WIDTH, WIDTH) ;                }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.