OpenGL反射高光绘制添加高Angular 视图

mwg9r5ms  于 2022-11-04  发布在  Angular
关注(0)|答案(1)|浏览(153)

我试着用OpenGL渲染一大块独立的立方体,现在我正在试验光照,效果很好,但是当从某个Angular 观察立方体时,它显示了不应该出现的高光。我修改了着色器,所以高光是红色的。我只是期望他们不会在那里,因为光来自与所有明亮的立方体的方向,应该没有反射,从这个立方体侧面的光源。
另一件困扰我的事情是,有一个边缘(图2)颜色的变化。这也是我不太明白,并寻求帮助。
当我使用一个定向光源并从某个Angular 观察大立方体时,就会出现这种情况。OpenGl版本是3.3,我在Windows上使用c++和cmake来构建。
以下是一些图片:

Github链接,因为SOT不允许我上传代码,因为“它是不格式化的”(尝试像20分钟格式化他们到SOT喜欢)。
编辑:它现在工作了,谁能想到这是图像的格式,而不是实际上的cpp,glsl代码。
Github
重要档案:灯光_原型.glsl,顶点.glsl,主视图. cpp
档案
main.cpp

// RENDER LOOP
while (!glfwWindowShouldClose(window))
{
    // INPUT
    //keysboard input, mouse input
    processInput(window);

    defaultShader.use();
    // CAMERA
    // model matrix set further down, dynamically for each object
    //view matrix, transform world space to camera space
    glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    defaultShader.setMatrix4fv("view", view);

    //projection matrix, view space to device cordinates
    glm::mat4 projection = glm::perspective(glm::radians(fov), 800.0f / 600.0f, 0.1f, 100.0f);
    defaultShader.setMatrix4fv("projection", projection);

    // DRAWING
    //clear color and depth buffer
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ////vertex data, shaders
    cube->use();
    defaultShader.setVec3("viewPos", cameraPos.x, cameraPos.y, cameraPos.z);

    defaultShader.setVec3("material.specular", 0.0f, 1.0f, 0.0f);
    defaultShader.setFloat("material.shininess", 32.0f);

    defaultShader.setVec3("dirLight.direction", -1.0f, -0.0f, 0.0f);
    defaultShader.setVec3("dirLight.ambient",  0.2f, 0.2f, 0.2f);
    defaultShader.setVec3("dirLight.diffuse",  0.8f, 0.8f, 0.8f); // darken diffuse light a bit
    defaultShader.setVec3("dirLight.specular", 1.0f, 1.0f, 1.0f);

    defaultShader.setVec3("pointLights[0].position", -1.0f, -1.0f, -1.0f);
    defaultShader.setVec3("pointLights[0].ambient", 0.2f, 0.2f, 0.2f);
    defaultShader.setVec3("pointLights[0].diffuse", 0.8f, 0.8f, 0.8f); // darken diffuse light a bit
    defaultShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
    defaultShader.setFloat("pointLights[0].constant", 1.0f);
    defaultShader.setFloat("pointLights[0].linear", 0.045f);
    defaultShader.setFloat("pointLights[0].quadratic", 0.0075f);

    //defaultShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
    //defaultShader.setVec3("lightPos", -1.0f, lighty, -1.0f);
    //defaultShader.setVec3("viewPos", cameraPos.x, cameraPos.y, cameraPos.z);

    //defaultShader.setVec3("material.specular", 0.0f, 1.0f, 0.0f);
    //defaultShader.setFloat("material.shininess", 128.0f);

    //defaultShader.setVec3("light.direction", -10.0f, lighty, 10.0f);
    //defaultShader.setBool("light.isDirectional", false);
    //defaultShader.setVec3("light.ambient",  0.2f, 0.2f, 0.2f);
    //defaultShader.setVec3("light.diffuse",  0.8f, 0.8f, 0.8f); // darken diffuse light a bit
    //defaultShader.setVec3("light.specular", 1.0f, 1.0f, 1.0f);
    //defaultShader.setFloat("light.constant", 1.0f);
    //defaultShader.setFloat("light.linear", 0.045f);
    //defaultShader.setFloat("light.quadratic", 0.0075f);

    int cubesize = 30;
    //draw cubes
    for (float i = 0.0f; i < cubesize; i++) {
        for (float j = 0.0f; j < cubesize; j++) {
            for (float k = 0.0f; k < cubesize; k++) {
                glm::mat4 model = glm::mat4(1.0f);
                model = glm::translate(model, glm::vec3(i, j, k));
                defaultShader.setMatrix4fv("model", model);
                glDrawArrays(GL_TRIANGLES, 0, 36);
            }
        }
    }

    //DRAW LIGHT

    light->use();
    lightshader.use();
    lightshader.setMatrix4fv("view", view);
    lightshader.setMatrix4fv("projection", projection);
    glm::mat4 model = glm::mat4(1.0f);
    lighty += 0.1f * deltaTime;
    model = glm::translate(model, glm::vec3(-1.0, -1.0, -1.0));
    lightshader.setMatrix4fv("model", model);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    //GLFW updating the window
    glfwSwapBuffers(window);
    glfwPollEvents();
}

片段着色器


# version 330 core

in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;

struct DirLight {
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};  
uniform DirLight dirLight;

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);

struct PointLight {    
    vec3 position;

    float constant;
    float linear;
    float quadratic;  

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};  

# define NR_POINT_LIGHTS 1

uniform PointLight pointLights[NR_POINT_LIGHTS];

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

struct Material {
    vec3 specular;
    float shininess;
}; 

uniform Material material;
uniform sampler2D texture0;
uniform vec3 viewPos;

out vec4 FragColor;

void main()
{
    // properties
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);

    // phase 1: Directional lighting
    vec3 result = CalcDirLight(dirLight, norm, viewDir);
    // phase 2: Point lights
    for(int i = 0; i < NR_POINT_LIGHTS; i++)
        //result += CalcPointLight(pointLights[i], norm, FragPos, viewDir); 

    FragColor = texture2D(texture0, TexCoord) * vec4(result, 1.0);
}

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
    vec3 lightDir = normalize(-light.direction);
    // diffuse shading
    float diff = max(dot(normal, lightDir), 0.0);
    // specular shading
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    // combine results
    vec3 ambient  = light.ambient  * vec3(texture(texture0, TexCoord));
    vec3 diffuse  = light.diffuse  * diff * vec3(texture(texture0, TexCoord));
    vec3 specular = light.specular * spec * vec3(1.0f, 0.0f, 0.0f);
    return (ambient + diffuse + specular);
} 

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);
    // diffuse shading
    float diff = max(dot(normal, lightDir), 0.0);
    // specular shading
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    // attenuation
    float distance    = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + 
                        light.quadratic * (distance * distance));    
    // combine results
    vec3 ambient  = light.ambient  * vec3(texture(texture0, TexCoord));
    vec3 diffuse  = light.diffuse  * diff * vec3(texture(texture0, TexCoord));
    vec3 specular = light.specular * spec * vec3(1.0f, 1.0f, 1.0f);
    ambient  *= attenuation;
    diffuse  *= attenuation;
    specular *= attenuation;
    return (ambient + diffuse + specular);
}

顶点着色器


# version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 2) in vec3 aNormal;

out vec2 TexCoord;
out vec3 Normal;
out vec3 FragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(){
    gl_Position = projection * view * model * vec4(aPos, 1.0f);
    FragPos = vec3(model * vec4(aPos, 1.0));
    TexCoord = vec2(aTexCoord.x, aTexCoord.y);
    Normal = aNormal;
};
nsc4cvqm

nsc4cvqm1#

看起来你有一期半
首先,在图像2(和3)中,菱形纹理中存在线的原因是由于纹理过滤。
我在Texture.cpp中检查了你的源代码,缩小过滤器设置为GL_NEAREST_MIPMAP_NEAREST,并且你也生成了相对的mipsmap。如果你改变

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

在Texture.cpp的第23行,并移除

glGenerateMipmap(GL_TEXTURE_2D);

线应消失
我不明白红色镜面反射光的问题在哪里,如果你能显示光源的方向,我想可能会有帮助

相关问题