Jump to content
  • Advertisement
Sign in to follow this  
AnujKalia

Shadow mapping: Objects are textured

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using a shadow mapping code from Paul's Projects. It works fine for untextured objects but not for textured objects:


#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <gl/glew.h>
#include <gl/glfw.h>
#include <gl/GLU.h>
#include "Maths/Maths.h"

GLuint textureID;
VECTOR3D cameraPosition(-2.5f, 3.5f,-2.5f); //Camera & light positions
VECTOR3D lightPosition(2.0f, 3.0f,-2.0f);
GLUquadricObj *quadric;

const int shadowMapSize=600; //Size of shadow map
GLuint shadowMapTexture; //Textures
int windowWidth, windowHeight; //window size

MATRIX4X4 lightProjectionMatrix, lightViewMatrix; //Matrices
MATRIX4X4 cameraProjectionMatrix, cameraViewMatrix;

void DrawScene(float angle)
{
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D,textureID);
glPushMatrix();
glColor3f(.8,.8,.8);
gluSphere(quadric,.5,50,50);
glTranslatef(-1,-.6,.4);
gluSphere(quadric,.5,50,50);
glPopMatrix();

glPushMatrix();
glBegin(GL_POLYGON);
glTexCoord2f(.5,.5);
glVertex2f(.5,.5);
glTexCoord2f(.5,-.5);
glVertex2f(.5,-.5);
glTexCoord2f(-.5,-.5);
glVertex2f(-.5,-.5);
glTexCoord2f(-.5,.5);
glVertex2f(-.5,.5);
glEnd();
glPopMatrix();
}

bool Init(void) //Called for initiation
{
glMatrixMode(GL_MODELVIEW); //Load identity modelview
glLoadIdentity();

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

//Depth states
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);

glEnable(GL_CULL_FACE);

//We use glScale when drawing the scene
glEnable(GL_NORMALIZE);

//Create the shadow map texture
glGenTextures(1, &shadowMapTexture);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapSize, shadowMapSize, 0,GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

//Use the color as the ambient and diffuse material
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

//White specular material color, shininess 16
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 16.0f);

//Calculate & save matrices
glPushMatrix();

glLoadIdentity();
gluPerspective(45.0f, (float)windowWidth/windowHeight, 1.0f, 100.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, cameraProjectionMatrix);

glLoadIdentity();
gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, cameraViewMatrix);

glLoadIdentity();
gluPerspective(45.0f, 1.0f, 2.0f, 8.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, lightProjectionMatrix);

glLoadIdentity();
gluLookAt(lightPosition.x, lightPosition.y, lightPosition.z,0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);

glPopMatrix();

glGenTextures(1,&textureID); // Generate and bind our texture ID
printf("\n\ntextureID=%d\n",textureID);
glBindTexture(GL_TEXTURE_2D,textureID);
printf("check\n%d\n",glfwLoadTexture2D("ranger.tga",GLFW_BUILD_MIPMAPS_BIT|GLFW_ORIGIN_UL_BIT));
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR ); // Use trilinear interpolation (GL_LINEAR_MIPMAP_LINEAR)
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );

return true;
}

//Called to draw scene
void Display(void)
{
printf("here");
//First pass - from light's point of view
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightViewMatrix);

glViewport(0, 0, shadowMapSize, shadowMapSize); //Use viewport the same size as the shadow map
glCullFace(GL_FRONT); //Draw back faces into the shadow map
glShadeModel(GL_FLAT); //Disable color writes, and use flat shading for speed
glColorMask(0, 0, 0, 0);

DrawScene(0); //Draw the scene

glBindTexture(GL_TEXTURE_2D, shadowMapTexture); //Read the depth buffer into the shadow map texture
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);

//restore states
glCullFace(GL_BACK);
glShadeModel(GL_SMOOTH);
glColorMask(1, 1, 1, 1);


//2nd pass - Draw from camera's point of view
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(cameraProjectionMatrix);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(cameraViewMatrix);

glViewport(0, 0, windowWidth, windowHeight);

glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(lightPosition)); //Use dim light to represent shadowed areas
glLightfv(GL_LIGHT1, GL_AMBIENT, white*0.2f);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white*0.2f);
glLightfv(GL_LIGHT1, GL_SPECULAR, black);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);

DrawScene(0);

//3rd pass
glLightfv(GL_LIGHT1, GL_DIFFUSE, white); //Draw with bright light
glLightfv(GL_LIGHT1, GL_SPECULAR, white);

//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]
MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix;

//Set up texture coordinate generation.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
glEnable(GL_TEXTURE_GEN_S);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
glEnable(GL_TEXTURE_GEN_T);

glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
glEnable(GL_TEXTURE_GEN_R);

glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
glEnable(GL_TEXTURE_GEN_Q);

//Bind & enable shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glEnable(GL_TEXTURE_2D);

//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);

//Set alpha test to discard false comparisons
glAlphaFunc(GL_GEQUAL, 0.99f);
glEnable(GL_ALPHA_TEST);

DrawScene(0);

//Disable textures and texgen
glDisable(GL_TEXTURE_2D);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);

//Restore other states
glDisable(GL_LIGHTING);
glDisable(GL_ALPHA_TEST);

glfwSwapBuffers();
}

//Called on window resize
void Reshape(int w, int h)
{
windowWidth=w, windowHeight=h;
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f, (float)windowWidth/windowHeight, 1.0f, 100.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, cameraProjectionMatrix);
glPopMatrix();
}

int main(int argc, char** argv)
{
glfwInit(); //start using glfw library
glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
glfwOpenWindow(400,400,0,0,0,0,0,0,GLFW_WINDOW );
glfwSetWindowTitle( "anuj" );
Reshape(400,400);
quadric = gluNewQuadric();
gluQuadricTexture(quadric,GL_TRUE);

Init();
glewInit();
while(1)
Display();
return 0;
}


I think the problem starts here when the program binds "shadowMapTexture"

glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glEnable(GL_TEXTURE_2D);


However, the first line of my drawScene function binds it to a different texture:

glBindTexture(GL_TEXTURE_2D,textureID);


"textureID" is the texture I want to appear on my spheres.

So, I can't use shadowMapTexture? Is there an alternative?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!