I’ve been staring at this for a day and half now and I can’t see the problem. I’m using Kevins texture loader code from the wiki. Output is a window with a shaded spinning triangle - non textured. Here’s a code dump. Can anyone see where the error is?
public class FrameTest extends Frame implements GLEventListener {
private int width;
private int height;
private GLCanvas canvas;
protected GL gl;
protected GLU glu;
private boolean stopRenderer = false;
private Animator animator;
private TextureLoader textureLoader;
float RotateX = 0.0f;
float RotationSpeed = 0.5f;
Texture texture;
public FrameTest(String title, int width, int height) {
super(title);
this.setUndecorated(false);
initFrame(width, height);
}
private void initFrame(int width, int height) {
this.width = width;
this.height = height;
GLCapabilities caps = new GLCapabilities();
canvas = GLDrawableFactory.getFactory().createGLCanvas(caps);
canvas.addGLEventListener(this);
canvas.setNoAutoRedrawMode(true);
canvas.setFocusable(true);
canvas.requestFocus();
animator = new Animator(canvas);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
System.exit(0);
}
public void windowActivated(WindowEvent e) {
canvas.requestFocus();
}
});
setLayout(new BorderLayout());
add(canvas);
setSize(width, height);
init(canvas);
show();
getTextureLoader();
try {
texture=textureLoader.getTexture("texture","hobgoblin.png");
}
catch (IOException ioe)
{
System.out.println("Failed to load texture.");
}
animator.start();
}
public void dispose() {
animator.stop();
stopRenderer = true;
}
public void display(GLDrawable drawable) {
if (stopRenderer) {
return;
}
if (!isVisible()) {
return;
}
draw();
}
public void draw() {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(0, 1.5f, 90,0, 0.5f, 0,0, 1, 0);
gl.glRotatef(RotateX, 0f, 1.0f, 0f);
RotateX += RotationSpeed;
texture.bind(gl);
gl.glBegin(GL.GL_TRIANGLES);
gl.glTexCoord2i(0,0);
gl.glVertex3f(0,0,0);
gl.glTexCoord2i(0,100);
gl.glVertex3f(0,30,0);
gl.glTexCoord2i(100,100);
gl.glVertex3f(30,0,0);
gl.glEnd();
}
public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
public void init(GLDrawable drawable) {
gl = canvas.getGL();
glu =canvas.getGLU();
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_COLOR_MATERIAL);
//gl.glEnable(GL.GL_CULL_FACE);
//gl.glCullFace(GL.GL_FRONT);
}
public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
this.width = width;
this.height = height;
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f,(float)width/(float)height, .5f ,150.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public TextureLoader getTextureLoader() {
if (textureLoader == null) {
textureLoader = new TextureLoader(canvas);
}
return textureLoader;
}
public static void main (String [] args) {
FrameTest test=new FrameTest("Test", 500,500);
}
}
console output:
Loading texture hobgoblin.png to ID: 0
The id is zero every time I run it. Thanks :-/