Can somebody tell me where I can find a good tutorial on how to load textures in JOGL/Java?
it’s as easy as:
Texture tex;
try {
tex = TextureIO.newTexture(TextureIO.newTextureData(ImageIO.read(new File("path_to_texture.png")), false));
} catch (IOException ioe) {
//handle error
}
Thanks alot, but it doesn’t seem to work for png files. everything looks messed up. BMP and jpeg works good.
Can you post a small test case including the PNG you’re having trouble loading? The PNG images I’ve tried with the TextureIO classes work fine (they’re handled by the core Java ImageIO classes under the hood).
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import com.sun.opengl.util.*;
public class Lesson06 {
public static void main(String args[]){
JFrame frame = new JFrame("Lesson06");
GLCanvas canvas = new GLCanvas();
frame.add(canvas);
final Animator animator = new Animator(canvas);
canvas.addGLEventListener(new Lesson06Renderer());
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
animator.stop();
System.exit(0);
}
});
frame.setVisible(true);
frame.setBounds(0, 0, 640+frame.getInsets().left+frame.getInsets().right, 480+frame.getInsets().top+frame.getInsets().bottom);
frame.requestFocus();
animator.start();
}
}
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.texture.*;
import javax.imageio.*;
import java.io.*;
public class Lesson06Renderer implements GLEventListener{
public void init(GLAutoDrawable drawable){
final GL gl = drawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glEnable(GL.GL_TEXTURE_2D);
Texture tex;
try {
tex = TextureIO.newTexture(TextureIO.newTextureData(ImageIO.read(new File("data/NeHe.png" +
"")), false));
} catch (IOException ioe) {
//handle error
}
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){
final GL gl = drawable.getGL();
final GLU glu = new GLU();
if(height <= 0)height = 1;
float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, h, 0.2, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable){
final GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -6);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glEnd();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){}
}
Thanks for the test case. It looks like PNG images were going down an untested code path. I’ve filed and fixed Issue 276 on this topic. The fix will be in nightly builds dated 2/23 and later.
I switched over from jogl-1.1.0-rc3 to jogl-1_0_0, there are problems with BMP files also.
They seem to be upside-down with jogl-1.1.0-rc3.
This is intentional. We changed the vertical orientation of BufferedImage-based Textures in 1.1.0-rc2 in order to be able to efficiently support updating of a sub-rectangle of a texture. If you’re rendering with a Texture then you need to either pay attention to getMustFlipVertically() or use getSubImageTexCoords() to get the Texture to compute the texture coordinates you should send down to OpenGL.
Just to be sure
when I use
Texture tex;
try {
tex = TextureIO.newTexture(TextureIO.newTextureData(ImageIO.read(new File("path_to_texture.png")), false));
} catch (IOException ioe) {
//handle error
}
It is totally unnecessary to use glGenTextures(), glBindTexture() (instead use bind()), glTexImage2D()…
sorry for the stupid questions… :-[
Correct.