光源设置不正确 [英] light source is not set correctly

查看:80
本文介绍了光源设置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,但是灯光似乎无法正常工作,当我渲染对象时,它是黑暗的,然后当我旋转它时,光源似乎也在旋转.我不知道问题出在哪里.我试图为灯光修改模型,但仍然无法正常工作

I have this code but the light seems not working correctly, when I render the object it's dark, then when I rotate it, the light source seems to be rotated too. I can't figure out where the problem is. I tried to modify the model for the light but still not working

这是程序:

while (!glfwWindowShouldClose(window))
    {

        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glm::mat4 view = glm::mat4(1.0f); 
        glm::mat4 projection = glm::mat4(1.0f);
        projection = glm::perspective(glm::radians(45.0f), (float)width / (float)heigh, 0.1f, 100.0f);

        view = glm::translate(view, glm::vec3(0.0f, 0.0f, transZ)); //this is for scroll mouse
        view = glm::translate(view, glm::vec3(0.0f, 0.0f, -2.0f));
        view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f));
                
        glUseProgram(programT);
        int lightColorLoc = glGetUniformLocation(programT, "lightColor");
        glUniformMatrix4fv(lightColorLoc, 1, GL_FALSE, glm::value_ptr(glm::vec3(1.0f, 0.0f, 0.0f)));

        int objectColorLoc = glGetUniformLocation(programT, "objectColor");
        glUniformMatrix4fv(objectColorLoc, 1, GL_FALSE, glm::value_ptr(glm::vec3(1.0f, 0.5f, 0.31f)));

        glm::vec3 lightPos(2.0f, 4.0f, 5.0f);
        int lightPosLoc = glGetUniformLocation(programT, "lightPos");
        glUniformMatrix4fv(lightPosLoc, 1, GL_FALSE, glm::value_ptr(lightPos));

        int projectionLocLight = glGetUniformLocation(programT, "projection");
            glUniformMatrix4fv(projectionLocLight, 1, GL_FALSE, glm::value_ptr(projection));
        glm::mat4 modelLight = glm::mat4(1.0f);
        int modelLocLight = glGetUniformLocation(programT, "model");
        glUniformMatrix4fv(modelLocLight, 1, GL_FALSE, glm::value_ptr(modelLight));

        glm::mat4 viewLight = glm::mat4(1.0f); 
        int viewLocLight = glGetUniformLocation(programT, "view");
        glUniformMatrix4fv(viewLocLight, 1, GL_FALSE, glm::value_ptr(viewLight));

        int viewLoc = glGetUniformLocation(programT, "view");
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

        int projectionLoc = glGetUniformLocation(programT, "projection");
        glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));

        model = glm::rotate(model, glm::radians(rotX), glm::vec3(1.0f, 0.0f, 0.0f));
        model = glm::rotate(model, glm::radians(rotY), glm::vec3(.0f, 1.0f, .0f));
        model = glm::rotate(model, glm::radians(rotZ), glm::vec3(.0f, 0.0f, 1.0f));
        int modelLoc = glGetUniformLocation(programT, "model");
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

                 

        glGenerateMipmap(GL_TEXTURE_2D);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureArray[0]);
        glUniform1i(glGetUniformLocation(programT, "ourTexture"), 0);
            


            glBindVertexArray(VAOArray[0]);
        glDrawArrays(GL_TRIANGLES, 0, myMeshes.at(0).Indices.size());
    

        
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
        glfwSetKeyCallback(window, key_callback);
        glfwSetScrollCallback(window, scroll_callback);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        
    }

以下是我的顶点着色器:

the following is my vertex shader:

#version 330 core


layout (location = 0) in vec3 RealPos;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec2 vertex_textcoord;
layout (location = 3) in vec3 RealNor;


out vec3 vs_pos;
out vec3 vs_color;
out vec2 vs_text;
out vec3 normal;
out vec3 FragPos;

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


void main()
{
    vs_pos = RealPos;
    vs_color = vertex_color;
    vs_text=vertex_textcoord;
    
    normal=RealNor;
    FragPos = vec3(model * vec4(vs_pos, 1.0));
    
    gl_Position = projection * view * model * vec4(vs_pos, 1.0);
}

以下是片段着色器:

#version 330 core

in vec2 vs_text;
in vec3 normal;
in vec3 FragPos;

uniform vec3 lightPos;
out vec4 gl_FragColor;



uniform vec3 objectColor;
uniform vec3 lightColor;


uniform sampler2D ourTexture;



void main()
{
    vec3 norm = normalize(normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float ambientStrength = 0.1;
        vec3 ambient = ambientStrength * lightColor;
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;
    vec3 result = (ambient + diffuse) * objectColor;
    gl_FragColor =  texture(ourTexture, vs_text) * vec4(result,1.0);
}

我该如何解决问题?

推荐答案

片段着色器中的光照计算是在世界空间中完成的.因此,必须将具有法线矩阵的法线向量从对象空间转换为世界空间:

The light calculations in the fragment shader are done in world space. Therefore, you have to transform the normal vector with the normal matrix from object space to world space:

mat3 normalMatrix = transpose(inverse(mat3(model)));

normal = normalMatrix * RealNor;
FragPos = vec3(model * vec4(vs_pos, 1.0));

请参见为什么要使用转换模型视图矩阵的逆矩阵?

想象一下桌上的微调框和阅读灯.你的眼睛就是相机. model 矩阵对旋转进行建模.这使旋转器的不同侧面在旋转时被照亮.但是,灯泡的位置不会相对于您的位置改变.
当您改变位置(眼睛的位置)时,灯泡的相对位置也会改变.可以通过更改 view 矩阵来建模.

Imagine a spinner and a reading lamp on a table. Your eyes are the camera. The model matrix models the spinning. This causes different sides of the spinner to be illuminated as it rotates. However, the position of the lamp does not change relative to your position.
The relative position of the lamp changes when you change your position (the position of the eyes). This can be modeled by changing the view matrix.

这篇关于光源设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆