Question

I am trying to render a simple jpg image using JOGL. However, I'm not sure how to wrap the pixel array data so that glDrawPixels will accept it. Here's the relevant pieces I have so far:

//In Main() Method
private static float[] pixels = {
    0.f, 1.f, 0.f,
    1.f, 0.f, 1.f,
    0.f, 1.f, 0.f
};

//In Render() Method
gl.glRasterPos3f(12.0f, 12.0f, 5.0f);
gl.glDrawPixels(3, 3, GL.GL_RGBA, GL.GL_FLOAT, pixels);

Eclipse tells me that glDrawPixels takes in a buffer and not a float array, so I guess I need to throw the pixels into a buffer first and specify the buffer, but I'm not sure how to do this, or which buffer to use. Do I need to use glPixelStore*()? Any help would be much appreciated. Thank you!

--Edit--

I tried to throw the information into a FloatBuffer and the method accepted it, but nothing shows up on the screen. Am I wrapping the array into a buffer correctly, or is the issue with my calls to the gl functions? Here's the updated code:

private static float[] pixels = {
    0.f, 1.f, 0.f,
    1.f, 0.f, 1.f,
    0.f, 1.f, 0.f
};

static FloatBuffer buf; 

//In Main() Method
buf.wrap(pixels);

//In Render() method
gl.glRasterPos3f(0.0f, 0.0f, 0.0f);
gl.glPixelZoom(50.0f, 50.0f);
gl.glDrawPixels(3, 3, GL.GL_RGBA, GL.GL_FLOAT, buf);

When I run the program, it just shows a black screen.

Was it helpful?

Solution

The buffer Eclipse is talking about is nothing OpenGL specific (per se). The OpenGL functions expect a pointer to the memory locations where to find the data. Java doesn't have pointers, so you need a Java buffer object (not to confuse with a OpenGL buffer object) that provides some way to extract a pointer that can be passed to OpenGL. For example a Java FloatBuffer. You can pass an instance of that to the OpenGL gl…Pointer functions as data parameter then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top