Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 + Shadow Mapping GLSL  (Read 2290 times)
0 Members and 1 Guest are viewing this topic.
Offline Darth Revan

Senior Newbie





« Posted 2012-09-25 18:14:03 »

Hi, I want to use diffuse and ambient light. My problem is, that if I move with the camera (translate the ModelViewMatrix) the brightness of the faces changes although the light should be fixed.

Here is my vertex shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
attribute vec3 position;
attribute vec3 normal;

void main() {
   gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1);
   gl_Normal = normalize(gl_NormalMatrix * normal);
                     
   vec3 light = normalize(vec3(gl_LightSource[0].position));

   float NdotL = max(dot(gl_Normal, light), 0.0);
                     
   vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
   vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
                     
   gl_FrontColor = NdotL * gl_LightSource[0].diffuse + globalAmbient + ambient;
}


I set the light position each frame after translate the MoselViewMatrix:
1  
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, mPosition);


What should I do?

PS: Is there an IDE/addon for Eclipse for GLSL?

PS: Sorry for my bad English.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2012-09-25 22:33:56 »

The light position is transformed by the current state of the model view matrix and those transformed values are what you see in the GLSL shader.

The effects that you might see depend on what matrix you've set before you call glLight().  If you have the identify matrix, and then call glLight(), and then perform your rendering, the light position you specified is already in "eye" space and it will move along with the camera.

If you set the matrix to the view's inverse transform, and then call glLight(), the light's position you specified is in "world" space and should be fixed.

If you set the matrix to the view inverse multiplied with the model matrix of an object, and then call glLight(), the light's position will be in "model" space and will move along with the object.

It sounds like you're having the first problem where you set the light position too soon, which is odd since you claim to be setting it after you translate the model view matrix.  I'd double check the flow of your operations to make sure you are doing it in the right order.  It might help me to see the rest of your Java code that sets everything up too.

As an aside, what type of diffuse light are you trying to simulate? An infinite direction light or a point light?

Offline Darth Revan

Senior Newbie





« Reply #2 - Posted 2012-09-27 08:39:10 »

I have solved the problem. Previously I first rotated and translated the modelviewmatrix and then set the light. Now I rotate the modelviewmatrix first, then set the light and move the modelviewmatrix at the end.

But now I would like to use shadow mapping, but I can't find a tutorial, that I understand.
I understand the theory (Render the scene from the view of the light, read the z-Buffer and test in the fragment shader, if z is bigger than the z from the rendering from light position), but not how to do this.

PS: Sorry for my bad English.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Darth Revan

Senior Newbie





« Reply #3 - Posted 2012-10-04 11:23:31 »

I have found this tutorial. It is written in Delphi, so I have to change the code a little.
But apparently I did something wrong. GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) returns 36054 (GL30.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) and if I bind the FBO I get the error "Invalid framebuffer operation".

original code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
GLuint shadow_map = 0; 
GLuint shadow_fbo = 0;
GLuint shadow_size = 4096;
 
glGenTextures(1, &shadow_map);
glBindTexture(GL_TEXTURE_2D, shadow_map);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, shadow_size, shadow_size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
glGenFramebuffersEXT(1, &shadow_fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadow_fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow_map, 0);
glDrawBuffer(GL_FALSE);
glReadBuffer(GL_FALSE);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);


my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
int shadow_map = 0; 
int shadow_fbo = 0;
int shadow_size = 4096;

shadow_map = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, shadow_map);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT16, shadow_size, shadow_size, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
     
shadow_fbo = GL30.glGenFramebuffers();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, shadow_fbo);
GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, shadow_map, 0);
GL11.glDrawBuffer(GL11.GL_FALSE);
GL11.glReadBuffer(GL11.GL_FALSE);
int status = GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER);


I thought that the problem is in line 7. So I tried to pass an empty ByteBuffer, but that did not work.

PS: Sorry for my bad English.
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!