Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  Lighting a cube  (Read 2114 times)
0 Members and 1 Guest are viewing this topic.
Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Posted 2012-05-03 19:13:51 »

I am trying to learn opengl 2 lighting. I have basic per pixel lighting working, but when I try light a cube, each face is lit separately. Each faces color remains static all over the face, which is not how I want it to look. What is a way fix this?
Offline pitbuller
« Reply #1 - Posted 2012-05-03 19:24:04 »

I bet that you are not interpolating light/vertex position correctly.
Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Reply #2 - Posted 2012-05-03 19:41:39 »

Here is my fragment shader, it does not seem like I am doing anything wrong. I am using a static light position since I am just learning.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
vec3 ambient = vec3(0.4,0.4,0.4);
vec3 lightcolor = vec3(1.0,1.0,1.0);
void main(void) {
  vec3 lightpos = vec3(0.4,0.8,0.4);
  vec4 coord = vec4(m*vec4(f_coord,1.0));
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = normalize(lightpos-vec3(coord));
  float distance = length(dirtolight);
  float diffuse = clamp(dot(normnorm,dirtolight),0.0,1.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2012-05-03 19:53:28 »

1  
vec4 coord = vec4(m*vec4(f_coord,1.0));


You are still doing redudant matrix math at fragment shader. This might work but nobody never does that and there is no point to learn wrong things at first becouse then you need to unlearn those next and that can be hard.

You calculate position at vertex shader and then send that to fragment shader. And this will then be interpolated smoothly for fragments.

Also you don't calculate vector length after normalization becouse normalization is used for setting the length to 1. So you are losing the information before you try to calculate it.
Offline theagentd
« Reply #4 - Posted 2012-05-03 20:02:16 »

And your "static light position" is static in view space, meaning that it follows the camera (assuming you do light calculations in view space).

Myomyomyo.
Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Reply #5 - Posted 2012-05-03 20:22:37 »

Ok so I've moved matrix math into the vertex shader. I am still getting the same result though. The light is still flat across each face. Here is my fragment shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.8,0.8,0.8);
void main(void) {
  vec3 lightpos = vec3(0.4,0.8,0.4);
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = lightpos-f_coord.xyz;
  float distance = length(dirtolight);
  float diffuse = clamp(dot(normnorm,normalize(dirtolight)),0.0,1.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}

I'll work on light position as the last steps. At the moment it does not appear to move with the camera.
Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Reply #6 - Posted 2012-05-04 15:05:08 »

Normalizing f_coord in the vertex shader seems to help. When I do this the light seems to become static during model rotations. Is this the problem with the static light position, or is this something I should not be doing?
Offline pitbuller
« Reply #7 - Posted 2012-05-05 00:01:07 »

Normalizing f_coord in the vertex shader seems to help. When I do this the light seems to become static during model rotations. Is this the problem with the static light position, or is this something I should not be doing?
Think a bit what normalization does and how you use f_coord.

Tip: Directions are normalized. Positions not.
Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Reply #8 - Posted 2012-05-05 00:57:38 »

Nevermind, I've fixed it.
Offline cylab

JGO Ninja


Medals: 38



« Reply #9 - Posted 2012-05-05 13:43:16 »

If you want others to benefit, post the source of the whole thing...

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #10 - Posted 2012-05-05 22:13:50 »

Wasn't this a skateboard movie?

Offline obsidian_golem

Senior Newbie




Computer programmer / Google oracle


« Reply #11 - Posted 2012-05-07 22:03:24 »

If you want others to benefit, post the source of the whole thing...
Thing is, I don't know how I fixed it. I have been trying to recreate the whole thing in libgdx and I am running into the same problem. Anyway, here are the shaders that worked:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
uniform vec3 lightpos;
vec3 ambient = vec3(0.4,0.4,0.4);
vec3 lightcolor = vec3(0.8,0.8,0.8);
void main(void) {
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = lightpos-(f_coord);
  float distance = length(dirtolight);
  float diffuse = max(dot(normnorm,normalize(dirtolight)),0.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
attribute vec3 coord;
attribute vec3 normal;
attribute vec3 color;
uniform mat4 matrix;
uniform mat4 m,v;
uniform mat3 nm;
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
//varying float diffuse;
void main() {
   //vec3 lightpos = vec3(0.4,0.8,0.4);
  f_color = color;
   f_coord = (vec3(m*vec4(coord,1.0)));
   f_normal = normalize(nm*normal);
   gl_Position = matrix * vec4(coord,1.0);
}


Oh... Figured it out. When the light gets more a certain distance above an object it begins to look like per vertex lighting.
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.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!