OpenGL Shadow Mapping färbt alles schwarz [geschlossen]C++

Programme in C++. Entwicklerforum
Guest
 OpenGL Shadow Mapping färbt alles schwarz [geschlossen]

Post by Guest »

Ich habe versucht, eine Schattenkarte zu implementieren, aber die gesamte Szene ist schwarz, obwohl sie definitiv nicht sein sollte.
Ich initialisiere ein Licht bei (4, 1, 4) mit Richtung (0, 0, 0). So sieht meine Haupt-Renderschleife aus:

Code: Select all

glfwMakeContextCurrent(window); // tell openGL we are outputting to this
target_scene->shadow_pass();    // create shadow maps
glViewport(0, 0, width, height); // swap back to our resolution
// render the scene
target_scene->render(target_camera, width / float(height));
// show the rendered scene
glfwSwapBuffers(window);
Ich weiß, dass die Render-Methode ordnungsgemäß funktioniert, da sie vor der Implementierung der Schattenzuordnung einwandfrei funktioniert hat.
Die Methode „shadow_pass“ sieht folgendermaßen aus:

Code: Select all

void scene::shadow_pass() const {
glCullFace(GL_FRONT);
// resize the viewport to the shadow resolution
glViewport(0, 0, SHADOW_RES, SHADOW_RES);
// activate the super simple shader for the shadow pass
light_pass_shader->use();
for (const light *lght : lights) {
lght->bind_depth_map(); // activate the framebuffer
light_pass_shader->apply_uniform_mat4(lght->get_light_space(),
"lightSpaceMatrix");
// draw all the objects
for (object *obj : objects) {
light_pass_shader->apply_uniform_mat4(obj->get_model_matrix(),
"model");
obj->draw();
}
// cleanup
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
glUseProgram(0);
glCullFace(GL_BACK);
}
So berechne ich die Lichtraummatrix und aktiviere den Framebuffer:

Code: Select all

const static glm::mat4 lightProjection =
glm::perspective(90.0f, 1.0f, 1.0f, 10.0f);

const glm::mat4 light::get_light_space() const {
return lightProjection * glm::lookAt(this->get_position(),
this->get_direction(),
glm::vec3(0.0f, 1.0f, 0.0f));
}

void light::bind_depth_map() const {
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
}
So sieht Draw aus, es ist wahrscheinlich in Ordnung, weil es im zweiten Durchgang verwendet wird.

Code: Select all

void object::draw() const {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
So sieht mein Light-Pass-Shader aus:

Code: Select all

#version 430 core

layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
layout(location = 3) in vec3 vertexTangent;
layout(location = 4) in vec3 vertexBitangent;

uniform mat4 lightSpaceMatrix;
uniform mat4 model;

void main()
{
gl_Position = lightSpaceMatrix * model * vec4(vertexPosition, 1.0);
}
Und so sehen meine Vertex- und Fragment-Shader während des Haupt-Rendering-Durchgangs aus.

Code: Select all

#version 430 core

layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 vertexNormal;
layout(location = 2) in vec2 vertexTexCoord;
layout(location = 3) in vec3 vertexTangent;
layout(location = 4) in vec3 vertexBitangent;

out vec2 texCoord;
out mat3 TBN;
out vec3 fragPos;
out vec3 normal;

out vec3 viewPos;
out vec3 shininess;

uniform mat4 model;
uniform mat4 viewProjection;

void main()
{
// Transform the vertex position by the model and view-projection matrices
gl_Position = viewProjection * model * vec4(vertexPosition, 1.0);
texCoord = vertexTexCoord;

// Transform the fragment position by the model matrix only
fragPos = vec3(model * vec4(vertexPosition, 1.0));

// Transform the normal by the model matrix only
normal = mat3(model) * vertexNormal;

// Calculate the TBN matrix using the model matrix only
vec3 T = normalize(mat3(model) * vertexTangent);
vec3 B = normalize(mat3(model) * vertexBitangent);
vec3 N = normalize(mat3(model) * vertexNormal);
TBN = mat3(T, B, N);
}

Code: Select all

#version 430 core

#define MAX_LIGHTS 10
#define bias 0.005

in vec2 texCoord;
in vec3 fragPos;
in vec3 normal;
in mat3 TBN;

uniform sampler2D texture0;
uniform sampler2D normal0;

struct Light {
vec3 direction;
vec3 color;
mat4 lightSpaceMatrix;
sampler2D shadowMap;
};

uniform Light lights[MAX_LIGHTS];
uniform int numLights;

uniform vec3 viewPos;
uniform float shininess;
uniform vec3 ambientLight;

out vec4 out_color;

vec3 CalcDirectionalLight(Light light, vec3 norm)
{
vec3 lightDir = normalize(-light.direction);

// Diffuse shading
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * light.color;

// Specular shading
vec3 viewDir = normalize(viewPos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
vec3 specular = spec * light.color;

// Shadow calculation
vec4 fragPosLightSpace = light.lightSpaceMatrix * vec4(fragPos, 1.0);
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;

float closestDepth = texture(light.shadowMap, projCoords.xy).r;
float currentDepth = projCoords.z;
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;

return (1.0 - shadow) * (diffuse + specular);
}

void main()
{
vec3 norm = texture(normal0, texCoord).rgb;
norm = normalize(norm * 2.0 - 1.0);
norm = normalize(TBN * norm);

vec3 result = ambientLight;
for (int i = 0; i < numLights; ++i) {
result += CalcDirectionalLight(lights[i], norm);
}

vec4 color = texture(texture0, texCoord);
out_color = vec4(result * color.rgb, color.a);
}
Hilf mir, ich bin so verwirrt.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post