Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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 Lighting problem: All faces have same color  (Read 2013 times)
0 Members and 1 Guest are viewing this topic.
Offline Zarkonnen
« Posted 2012-03-23 19:48:45 »

So I've been trying to get the lighting code from penguin's post at http://www.java-gaming.org/index.php?topic=23951.0 to work. (Including the missing GL11.glEnable(GL11.GL_LIGHTING);.

However, what I'm actually seeing is that instead of each face of the pyramid being lit up by an amount depending on its orientation, all faces are lit up by the same amount, which changes as the pyramid rotates. Does anyone have any insight? This is using lwjgl 2.8.3 on Mac OS X.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-03-23 19:50:54 »

Show us your code.

Did you add normals to each vertex?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Zarkonnen
« Reply #2 - Posted 2012-03-23 19:54:14 »

Show us your code.

Did you add normals to each vertex?

There you go:

And no - I didn't know vertex normals were a thing! That might explain a lot! (Can you tell I'm new at this OpenGL thing?)

Anyway, here's the code:

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  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
package com;

import com.zarkonnen.lwjgltest.Main;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.lwjgl.util.glu.*;
import org.lwjgl.input.Keyboard;

import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 *
 * @author penguin
 */

public class main {

   public static void main(String[] args) {
      try {
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.setTitle("3D Pyramid");
         Display.create();
      } catch (Exception e) {
      }
      initGL();

      float rtri = 0.0f;
      Texture texture = null;
      try {
         texture = TextureLoader.getTexture("png", Main.class.getResourceAsStream("tex.png"));
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      while (!Display.isCloseRequested()) {
         // Draw a Triangle :D

         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

         GL11.glLoadIdentity();

         GL11.glTranslatef(0.0f, 0.0f, -10.0f);

         GL11.glRotatef(rtri, 0.0f, 1.0f, 0.0f);

         texture.bind();

         GL11.glBegin(GL11.GL_TRIANGLES);

         GL11.glTexCoord2f(0.0f, 1.0f);
         GL11.glVertex3f(0.0f, 1.0f, 0.0f);
         GL11.glTexCoord2f(-1.0f, -1.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(0.0f, 1.0f, 0.0f);
         GL11.glTexCoord2f(-1.0f, -1.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(0.0f, 1.0f, 0.0f);
         GL11.glTexCoord2f(-1.0f, -1.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(0.0f, 1.0f, 0.0f);
         GL11.glTexCoord2f(-1.0f, -1.0f);
         GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
         GL11.glTexCoord2f(1.0f, -1.0f);
         GL11.glVertex3f(-1.0f, -1.0f, 1.0f);


         GL11.glEnd();

         GL11.glBegin(GL11.GL_QUADS);
         GL11.glVertex3f(1.0f, -1.0f, 1.0f);
         GL11.glVertex3f(1.0f, -1.0f, -1.0f);
         GL11.glVertex3f(-1.0f, -1.0f, -1.0f);
         GL11.glVertex3f(-1.0f, -1.0f, 1.0f);
         GL11.glEnd();

         Display.update();
         rtri += 0.05f;
         // Exit-Key = ESC
         boolean exitPressed = Keyboard.isKeyDown(Keyboard.KEY_ESCAPE);
         if (exitPressed) {
            System.out.println("Escape was pressed!");
            Display.destroy();

         }
      }

      Display.destroy();
   }

   private static void initGL() {
      GL11.glEnable(GL11.GL_LIGHTING);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GLU.gluPerspective(45.0f, ((float) 800) / ((float) 600), 0.1f, 100.0f);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glLoadIdentity();

      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glShadeModel(GL11.GL_SMOOTH);
      GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      GL11.glClearDepth(1.0f);
      GL11.glEnable(GL11.GL_DEPTH_TEST);
      GL11.glDepthFunc(GL11.GL_LEQUAL);
      GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
      float lightAmbient[] = {0.5f, 0.5f, 0.5f, 1.0f};  // Ambient Light Values
      float lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};      // Diffuse Light Values
      float lightPosition[] = {0.0f, 0.0f, 2.0f, 1.0f}; // Light Position

      ByteBuffer temp = ByteBuffer.allocateDirect(16);
      temp.order(ByteOrder.nativeOrder());
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer) temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer) temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, (FloatBuffer) temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
      GL11.glEnable(GL11.GL_LIGHT1);                          // Enable Light One
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2012-03-23 19:56:21 »

Quote
All faces have the same color

... that could be the title of a John Lennon song...

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-03-23 19:58:32 »

Lighting depends on normals. Google will answer most of your questions. Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Zarkonnen
« Reply #5 - Posted 2012-03-23 20:00:08 »


Well, I assumed they'd be calculated from the vertices using the right-hand or left-hand rule. Learning! Trying it out now...
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-03-23 20:10:37 »

You often don't want each vertex of a triangle to have to same normal.

Imagine rendering a sphere, or other curved shapes, it would look rather bad as the shade wouldn't be smooth across the surface.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Zarkonnen
« Reply #7 - Posted 2012-03-23 20:19:50 »

You often don't want each vertex of a triangle to have to same normal.

Imagine rendering a sphere, or other curved shapes, it would look rather bad as the shade wouldn't be smooth across the surface.

Aaah, of course. OK, I'm afraid I have another question. (And BTW, thank you so much for your help so far.) So I'm now trying to render a cube, and I'm setting the normals to point straight out of the faces. However, I'm now seeing that the top face of the cube, with a normal call of

1  
glNormal3f(0.0f, 0.0f, 1.0f);


gets the most illumination, independent of what light position I supply using

1  
glLight(GL_LIGHT1, GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());


Complete code:

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  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
package com.zarkonnen.lwjgltest;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Main {
   public static final int DISPLAY_HEIGHT = 480;
   public static final int DISPLAY_WIDTH = 640;
   public static final Logger LOGGER = Logger.getLogger(Main.class.getName());

   private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };  // Ambient Light Values ( NEW )
   private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values ( NEW )
   private float lightPosition[] = { 0.0f, 1.0f, 0.0f, 1.0f }; // Light Position ( NEW )
   
   static {
      try {
         LOGGER.addHandler(new FileHandler("errors.log", true));
      } catch (IOException ex) {
         LOGGER.log(Level.WARNING, ex.toString(), ex);
      }
   }

   public static void main(String[] args) {
      Main main = null;
      try {
         main = new Main();
         main.create();
         main.run();
      } catch (Exception ex) {
         LOGGER.log(Level.SEVERE, ex.toString(), ex);
      } finally {
         if (main != null) {
            main.destroy();
         }
      }
   }
   
   ArrayList<Box> boxes = new ArrayList<Box>();

   public void create() throws LWJGLException, IOException {
      //Display
      Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH, DISPLAY_HEIGHT));
      Display.setFullscreen(false);
      Display.setTitle("Hello LWJGL World!");
      Display.create();

      //Keyboard
      Keyboard.create();

      //Mouse
      Mouse.setGrabbed(false);
      Mouse.create();

      //OpenGL
      initGL();
     
      Texture tex = TextureLoader.getTexture("png", Main.class.getResourceAsStream("tex.png"));
      boxes.add(new Box(0, 0, 0, 1, 1, 1, tex));
   }

   public void destroy() {
      //Methods already check if created before destroying.
      Mouse.destroy();
      Keyboard.destroy();
      Display.destroy();
   }
   
   public void initGL() {
      glEnable(GL_LIGHTING);
      glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
        glShadeModel(GL_SMOOTH); // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
        glClearDepth(1.0f); // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST); // Enables Depth Testing
        glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

        glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
        glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
       gluPerspective(
            45.0f,
            DISPLAY_WIDTH * 1.0f / DISPLAY_HEIGHT,
            0.1f,
            100.0f);
        glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

        // Really Nice Perspective Calculations
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        ByteBuffer temp = ByteBuffer.allocateDirect(16);
        temp.order(ByteOrder.nativeOrder());
        glLight(GL_LIGHT1, GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
        glLight(GL_LIGHT1, GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
        glLight(GL_LIGHT1, GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
      glEnable(GL_LIGHT1);                          // Enable Light One
   }

   public void processKeyboard() {
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) { cx += 0.03f; }
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { cx -= 0.03f; }
      if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { cy -= 0.03f; }
      if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { cy += 0.03f; }
   }

   public void processMouse() {
   }
   float cx, cy;
   
   public Main() throws IOException {
     
   }
   
   static class Box {
      float x, y, z, w, d, h;
      Texture tex;

      public Box(float x, float y, float z, float w, float d, float h, Texture tex) {
         this.x = x;
         this.y = y;
         this.z = z;
         this.w = w;
         this.d = d;
         this.h = h;
         this.tex = tex;
      }
   }
   
   public void render() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
      for (Box b : boxes) {
         glLoadIdentity();
         // Camera
         glTranslatef(cx, cy, -8);
         glTranslatef(b.x, b.y, b.z);
         b.tex.bind();
         glBegin(GL_QUADS);
         // North face
         glNormal3f(0.0f, -1.0f, 0.0f);
         glTexCoord2f(0.0f, 0.0f); glVertex3f(b.x, b.y + b.h, b.z);         glTexCoord2f(b.tex.getWidth(), 0.0f); glVertex3f(b.x + b.w, b.y + b.h, b.z);
                                                            glTexCoord2f(b.tex.getWidth(), b.tex.getHeight()); glVertex3f(b.x + b.w, b.y + b.h, b.z + b.d);
         glTexCoord2f(0.0f, b.tex.getHeight()); glVertex3f(b.x, b.y + b.h, b.z + b.d);
         // East face
         glNormal3f(1.0f, 0.0f, 0.0f);
         glTexCoord2f(0.0f, 0.0f); glVertex3f(b.x + b.w, b.y, b.z);         glTexCoord2f(b.tex.getWidth(), 0.0f); glVertex3f(b.x + b.w, b.y, b.z + b.d);
                                                            glTexCoord2f(b.tex.getWidth(), b.tex.getHeight()); glVertex3f(b.x + b.w, b.y + b.h, b.z + b.d);
         glTexCoord2f(0.0f, b.tex.getHeight()); glVertex3f(b.x + b.w, b.y + b.h, b.z);
         // West face
         glNormal3f(-1.0f, 0.0f, 0.0f);
         glTexCoord2f(0.0f, 0.0f); glVertex3f(b.x, b.y, b.z);            glTexCoord2f(b.tex.getWidth(), 0.0f); glVertex3f(b.x, b.y + b.h, b.z);
                                                            glTexCoord2f(b.tex.getWidth(), b.tex.getHeight()); glVertex3f(b.x, b.y + b.h, b.z + b.d);
         glTexCoord2f(0.0f, b.tex.getHeight()); glVertex3f(b.x, b.y, b.z + b.d);
         // South face
         glNormal3f(0.0f, 1.0f, 0.0f);
         glTexCoord2f(0.0f, 0.0f); glVertex3f(b.x, b.y, b.z);            glTexCoord2f(b.tex.getWidth(), 0.0f); glVertex3f(b.x, b.y, b.z + b.d);
                                                            glTexCoord2f(b.tex.getWidth(), b.tex.getHeight()); glVertex3f(b.x + b.w, b.y, b.z + b.d);
         glTexCoord2f(0.0f, b.tex.getHeight()); glVertex3f(b.x + b.w, b.y, b.z);
         // Top face
         glNormal3f(0.0f, 0.0f, 1.0f);
         glTexCoord2f(0.0f, 0.0f); glVertex3f(b.x, b.y, b.z + b.d);         glTexCoord2f(b.tex.getWidth(), 0.0f); glVertex3f(b.x, b.y + b.h, b.z + b.d);
                                                            glTexCoord2f(b.tex.getWidth(), b.tex.getHeight()); glVertex3f(b.x + b.w, b.y + b.h, b.z + b.d);
         glTexCoord2f(0.0f, b.tex.getHeight()); glVertex3f(b.x + b.w, b.y, b.z + b.d);
         glEnd();
      }
   }

   public void run() {
      while (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
         if (Display.isVisible()) {
            processKeyboard();
            processMouse();
            update();
            render();
         } else {
            if (Display.isDirty()) {
               render();
            }
            try {
               Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
         }
         Display.update();
         Display.sync(60);
      }
   }

   public void update() {
   }
}
Offline Zarkonnen
« Reply #8 - Posted 2012-03-23 20:25:46 »

Never mind, I finally figured it out: it was just that I was moving the light so little that for all intents and purposes it was always at the top. It works now!

Thank you so much for your help!
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.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
My New Android Sudoku 2d Game
by gamerfan
2014-10-31 06:12:26

My New Android Sudoku 2d Game
by gamerfan
2014-10-31 06:05:29

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!