Reputation: 1266
I'm trying to give the user of my application the ability to load an image of their choice to make the background. Loading the image via Java is no problem, but I can't get the image into a texture....I just end up with a big grey box on my GLCanvas. This is the code I have so far:
//if there's an image to overlay, render it
if (renderImage) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
if (texture == null && img != null) {
texture = TextureIO.newTexture(img, true);
texture.enable();
texture.bind();
}
gl.glBegin(GL.GL_POLYGON);
gl.glNormal3f(0,0,1);
gl.glTexCoord2d(-texture.getWidth(), -texture.getHeight());
gl.glVertex2d(-25, -25);
gl.glTexCoord2d(-texture.getWidth(), texture.getHeight());
gl.glVertex2d(canvas.getWidth(),0);
gl.glTexCoord2d(texture.getWidth(), texture.getHeight());
gl.glVertex2d(canvas.getWidth(), canvas.getHeight());
gl.glTexCoord2d(texture.getWidth(), -texture.getHeight());
gl.glVertex2d(0, canvas.getHeight());
gl.glEnd();
gl.glFlush();
}
//otherwise, render "grass"
else {
gl.glClearColor(0.0f, 0.65f, 0.0f, 0.0f);
//Clear buffer and set background color to green (the "grass" on the sides of the intersection)
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
Upvotes: 2
Views: 8238
Reputation: 21
I put
if (texture == null && img != null) {
texture = TextureIO.newTexture(img, true);
texture.enable();
texture.bind();
}
in a try, catch statement.
Upvotes: 1
Reputation: 52167
Try this:
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0,0,1);
gl.glTexCoord2d(0.0, 0.0);
gl.glVertex2d(0.0, 0.0);
gl.glTexCoord2d(1.0, 0.0);
gl.glVertex2d(canvas.getWidth(), 0.0);
gl.glTexCoord2d(1.0, 1.0);
gl.glVertex2d(canvas.getWidth(), canvas.getHeight());
gl.glTexCoord2d(0.0, 1.0);
gl.glVertex2d(0.0, canvas.getHeight());
gl.glEnd();
Non-repeating texture coordinates are in the 0.0 to 1.0 range by default.
Upvotes: 4