Sorry for double post but I have some developments. I was going to try get buffered image and graphics2D involved until I found this tutorial which seems to be the thing I was looking for. It doesn’t use glDrawPixels instead it used glBindTexture.
http://www.java-tips.org/other-api-tips/jogl/demonstration-of-using-glbindtexture-by-creating-and-managing-two-tex.html
Until I manage to get a book on JOGL or OpenGL I am still in the learning stages, but at the moment I am wondering that instead of using bytes for the RGBA values I could use Integers instead. I am aware there might be some efficiency problems there but at the moment I find it easier working with simple ints.
I tried converting that example to use ints but so far it hasn’t worked and I am currently left with two white textures.
Here is what I have tried to do.
import java.awt.event.*;
import javax.swing.JFrame;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import java.nio.IntBuffer;
/**
* This program demonstrates using glBindTexture() by creating and managing two
* textures.
*
* @author Kiet Le (Java port)
*/
public class texbind extends JFrame implements GLEventListener, KeyListener {
private GLCapabilities caps;
private GLCanvas canvas;
private GLU glu;
private GLUT glut;
//
private static final int rgba = 4;
private static final int checkImageWidth = 64;
private static final int checkImageHeight = 64;
// private byte checkImage[][][];
// private byte otherImage[][][];
private IntBuffer checkImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba);
private IntBuffer otherImageBuf = BufferUtil.newIntBuffer(checkImageWidth * checkImageHeight * rgba);
private int[] texName = new int[2];
public texbind() {
super("texbind");
caps = new GLCapabilities();
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
getContentPane().add(canvas);
}
public void run() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 250);
setLocationRelativeTo(null);
setVisible(true);
canvas.requestFocusInWindow();
}
public static void main(String[] args) {
new texbind().run();
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_FLAT);
gl.glEnable(GL.GL_DEPTH_TEST);
makeCheckImages();
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glGenTextures(2, texName, 0);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, checkImageBuf);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth, checkImageHeight, 0, GL.GL_RGBA, GL.GL_INT, otherImageBuf);
gl.glEnable(GL.GL_TEXTURE_2D);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
//First texture
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2d(0.0, 0.0);
gl.glVertex3d(-2.0, -1.0, 0.0);
gl.glTexCoord2d(0.0, 1.0);
gl.glVertex3d(-2.0, 1.0, 0.0);
gl.glTexCoord2d(1.0, 1.0);
gl.glVertex3d(0.0, 1.0, 0.0);
gl.glTexCoord2d(1.0, 0.0);
gl.glVertex3d(0.0, -1.0, 0.0);
gl.glEnd();
//Second texture
gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2d(0.0, 0.0);
gl.glVertex3d(1.0, -1.0, 0.0);
gl.glTexCoord2d(0.0, 1.0);
gl.glVertex3d(1.0, 1.0, 0.0);
gl.glTexCoord2d(1.0, 1.0);
gl.glVertex3d(2.41421, 1.0, -1.41421);
gl.glTexCoord2d(1.0, 0.0);
gl.glVertex3d(2.41421, -1.0, -1.41421);
gl.glEnd();
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslated(0.0, 0.0, -3.6);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
private void makeCheckImages() {
//byte c = 0x00;
for (int i = 0; i < checkImageWidth; i++) {
for (int j = 0; j < checkImageHeight; j++) {
checkImageBuf.put((int) (Math.random()*256)); // R
checkImageBuf.put((int) (Math.random()*256)); // G
checkImageBuf.put((int) (Math.random()*256)); // B
checkImageBuf.put((int) (Math.random()*256)); // A
otherImageBuf.put((int) (Math.random()*256)); // R
otherImageBuf.put((int) (Math.random()*256)); // G
otherImageBuf.put((int) (Math.random()*256)); // B
otherImageBuf.put((int) (Math.random()*256)); // A
}
}
checkImageBuf.rewind();
otherImageBuf.rewind();
}
public void keyTyped(KeyEvent key) {
}
public void keyPressed(KeyEvent key) {
switch (key.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
System.exit(0);
default:
break;
}
}
public void keyReleased(KeyEvent key) {
}
}