Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  How to transfer lighting code from vertex shader to fragment shader?  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-03-18 15:48:00 »

Vertex shader:
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  
#version 120

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;

uniform vec3[64] lights;
uniform vec3[64] lightColors;
uniform int lightingEnabled;
uniform int lightCount;

varying vec4 v_color;
varying vec2 v_texCoords;


float shade(vec3 light) {
   float lx = light.x;
   float ly = light.y;
   float dist = distance(a_position.xy, vec2(lx, ly)) / light.z;
   float c = (1 - sqrt(dist * dist * 2) / 300);
   return c;
}

vec3 light(vec3 light, vec3 lightColor) {
   float c = shade(light);
   if(c < 0) c = 0;
   
   vec3 l = vec3(lightColor.r * c, lightColor.g * c, lightColor.b * c);
     
   return l;
}

vec4 calculateLighting() {
   vec4 lighting=vec4(0.0, 0.0, 0.0, 1.0);

   for(int i=0;i<lightCount;i++) {
      lighting.rgb = max(light(lights[i], lightColors[i]), lighting.rgb);
   }
   
   return lighting;
}

void main() {
   
   vec4 lighting = vec4(1.0, 1.0, 1.0, 1.0);

   if(lightingEnabled == 1) {
   
      lighting = vec4(0.0, 0.0, 0.0, 1.0);      
     
      lighting = calculateLighting();
     
   }
   
   vec4 pos = a_position;
   pos.x = pos.x;
   pos.y = pos.y;

   v_color = a_color * lighting;
   v_texCoords = a_texCoord;
   gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * pos;
}


Fragment:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
#version 120

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;

void main() {
   vec4 col = texture2D(u_texture, v_texCoords);
    gl_FragColor = col * v_color;
}



This code works properly, but is per vertex so it has some not nice features as one might expect. How do I move lighting calculations to fragment shader? I know I'm asking for quite a lot, but I'm kinda lost here..
Offline Drenius
« Reply #1 - Posted 2014-03-18 16:20:13 »

Whats your actual question? How glsl works at all? You will have to specify this a little more...
You can use a varying position vector for every vertex to give each fragment its own position (possibly there is a more direct way, no idea, however).
A varying variable varies for each fragment, depending on how close it is to the vertices, in case you did not already know.
Offline trollwarrior1
« Reply #2 - Posted 2014-03-18 16:24:04 »

Yea maybe I have no idea how glsl works, sorry..

So lets say I make a varying vec2 v_position.
It will be different for each pixel on the screen somehow??

edit ---------------
Here is the problem..

If you look closely you can see entities that shouldn't be seen, because they are pretty big.
Also, with this code you can see where the vertices are at certain light combinations. (When lighting difference is very extreme)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Simn
« Reply #3 - Posted 2014-03-18 18:06:08 »

If you pass the fragment shader the position from the vertex shader and then calculate the lightning in the fragment shader your problem should be solved. GLSL interpolates values from the vertex shader to the fragment shader. Haven't you ever wondered why you can pass the fragment shader a bunch of texture coordinates and it suddenly outputs the correct fragment for each pixel in the image?  Roll Eyes

- Simn
Offline trollwarrior1
« Reply #4 - Posted 2014-03-18 19:08:45 »

If you feel this is stupid question, just ignore it. I'm playing LoL atm..

Can I have same uniforms in fragment shader as in vertex shader?
Offline Drenius
« Reply #5 - Posted 2014-03-18 19:21:55 »

You can.
Offline trollwarrior1
« Reply #6 - Posted 2014-03-18 19:39:33 »

You can.

i don't know what to say..
Offline quew8

JGO Coder


Medals: 29



« Reply #7 - Posted 2014-03-18 20:52:24 »

Try working through these tutorials before you try to implement any kind of per-fragment lighting.
http://www.lighthouse3d.com/tutorials/glsl-tutorial/
Offline trollwarrior1
« Reply #8 - Posted 2014-03-18 21:13:24 »

This might be off topic, but:

Quote
Full DirectX® 9.0 support (Vertex Shader version 2.0 and Pixel Shader version 2.0)

This is from my very old laptop specs.
Does this mean it has opengl version 2.0 support, meaning glsl supported version is 1.10?
Offline quew8

JGO Coder


Medals: 29



« Reply #9 - Posted 2014-03-18 23:01:06 »

No that's completely a DirectX thing. There is a loose equivalence between DirectX shaders versions and OpenGL shader versions but A) I don't know it so you'd have to look it up and B) it's only a rough and ready rule and might not be accurate.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #10 - Posted 2014-03-19 16:18:42 »

hmmm....almost all integrated graphics chips in the last 6 years support shaders. At least the ones from intel. Not sure if you are using any special or more modern extensions/versions but generally DX9 means at least opengl 2.something and dx11 means at least opengl 3.something.

Also, why not google how to check what your computer supports? Or even better, use the bindings to opengl to find out.

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.

BurntPizza (21 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (87 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!