Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (683)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (751)
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 engine isn't working  (Read 827 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans
« Posted 2014-05-24 02:57:00 »

So I've got this shader program that shades a vbo the size of the screen for every light, and entity

How it works:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
for(Light light : lights){
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE);
         
   _lighting.bind();
         
   glUniform3f(glGetUniformLocation(_lighting.programID, "ambientLight"), ambientLight.x, ambientLight.y, ambientLight.z);
   glUniform2f(glGetUniformLocation(_lighting.programID, "lightLocation"), light.lightLocation.x, Display.getHeight() - light.lightLocation.y);
   glUniform3f(glGetUniformLocation(_lighting.programID, "lightColor"), light.lightColor.x, light.lightColor.y, light.lightColor.z);
                 
   glPushMatrix();
      glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0); //Divided because of a scaling issue
           
      _screen.bind();
         glDrawArrays(GL_TRIANGLES, 0, 6); //Size of the screen
      _screen.unbind();
   glPopMatrix();
         
   for(WorldEntity entity : worldEntities){
      entity.onRender(this);
   }
         
   glDisable(GL_BLEND);
}


Fragment 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  
varying position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
   vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);  
   vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
   vec4 spec = texture2D(spec, gl_TexCoord[2].st);
   
   vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
   vec4 diffuse = diff;
   vec4 specular = spec;
   
   vec3 vector = lightLocation.xy - position.xy;
   float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
   
   vec4 final = ambient + diffuse + specular;
   final.xy *= max(0.0, dot(normalize(vector), bump)) / distance;
   
   gl_FragColor = final;
}


Vertex shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
varying position;

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
   position = gl_Position;
   
   gl_TexCoord[0] = gl_MultiTexCoord0;
   gl_TexCoord[1] = gl_MultiTexCoord1;
   gl_TexCoord[2] = gl_MultiTexCoord2;
}


Test entity rendering 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  
glPushMatrix();
   glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "diffuse"),  0);
   glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "normal"),   1);
   glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "specular"), 2);
         
   glActiveTexture(GL_TEXTURE0);
   glEnable(GL_TEXTURE_2D);
   testBatch.bind();
         
   glActiveTexture(GL_TEXTURE1);
   glEnable(GL_TEXTURE_2D);
   testNormal.bind();
         
         glActiveTexture(GL_TEXTURE2);
         glEnable(GL_TEXTURE_2D);
         testSpecular.bind();
           
      testBatch.bind();
       glDrawArrays(GL_TRIANGLES, 0, 6);
      testBatch.unbind();
         
   testTexture.unbind();
   testNormal.unbind();
   testSpecular.unbind();
glPopMatrix();


Result:
Offline Slyth2727
« Reply #1 - Posted 2014-05-24 03:08:54 »

Well first of all, check for any errors the shader might be throwing. I don't see any code in there doing that.
Once you've done that you'll most likely find some errors. Fix them, then come back.
If there's no errors then, well, we'll figure out what's going on.

Check this out:
http://www.opengl.org/wiki/OpenGL_Shading_Language#Error_Checking
Offline Spacebeans
« Reply #2 - Posted 2014-05-24 03:21:08 »

The log says;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Errors in fragment shader:
 0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"
0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization

Errors in vertex shader:
0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"

Errors with link:
0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 381
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2014-05-24 03:24:12 »

1  
2  
3  
...
varying position;
...


There's your problem. 'position' isn't given a type.

Offline Spacebeans
« Reply #4 - Posted 2014-05-24 03:28:11 »

Alright, fixed that.

1  
2  
3  
4  
5  
6  
Errors in fragment shader: 

0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization
0(20) : error C1060: incompatible types in initialization
0(20) : error C1056: invalid initialization


New fragment 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  
varying vec2 position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
   vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);  
   vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
   vec4 spec = texture2D(spec, gl_TexCoord[2].st);
   
   vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
   vec4 diffuse = diff;
   vec4 specular = spec;
   
   vec3 vector = lightLocation.xy - position.xy;
   float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
   
   vec4 final = ambient + diffuse + specular;
   final.xy *= max(0.0, dot(normalize(vector), bump)) / distance;
   
   gl_FragColor = final;
}


New vertex shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
varying vec2 position;

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
   position = gl_Position;
   
   gl_TexCoord[0] = gl_MultiTexCoord0;
   gl_TexCoord[1] = gl_MultiTexCoord1;
   gl_TexCoord[2] = gl_MultiTexCoord2;
}
Offline NegativeZero

JGO Kernel


Medals: 264
Exp: 1 month or less


Zero but not.


« Reply #5 - Posted 2014-05-24 03:45:19 »

1  
   vec3 vector = lightLocation.xy - position.xy;

should be

1  
   vec2 vector = lightLocation.xy - position.xy;

Makes 100% sugar-free code filled with lots of healthy minerals for clean, stress-free skin.
Offline Spacebeans
« Reply #6 - Posted 2014-05-24 03:49:48 »

Okay, fixed that, and anouther warning from the normalize method because it can't take a vec2, fixed that.

1  
2  
3  
Errors in fragment shader:
 0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization


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  
varying vec2 position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
   vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);  
   vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
   vec4 spec = texture2D(spec, gl_TexCoord[2].st);
   
   vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
   vec4 diffuse = diff;
   vec4 specular = spec;
   
   vec2 vector = lightLocation.xy - position.xy;
   float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
   
   vec4 final = ambient + diffuse + specular;
   final.xy *= max(0.0, dot(normalize(vec3(vector, 0)), bump)) / distance;
   
   gl_FragColor = final;
}
Offline HeroesGraveDev

JGO Kernel


Medals: 381
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2014-05-24 03:56:59 »

This line:
1  
vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);

You are trying to make a vec3 from a vec2.

What you probably want is:
1  
vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).rgb * 2.0 - 1.0);

(Using rgb instead of xyz. Using xyz would be equivalent, but is easier to understand we're dealing with a texture)

Offline Spacebeans
« Reply #8 - Posted 2014-05-24 04:00:29 »

I compiled, now I have to fix some graphical errors. Thanks guys!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
orrenravid (220 views)
2016-07-16 03:57:23

theagentd (290 views)
2016-07-11 14:28:54

Hydroque (378 views)
2016-07-06 05:56:57

Hydroque (529 views)
2016-07-03 08:52:54

GrandCastle (400 views)
2016-07-01 09:13:47

GrandCastle (393 views)
2016-07-01 09:09:45

CopyableCougar4 (446 views)
2016-06-25 16:56:52

Hydroque (420 views)
2016-06-22 02:17:53

SwampChicken (386 views)
2016-06-20 13:22:57

SwampChicken (306 views)
2016-06-20 13:22:49
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17
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!