Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL Difficulties (Poss Lighting?)  (Read 742 times)
0 Members and 1 Guest are viewing this topic.
Offline evanrc

Junior Newbie





« Posted 2012-01-10 22:06:04 »

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...):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
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 :)


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:
1  
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(LightAmbient).flip());


it is
1  
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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-01-10 22:09:38 »

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:

1  
2  
3  
4  
5  
6  
private FloatBuffer loadTemp(float[] arr) {
   temp.rewind();
   return (FloatBuffer)temp.asFloatBuffer().put(arr).flip();
}
...
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, loadTemp(LightAmbient));
Offline gouessej
« Reply #2 - Posted 2012-01-10 22:10:12 »

Hi

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-01-10 22:16:50 »

As for your NullPointerException:

1  
      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.
Offline evanrc

Junior Newbie





« Reply #4 - Posted 2012-01-10 22:39:07 »

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
1  
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:


1  
2  
3  
4  
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 Cheesy

Evan
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-01-10 22:42:19 »

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

Instead of this:
1  
private Texture texture[];


Do something like this:
1  
2  
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)

Offline evanrc

Junior Newbie





« Reply #6 - Posted 2012-01-10 22:46:07 »

Thank you so much guys Smiley

Its actually runnable... at last lol 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 Cheesy

Evan.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-01-10 22:55:30 »

You need to enable textures.  GL11.glEnable(GL11.GL_TEXTURE_2D);
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (16 views)
2014-12-28 02:10:29

BurntPizza (17 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!