Closed ggetz closed 7 years ago
GLuint _depthBuffer; GLuint _shadowMap; GLuint _shadowMapSize = 1024; //---------------------------------------------------------------------------- void initShadowMapping() { glGenFramebuffers(1, &_depthBuffer); glGenTextures(1, &_shadowMap); glBindTexture(GL_TEXTURE_2D, _shadowMap); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _shadowMapSize, _shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); } // OpenGL initialization void init() { camera1 = Camera(true, vec4(0.0, 2.0, 0.0, 1.0)); sun = DirectionalLight(); sun.shadow = (vec4(0.5, 0.5, 0.7, 1.0)); Material m = Material(); m.texturePath = texture1; m.ambient = vec4(0.5, 0.5, 0.5, 1.0); m.diffuse = vec4(0.8, 0.8, 0.8, 1.0); cube = new Cube(); cube->setMaterial(m); cube->setPosition(vec4(0, 0.5, -7, 1)); meshes.push_back(cube); // Init other meshes for (auto &mesh : meshes) { mesh->init(); } initShadowMapping(); glClearColor( 0.8, 0.9, 1.0, 1.0 ); }
void generateShadowMap() { glBindFramebuffer(GL_FRAMEBUFFER, _depthBuffer); glViewport(0, 0, _shadowMapSize, _shadowMapSize); glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); glBindTexture(GL_TEXTURE_2D, _shadowMap); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowMap, 0); glClear(GL_DEPTH_BUFFER_BIT); for (auto &mesh : meshes) { if (mesh != plane) // don't have the ground plan cast shadows { mesh->drawShadowMap(sun); } } glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, windowSize.x, windowSize.y); } void display( void ) { // SHADOW PASS glCullFace(GL_FRONT); generateShadowMap(); glCullFace(GL_NONE); // RENDER PASS glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (auto &mesh : meshes) { mesh->draw(camera1, sun, _shadowMap); } glFlush(); }
Initialization
Rendering