Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This part of program use 2 2d array to draw a map, add a light and generated shadow.
- //All the variable i'm using are at the end
- ///////////////////////////////////////////////////////////////////////
- //Function
- ///////////////////////////////////////////////////////////////////////
- void initShadowsAndMatrices()
- {
- //Create the shadow map texture
- glGenTextures(1, &shadowMapTexture);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 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_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- //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(90.0f, (float)windowWidth/windowHeight, 1.0f, 100.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, cameraProjectionMatrix);
- glLoadIdentity();
- gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,cameraTarget.x, cameraTarget.y, cameraTarget.z,0.0f, 1.0f, 0.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, cameraViewMatrix);
- glLoadIdentity();
- gluPerspective(140.0f, ((float)windowWidth/windowHeight), 1.0f, 100.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, lightProjectionMatrix);
- glLoadIdentity();
- gluLookAt(lightPosition.x, lightPosition.y, lightPosition.z,lightPosition.x, lightPosition.y, 0,0.0f, 1.0f, 0.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
- glPopMatrix();
- }
- //This function do shadow mapping in 3 draw.
- void displayHandler()
- {
- setCamera(); //function that simply update camera position
- keyOperation(); //keypress are process in these function
- keySpecialOperation();
- //Calculate & save matrice to update light position
- glPushMatrix();
- glLoadIdentity();
- gluLookAt( lightPosition.x, lightPosition.y, lightPosition.z,lightPosition.x, lightPosition.y, 0,0.0f, 1.0f, 0.0f);
- glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix);
- glPopMatrix();
- //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);
- //Use viewport the same size as the shadow map
- glViewport(0, 0, shadowMapWidth, shadowMapHeight);
- //Draw back faces into the shadow map
- glCullFace(GL_FRONT);
- //Disable color writes, and use flat shading for speed
- glShadeModel(GL_FLAT);
- glColorMask(0, 0, 0, 0);
- //Draw the scene
- drawMap();
- //Read the depth buffer into the shadow map texture
- glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
- glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapWidth, shadowMapHeight);
- //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);
- //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);
- glEnable(GL_LIGHT1);
- glEnable(GL_LIGHTING);
- drawMap();
- //3rd pass
- //Draw with bright light
- glLightfv(GL_LIGHT1, GL_DIFFUSE, white);
- 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
- 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);
- drawMap();
- //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);
- //Update frames per second counter
- fpsCounter.Update();
- //Print fps
- static char fpsString[32];
- sprintf(fpsString, "%.2f", fpsCounter.GetFps());
- //Set matrices for ortho
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glLoadIdentity();
- //Print text
- glRasterPos2f(-1.0f, 0.9f);
- for(unsigned int i=0; i<strlen(fpsString); ++i)
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, fpsString[i]);
- //reset matrices
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- glPopMatrix();
- glFinish();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- //This function draw a cube ; forget the texture parameter for now
- void drawCube(GLfloat x,GLfloat y,GLfloat z,int texture,GLdouble size)
- {
- GLfloat v[8][3];
- GLint i;
- v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
- v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
- v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
- v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
- v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
- v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
- glTranslatef(x, y, z);
- for (i = 5; i >= 0; i--) {
- glBegin(GL_QUADS);
- glNormal3fv(&n[i][0]);
- glVertex3fv(&v[faces[i][0]][0]);
- glVertex3fv(&v[faces[i][1]][0]);
- glVertex3fv(&v[faces[i][2]][0]);
- glVertex3fv(&v[faces[i][3]][0]);
- glEnd();
- }
- }
- //This function use 2 2d array (1 for height and one for texture) and draw the map.
- //It draw the map only once but the light is updatable.
- void drawMap()
- {
- //Display lists for objects
- static GLuint mapList=0, lightList=0;
- int height;
- int texture;
- int i,j,n;
- //Create base if necessary
- if(!mapList)
- {
- mapList=glGenLists(1);
- glNewList(mapList, GL_COMPILE);
- {
- for (i = 0 ; i < MAP_ACROSS ; i++)
- {
- for (j = 0 ; j <MAP_DOWN ; j++)
- {
- height = heightMap1[j][i];
- texture = textureMap1[j][i];
- for (n=1 ; n<=height ; n++)
- {
- glColor3f(0.5f, 0.5f, 0.5f);
- glPushMatrix();
- drawCube(i*2,-(j*2),n*2,texture,2);
- glPopMatrix();
- }
- }
- }
- }
- glEndList();
- }
- lightList=glGenLists(1);
- glNewList(lightList, GL_COMPILE);
- {
- glColor3f(1.0f, 0.0f, 0.0f);
- glPushMatrix();
- glTranslatef(lightPosition.x, lightPosition.y, lightPosition.z);
- glutSolidCube(0.5f);
- glPopMatrix();
- }
- glEndList();
- //Draw objects
- glCallList(mapList);
- glCallList(lightList);
- }
- ///////////////////////////////////////////////////////////////////////
- //Variable and constant
- ///////////////////////////////////////////////////////////////////////
- //for cube drawing
- static GLfloat n[6][3] =
- {
- {-1.0, 0.0, 0.0},
- {0.0, 1.0, 0.0},
- {1.0, 0.0, 0.0},
- {0.0, -1.0, 0.0},
- {0.0, 0.0, 1.0},
- {0.0, 0.0, -1.0}
- };
- static GLint faces[6][4] =
- {
- {0, 1, 2, 3},
- {3, 2, 6, 7},
- {7, 6, 5, 4},
- {4, 5, 1, 0},
- {5, 6, 2, 1},
- {7, 4, 0, 3}
- };
- //bias matrix
- 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);
- //camera and light
- VECTOR3D lightPosition(48.0f,-10.0f,13.0f);
- VECTOR3D cameraPosition(50.0f,-10.0f,50.0f);
- VECTOR3D cameraTarget(50.0f,-10.0f, 0.0f);
- //shadow and matrix declaration
- int shadowMapWidth = windowWidth,shadowMapHeight = windowHeight;
- MATRIX4X4 lightProjectionMatrix, lightViewMatrix;
- MATRIX4X4 cameraProjectionMatrix, cameraViewMatrix;
- GLuint shadowMapTexture;
- //i think that's pretty it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement