Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  Libgdx shaders not working.  (Read 716 times)
0 Members and 1 Guest are viewing this topic.
Online trollwarrior1
« Posted 2014-02-03 07:55:18 »

Here are my shaders.

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

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;
uniform vec4 shaderColor;

uniform float WIDTH, HEIGHT, SCALE, WINDOW_WIDTH, WINDOW_HEIGHT;

uniform float[384] lights;
uniform int lightingEnabled;
uniform int lightCount;

varying vec4 v_color;
varying vec2 v_texCoords;


float dist(vec2 v1, vec2 v2);
float shade(vec3 light);
vec3 light(vec3 light, vec3 lightColor);


void main() {
   
   vec4 lighting = vec4(1.0, 1.0, 1.0, 1.0);
   
   if(1 == 1) {
      lighting = vec4(0.0, 0.0, 0.0, 1.0);
     
      for(int i= 0; i < lightCount; i++) {
         int index = i * 6;
         vec3 _light = vec3(lights[index + 0], lights[index + 1], lights[index + 2]);
         vec3 _lightColor= vec3(lights[index + 3], lights[index + 4], lights[index + 5]);
         
         vec3 light = light(_light, _lightColor);
         
         if(light.r > lighting.r) lighting.r = light.r;
         if(light.g > lighting.g) lighting.g = light.g;
         if(light.b > lighting.b) lighting.b = light.b;
      }
     
   }
   
   vec4 pos = a_position;
   pos.x = pos.x / (WIDTH*SCALE) * WINDOW_WIDTH;
   pos.y = pos.y / (HEIGHT*SCALE) * WINDOW_HEIGHT;

   float tone = 2.0;
   v_color = a_color * vec4(tone, tone, tone, tone) * lighting;
   v_texCoords = a_texCoord0;
   gl_Position = u_projTrans * pos;
}


float dist(vec2 v1, vec2 v2) {
   float xd = v1.x - v2.x;
   float yd = v1.y - v2.y;
   
   if(xd < 0.0) xd = -xd;
   if(yd < 0.0) yd = -yd;
   
   return sqrt(xd * xd + yd * yd);
}

float shade(vec3 light) {
   float lx = light.x * SCALE;
   float ly = (HEIGHT - light.y) * SCALE;

   float dist = dist(vec2(lx, ly), a_position.xy) / light.z;

   float c = (1.0 - sqrt(dist * dist * 2.0) / 300.0);
   return c;
}

vec3 light(vec3 light, vec3 lightColor) {

   float c = shade(light);

   if(c < 0.0) c = 0.0;
   
   vec3 lighting = vec3(lightColor.r * c, lightColor.g * c, lightColor.b * c);
   
   return lighting;
}


Fragment:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
#version 120

#ifdef GL_ES
    precision mediump float;
#endif

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


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



Here is the code where I send stuff to shaders.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   public void flushLights() {
           
      Game.shader.setUniformi("lightCount", lights.size());

      float[] lightData = new float[lights.size() * 6];
     
      for(int i=0;i<lights.size();i++) {
         int index = i * 6;
         Light light = lights.get(i);
         
         lightData[index + 0] = light.x;
         lightData[index + 1] = light.y;
         lightData[index + 2] = light.intensity;
         
         lightData[index + 3] = light.color.r;
         lightData[index + 4] = light.color.g;
         lightData[index + 5] = light.color.b;
         
      }
         
      Game.shader.setUniform1fv("lights", lightData, 0, lightData.length);
     
      lights.clear();
   }



Any ideas what I'm doing wrong here? This code works perfectly fine on 2 of newer computers. It doesn't work on older laptops. Any ideas why?
I found that the problem is when I try to calculate lights by accessing lights uniform.
The only thing that shader.getInfoLog says is "Fragment shader linked, Vertex shader not linked". What am I supposed to make out of that?.....
Offline zFollette

Junior Devvie


Exp: 2 years


I like jokes


« Reply #1 - Posted 2014-02-03 17:19:53 »

If the laptop you are testing on came out in and around 2006, it will not work, your GLSL shaders are marked with version 120, which is a pre-processor of openGL 2.1, which came out in 2006. See: http://en.wikipedia.org/wiki/OpenGL_Shading_Language

Humor will keep you alive.
Online trollwarrior1
« Reply #2 - Posted 2014-02-03 17:24:41 »

It doesn't work with either version Sad
If I remove the code which calculates the lights from uniforms, it works for some reason.. Doesn't seem to be the problem with sending data to shader.
I tried 110 and no version at all and it still doesn't work..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zFollette

Junior Devvie


Exp: 2 years


I like jokes


« Reply #3 - Posted 2014-02-03 17:30:26 »

It doesn't work with either version Sad
If I remove the code which calculates the lights from uniforms, it works for some reason.. Doesn't seem to be the problem with sending data to shader.
I tried 110 and no version at all and it still doesn't work..

What code are you using to run the shader programs?

Humor will keep you alive.
Online trollwarrior1
« Reply #4 - Posted 2014-02-03 17:33:42 »

What does that mean?
I do ShaderProgram shader = new ShaderProgram("", "");
SpriteBatch.setShader(shader);

EDIT------------

AHhhhhh behhh I guess I will just have to implement FBO version of lighting somehow..
Offline Longarmx
« Reply #5 - Posted 2014-02-03 23:16:27 »

If you are using older versions (can't remember what one) you will have to tell the shader program about attributes before hand in the shader creation and linkage code. Looks something like this using LWJGL:
1  
2  
3  
4  
// program is program handle
// location is the attribute number (start with 0 and increment)
// attribute is a string of the attribute name
GL20.glBindAttribLocation(program, location, attribute);


Edit: I think that this is required before V3.3 GLSL #version 330

Offline davedes
« Reply #6 - Posted 2014-02-04 01:49:43 »

You should print the log after creating a shader as it will give you a clue.

You also have a pretty ridiculous number of lights, I wouldn't be surprised if some devices don't support that many uniforms.

Further, some drivers don't support dynamic indexing.

Ps longarmx this is only necessary if you want custom attribute locations.  It's unrelated in the OPs case... Also it has to be done before linking, which I'm not sure LibGDX even supports.

Online trollwarrior1
« Reply #7 - Posted 2014-02-04 07:48:43 »

@davedes

Its not really that big. 1 light is x,y,intensity,r,g,b. So 384/6 = 64 lights.
Maybe it really doesn't support dynamic indexing.. That would be bad.. Very bad..
Offline davedes
« Reply #8 - Posted 2014-02-04 15:53:18 »

Query GL_MAX_VERTEX_UNIFORM_COMPONENTS on those machines to see if you're going over the max.

There are some quirks with this query, see here:
http://www.opengl.org/wiki/GLSL_Uniform

Instead of a single flat array, try using a vec3 array to define the light position, and another parallel vec4 array to define their colours. This should clean up code and remove the need for dynamic indexing.

Also I'm just quickly glancing at your code; but you should avoid if statements and try using built-ins like length(), distance(), abs(), clamp(), max(), etc. as it will lead to a big boost in performance.

If you really feel like you need that many lights, maybe a deferred solution would be more suitable.

I have some tutorials on shaders that you might find useful. They cover some basics like the built-in smoothstep() and other useful functions.
https://github.com/mattdesl/lwjgl-basics/wiki/Shaders

There is also a section on lighting, and you'll probably be able to take some aspects of it (like falloff etc).
https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6

Online trollwarrior1
« Reply #9 - Posted 2014-02-04 17:57:25 »

I came to conclusion that older stuff doesn't support dynamic indexing..

Here is vertex shader I wrote. This one seems to work
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  
#version 120

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

uniform vec3[16] lights;
uniform vec3[16] 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);
   
   int counter = 0;
   
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[0], lightColors[0])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[1], lightColors[1])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[2], lightColors[2])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[3], lightColors[3])); } else return lighting;
   
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[4], lightColors[4])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[5], lightColors[5])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[6], lightColors[6])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[7], lightColors[7])); } else return lighting;
   
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[8], lightColors[8])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[9], lightColors[9])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[10], lightColors[10])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[11], lightColors[11])); } else return lighting;
   
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[12], lightColors[12])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[13], lightColors[13])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[14], lightColors[14])); } else return lighting;
   if(counter++ < lightCount) { lighting.rgb = max(lighting.rgb, light(lights[15], lightColors[15])); } else return lighting;
   
   
   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;

   float tone = 2.0;
   v_color = a_color * vec4(tone, tone, tone, tone) * lighting;
   v_texCoords = a_texCoord0;
   gl_Position = u_projTrans * pos;
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #10 - Posted 2014-02-04 19:14:50 »

Light from different sourec are additive why you are doign those complex branchess?
Online trollwarrior1
« Reply #11 - Posted 2014-02-04 20:43:32 »

Light from different sourec are additive why you are doign those complex branchess?

When I do additive, stuff starts looking too bright.. This looks a lot better, but has the drawback of making circle-like dark patterns when 2 lights add up. Will need to fix it somehow.
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.

trollwarrior1 (15 views)
2014-11-22 12:13:56

xFryIx (68 views)
2014-11-13 12:34:49

digdugdiggy (47 views)
2014-11-12 21:11:50

digdugdiggy (41 views)
2014-11-12 21:10:15

digdugdiggy (35 views)
2014-11-12 21:09:33

kovacsa (59 views)
2014-11-07 19:57:14

TehJavaDev (61 views)
2014-11-03 22:04:50

BurntPizza (60 views)
2014-11-03 18:54:52

moogie (75 views)
2014-11-03 06:22:04

CopyableCougar4 (76 views)
2014-11-01 23:36:41
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

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