Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 381 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:
Online 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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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: 269
Projects: 11
Exp: 2 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 Knight


Medals: 30
Projects: 2
Exp: 3 years


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;
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: 269
Projects: 11
Exp: 2 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  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (31 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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!