LWJGL Difficulties (Poss Lighting?)

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.

You’ll have to use the FloatBuffer for GL11.glLight, there’s simply no other API. But wherever you see repeated code, it’s a candidate for breaking it out into a convenience function:


private FloatBuffer loadTemp(float[] arr) {
   temp.rewind();
   return (FloatBuffer)temp.asFloatBuffer().put(arr).flip();
}
... 
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, loadTemp(LightAmbient));


Hi

Just look at Java API documentation. NIO buffers are simple to use. Rather use org.lwjgl.BufferUtils to create your float buffers.

As for your NullPointerException:


      GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[filter].getTextureID()); // <-- Here, this line is where the error is appearing

There is only one possible cause for it: texture[filter] is null. Since every entry in texture[filter] is the same texture, it’s clear you’re getting a null result from your texture loader. You should put Crate.bmp somewhere in your project’s resources, and use getResourceAsStream to get at it rather than that torturously long path.

Turns out I was really stupid and forgot to add control.init() to the main…

But now its got a new problem: “Tue Jan 10 22:33:28 GMT 2012 INFO:Use Java PNG Loader = true
Exception in thread “main” java.lang.NullPointerException”

The error is comming from

texture[0] = TextureLoader.getTexture("png", new FileInputStream("C:\\Documents and Settings\\home user\\My Documents\\My Pictures\\Crate.png"));

And I’m guessing it will then have the same problem with texture[1] and texture[2] aswell. I did change it from bmp to png, but it was the same problem before the change.

I did however define the variable ‘tex’ as a ‘Texture’, and changed it so error section of the code is:

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"));

The first line isn’t comming up with any errors, only the “texture[0]” bit, despite the only thing being different is the variable.

I could come up with a easy fix by turning texture[] into 3 variables, but does anyone know how I can fix it while keeping the array?

Thanks for the help guys :smiley:

Evan

If you declare an array with an empty size and don’t assign it to an array value, it’s null.

Instead of this:


private Texture texture[];

Do something like this:


public final static int NUM_TEXTURES = 3;
private Texture[] texture = new Texture[NUM_TEXTURES];

(Notice I put the [] after the type and not the identifier, which is partly just style nitpicking, but it’s also the more common idiom in java)

Thank you so much guys :slight_smile:

Its actually runnable… at last lol :stuck_out_tongue:

Annoyingly it just shows a white square, but I’ll get on it tomorrow when I’ve got a fresh mind, since I reckon I can fix that.

Thanks, I appreciate it :smiley:

Evan.

You need to enable textures. GL11.glEnable(GL11.GL_TEXTURE_2D);