Hello everyone, I've found something pretty helpful:
http://www.paulsprojects.net/opengl/q3bsp/q3bsp.htmlSo I think I know how to render the polygons and I've been able to load in a jpeg texture,
my texture loading code is almost boiler plate but it seems that when I load the jpeg texture
it puts opengl in some bad state where it crashes at the render-loop.
This is my texture loading code:
package game.util;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.HashMap;
import org.lwjgl.LWJGLException;
import org.lwjgl.devil.IL;
import org.lwjgl.opengl.GL11;
public class TextureManager {
private static HashMap textures = new HashMap();
/**
* Texture loading using DevIL Example created by Mark Bernard
*/
public static Texture loadTexture(String path) {
if (textures.containsKey(path)) {
return (Texture) textures.get(path);
}
// Create a 4 byte ID
IntBuffer image = ByteBuffer.allocateDirect(4).order(
ByteOrder.nativeOrder()).asIntBuffer();
// Generate a name for a image
IL.ilGenImages(image);
// Bind an image to the name
IL.ilBindImage(image.get(0));
String path2 = path.trim();
if (path2.endsWith(".tga")) {
IL.ilLoadFromStream(TextureManager.class.getClassLoader()
.getResourceAsStream(path), IL.IL_TGA);
} else if (path2.endsWith(".bmp")) {
IL.ilLoadFromStream(TextureManager.class.getClassLoader()
.getResourceAsStream(path), IL.IL_BMP);
} else if (path2.endsWith(".png")) {
IL.ilLoadFromStream(TextureManager.class.getClassLoader()
.getResourceAsStream(path), IL.IL_PNG);
} else if (path2.endsWith(".jpg")) {
IL.ilLoadFromStream(TextureManager.class.getClassLoader()
.getResourceAsStream(path), IL.IL_JPG);
} else {
throw new RuntimeException(
"Can't load that file format, must be (tga, bmp, png)");
}
// Convert to RGB with Alpha
IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
// Create a buffer to copy the image into
ByteBuffer scratch = ByteBuffer.allocateDirect(IL
.ilGetInteger(IL.IL_IMAGE_WIDTH)
* IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4);
// Copy image into buffer
IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL
.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGBA, IL.IL_BYTE,
scratch);
// Create memory for a name in opengl
IntBuffer buf = ByteBuffer.allocateDirect(4).order(
ByteOrder.nativeOrder()).asIntBuffer();
// Generate a name
GL11.glGenTextures(buf);
// Bind the name
GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
// Set parameters, linear filtering
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
GL11.GL_LINEAR);
// Pass the image data to opengl
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, IL
.ilGetInteger(IL.IL_IMAGE_WIDTH), IL
.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGBA,
GL11.GL_UNSIGNED_BYTE, scratch);
Texture t = new Texture(buf.get(0), GL11.GL_TEXTURE_2D, IL
.ilGetInteger(IL.IL_IMAGE_WIDTH), IL
.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, 1);
textures.put(path, t);
return t;
}
}
Any ideas how to fix this? I noticed that for every texture structure in the map file there's also a integer called flags and another called contents,
perhaps I need to specify these to devil somehow?
If anyone has any ideas, that would be great!
