Backgrounds using OpenGL and LWJGL
Not sure if its any different using LWJGL but how would i go about putting a background to my game, i have a fixed perspective and need a background, are there also any good places to find space backgrounds?
this adds a texture to a polygon doesnt it?
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureArray[0]); // Select Our Texture
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
GL11.glEnd();
where texture[0] is my background?
so if i add the above inside my render method, why do i get the following:
java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0
at java.awt.image.SampleModel.<init>(SampleModel.java:108)
at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:
128)
at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSam
pleModel.java:69)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:638)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:265)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:367)
at OpenGLWindow.loadTexture(OpenGLWindow.java:554)
at OpenGLWindow.loadTextures(OpenGLWindow.java:308)
at OpenGLWindow.init(OpenGLWindow.java:296)
at OpenGLWindow.run(OpenGLWindow.java:92)
at OpenGLWindow.<init>(OpenGLWindow.java:45)
at Main.main(Main.java:7)
Press any key to continue...
does anyone know?
thanks
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureArray[0]); // Select Our Texture
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
GL11.glEnd();
where texture[0] is my background?
so if i add the above inside my render method, why do i get the following:
java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0
at java.awt.image.SampleModel.<init>(SampleModel.java:108)
at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:
128)
at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSam
pleModel.java:69)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:638)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:265)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:367)
at OpenGLWindow.loadTexture(OpenGLWindow.java:554)
at OpenGLWindow.loadTextures(OpenGLWindow.java:308)
at OpenGLWindow.init(OpenGLWindow.java:296)
at OpenGLWindow.run(OpenGLWindow.java:92)
at OpenGLWindow.<init>(OpenGLWindow.java:45)
at Main.main(Main.java:7)
Press any key to continue...
does anyone know?
thanks
it is defintely soemthing to do with the image becuase i can get another one to work
ok, doesnt look brilliant but i got it to work by resizing and changing quality using photoshop
how do i modify neHe's code so that i can insert textures (background) that are larger than 256 x 256 because the quality of the background is awful! :(
this is the load texture method that i have:
/**
* Texture loading directly from LWJGL examples
*/
private final int loadTexture(String path) {
Image image = (new javax.swing.ImageIcon(path)).getImage();
// Exctract The Image
BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
// Flip Image
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
tex = op.filter(tex, null);
// Put Image In Memory
ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
scratch.clear();
scratch.put(data);
scratch.rewind();
// Create A IntBuffer For Image Address In Memory
IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf); // Create Texture In OpenGL
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
// Typical Texture Generation Using Data From The Image
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
// Generate The Texture
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, tex.getWidth(), tex.getHeight(), GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
return buf.get(0); // Return Image Address In Memory
}
}
now this is the error i get if i try and load my high quality image which is
600 x 405 jpeg image:
org.lwjgl.opengl.OpenGLException: Invalid value (1281)
at org.lwjgl.opengl.Util.checkGLError(Util.java:56)
at org.lwjgl.opengl.Display.update(Display.java:457)
at OpenGLWindow.run(OpenGLWindow.java:107)
at OpenGLWindow.<init>(OpenGLWindow.java:55)
at Main.main(Main.java:7)
Press any key to continue...
my graphics card is an ati 9600 xt so i dont think graphics card is the problem
thanks :)
/**
* Texture loading directly from LWJGL examples
*/
private final int loadTexture(String path) {
Image image = (new javax.swing.ImageIcon(path)).getImage();
// Exctract The Image
BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
// Flip Image
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
tex = op.filter(tex, null);
// Put Image In Memory
ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
scratch.clear();
scratch.put(data);
scratch.rewind();
// Create A IntBuffer For Image Address In Memory
IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
GL11.glGenTextures(buf); // Create Texture In OpenGL
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
// Typical Texture Generation Using Data From The Image
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
// Linear Filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
// Generate The Texture
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, tex.getWidth(), tex.getHeight(), GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);
return buf.get(0); // Return Image Address In Memory
}
}
now this is the error i get if i try and load my high quality image which is
600 x 405 jpeg image:
org.lwjgl.opengl.OpenGLException: Invalid value (1281)
at org.lwjgl.opengl.Util.checkGLError(Util.java:56)
at org.lwjgl.opengl.Display.update(Display.java:457)
at OpenGLWindow.run(OpenGLWindow.java:107)
at OpenGLWindow.<init>(OpenGLWindow.java:55)
at Main.main(Main.java:7)
Press any key to continue...
my graphics card is an ati 9600 xt so i dont think graphics card is the problem
thanks :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement