OK, I’m trying to set up a TextureLibrary class that could abstract some of my interactions with OpenGL calls. For some reason, I can’t get any textures to be displayed at all. I’m hoping that I’m doing something quite obviously wrong – and that one of you might be able to help me identify the problem. I’ve spent several hours now, trying to root out the source of the problem – but with little joy. Any suggestions will be greatly appreciated.
The segments of the code below are part of a little program that I’m writting that should just draw a texture mapped quad to the screen. It draws the quad – but in black, rather than with the NeHe.png texture that I want.
Once again, thanks for any help you might be able to provide – or for even scanning the code.
Cheers,
Matt
The Display Method
public void display(GLDrawable drawable) {
frameCounter_++;
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
GLUT glut = new GLUT();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
//Draw Frame Rate:
gl.glPushMatrix();
gl.glColor4d(0.0, 0.0, 0.0, 1.0);
gl.glTranslated(1000.0, 700.0, 0.0);
gl.glScaled(0.5, 0.5, 1.0);
glut.glutStrokeString(gl, GLUT.STROKE_ROMAN, new Integer(fps_).toString());
gl.glPopMatrix();
gl.glEnable(GL.GL_TEXTURE_2D);
if(gl.glIsTexture(textureLibrary_.getTexture(0))) {
gl.glBindTexture(GL.GL_TEXTURE_2D, textureLibrary_.getTexture(0));
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);
System.out.println("Using texture#" + textureLibrary_.getTexture(0));
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex2f(10.0f, 10.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex2f(100.0f, 10.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex2f(100.0f, 100.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex2f(10.0f, 100.0f);
gl.glEnd();
}
int errorValue = gl.glGetError();
if(errorValue != GL.GL_NO_ERROR) {
System.out.println("Error:"+gl.glGetError());
exit();
}
}
The Init Method:
public void init(GLDrawable drawable) {
//init openGL
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glEnable(GL.GL_BLEND);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL.GL_SMOOTH);
//init erase color
gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f); //init erase color
textureLibrary_ = new TextureLibrary(gl);
textureLibrary_.addTexture(gl, glu, "images/NeHe.png");
}
The TextureLibrary Class
public class TextureLibrary {
private static final int maxNumberOfTextures_ = 10;
private static int numLoadedTextures_ = 0;
private final static int[] textureAddressLookupTable_ = new int[maxNumberOfTextures_];
public TextureLibrary(GL gl) {
//init the textureAddressLookupTable
gl.glGenTextures(maxNumberOfTextures_, textureAddressLookupTable_);
}
public void addTexture(GL gl, GLU glu, String pngFileName) {
int textureIDNum = genTextureIDNum();
BufferedImage img = ImageUtils.readPNGImage(pngFileName);
System.out.println(
"Reading Texture: '"
+ pngFileName
+ "', width="
+ img.getWidth()
+ ", height="
+ img.getHeight());
//the lines below verify that the image has been read into
//img properly by drawing the image to the screen (the colors are a bit messed up
//But I imagine that's appropriate...because of the different ways openGL and java imgs are stored in mem
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glDrawPixels (img.getWidth(), img.getHeight(), GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, img.getRGB(0,
0,
img.getWidth(),
img.getHeight(),
null,
0,
img.getWidth()));
gl.glPopMatrix();
System.out.println("Creating texture# " + textureIDNum);
gl.glBindTexture(GL.GL_TEXTURE_2D, textureIDNum);
ImageUtils.makeRGBTexture(gl, glu, img, GL.GL_TEXTURE_2D, false);
//texture filtering. Don't understand...
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);
numLoadedTextures_++;
}
private int genTextureIDNum() {
return textureAddressLookupTable_[numLoadedTextures_];
}
/**
* @param i
* @return
*/
public int getTexture(int i) {
return textureAddressLookupTable_[i];
}
}
StdOut
init
Reading Texture: 'images/NeHe.png', width=256, height=256
Creating texture# 1
reshape
1 is a texture
Using texture#1
Using texture#1
Using texture#1
Using texture#1
...
Using texture#1
Using texture#1
Using texture#1
1 is a texture
Using texture#1
Using texture#1
...
Cheers Everyone!