LWJGL Forum

Programming => OpenGL => Topic started by: poo2thegeek on June 30, 2014, 15:14:13

Title: Shader based shadow mapping
Post by: poo2thegeek on June 30, 2014, 15:14:13
I am trying to dynamically draw shadows on a scene I have created.
At current I am using code that I have copied directly from a C++ shadow drawing program into LWJGL. http://www.fabiensanglard.net/shadowmapping/ (http://www.fabiensanglard.net/shadowmapping/)

However, it does not seemed to have worked.
I create the program by creating a new instance of "ShadowProjection2":
WIDTH = 1024;
HEIGHT = 1024;

//GLfloat borderColor[4] = {0,0,0,0};

// Try to use a texture depth component
textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);

// GL_LINEAR does not make sense for depth texture. However, next tutorial shows usage of GL_LINEAR and PCF
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Remove artefact on the edges of the shadowmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

//glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );



// No need to force GL_DEPTH_COMPONENT24, drivers usually give you the max precision if available
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, WIDTH, HEIGHT, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, (FloatBuffer)null);
glBindTexture(GL_TEXTURE_2D, 0);

// create a framebuffer object
fboID = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fboID);

// Instruct openGL that we won't bind a color texture with the currently binded FBO

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

// attach the texture to FBO depth attachment point
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, textureID, 0);
// check FBO status
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
System.err.println("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use FBO\n");

// switch back to window-system-provided framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);

shadow = AgeOfCivilisation.get().resources().getShader("shadow");
try{
shadow.load();
}catch(Exception e){
Log.getInstance().log(e);
}


to render the code I have directly copied the initial source code:
public void drawShadow(PlayerCamera camera){

glBindFramebuffer(GL_FRAMEBUFFER, fboID);
GL20.glUseProgram(0);
glViewport(0,0, WIDTH, HEIGHT);
glClear(GL_DEPTH_BUFFER_BIT);
glColorMask(false,false,false,false);
setupMatrices(p_light[0],p_light[1],p_light[2],l_light[0],l_light[1],l_light[2]);
//glCullFace(GL_FRONT);
drawObjects();
setTextureMatrix();


glBindFramebuffer(GL_FRAMEBUFFER,0);

glViewport(0,0,Display.getWidth(), Display.getHeight());

glColorMask(true,true,true,true);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

shadow.bind();
shadow.sendInt("ShadowMap", 7);
GL13.glActiveTexture(GL13.GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, textureID);

setupMatrices(p_camera[0],p_camera[1],p_camera[2],l_camera[0],l_camera[1],l_camera[2]);
//glCullFace(GL_BACK);
drawObjects();

GL20.glUseProgram(0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-Display.getWidth()/2,Display.getWidth()/2,-Display.getHeight()/2,Display.getHeight()/2,1,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(1,1,1,1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textureID);
glEnable(GL_TEXTURE_2D);
glTranslated(0,0,-1);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3f(0,0,0);
glTexCoord2d(1,0);glVertex3f(Display.getWidth()/2,0,0);
glTexCoord2d(1,1);glVertex3f(Display.getWidth()/2,Display.getHeight()/2,0);
glTexCoord2d(0,1);glVertex3f(0,Display.getHeight()/2,0);


glEnd();
glDisable(GL_TEXTURE_2D);
}

public void setTextureMatrix(){
FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
FloatBuffer projection = BufferUtils.createFloatBuffer(16);

// Moving from unit cube [-1,1] to [0,1] 
float[] bias_ = {
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};

FloatBuffer bias = (FloatBuffer) BufferUtils.createFloatBuffer(16).put(bias_).flip();

// Grab modelview and transformation matrices
glGetFloat(GL_MODELVIEW_MATRIX, modelView);
glGetFloat(GL_PROJECTION_MATRIX, projection);

//modelView.flip().limit(16);
//projection.flip().limit(16);

//modelView.flip();
//projection.flip();

glMatrixMode(GL_TEXTURE);
GL13.glActiveTexture(GL13.GL_TEXTURE7);

glLoadIdentity();
GL11.glLoadMatrix(bias);
GL11.glMultMatrix(projection);
GL11.glMultMatrix(modelView);

// Go back to normal matrix mode
glMatrixMode(GL_MODELVIEW);
}

private void setupMatrices(float position_x,float position_y,float position_z,float lookAt_x,float lookAt_y,float lookAt_z){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluPerspective(45,WIDTH/(float)HEIGHT,10,40000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLU.gluLookAt(position_x,position_y,position_z,lookAt_x,lookAt_y,lookAt_z,0,1,0);
}


private void drawObjects(){
glColor4f(0.3f,0.3f,0.3f,1);
glBegin(GL_QUADS);
glVertex3f(-35,2,-35);
glVertex3f(-35,2, 15);
glVertex3f( 15,2, 15);
glVertex3f( 15,2,-35);
glEnd();

glColor4f(0.9f,0.9f,0.9f,1);

startTranslate(0,4,-16);
drawQuad(4);
endTranslate();

startTranslate(0,4,-5);
drawQuad(4);
endTranslate();
}


private void drawQuad(float radius){
glPushMatrix();
glScalef(radius,radius,radius);
GL11.glBegin(GL11.GL_QUADS);   
        GL11.glColor3f(1.0f,1.0f,0.0f);           
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);       
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);       
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f); 
        GL11.glColor3f(1.0f,0.5f,0.0f);           
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
        GL11.glColor3f(1.0f,0.0f,0.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glColor3f(1.0f,1.0f,0.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
        GL11.glColor3f(0.0f,0.0f,1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f,-1.0f);
        GL11.glVertex3f(-1.0f,-1.0f, 1.0f);
        GL11.glColor3f(1.0f,0.0f,1.0f);
        GL11.glVertex3f( 1.0f, 1.0f,-1.0f);
        GL11.glVertex3f( 1.0f, 1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,-1.0f,-1.0f);
    GL11.glEnd();   
    glPopMatrix();
}

private void startTranslate(float x,float y,float z)
{
glPushMatrix();
glTranslatef(x,y,z);

glMatrixMode(GL_TEXTURE);
GL13.glActiveTexture(GL13.GL_TEXTURE7);
glPushMatrix();
glTranslatef(x,y,z);
}

private void endTranslate(){
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


But for some reason my shaded textureID texture is coming up black.

I assume the problem is either in constructor, the "drawShadow" method, or the "setTextureMatrix" method.

Hopefully someone will be able to fix this?

Thanks in advanced :)