Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-24 05:06:37
I seem to have fixed it. By rendering the tiles as meshbatches the rendering seems to work. Can anyone guess how this works?
2  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-23 23:58:59
Would it help if I gave Renderer.render(Mesh m)?
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  
public void render(Mesh m) {
      Matrix3 nm = makeNormalMatrix(model);
      model.idt();
      model.rotate(new Vector3(1.0f,0.0f,0.0f), xrot);
      model.rotate(new Vector3(0.0f,1.0f,0.0f), yrot);
      model.translate(new Vector3(x, y, z));
      shader.begin();
      shader.setUniformMatrix("u_pv", Camera.getInstance().get());
      shader.setUniformMatrix("u_m", model);
      shader.setUniformMatrix("u_nm", nm, true);
      float l1[],l2[],l3[],l4[];
      if(cache == null) {
         l1 = LightManager.getInstance().updateAndGetLights(0);
         l2 = LightManager.getInstance().getLights(1);
         l3 = LightManager.getInstance().getLights(2);
         l4 = LightManager.getInstance().getLights(3);
      }
      else {
         l1 = cache.asFloat(0);
         l2 = cache.asFloat(1);
         l3 = cache.asFloat(2);
         l4 = cache.asFloat(3);
      }
      //System.out.println(l1[0]+" "+l1[1]+" "+l1[2]+" "+l1[3]);
     shader.setUniformf("ulight[0]", l1[0],l1[1],l1[2],l1[3]);
      shader.setUniformf("ulight[1]", l2[0],l2[1],l2[2],l2[3]);
      shader.setUniformf("ulight[2]", l3[0],l3[1],l3[2],l3[3]);
      shader.setUniformf("ulight[3]", l4[0],l4[1],l4[2],l4[3]);
      m.render(shader,GL10.GL_TRIANGLES);
      shader.end();
   }

Basicly it binds the shader, sets a few attributes and uniforms, and renders it. LightCache is a cached version of the 4 closest lights.
3  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-23 14:20:00
The vertices look good. After a bit of debugging I got this: http://www.java-gaming.org/?action=pastebin&id=97. These are the values sent into the mesh. Normals appear to be good. Can anyone see anything wrong with these? The order is 3 floats for vertices, 1 for color, and 3 more for normals. The pastebin might have cut off some of the data.
4  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-22 02:30:58
Ok, different question. Is there any way that the amount of lighting on one object can effect the amount on another? If so how, and how could my a combined mesh be causing it?
5  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-21 20:34:28
Thanks, I do prototyping first and cleanup afterwards. I like to know how to do something before I make it pretty.
6  Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx on: 2012-05-21 20:07:45
Can I have some help? I really need to fix this. When I disable the combined mesh altogether the lighting looks like it does without the combined mesh.
7  Java Game APIs & Engines / OpenGL Development / Combining meshes opengl and libgdx on: 2012-05-21 00:37:56
I am having some problems with lighting when combining meshes. I am creating what I call a mesh batch and creating a mesh from vertices and generated indices. My problem is that the normals seem to be wrong. Here is my mesh batch code: http://www.java-gaming.org/?action=pastebin&id=92. Vertices are passed into addmesh in the same order as the generated mesh, namely three coords, the color for that vertex, and then the normals. Why am I getting this result with my lighting? Here are some screenshots illustrating my problem:

With combined:


Without combined:

8  Java Game APIs & Engines / OpenGL Development / Re: Optimizing lighting opengl on: 2012-05-20 22:17:41
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  
#version 120
const int LIGHT_COUNT = 4;
uniform vec4 ulight[LIGHT_COUNT];

varying vec4 v_color;
varying vec3 v_position;
varying vec3 v_normal;


vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.6,0.6,0.6);

vec3 lambert() {
   vec3 nn = normalize(v_normal);
        vec3 dtl[LIGHT_COUNT];
        float disSquare[LIGHT_COUNT];
        float d[LIGHT_COUNT];
        vec3 lw[LIGHT_COUNT];
       
        for(int i=0; i<LIGHT_COUNT; ++i)
         dtl[i] = ulight[i].xyz-(v_position);

        for(int i=0; i<LIGHT_COUNT; ++i)
         disSquare[i] = dtl[i].x*dtl[i].x + dtl[i].y*dtl[i].y + dtl[i].z*dtl[i].z;

        for(int i=0; i<LIGHT_COUNT; ++i)
         d[i] = max(dot(nn,normalize(dtl[i])),0.0);

        for(int i=0; i<LIGHT_COUNT; ++i)
         lw[i] =(lightcolor*d[i] + ambient)*ulight[i].w / (1.0 + disSquare[i]);

        vec3 result = lw[0];
        for(int i=1; i<LIGHT_COUNT; ++i)
              result += lw[i];

   return result;
}

Again, would this be faster than static code?
9  Java Game APIs & Engines / OpenGL Development / Re: Optimizing lighting opengl on: 2012-05-20 22:12:42
Double-double also: you're going to be completely vector op bound (obviously) while the texture unit sits idle. It may be worth moving some of your computation into a texture lookup. Possibly the light attenuation calculation?
How would I do this? I am not using textures in my program.
10  Java Game APIs & Engines / OpenGL Development / Re: Optimizing lighting opengl on: 2012-05-20 22:04:23
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  
#version 120
uniform vec4 ulight[4];

varying vec4 v_color;
varying vec3 v_position;
varying vec3 v_normal;


vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.6,0.6,0.6);

vec3 phong() {
   vec3 nn = normalize(v_normal);
        vec3 dtl[4];
        float dis[4];

for(
   vec3 dtl1 = ulight1.xyz-(v_position);
   vec3 dtl2 = ulight2.xyz-(v_position);
   vec3 dtl3 = ulight3.xyz-(v_position);
   vec3 dtl4 = ulight4.xyz-(v_position);
   float dis1 = length(dtl1);
   float dis2 = length(dtl2);
   float dis3 = length(dtl3);
   float dis4 = length(dtl4);
   float att1 = (1.0 / (1.0 + (dis1 * dis1)));
   float att2 = (1.0 / (1.0 + (dis2 * dis2)));
   float att3 = (1.0 / (1.0 + (dis3 * dis3)));
   float att4 = (1.0 / (1.0 + (dis4 * dis4)));
   float d1 = max(dot(nn,normalize(dtl1)),0.0);
   float d2 = max(dot(nn,normalize(dtl2)),0.0);
   float d3 = max(dot(nn,normalize(dtl3)),0.0);
   float d4 = max(dot(nn,normalize(dtl4)),0.0);
   vec3 lw1 = att1*(ambient+lightcolor*d1)*ulight1.w;
   vec3 lw2 = att2*(ambient+lightcolor*d2)*ulight2.w;
   vec3 lw3 = att3*(ambient+lightcolor*d3)*ulight3.w;
   vec3 lw4 = att4*(ambient+lightcolor*d4)*ulight4.w;
   return lw1+lw2+lw3+lw4;
}

Are for loops faster than static code? I thought that static code was fastest in shaders.
11  Java Game APIs & Engines / OpenGL Development / Optimizing lighting opengl on: 2012-05-20 21:20:24
I am trying to figure out how to optimize lighting in opengl 2. I am using 4 lights per object, with the lights sorted by distance. It works well with just one light, but with any more than that It starts to slow down. I would like a way to speed up my lighting while getting similar visual results. Here is my lighting 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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
#version 120
uniform vec4 ulight1;
uniform vec4 ulight2;
uniform vec4 ulight3;
uniform vec4 ulight4;

varying vec4 v_color;
varying vec3 v_position;
varying vec3 v_normal;


vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.6,0.6,0.6);

vec3 phong() {
   vec3 nn = normalize(v_normal);
   vec3 dtl1 = ulight1.xyz-(v_position);
   vec3 dtl2 = ulight2.xyz-(v_position);
   vec3 dtl3 = ulight3.xyz-(v_position);
   vec3 dtl4 = ulight4.xyz-(v_position);
   float dis1 = length(dtl1);
   float dis2 = length(dtl2);
   float dis3 = length(dtl3);
   float dis4 = length(dtl4);
   float att1 = (1.0 / (1.0 + (dis1 * dis1)));
   float att2 = (1.0 / (1.0 + (dis2 * dis2)));
   float att3 = (1.0 / (1.0 + (dis3 * dis3)));
   float att4 = (1.0 / (1.0 + (dis4 * dis4)));
   float d1 = max(dot(nn,normalize(dtl1)),0.0);
   float d2 = max(dot(nn,normalize(dtl2)),0.0);
   float d3 = max(dot(nn,normalize(dtl3)),0.0);
   float d4 = max(dot(nn,normalize(dtl4)),0.0);
   vec3 lw1 = att1*(ambient+lightcolor*d1)*ulight1.w;
   vec3 lw2 = att2*(ambient+lightcolor*d2)*ulight2.w;
   vec3 lw3 = att3*(ambient+lightcolor*d3)*ulight3.w;
   vec3 lw4 = att4*(ambient+lightcolor*d4)*ulight4.w;
   return lw1+lw2+lw3+lw4;
}


This function is called once per pixel in the fragment shader. The width of each light tells it whether or not that light is enabled.
12  Java Game APIs & Engines / OpenGL Development / Re: Dark lighting with multiple lights on: 2012-05-16 04:59:12
This chunk of code is my suspect.
1  
float ff = (-((ulight1.w+ulight2.w+ulight3.w+ulight4.w)-1))/2;// Increase ambient when no light

If ulight1.w + ulight2.w + ulight3.w + ulight4.w > 1, then ff's result will be a negative number, rather than a positive. Check these values if you can.

Oops. Removing that fixed it. That code was old and unnecessary anyway.
13  Java Game APIs & Engines / OpenGL Development / Dark lighting with multiple lights on: 2012-05-15 15:07:25
I am working on using multiple lights. When I add more than one light the dark parts of the lighting become darker. What am I doing wrong? Here is the important part of my 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  
#version 120
uniform vec4 ulight1;
uniform vec4 ulight2;
uniform vec4 ulight3;
uniform vec4 ulight4;

varying vec4 v_color;
varying vec3 v_position;
varying vec3 v_normal;


vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.6,0.6,0.6);

vec3 phong() {
   vec3 nn = normalize(v_normal);
   vec3 dtl1 = ulight1.xyz-(v_position);
   vec3 dtl2 = ulight2.xyz-(v_position);
   vec3 dtl3 = ulight3.xyz-(v_position);
   vec3 dtl4 = ulight4.xyz-(v_position);
   float dis1 = length(dtl1);
   float dis2 = length(dtl2);
   float dis3 = length(dtl3);
   float dis4 = length(dtl4);
   float att1 = (1.0 / (1.0 + (dis1 * dis1)));
   float att2 = (1.0 / (1.0 + (dis2 * dis2)));
   float att3 = (1.0 / (1.0 + (dis3 * dis3)));
   float att4 = (1.0 / (1.0 + (dis4 * dis4)));
   float d1 = max(dot(nn,normalize(dtl1)),0.0);
   float d2 = max(dot(nn,normalize(dtl2)),0.0);
   float d3 = max(dot(nn,normalize(dtl3)),0.0);
   float d4 = max(dot(nn,normalize(dtl4)),0.0);
   vec3 lw1 = att1*(ambient+lightcolor*d1)*ulight1.w; // w of 0 = no light
  vec3 lw2 = att2*(ambient+lightcolor*d2)*ulight2.w;
   vec3 lw3 = att3*(ambient+lightcolor*d3)*ulight3.w;
   vec3 lw4 = att4*(ambient+lightcolor*d4)*ulight4.w;
   float ff = (-((ulight1.w+ulight2.w+ulight3.w+ulight4.w)-1))/2;// Increase ambient when no light
  vec3 final = vec3(ambient+ff);
   return lw1+lw2+lw3+lw4+final; //add together lights
}

Here are some screenshots.

One Light:


Two Lights:
14  Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube on: 2012-05-07 20:03:24
If you want others to benefit, post the source of the whole thing...
Thing is, I don't know how I fixed it. I have been trying to recreate the whole thing in libgdx and I am running into the same problem. Anyway, here are the shaders that worked:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
uniform vec3 lightpos;
vec3 ambient = vec3(0.4,0.4,0.4);
vec3 lightcolor = vec3(0.8,0.8,0.8);
void main(void) {
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = lightpos-(f_coord);
  float distance = length(dirtolight);
  float diffuse = max(dot(normnorm,normalize(dirtolight)),0.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
attribute vec3 coord;
attribute vec3 normal;
attribute vec3 color;
uniform mat4 matrix;
uniform mat4 m,v;
uniform mat3 nm;
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
//varying float diffuse;
void main() {
   //vec3 lightpos = vec3(0.4,0.8,0.4);
  f_color = color;
   f_coord = (vec3(m*vec4(coord,1.0)));
   f_normal = normalize(nm*normal);
   gl_Position = matrix * vec4(coord,1.0);
}


Oh... Figured it out. When the light gets more a certain distance above an object it begins to look like per vertex lighting.
15  Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube on: 2012-05-04 22:57:38
Nevermind, I've fixed it.
16  Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube on: 2012-05-04 13:05:08
Normalizing f_coord in the vertex shader seems to help. When I do this the light seems to become static during model rotations. Is this the problem with the static light position, or is this something I should not be doing?
17  Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube on: 2012-05-03 18:22:37
Ok so I've moved matrix math into the vertex shader. I am still getting the same result though. The light is still flat across each face. Here is my fragment shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
vec3 ambient = vec3(0.2,0.2,0.2);
vec3 lightcolor = vec3(0.8,0.8,0.8);
void main(void) {
  vec3 lightpos = vec3(0.4,0.8,0.4);
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = lightpos-f_coord.xyz;
  float distance = length(dirtolight);
  float diffuse = clamp(dot(normnorm,normalize(dirtolight)),0.0,1.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}

I'll work on light position as the last steps. At the moment it does not appear to move with the camera.
18  Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube on: 2012-05-03 17:41:39
Here is my fragment shader, it does not seem like I am doing anything wrong. I am using a static light position since I am just learning.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
varying vec3 f_coord;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
vec3 ambient = vec3(0.4,0.4,0.4);
vec3 lightcolor = vec3(1.0,1.0,1.0);
void main(void) {
  vec3 lightpos = vec3(0.4,0.8,0.4);
  vec4 coord = vec4(m*vec4(f_coord,1.0));
  vec3 normnorm = normalize(f_normal);
  vec3 dirtolight = normalize(lightpos-vec3(coord));
  float distance = length(dirtolight);
  float diffuse = clamp(dot(normnorm,dirtolight),0.0,1.0);
  vec3 lightweight = (ambient+lightcolor*diffuse);
  vec4 color = vec4(f_color*lightweight,1.0);
  gl_FragColor = color;
}
19  Java Game APIs & Engines / OpenGL Development / Lighting a cube on: 2012-05-03 17:13:51
I am trying to learn opengl 2 lighting. I have basic per pixel lighting working, but when I try light a cube, each face is lit separately. Each faces color remains static all over the face, which is not how I want it to look. What is a way fix this?
20  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-28 21:41:40
I think I might have figured out a problem. I was sending the objects vertices instead of normals. It was a very stupid mistake.
21  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-28 01:27:14
Could you show me a version of inversesqrt that works in glsl 120? According to the reference this inversesqrt is not available in glsl 120. I am not using opengl 3, so I can not use glsl 130. Also, would this fix the problem with the left movement?

One more thing. I just noticed that the light does not seem to shift positions on the model. It always seems to light the same portion of the model. If I translate it to the right, the left side should get lighted, but instead it appears to simply fade away to nothing. When I translate to the left, the light covers more and more of the model, until all but the very left of the model is white. Is this because I missed a step in making the light a point light? If not then what could cause something like this to happen?
22  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-27 21:54:42
I am using a static varable for the light position since I'm just trying to learn the theory. I have cleaned up my variable names a bit, and have multiplied the view matrix by the light position. I am still having the problem with the left right light intensity, and now it also gets more intense the farther back I move from it. Here is my current shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
uniform mat4 m,v;
uniform mat3 nm; //normal matrix
varying vec3 f_coord;
void main(void) {
  float diffuse;
  vec4 color;
  vec3 lightpos = (v*vec4(0.4,0.8,0.4,1.0)).xyz; // using static light position for debugging
 vec3 n = normalize(f_normal);
  vec3 newpos = lightpos-(m*vec4(f_coord,1.0)).xyz;
  float dis = length(newpos);
  vec3 direction = normalize(newpos/dis);
  diffuse = max(dot(direction,n), 0.0f);
  color = vec4(f_color+diffuse,1.0);
  gl_FragColor = color;
}
23  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-27 01:36:43
Now it seems to work, but when I translate the model to the left the lighting gets brighter. When I translate the model to the left the lighting disappears. Here is my frag shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
#version 120
varying vec3 f_color;
varying vec3 f_normal;
uniform mat4 m,v;
uniform mat3 nm;
varying vec3 f_coord;
void main(void) {
  float diffuse;
  vec4 color;
  vec3 n = normalize(f_normal);
  vec3 udir = vec3(0.4,0.8,0.4)-(m*vec4(f_coord,1.0)).xyz;
  float dis = length(udir);
  vec3 direction = normalize(udir/dis);
  diffuse = max(dot(direction,n), 0.0f);
  color = vec4(f_color+diffuse,1.0);
  gl_FragColor = color;
}
24  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-26 03:35:28
The normal rotation should be done in the vertex shader. Your fragment shader should just normalize the interpolated normal. Also make sure your uniforms and attributes are working correctly.
Oops... I was using glUniformMatrix4 instead of glUniformMatrix3. Thanks for the help.
25  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-25 20:47:35
I am running into another few problems. First when I multiply my normal matrix(gotten by the method a couple posts back) by the current point and normalize it the light does nothing. I am using separate model view and projection matrices, and I am using the model matrix as the base for the normal matrix. Am I that the wrong matrix, or should I be multiplying the model and view matrices before inverting and transposing. My other problem is that when I multiply any of the matrices by the vector coord the whole thing disappears. Here is my current fragment shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
#version 120
varying vec3 f_color; //color from vertex shader
varying vec3 f_normal; //normal
uniform mat4 m,v; // These are just here if necessary I prefer do my matrix multiplication on the cpu.
uniform mat3 nm; // normal matrix
varying vec3 f_coord;
void main(void) {
  float intensity;
  vec4 color;
  vec3 n = normalize(nm*f_normal); //nm = first 3x3 elems of model matrix.invert().transpose(). These are the matrices from org.lwjgl.util
 vec3 pos = normalize(vec3(0.4,0.8,0.4)-(f_coord));
  intensity = max(dot(pos,n),0.0f);
  color = vec4(f_color+intensity,1.0);
  gl_FragColor = color;
}
26  Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2 on: 2012-04-25 14:14:03
I just made my own function to extract those numbers using the LWJGL library...

If you use the built-in matrices in OpenGL you can just use gl_NormalMatrix in your shader, but if you want to only use your own attributes/uniforms you´ll have to calculate it yourself the way you wrote.

Diffuse lighting is very simple. You pretty much just calculate the cosine of the angle between the normal and the light direction vector and that´s it. This should be done in eye space though, so the normals have to be rotated which is what multiplying them with the normal matrix does. There are basically 3 different kind of lights:

 - Direction lights. Example: the sun. This is the easiest one, just give upload the light direction vector to your shader, do a dot product and you´re done.

 - Point lights. Example: a light bulb. This one is slighty more difficult. You upload the light eye space position, calculate a light direction vector per pixel (normalize(pixelEyePosition - lightPosition), or was it the other way around? xD) and use that instead of a constant direction as with directional lighting.

 - Spot light: Example: a flash light This one uses 3 variables: A position, a direction and an angle stored as the cosine of that angle. This one is obviously the most difficult one, but not by much. It´s pretty much a fusion of the math done by direction and point lights. First we calculate lighting just as we did for point lights, but we also need to take the direction of the point light and the angle of the light one into account. This is done by dot producting the calculated light direction to the pixel and the direction of the spot light. Together with the cosine of the light cone (supplied from the program to the shader) we can limit the light to a cone in front of the spot light.

Another thing you can add to point and spot lights are distance fall off. We can use the calculated unnormalized light direction vector before it´s normalized to do this. Real life lights never disappear completely with distance; you wouldn´t be able to see a lot of things if light didn´t travel infinitely. The most accurate way is therefore to just divide the calculated intensity by (distance^2) which is what happens in real life. However, it´s pretty impractical for lights to reach infinitely in games since a point light, no matter how weak, will always affect everything in the world. Therefore most lighting engines use a fall off equation that does end up at zero at a certain distance.

Oh, I forgot, of course all 3 light types have an intensity value. If you use fall off, this value can be a lot higher than 1 even though you can´t store color values over that in the standard framebuffer. Consider a pixel 4 meters away from a point light. The pixel is perfectly facing the light so the angle between the pixel´s normal and the calculated light direction vector equals 0. The cosine value of 0 degrees is 1.0, and the pixel´s texture value is multiplied by this value. However, the pixel is 4 meters away from the light! With a fall off of distance^2, we end up with an intensity of cosine(0) / (4^2) = 1 / 16. in this case, it makes perfect sense having a light with an intensity much higher than 1.

This is pretty much the basics of the math behind lights. I don´t have a working computer, so I can´t give you much code, but the tutorials are out there everywhere. If you run into any problems with them, just ask. =D This is literally just scratching the surface of lighting though. There´s specular lighting, shadow mapping, different light equations to give the look of different materials, HDR rendering, tone mapping, bloom, deferred shading, ambient occlusion, global illumination, volumetric lighting, reflections... I mean, you can stay busy for a life time.

Does javax.vecmath include a function to get the top 3x3 elements of a matrix? I do not know how to do this myself.
27  Java Game APIs & Engines / OpenGL Development / Lighting in LWJGL and opengl 2 on: 2012-04-24 20:10:01
I am trying to figure out lighting in Opengl 2 with lwjgl. I have two problems. The first is a lack of information. I do not understand how lighting works. Could someone point out a good some information on how lighting is done? My other problem is that I am using lwjgl's built in Matrix classes. The tutorials I have found use a matrix called a normal matrix (or the Opengl 2 equivalent) to get the normal direction. In my understanding the Opengl 2 equivalent of the normal matrix it the same as the transposed inverse of the top 3x3 elements of the model matrix. The functions to do this do not exist in lwjgl's Matrix class, and I do not know the math behind them. Do I really need the normal matrix, and if I do how do I get it?
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
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!