Hello,
I am attempting to follow the NeHe tutorials, but am converting it to LWJGL as its what I am most familiar with. However I have encountered a problem in lesson 7, which I cannot get past no matter what.
Below is the relevant code in which I think the solution will be in (I’ll ignore putting everything else here, seeing as its so much…):
[spoiler]
private static float[] LightAmbient = {0.5f, 0.5f, 0.5f, 1.0f}; // Red Green Blue Alpha
private static float[] LightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f };
private static float[] LightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };
int filter = 0;
private Texture texture[];
private Texture tex;
private static boolean initGL() {
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, 800/600,0.1f,100f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0f);
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(LightAmbient).flip());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(LightDiffuse).flip());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, (FloatBuffer)temp.asFloatBuffer().put(LightPosition).flip());
GL11.glEnable(GL11.GL_LIGHT1);
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
return true;
}
public void init() {
try {
tex = TextureLoader.getTexture("png", new FileInputStream("C:\\Documents and Settings\\home user\\My Documents\\My Pictures\\Crate.png"));
texture[0] = TextureLoader.getTexture("png", new FileInputStream("C:\\Documents and Settings\\home user\\My Documents\\My Pictures\\Crate.png"));
texture[1] = TextureLoader.getTexture("png", new FileInputStream("C:\\Documents and Settings\\home user\\My Documents\\My Pictures\\Crate.png"));
texture[2] = TextureLoader.getTexture("png", new FileInputStream("C:\\Documents and Settings\\home user\\My Documents\\My Pictures\\Crate.png"));
} catch (IOException e) {
System.err.println("No picy!");
e.printStackTrace();
}
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[0].getTextureID());
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[1].getTextureID());
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[2].getTextureID());
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);
//GL11.glTexImage2D(0,3,texture.getImageWidth(),texture.getImageHeight(),0,GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,texture.GL11.getTexture());
}
private void render(){
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glTranslatef(0.0f, 0.0f, z);
GL11.glRotatef(xrot,1.0f,0.0f,0.0f);
GL11.glRotatef(yrot,0.0f,1.0f,0.0f);
if (filter == 0) {
}
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[filter].getTextureID()); // <-- Here, this line is where the error is appearing
GL11.glBegin(GL11.GL_QUADS);
//front
GL11.glNormal3f(0.0f, 0.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
//The rest of the code is fine, so I left it out :)
[/spoiler]
The error that eclipse gives me is “Tue Jan 10 22:33:28 GMT 2012 INFO:Use Java PNG Loader = true
Exception in thread “main” java.lang.NullPointerException” and it is at the
texture[0] = TextureLoader.getTexture(“png”, new FileInputStream(“C:\Documents and Settings\home user\My Documents\My Pictures\Crate.png”));
line in init(). I have tried loads of different methods to doing this, and all have came up with different errors, and this is the last error I have which I cant fix.
The reason I have added the ‘tex = TextureLoader…’ above the 'texture[0] line is simply to check the last section of the error line is correct, and it is, as the tex line isn’t throwing up any errors.
Also, is anyone able to give me a hand in avoiding using the floatBuffers in InitGL so instead of:
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(LightAmbient).flip());
it is
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, LightAmbient);
The floatbuffer I found after a few google searches, but Im struggling to get my head around float buffer, so was wondering if there is anyway around this?
Thanks,
Evan.
EDIT: I change the code a little to a revised one since I posted this, so the question also changed, just to clear up any confusion.