0

まず第一に、一日中この仕事をしようとした後、この長い投稿を申し訳ありません.

特に C++ で継承を使用してライトを作成しているため、これについて多くの質問があります。

私はライトの方向を与えることができ、ライトを計算するので、ディレクショナル ライトをライトのコア モデルとして使用します。次に、その上に、ライトからフラグメント位置へのベクトルを計算するだけのポイント ライトを構築し、最後にスポット ライトを作成します。スポット ライトを作成するために、カット オフ アングルを追加したポイント ライトを使用します (コーンの外側にあるものはすべて無視します)。ライトをテストしたところ、フォワード レンダリングで問題なく動作しましたが、ライト モデルを PBR に変更し (基本的には、ディレクショナル ライトでライトを計算する方法を変更するだけです)、別のレンダリングに移行したいと考えています。

今日、遅延レンダリングの作業を開始し、位置、テクスチャ、法線、深度のバッファを取得できますが、ライトをレンダリングしようとすると問題が発生します。

これが最初の問題であり、2 番目の問題でした。それぞれのタイプのライトには独自のシェーダーがあり、ポリモーフィズムを使用してそれらを構築したからです。私の 2 番目の質問は、C++ で各ライトをループして、各ライトをレンダラーとして呼び出すか、シェーダーでこれを解決できる別の方法があるかということです。ライトのプロトタイプは編集です: VP プロジェクションを使用してレンダー クアットを変換する小さな問題を修正しましたが、まだ何も描画できず、FB が正しく機能しているかどうかわかりません。Nvidia opengl デバッガーがクラッシュします。

 Light(glm::vec3& color, float intensity, float ambient, ShaderProgram& lightShader);
 DirectionalLight(glm::vec3& color = glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), float intensity = 1.0f, float ambient = 0.0f, ShaderProgram& lightShader = ShaderProgram("Directional Light"));
    PointLight(glm::vec3& color = glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), float intensity = 1.0f, float ambient = 0.0f, LightAttenuation& lightAttenuation = LightAttenuation(), ShaderProgram& lightShader = ShaderProgram("Point Light"));
 SpotLight(glm::vec3& color = glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f),

私のレンダリングパスは次のようになります。

            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LESS);

        defferedShader_.startProgram();
        defferedShader_.setUniformMat4("VP", camera.getVP());


        glBindFramebuffer(GL_FRAMEBUFFER, deferredFbo);

        //the scene is small and does not need culling.
        for (auto* mesh : world.getMeshes()) {

            //mesh->draw(light->getLightShader());
            //mesh->draw(activeLight_->getLightShader());
            mesh->draw(defferedShader_);

            drawCallCounter += mesh->getMeshObjectSize();
        }

glBindFramebuffer(GL_FRAMEBUFFER, 0);
        defferedShader_.stopProgram();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, positionFbo);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, normalFbo);
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, albedoFbo);

               //This is where i got stuck, I would like to make directional light work then test other lights then test the whole program with more than one light
        //for (auto* light : world.getLights()) {
        //  //glEnable(GL_CULL_FACE);
        //  //glCullFace(GL_FRONT);
            glDisable(GL_DEPTH_TEST);
            activeLight_->getLightShader().startProgram();

            activeLight_->getLightShader().setUniformMat4("VP", camera.getVP());
            activeLight_->getLightShader().setUniformVec3("eyePosition", camera.getCameraPosition());

            //activeLight_->getLightShader();
            RenderQuad();

            activeLight_->getLightShader().stopProgram();

  //}

私が構築し始めたシェーダー コードは (PS 私は今のところ影を削除しました) 頂点シェーダー

    #version 410 core

#include "../Global/GlobalShader.inc"
#include "../Global/GlobalMesh.inc"

out vec3 Position;
out vec2 TexCoord;
//out vec4 ShadowCoord;

//uniform mat4 ShadowMatrix;

void main() {

    Position = position;
    TexCoord = texCoord;

    //ShadowCoord = ShadowMatrix * vec4(position, 1.0);
    gl_Position = VP * vec4(position, 1.0);
}

Fragment shader 私を悩ませているのは、gPosition、gPosition、gAlbedoSpec を使用しても均一な値を設定できず、シェーダーで何を変更しても出力が同じになることです。

        #version 410 core

    #include "../Global/GlobalShader.inc"
    #include "../Global/GlobalMesh.inc"
    #include "../Global/GlobalLight.inc"
    //#include "../Global/ShadowSampling.inc"

    in vec3 Position;
    in vec2 TexCoord;
    //in vec4 ShadowCoord;

    uniform sampler2D gPosition;
    uniform sampler2D gNormal;
    uniform sampler2D gAlbedoSpec;

    float specularStrength = 32.0f; // to be impelemented

    out vec4 gl_FragColor;
    void main() {

        //vec4 lightning = vec4(0.0f);
        ////vec4 shadowMapping = vec4(0.0f);
        //
        vec3 FragPos = texture(gPosition, TexCoord).rgb;
        vec3 Normal = texture(gNormal, TexCoord).rgb;
        vec3 Diffuse = texture(gAlbedoSpec, TexCoord).rgb;
        float Specular = texture(gAlbedoSpec, TexCoord).a;

        //vec3 Diffuse = texture(gAlbedoSpec, TexCoord).rgb;
        //lightning = calculateDirectionalLight(directionalLight.light, directionalLight.position, Normal, Position, specularStrength, eyePosition, material, TexCoord);
        //gl_fragColor = vec3(Position, 1.0);
        //shadowMapping = calculateShadow(shadowMap, ShadowCoord, directionalLight.light.ambient);
        //gl_FragColor = vec4(Diffuse, 1.0);
        gl_FragColor = vec4(1.0); //vec4(Diffuse, 1.0);// lightning;//g * shadowMapping;

        //gl_FragColor = lightning;// * shadowMapping;
    }

in case you want to see global light 

    struct Light
{
    vec3 color;
    float intensity;
    float ambient;
};

struct DirectionalLight
{
    Light light;
    vec3 position;
};

struct Attenuation
{
    float constant;
    float linear;
    float quadratic;
};

struct PointLight
{
    Light light;
    Attenuation atten;
    vec3 position;
    float range;
};

struct SpotLight
{
    PointLight pointLight;
    //vec3 lookAt;
    vec3 direction;
    float cutOff;
};

vec3 GAMMA = vec3(1.0/2.2);

vec4 calculateDirectionalLight(Light light, vec3 direction, vec3 normal, vec3 worldPosition, float specularIntensity, vec3 eyePosition, Material material, vec2 texCoord)
{
        vec3 diffuseFactor = ( light.color * material.diffuse * vec3(texture(material.texture.diffuse, texCoord.st)) )
                                                 * (light.intensity * clamp(dot(normal, direction), 0.0, 1.0) ) ;

        vec3 viewDir = normalize(eyePosition - worldPosition);
        vec3 reflectDir = normalize(reflect(-direction, normal));

        float specularFactor = pow(clamp(dot(viewDir, reflectDir), 0.0, 1.0), specularIntensity);
        vec3 specularColor = ( light.color * material.specular * vec3(texture(material.texture.specular, texCoord.st)) ) * (specularFactor * material.shininess);

        return vec4(pow((diffuseFactor + specularColor + light.ambient + material.ambient), GAMMA), 1.0);
}

vec4 calculatePointLight(PointLight pointLight, vec3 normal, vec3 worldPosition, float specularIntensity, vec3 eyePosition, Material material, vec2 texCoord)
{
        // DO NOT NORMALIZE lightDirection, WE NEED IT TO CALCULATE THE DISTANCE TO COMPARE RANGE OF LIGHT
        vec3 lightDirection = pointLight.position - worldPosition;
        float distanceToPoint = length(lightDirection);

        // I dont like conditionals in shader, but since this is fragment based lighting i believe
        // this will speed-up things insetead of calculating the light
        if(distanceToPoint > pointLight.range)
                return vec4(0.0,0.0,0.0,0.0);

        vec4 light = calculateDirectionalLight(pointLight.light, lightDirection, normal, worldPosition,  specularIntensity, eyePosition, material, texCoord);

        // light attenuateion explained https://developer.valvesoftware.com/wiki/Constant-Linear-Quadratic_Falloff
        // http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Light+Attenuation+Shortcut
        float attenuation = max(pointLight.atten.constant
                                + pointLight.atten.linear * distanceToPoint
                                + pointLight.atten.quadratic * distanceToPoint * distanceToPoint,
                                1.0);

    return light / attenuation;
}

vec4 calculateSpotLight(SpotLight spotLight, vec3 normal, vec3 worldPosition, float specularIntensity, vec3 eyePosition, Material material, vec2 texCoord) 
{
    vec3 lightDirection = normalize(spotLight.pointLight.position - worldPosition);
    float spotFactor = dot(lightDirection, spotLight.direction);

    vec4 light = vec4(0.0f);
    if(spotFactor > spotLight.cutOff)
    {
        light = calculatePointLight(spotLight.pointLight, normal, worldPosition, specularIntensity, eyePosition, material, texCoord) * (1.0 - (1.0 - spotFactor)/(1.0 - spotLight.cutOff));
    }

    return light;
}

グローバル メッシュ

   struct Texture {
    sampler2D diffuse;
    sampler2D specular;
    sampler2D normal;
    sampler2D ambient;
    sampler2D height;
    //vec2 texCoord;            
};

struct Material {
    vec3 ambient;           // Ka
    vec3 diffuse;           // Kd
    vec3 specular;          // Ks
    vec3 transmittance;     // Tr
    vec3 emission;          // Ke
    float shininess;        // Ns
    float ior;              // Ni
    float dissolve;         // Dissolve
    int illum;              // Illum
    Texture texture;
};

uniform Material material;

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 normal;

グローバル シェーダー

    uniform mat4 VP;
uniform mat4 P;

バッファをバインドして指向性シェーダを実行した後、私が今得ているのは ここに画像の説明を入力

シーンを見るための例として、これは位置バッファです ここに画像の説明を入力

4

1 に答える 1

0

修正しました。きれいなバッファーと色が間違った場所にありました。各フレームの先頭ではなく、バッファをバインドした後である必要があります。

glEnable(GL_DEPTH_TEST);


glBindFramebuffer(GL_FRAMEBUFFER, deferredFbo);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    defferedShader_.startProgram();
    defferedShader_.setUniformMat4("VP", camera.getVP());
.
.
. rest of the code
于 2016-02-02T04:29:28.370 に答える