• Advertisement
Sign in to follow this  

Backgrounds using OpenGL and LWJGL

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Draw a polygon and use your background image as texture.

I would use google to find a suitable space background image.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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! :(

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As long as your graphics card supports textures larger than 256x256 (probably every card younger than 3-4 years) you should be able to just load the texture as any other texture.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The image size must be power of 2. Only a few graphics cards can deal with non power of 2 textures. The texture containing your 600x405 image must therefore be of size atleast 1024x512. This doesn't neccesarly mean a lot of waisted space as you can put many images in that texture.

I would also recommend DevIL instead of javas builtin image capabilities. DevIL has much more functionallity and is (in my opinion) easier to use.

Share this post


Link to post
Share on other sites
what is devil mate and where can i find it, do i then just simply type in which texture to use using that?
thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
DevIL is an open source image library. You can find more information at http://openil.sourceforge.net/

Unfortunately you can't just supply a filename and expect a texture id in return, but it's almost as easy. On NeHe's site, you should be able to find his tutorials ported to Java and DevIL.

LWJGLs API doc does also contain some information about DevIL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement