Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  GLSL Lighting a 3D box  (Read 646 times)
0 Members and 1 Guest are viewing this topic.
Offline Quaker762

Junior Newbie


Exp: 3 years



« Posted 2014-08-21 10:04:26 »

Hey guys, long time lurker, first (well second really) time poster,

I'm a bit of a newbie when it comes to OGL, and I recently started playing around with shaders in lwjgl. I wanted to try lighting out first, and borrowed code for a simple vertex and fragment shader, which is as follows:

Vertex Shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
varying float diffuse_value;

void main() {            
    vec3 vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
    vec3 vertex_light_position = gl_LightSource[0].position.xyz;
    diffuse_value = max(dot(vertex_normal, vertex_light_position), 0.0);

    gl_FrontColor = gl_Color;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


Fragment Shader
1  
2  
3  
4  
5  
varying float diffuse_value;

void main() {
    gl_FragColor = gl_Color * diffuse_value;
}


Now, I'm drawing a basic 3D box to go along with this, which is just basic quads put together. However, when the program is run, instead of getting "lighting" so to speak, each face receives the same color/amount of light, thus the box goes in and out of being black and being colored.

Any help would be greatly appreciated!

-Quaker762
Offline quew8

JGO Coder


Medals: 33



« Reply #1 - Posted 2014-08-21 18:58:53 »

I think this is more of a problem with how you have set up the light rather than the shader.
Offline Quaker762

Junior Newbie


Exp: 3 years



« Reply #2 - Posted 2014-08-22 09:27:25 »

How should I got about setting up my lights then? Could you possibly link to some good tutorials to get started?

At the moment I'm using:

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  
      glViewport(0, 0, width, height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      GLU.gluPerspective(60.0f, (float) width / (float) height, 0.1f, 100.0f); // Set
                                                               // up
                                                               // our
                                                               // 3D
                                                               // perspective
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glPushMatrix();
     
      FloatBuffer position = ByteBuffer.allocateDirect(16).asFloatBuffer();
       position.mark();
       position.put(new float[] {0f, 0f, 0f, 0f }); // even values about 10e3 for the first three parameters aren't changing anything
       position.reset();
      //Enable lighting
      glLight(GL_LIGHT0, GL_POSITION, position);
     
      FloatBuffer ambient = ByteBuffer.allocateDirect(16).asFloatBuffer();
       ambient.mark();
       ambient.put(new float[] { 0.5f, 0.5f, 0.5f, 1f });
       ambient.reset();
       glLight(GL_LIGHT0, GL_AMBIENT, ambient);
       
      glEnable(GL_DEPTH_TEST);
      glDepthMask(true);
      glEnable(GL_CULL_FACE);
     
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
      glClearColor(1f, 1f, 1f, 1f);


However, this makes my box completely black now.
Sorry to ask really simple questions, I'm just so unsure how to even enable lighting properly.

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

JGO Coder


Medals: 33



« Reply #3 - Posted 2014-08-22 10:19:01 »

OK, here is your problem. Your shader is designed to work for directional lights. A directional light is one considered to be infinitely far away which has two effects: 1) It (can be considered to have) has a uniform intensity. 2) The rays of light (can be considered to be) are parallel. An example of a real world directional light is the sun. So obviously a directional light doesn't really have a position, only a direction.

Now in fixed function pipeline OpenGL lighting, the direction of a directional light was set with the light's position. I don't know why this is, probably to ease the implementation, but whoever wrote your shader has emulated this behaviour, getting the light's direction from it's position. Now you setting the position to (0, 0, 0) essentially kills the light. But that isn't your only problem, hence why setting it to (10E3, 10E3, 10E3) didn't work either. And it is capital 'E' btw. Lower case e is natural logarithm base.

The dot product only works in the way you are using it when both vectors are normalized. (10E3, 10E3, 10E3) is obviously not normalized giving you undefined results. Either normalize it in the shader or better yet normalize it cpu side before setting it.

Two last things. Since you are using your own shaders, enabling GL_LIGHTING and GL_LIGHT0 does absolutely nothing. And if you want to read about how OpenGL used to do these lighting calculations: http://www.glprogramming.com/red/chapter05.html. It is a good read for explaining lighting things.

Hope I've helped.
Online SHC
« Reply #4 - Posted 2014-08-22 15:50:45 »

You shouldn't reset the buffers, you should rewind them.

Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #5 - Posted 2014-08-22 18:04:21 »

You shouldn't reset the buffers, you should rewind them.
Yeah, either
buffer.rewind();
or
buffer.flip();
.
With rewind the buffer becomes readable, while with flip you can write to it too.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2014-08-22 18:35:58 »

You shouldn't reset the buffers, you should rewind them.
Yeah, either
buffer.rewind();
or
buffer.flip();
.
With rewind the buffer becomes readable, while with flip you can write to it too.
I'd suggest to read the javadocs. reset(), flip(), clear() and rewind() alter the position and limit of a buffer, and has nothing to do with readability/writability.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #7 - Posted 2014-08-22 21:26:40 »

Yeah, either
buffer.rewind();
or
buffer.flip();
.
With rewind the buffer becomes readable, while with flip you can write to it too.
I'd suggest to read the javadocs. reset(), flip(), clear() and rewind() alter the position and limit of a buffer, and has nothing to do with readability/writability.
Copied straight from the Javadocs:
Quote
In addition to methods for accessing the position, limit, and capacity values and for marking and resetting, this class also defines the following operations upon buffers:

clear() makes a buffer ready for a new sequence of channel-read or relative put operations: It sets the limit to the capacity and the position to zero.

flip() makes a buffer ready for a new sequence of channel-write or relative get operations: It sets the limit to the current position and then sets the position to zero.

rewind() makes a buffer ready for re-reading the data that it already contains: It leaves the limit unchanged and sets the position to zero.

So they clearly alter the readability/writeability of a buffer, just not in a direct way but with settings the appropriate variables to the correct values.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 848
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2014-08-23 10:44:52 »

Those are example use-cases, they have nothing to do with the buffer's properties.

buffer.rewind() is the equivalent of buffer.position(0)
buffer.clear() is the equivalent of buffer.position(0) + buffer.limit(buffer.capacity())
buffer.flip() is the equivalent of buffer.limit(buffer.position()) + buffer.position(0)
buffer.reset() is the equivalent of buffer.position(mark);

buffer.clear() does not affect the readability or writability of a buffer, neither directly nor indirectly. it's just common that one starts writing after a clear, and reading after a flip.


while with flip you can write to it too.
this is typically exactly what you would not do after a flip.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

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

TheDudeFromCI (12 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 (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29: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!