Before I come with my next question… thanks for all your help Vorax, i really appreciate it!! I’m not sure why the NEHE tutorial in JOGL for that had the texture variable there rather than that particular GLenum, but that advice worked.
I’ve got it all set up the way I think its supposed to be according to your instructions, yet all i get drawn is a black box. Here’s the code for the constructor (in which I create the buffered image)
private String text;
private Point2D.Double pt;
private Plot2DPanel parent = null;
private Color color;
private Font font;
private BufferedImage data;
private int texture;
private boolean textureMade;
public TextObject(Point2D.Double pt, String text,
Plot2DPanel plotPanel, Color color, Font font){
super();
this.text=text;
this.pt = pt;
this.parent = plotPanel;
this.color = color;
this.font = font;
textureMade = false;
BufferedImage temp = new BufferedImage(1,1, BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = temp.getGraphics();
g.setFont(font);
int str_width = g.getFontMetrics().stringWidth(text)+2;
int str_height = g.getFontMetrics().getHeight()+2;
// create an image with dimensions that are powers of two
// which is large enough to hold the desired text
int width, height;
for (width = 2; width<str_width; width *= 2)
;
for (height = 2; height<str_height; height *= 2)
;
// draw the string to a BufferedImage
data = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2d = (Graphics2D) data.getGraphics();
g2d.setColor(Color.white);
g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0,0,width,height);
g2d.setFont(font);
g2d.setColor(color);
g2d.setComposite(AlphaComposite.SrcOver);
g2d.drawString(text,0,str_height);
}
Here is the glDraw method (which gets called from the display() method of another class. this bit of the functionality works as i have it working in other classes)
public void glDraw(GL gl) {
System.out.println("TextObject");
// create the texture on the first time through only
if (!textureMade) {
System.out.println("Create Texture");
// set up the texture we want to use
gl.glEnable(GL.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
makeRGBATexture(gl);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
textureMade = true;
}
double xmin = parent.getXMin();
double xmax = parent.getXMax();
double ymin = parent.getYMin();
double ymax = parent.getYMax();
double x = pt.getX();
double y = pt.getY();
// figure out how big the image is in our current GL coordinate system
double quadWidth = (double)(data.getWidth()*(xmax-xmin)/parent.getWidth());
double quadHeight = (double)(data.getHeight()*(ymax-ymin)/parent.getHeight());
// using this information, we can position the quad and map the texture
// containing the string to its 4 corners appropriately
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2d(0.0, 0.0); gl.glVertex2d(x-quadWidth/2,y-quadHeight/2);
gl.glTexCoord2d(1.0, 0.0); gl.glVertex2d(x+quadWidth/2,y-quadHeight/2);
gl.glTexCoord2d(1.0, 1.0); gl.glVertex2d(x+quadWidth/2,y+quadHeight/2);
gl.glTexCoord2d(0.0, 1.0); gl.glVertex2d(x-quadWidth/2,y+quadHeight/2);
gl.glEnd();
}
and, finally, this is the code that creates the texture (most of which was taken from the JOGL version of NEHE lesson 13)
private int genTexture(GL gl) {
final int[] tmp = new int[1];
gl.glGenTextures(1, tmp, 0);
return tmp[0];
}
private void makeRGBATexture(GL gl) {
ByteBuffer dest = null;
GLU glu = new GLU();
// recall member variables:
// - data: BufferedImage containing the image to draw
// - texture: integer identifying the texture to OpenGL
// grab the byte data from the BufferedImage and put it into the
// 'dest' ByteBuffer
byte [] info = ((DataBufferByte)data.getRaster().getDataBuffer()).getData();
for (int i=0; i<info.length; i+=4) {
// switch the ABGR data around to RBGA
byte a = info[i];
byte b = info[i+1];
byte g = info[i+2];
byte r = info[i+3];
info[i] = r;
info[i+1] = g;
info[i+2] = b;
info[i+3] = a;
}
dest = ByteBuffer.allocateDirect(info.length);
dest.order(ByteOrder.nativeOrder());
dest.put(info, 0, info.length);
gl.glTexImage2D(
GL.GL_TEXTURE_2D,
0,
GL.GL_RGBA,
data.getWidth(),
data.getHeight(),
0,
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
dest
);
}
I’m not sure why its giving me that black box. I’m sure its got something to do with the alpha value setting and then my manipulations of the bytes, since my knowledge about that is lacking. but then again i have never used textures before in anything, so there could always be a problem with the texture binding as well. thanks!