Show Posts

Pages: [1]

2

Java Game APIs & Engines / OpenGL Development / Re: Combining meshes opengl and libgdx

on: 20120523 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); } 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.



7

Java Game APIs & Engines / OpenGL Development / Combining meshes opengl and libgdx

on: 20120521 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.javagaming.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: 20120520 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: 20120520 22:12:42

Doubledouble 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: 20120520 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: 20120520 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: 20120516 04:59:12

This chunk of code is my suspect. 1
 float ff = (((ulight1.w+ulight2.w+ulight3.w+ulight4.w)1))/2; 
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: 20120515 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; 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; vec3 final = vec3(ambient+ff); return lw1+lw2+lw3+lw4+final; } 
Here are some screenshots. One Light: Two Lights:



14

Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube

on: 20120507 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; 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;
void main() { 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.



16

Java Game APIs & Engines / OpenGL Development / Re: Lighting a cube

on: 20120504 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: 20120503 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; 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 = lightposf_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: 20120503 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; 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(lightposvec3(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: 20120503 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?



21

Java Game APIs & Engines / OpenGL Development / Re: Lighting in LWJGL and opengl 2

on: 20120428 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: 20120427 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; varying vec3 f_coord; void main(void) { float diffuse; vec4 color; vec3 lightpos = (v*vec4(0.4,0.8,0.4,1.0)).xyz; 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: 20120427 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: 20120426 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: 20120425 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; varying vec3 f_normal; uniform mat4 m,v; uniform mat3 nm; varying vec3 f_coord; void main(void) { float intensity; vec4 color; vec3 n = normalize(nm*f_normal); 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: 20120425 14:14:03

I just made my own function to extract those numbers using the LWJGL library...
If you use the builtin 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: 20120424 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?





javagaming.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

