[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

OpenGL: flip image before glDrawPixels using Jogl

Posted on 2006-03-27
13
Medium Priority
?
3,633 Views
Last Modified: 2013-12-06
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++;
}
0
Comment
Question by:amitzo
  • 6
  • 6
13 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 16303399
glPixelZoom(1.0,-1.0)


0
 

Author Comment

by:amitzo
ID: 16305978
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
 

Author Comment

by:amitzo
ID: 16306004
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16326373
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16326382
Sorry, I'm not sure what I was thinking there ...  <slaps head>
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16326440
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
 

Author Comment

by:amitzo
ID: 16326938
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16326970
try

newPixelData[j*WIDTH+i] = pixelData[HEIGHT*WIDTH-(j+1)*WIDTH+i] ;
0
 

Author Comment

by:amitzo
ID: 16327061
even warmer, still one problem, the image is flipped left to right
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16327168
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
 

Author Comment

by:amitzo
ID: 16327203
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
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 1000 total points
ID: 16327209
I bloody well hope so.

Will have a flick through my books ...
0
 

Author Comment

by:amitzo
ID: 16433516
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question