Okay . . . I’ve done as much research as I can, looked at a bunch of tutorials and am baffled why I can’t texture a simple polygon. I created a 64x64 texture in .png format and took some demo code I’d already used and adapted it to map a texture onto my polygon. There’s two variations where I get 2 different results.
Variation 1 - I insert two lines of code when I’m loading the texture
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MIN_FILTER,GL.LINEAR);
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MAG_FILTER,GL.LINEAR);
In this variation, I can tell from the logs that the program runs just fine, but I get a black screen. I would assume it’s displaying a beautiful black polygon at me.
Variation 2 - In this variation, I remove those two lines and VOILA, I get a nifty white polygon bouncing around my screen
I’m positive my texture is loading from the file okay, because in the logs I see the right sizes for the height and width. I’ve tried 64x64, 128x128 and 256x256, and when I change the image, it shows up in the logs.
I’m hoping wiser heads than mine might make something of this.
If anybody wants to see it, here’s the relevant code below. If there’s any important bits missing that may be important, let me know and I’ll post that too.
(sorry my code’s displaying all nasty . . . can’t get the indentations to work right.)
private void glInit()
{
if(log.isDebugEnabled()) log.debug("glInit()");
gl.determineAvailableExtensions();
gl.enable(GL.TEXTURE_2D);
gl.shadeModel(GL.SMOOTH);
gl.matrixMode(GL.PROJECTION);
gl.loadIdentity();
glu.ortho2D(0,mode.width,0,mode.height);
gl.matrixMode(GL.MODELVIEW);
gl.loadIdentity();
gl.viewport(0,0,mode.width,mode.height);
gl.clearColor(0.0f,0.0f,0.0f,0.0f);
if(GL.WGL_EXT_swap_control)
{
if(log.isDebugEnabled()) log.debug("swapcontrol");
GL.wglSwapIntervalEXT(1);
}
}
private Texture loadTexture(String filename)
{
BufferedImage newImage=null;
File newFile=new File(filename);
try
{
newImage=ImageIO.read(newFile);
}
catch(IOException e)
{
log.error("Could not load image in file: "+filename);
}
if(newImage==null && log.isDebugEnabled())log.debug("Texture didn't load correctly");
Texture newTex=new Texture(newImage);
return newTex;
}
protected void makeGLTextures(String[] newImages)
{
IntBuffer tempBuf=ByteBuffer.allocateDirect(4*(newImages.length)).asIntBuffer();
Texture[] texList=new Texture[newImages.length];
for(int loop=0; loop<newImages.length; loop++)
{
texList[loop]=loadTexture(newImages[loop]);
}
gl.genTextures(texList.length,Sys.getDirectBufferAddress(tempBuf));
for(int loop=0; loop<texList.length; loop++)
{
if(log.isDebugEnabled()) log.debug("binding texture to name="+tempBuf.get(loop));
if(log.isDebugEnabled()) log.debug("w="+texList[loop].getWidth());
if(log.isDebugEnabled()) log.debug("h="+texList[loop].getHeight());
if(log.isDebugEnabled()) log.debug("p="+texList[loop].getPtr());
gl.bindTexture(GL.TEXTURE_2D,tempBuf.get(loop));
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MIN_FILTER,GL.LINEAR);
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MAG_FILTER,GL.LINEAR);
gl.texImage2D(GL.TEXTURE_2D,0,GL.RGB,
texList[loop].getWidth(),texList[loop].getHeight(),0,GL.RGB,
GL.UNSIGNED_BYTE,texList[loop].getPtr());
if(log.isDebugEnabled()) log.debug("texture "+tempBuf.get(loop)+" successfully bound.");
}
addToTexIndex(tempBuf); //my own method defined elsewhere. It works okay.
}
Finally, my display code:
halfx=size.x/2;
halfy=size.y/2;
gl.pushMatrix();
gl.translatef(position.x,position.y,0);
gl.rotatef(angle,0.0f,0.0f,1.0f);
if(textured)
{
gl.bindTexture(GL.TEXTURE_2D,texName);
if(log.isDebugEnabled()) log.debug("Binding Texture "+texName);
}
gl.begin(GL.QUADS);
if(textured)
{
if(log.isDebugEnabled()) log.debug("textured");
gl.texCoord2f(0.0f,0.0f);
gl.vertex2f(-halfx,-halfy);
gl.texCoord2f(1.0f,0.0f);
gl.vertex2f(halfx,-halfy);
gl.texCoord2f(1.0f,1.0f);
gl.vertex2f(halfx,halfy);
gl.texCoord2f(0.0f,1.0f);
gl.vertex2f(-halfx,halfy);
} else {
if(log.isDebugEnabled()) log.debug("untextured");
gl.vertex2f(-halfx,-halfy);
gl.vertex2f(halfx,-halfy);
gl.vertex2f(halfx,halfy);
gl.vertex2f(-halfx,halfy);
}
gl.end();
gl.popMatrix();