Problem with spotlight on the wall

Started by
1 comment, last by ilbassa 14 years, 5 months ago
Hi everyone, I'm programming a small scenario in which there is a building and a garden. I'm trying to implement an headligh that follows the player. The problem is that each wall is created with 2 big triangle, so the light is calculated on the vertex and with the smooth shade model it is interpolated between a lighted vertex and a non lighted vertex, here there is the result: http://img94.imageshack.us/img94/5782/spotlight.png I would a spotlight more realistic so i think that i should tessellate all the walls to create sub triangles (this could render better also the other lights). I find that there is the GLUTessellation class that implements the tessellation, but seems that it only works with polygon that intersects each other or polygon inside other one... (http://www.java-tips.org/other-api-tips/jogl/polygon-tessellation-in-jogl.html) Some suggestion? Thanks
Advertisement
While tesselation is certainly one way to do it, in these days of programmable shaders per-pixel lighting is usually the way to go. Basically you write a pixel (fragment) shader where you do your lighting calculations. Then the tessellation of the underlying mesh doesn't matter as much, especially for flat surfaces such as walls.
Thanks for the reply.

I have just read some things about fragment shader, the fragment shader replaces all the code about texturing, color sum and fog, so I should have to rewrite all the code about the texturing and mix together light color and texture color.

I found this code that add some red color to a textured triangle:

String[] shader ={
"uniform sampler2D texUnit;\n" +
"fragment" in GLSL) while rasterizing
"void main(void)\n" +
"{\n" +
" vec2 texCoord = gl_TexCoord[0].xy;\n" +
" vec4 texel = texture2D(texUnit, texCoord);\n" +
" gl_FragColor = texel * vec4(1.0, 0.0, 0.0, 1.0);\n" +
"}\n"
};

The thing that looks complex is to pass the information about the light from the JOGL code to the shader and than implement the right operation... I have to manage the normals and compute the sum of the different type of light like this?

//FRAGMENT SHADER
varying vec3 N;
varying vec3 v;

void main (void)
{
vec3 L = normalize(gl_LightSource[0].position.xyz - v);
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
vec3 R = normalize(-reflect(L,N));

//calculate Ambient Term:
vec4 Iamb = gl_FrontLightProduct[0].ambient;

//calculate Diffuse Term:
vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0);
Idiff = clamp(Idiff, 0.0, 1.0);

// calculate Specular Term:
vec4 Ispec = gl_FrontLightProduct[0].specular
* pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
Ispec = clamp(Ispec, 0.0, 1.0);

// write Total Color:
gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec;
}

It uses also a vertex shader that write v and N, i can write them in the JOGL code instead of using the vertex shader??

//VERTEX SHADER
varying vec3 N;
varying vec3 v;

void main(void)
{
v = vec3(gl_ModelViewMatrix * gl_Vertex);
N = normalize(gl_NormalMatrix * gl_Normal);

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Thansk a lot!

This topic is closed to new replies.

Advertisement