void ShadowMapExample::Initialize(const char * title) { base::Initialize(title); // Create the program for rendering the scene from the light's POV. glCreateProgram(); ShaderInfo light_shaders[] = { { GL_VERTEX_SHADER, "shadowmap_shadow.vs.glsl" }, { GL_FRAGMENT_SHADER, "shadowmap_shadow.fs.glsl" }, { GL_NONE } }; render_light_prog = LoadShaders(light_shaders); // Get the location of the projetion_matrix uniform render_light_uniforms.model_view_projection_matrix = glGetUniformLocation(render_light_prog, "model_view_projection_matrix"); // Create the program for rendering the scene from the viewer's position ShaderInfo scene_shaders[] = { { GL_VERTEX_SHADER, "shadowmap_scene.vs.glsl" }, { GL_FRAGMENT_SHADER, "shadowmap_scene.fs.glsl" }, { GL_NONE } }; render_scene_prog = LoadShaders(scene_shaders); // Get the locations of all the uniforms in the program render_scene_uniforms.model_matrix = glGetUniformLocation(render_scene_prog, "model_matrix"); render_scene_uniforms.view_matrix = glGetUniformLocation(render_scene_prog, "view_matrix"); render_scene_uniforms.projection_matrix = glGetUniformLocation(render_scene_prog, "projection_matrix"); render_scene_uniforms.shadow_matrix = glGetUniformLocation(render_scene_prog, "shadow_matrix"); render_scene_uniforms.light_position = glGetUniformLocation(render_scene_prog, "light_position"); render_scene_uniforms.material_ambient = glGetUniformLocation(render_scene_prog, "material_ambient"); render_scene_uniforms.material_diffuse = glGetUniformLocation(render_scene_prog, "material_diffuse"); render_scene_uniforms.material_specular = glGetUniformLocation(render_scene_prog, "material_specular"); render_scene_uniforms.material_specular_power = glGetUniformLocation(render_scene_prog, "material_specular_power"); // Set the depth texture uniform to unit 0 glUseProgram(render_scene_prog); glUniform1i(glGetUniformLocation(render_scene_prog, "depth_texture"), 0); // Create a depth texture glGenTextures(1, &depth_texture); glBindTexture(GL_TEXTURE_2D, depth_texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, DEPTH_TEXTURE_SIZE, DEPTH_TEXTURE_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // Create FBO to render depth into glGenFramebuffers(1, &depth_fbo); glBindFramebuffer(GL_FRAMEBUFFER, depth_fbo); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, depth_texture, 0); glDrawBuffer(GL_NONE); glBindFramebuffer(GL_FRAMEBUFFER, 0); // Upload geometry for the ground plane static const float ground_vertices[] = { -500.0f, -50.0f, -500.0f, 1.0f, -500.0f, -50.0f, 500.0f, 1.0f, 500.0f, -50.0f, 500.0f, 1.0f, 500.0f, -50.0f, -500.0f, 1.0f, }; static const float ground_normals[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; glGenVertexArrays(1, &ground_vao); glGenBuffers(1, &ground_vbo); glBindVertexArray(ground_vao); glBindBuffer(GL_ARRAY_BUFFER, ground_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(ground_vertices) + sizeof(ground_normals), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(ground_vertices), ground_vertices); glBufferSubData(GL_ARRAY_BUFFER, sizeof(ground_vertices), sizeof(ground_normals), ground_normals); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(ground_vertices)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // Load the object object.LoadFromVBM("C:/Vermilion-Book/trunk/Code/media/armadillo_low.vbm", 0, 1, 2); } 校正为: void ShadowMapExample::Initialize(const char * title) { base::Initialize(title); // Create the program for rendering the scene from the light's POV. glCreateProgram(); ShaderInfo light_shaders[] = { { GL_VERTEX_SHADER, "shadowmap_shadow.vs.glsl" }, { GL_FRAGMENT_SHADER, "shadowmap_shadow.fs.glsl" }, { GL_NONE } }; render_light_prog = LoadShaders(light_shaders); // Get the location of the projetion_matrix uniform render_light_uniforms.model_view_projection_matrix = glGetUniformLocation(render_light_prog, "model_view_projection_matrix"); // Create the program for rendering the scene from the viewer's position ShaderInfo scene_shaders[] = { { GL_VERTEX_SHADER, "shadowmap_scene.vs.glsl" }, { GL_FRAGMENT_SHADER, "shadowmap_scene.fs.glsl" }, { GL_NONE } }; render_scene_prog = LoadShaders(scene_shaders); // Get the locations of all the uniforms in the program render_scene_uniforms.model_matrix = glGetUniformLocation(render_scene_prog, "model_matrix"); render_scene_uniforms.view_matrix = glGetUniformLocation(render_scene_prog, "view_matrix"); render_scene_uniforms.projection_matrix = glGetUniformLocation(render_scene_prog, "projection_matrix"); render_scene_uniforms.shadow_matrix = glGetUniformLocation(render_scene_prog, "shadow_matrix"); render_scene_uniforms.light_position = glGetUniformLocation(render_scene_prog, "light_position"); render_scene_uniforms.material_ambient = glGetUniformLocation(render_scene_prog, "material_ambient"); render_scene_uniforms.material_diffuse = glGetUniformLocation(render_scene_prog, "material_diffuse"); render_scene_uniforms.material_specular = glGetUniformLocation(render_scene_prog, "material_specular"); render_scene_uniforms.material_specular_power = glGetUniformLocation(render_scene_prog, "material_specular_power"); // Set the depth texture uniform to unit 0 glUseProgram(render_scene_prog); glUniform1i(glGetUniformLocation(render_scene_prog, "depth_texture"), 0); // Create a depth texture glGenTextures(1, &depth_texture); glBindTexture(GL_TEXTURE_2D, depth_texture); // glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, DEPTH_TEXTURE_SIZE, DEPTH_TEXTURE_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH32F_STENCIL8, DEPTH_TEXTURE_SIZE, DEPTH_TEXTURE_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // Create FBO to render depth into glGenFramebuffers(1, &depth_fbo); glBindFramebuffer(GL_FRAMEBUFFER, depth_fbo); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, depth_texture, 0); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); glDrawBuffer(GL_NONE); glBindFramebuffer(GL_FRAMEBUFFER, 0); // Upload geometry for the ground plane static const float ground_vertices[] = { -500.0f, -50.0f, -500.0f, 1.0f, -500.0f, -50.0f, 500.0f, 1.0f, 500.0f, -50.0f, 500.0f, 1.0f, 500.0f, -50.0f, -500.0f, 1.0f, }; static const float ground_normals[] = { 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; glGenVertexArrays(1, &ground_vao); glGenBuffers(1, &ground_vbo); glBindVertexArray(ground_vao); glBindBuffer(GL_ARRAY_BUFFER, ground_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(ground_vertices) + sizeof(ground_normals), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(ground_vertices), ground_vertices); glBufferSubData(GL_ARRAY_BUFFER, sizeof(ground_vertices), sizeof(ground_normals), ground_normals); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(ground_vertices)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); // Load the object object.LoadFromVBM("C:/Vermilion-Book/trunk/Code/media/armadillo_low.vbm", 0, 1, 2); }