Hello guys,
Im working at an 3d strategy game at the moment, where i would like to add shadows.
Im trying to implement this for the third time now, but i keep getting nothing.
Could anyone please help me?
I already have tryed to create some kind of buffer texture:
public FrameBufferObject3D(int texturew, int textureh){
width = texturew;
height = textureh;
FBOId = glGenFramebuffers();
biasMatrix.load(bM);
createTexture();
glBindFramebuffer(GL_FRAMEBUFFER, FBOId);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textId, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
int framebuffer = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch ( framebuffer ) {
case GL_FRAMEBUFFER_COMPLETE:
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
throw new RuntimeException( "FrameBuffer: " + FBOId
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception" );
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
throw new RuntimeException( "FrameBuffer: " + FBOId
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception" );
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
throw new RuntimeException( "FrameBuffer: " + FBOId
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception" );
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
throw new RuntimeException( "FrameBuffer: " + FBOId
+ ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception" );
default:
throw new RuntimeException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer );
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
private void createTexture(){
textId = TextureLoader.createTextureID();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (ByteBuffer)null);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
public void Bind(){
if(updatelight){
glLoadIdentity();
glPushMatrix();
Vector3f l = (Vector3f)new Vector3f(Panel.getLightPos()).negate();
glLoadIdentity();
glOrtho(-10,10,-10,10,-10,20);
lightbuffer.position(0);
glGetFloat(GL_PROJECTION_MATRIX, lightbuffer);
projectionMatrix.load(lightbuffer);
GLU.gluLookAt(l.x, l.y, l.z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
lightbuffer.position(0);
glGetFloat(GL_MODELVIEW_MATRIX, lightbuffer);
lightMatrix.load(lightbuffer);
shadowMatrix.load(Panel.getProjection());
shadowMatrix.mul(lightMatrix);
biasshadowMatrix.load(Panel.getProjection());
biasshadowMatrix.mul(lightMatrix);
glPopMatrix();
updatelight = false;
}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBOId);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport( 0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
Im sure this is a mess, as i understand only little about it.
My problems are starting with generating the correct matrices:
shadowMatrix.load(Panel.getProjection());
shadowMatrix.mul(lightMatrix);
biasshadowMatrix.load(Panel.getProjection());
biasshadowMatrix.mul(lightMatrix);
I need one matrix to transform the models to the light view (a vec3 with position).
Second, i would need a matrix to map the buffer on the model.
Im sorry for the mess, thanks in advance.