I can't find a way to use correctly JOGL with Textures in my 2D game, here's my code:
Init method
public void init(GLAutoDrawable drawable) {
[...]
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_BLEND);
box.initTexture(gl, "image/tex1.png");
}
Display method
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
box.render(gl);
gl.glFlush();
}
Myclass.init
public boolean initTexture(GL gl,String textureName){
boolean ris=true;
try
{
Texture = TextureIO.newTexture(new File(textureName),false);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
}
catch (IOException ex)
{
System.err.println(ex);
ris=false;
}
return ris;
}
Myclass.render
public void render(GL gl){
gl.glTranslatef(X,Y,0.0f);
gl.glRotatef(Angle, 0.0f, 0.0f, 1.0f);
Texture.enable();
Texture.bind();
gl.glBegin(GL.GL_QUADS);
gl.glColor3i(1,1,1);
gl.glTexCoord2f(0, 0);
gl.glVertex2f(-Width/2,-Height/2);
gl.glTexCoord2i(0, 1);
gl.glVertex2f(-Width/2,Height/2);
gl.glTexCoord2i(1, 1);
gl.glVertex2f(Width/2,Height/2);
gl.glTexCoord2i(1, 0);
gl.glVertex2f(Width/2,-Height/2);
gl.glEnd();
gl.glLoadIdentity();
}
This code shows an empty screen and it doesn't give any sort of errors. I've tested this code without texture and it works just fine.
PS: the texture i'm using is in myprojectfolder\image\ and it is a 128 x 128 pixel png image