• Advertisement
Sign in to follow this  

Strange light in my simple game

This topic is 3986 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

Hi I want to add simple light to my simple game it sound simple:). But look at result:(it avi video 0.3MB made in fraps) www.kpk.unas.cz/video.avi I used this codes:(I am using java + lwjgl) First one:(copied from nehe tutorial lesson 7) private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ByteBuffer temp = ByteBuffer.allocateDirect(16); temp.order(ByteOrder.nativeOrder()); 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); private float lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; Seconde one is:(from: http://www.cokeandcode.com/asteroidstutorial) private void defineLight() { FloatBuffer buffer; buffer = BufferUtils.createFloatBuffer(4); buffer.put(1).put(1).put(1).put(1); buffer.flip(); GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, buffer); buffer = BufferUtils.createFloatBuffer(4); buffer.put(1).put(1).put(1).put(1); buffer.flip(); GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, buffer); // setup the ambient light buffer = BufferUtils.createFloatBuffer(4); buffer.put(0.8f).put(0.8f).put(0.8f).put(0.8f); buffer.flip(); GL11.glLightModel(GL11.GL_LIGHT_MODEL_AMBIENT, buffer); GL11.glLightModeli(GL11.GL_LIGHT_MODEL_TWO_SIDE, GL11.GL_TRUE); // set up the position of the light buffer = BufferUtils.createFloatBuffer(4); buffer.put(10).put(10).put(5).put(0); buffer.flip(); GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, buffer); GL11.glEnable(GL11.GL_LIGHT0); material = BufferUtils.createFloatBuffer(4); And this is how do I render a player: GL11.glPushMatrix(); GL11.glTranslatef(positionX, positionY, 0f); GL11.glScalef(0.0055f, 0.0055f, 0.0055f); texture.bind(); model.render(); GL11.glPopMatrix(); Does somene hav any idea ?

Share this post


Link to post
Share on other sites
Advertisement
HI,

Not big on Java, and not really gone in-depth on much lighting yet,
but my first thought would be either something wrong with your normals, or something with your scaling.

glScalef(0.0055f, 0.0055f, 0.0055f);

Why scale down to such a small value ??

Share this post


Link to post
Share on other sites
Um, the first thing that comes to mind is that in your first light, you define the position variable after you set the position, but I assume this is just something with your code, cause this would return an error?? Not much for Java either.


GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());
GL11.glEnable(GL11.GL_LIGHT1);
private float lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };





Also, darren might be right. Try enabling GL_NORMALIZE or GL_RESCALE_NORMAL before you draw a rescaled object, then disabling it after you draw the rescaled objects. Try the rescale one firts since it is less expensive. When you rescale an object, the normals aren't rescaled, so calling this will rescale the normals for you.

Also, about drawing the character, you have it about right.
Levi

P.S. Please use the source tag when putting longer strands of code in your posts. See the forum FAQ

Share this post


Link to post
Share on other sites
I've used:

GL11.glEnable(GL12.GL_RESCALE_NORMAL);
GL11.glScalef(0.0055f, 0.0055f, 0.0055f);



GL11.glEnable(GL11.GL_NORMALIZE);
GL11.glScalef(0.0055f, 0.0055f, 0.0055f);


But nothing changed.
Iam note sure about normals because I didnt make them. My friend add them when he was doing models, but trully he really didnt know what he was doing so problem could be there. The normals are there I've looked in source code but who knows if they are good. Please have a look on them. They are in wavefront obj format:
www.kpk.unas.cz/models.zip (12kb)

I've set that settings by method attempt-misstake. What is another way to set size of objects ? I know that it can be changed in model editor but as I said I didn't make them I dont know graphics so I want it to change the size through code so I've used scalef.

Share this post


Link to post
Share on other sites
Make a triangle or cube yourself (GL_TRIANGLE) and rotate it ;-)
Add the normal yourself, and rule out a model problem or a lighting problem.

It's not your answer but it will help you think of alternative methods to debug/fault find.

If you get the same problem, it's your lighting.
If you don't then it's your model (or model loader code)

[wink]

Also both code examples you have seem very messy, even the first one by NeHe, seems very 'modified'
Maybe thats a Java thing, I don't know Java.

But I would simplify it a little

private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, lightAmbient);
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, lightDiffuse);




Next, but I'm sure you must of done it somewhere:
you enable .. glEnable(GL_LIGHT1);
but I don't see glEnable(GL_LIGHTING);

Share this post


Link to post
Share on other sites
I have to thank you all. I finally found problem (was in models) but after I added normal vector my ball is little ..strange look by your self
www.kpk.unas.cz/clip.avi

Share this post


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

  • Advertisement