I was messing with the dpeth test thingy in simple game "F3" and noticed that I can be pretty close to something and get no depth value. Is something wrong with jme's depth range maybe?
Also i am tring to implament a shadow mapping with out shaders and i was wondering if i did something like the following, would it work?
Render from lights point of view filling z buffer state with depthe values thn when I render from the camera's point of view I enable the comparison and
depth funtion being if A(lights pov) > B(cameras pov) it's in shadows ,then I use stencil on zpass or zfail which ever one is the right one to tag it and not light that pixel. Only probly is that A is in light clip space and B is in camera eye space. Any ideas on fixing that one problem?
Figuered it out I think ya i’m fast. Ijust found a site doing something i sorta wanna do.My idea is 3 passes and instead of using the alpha(see below)I use stencil.
I need somebody to hold my hand.
This much I found from http://www.paulsprojects.net/tutorials/smt/smt.html
For the first pass, we draw the scene from the light’s point of view. Clear the color and depth buffers and set the matrices to those for the light. Use a viewport of the same size as the shadow map.
//First pass - from light's point of view
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Use viewport the same size as the shadow map
glViewport(0, 0, shadowMapSize, shadowMapSize);
Here we instruct OpenGL to cull front faces, so the back faces are drawn into the shadow map. This deals with the issue of finite precision as explained above. We also disable color writes and use flat shading, since we are only interested in the contents of the depth buffer.
//Draw back faces into the shadow map
//Disable color writes, and use flat shading for speed
glColorMask(0, 0, 0, 0);
We are now ready to draw the scene.
CopyTexSubImage2D is used to copy the contents of the frame buffer into a texture. First we bind the shadow map texture, then copy the viewport into the texture. Since we have bound a DEPTH_COMPONENT texture, the data read will automatically come from the depth buffer.
//Read the depth buffer into the shadow map texture
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);
Finally for this pass, restore the states we have changed.
glColorMask(1, 1, 1, 1);
In the second pass, we draw the scene from the camera's point of view, with the light set to the brightness of the shadowed areas. Firstly, clear the depth buffer. There is no need to clear the color buffer since it has not yet been written to. Then, set up the matrices to draw from the camera's point of view, and use a viewport which covers the whole window.
//2nd pass - Draw from camera's point of view
glViewport(0, 0, windowWidth, windowHeight);
The light is set up as necessary for the unshadowed regions. In particular, we use a dim diffuse brightness and a zero specular brightness.
//Use dim light to represent shadowed areas
glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(lightPosition));
glLightfv(GL_LIGHT1, GL_AMBIENT, white*0.2f);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white*0.2f);
glLightfv(GL_LIGHT1, GL_SPECULAR, black);
The third pass is where the actual shadow calculations take place. If a fragment passes the shadow test (i.e. is unshadowed) then we want it to be lit brightly, overwriting the dim pixel from the previous pass. So, enable a bright light, with full specular brightness.
//Draw with bright light
glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
glLightfv(GL_LIGHT1, GL_SPECULAR, white);
Here, we calculate the texgen matrix which we will use for projecting the shadow map onto the scene and enable texture coordinate generation, as described above.
//Calculate texture matrix for projection
//This matrix takes us from eye space to the light's clip space
//It is postmultiplied by the inverse of the current view matrix when specifying texgen
static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
//Set up texture coordinate generation.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
Now we bind and enable the shadow map texture, and set up the automatic shadow comparison. First we enable the comparison, then tell the GL to generate a "true" result if r is less than or equal to the value stored in the texture. The shadow comparison produces either a 0 or 1 per fragment for a result. We instruct the GL to replicate this to all 4 color channels, i.e. to generate an intensity result.
//Bind & enable shadow map texture
//Enable shadow comparison
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
//Shadow comparison should be true (ie not in shadow) if r<=texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
//Shadow comparison should generate an INTENSITY result
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
If the shadow comparison passes, an alpha value of 1 will be generated. So, we use the alpha test to discard all fragments with alpha less than 0.99. This way, fragments which fail the shadow test will not be displayed, so allowing the darker result from the previous pass to show.
//Set alpha test to discard false comparisons
The scene is then drawn for the third and final time, then any changed states are reset.
//Disable textures and texgen
//Restore other states
You should be able to do much or all of what you're saying by extedning Pass as done in ShadowedRenderPass.
oh alrite, only question is all the calls in this are low level(direct opengl calls) do i need to also make states?
I also ran into another problem when I do LightState.get( i ); it returns Light(the super class) how am i suppose to get location and position from that?
I’ve put together a group of states I’ll need,no worries I plan to write it. States are suppose to give you some access to the lower API right? Seems there are a few missing mccoder posted a plan clipping one, i’ll soon post a
MatrixMode one,ShadeModel, and TextureGen to gain access to
If there is already a way to get these tell me before i get to in over my head.
shade state already in, I think because under attributestate is says " Constructor instantiates a new ShadeState object with the default mode being smooth."
got first pass done. Marry Chirstmas!
Marry Chirstmas to you too!