[OpenGL]材质

文章目录[x]
  1. 1:新建材质
  2. 2:新建灯光
  3. 3:测试

新建材质


#pragma once

#include "glm/glm.hpp"

struct Material
{
    glm::vec3 ambient   = glm::vec3(1.0f);
    glm::vec3 diffuse   = glm::vec3(1.0f);
    glm::vec3 specular  = glm::vec3(1.0f);
    float shininess     = 32.0f;

    Material() {}
    Material(glm::vec3 ambient, glm::vec3 diffuse, glm::vec3 specular, float shiniess)
    {
        this->ambient = ambient;
        this->diffuse = diffuse;
        this->specular = specular;
        this->shininess = shiniess;
    }
};

 

新建灯光


#pragma once

#include "glm/glm.hpp"

struct Light
{
    glm::vec3 position = glm::vec3(1.0f);
    glm::vec3 ambient  = glm::vec3(1.0f);
    glm::vec3 diffuse  = glm::vec3(1.0f);
    glm::vec3 specular = glm::vec3(1.0f);

    Light() {}
    Light(glm::vec3 position, glm::vec3 ambient, glm::vec3 diffuse, glm::vec3 specular)
    {
        this->position = position;
        this->ambient = ambient;
        this->diffuse = diffuse;
        this->specular = specular;
    }
};

 

测试


class BasicMaterial : public SubTestBed
{
protected:
    ::BaseCamera* m_Camera;
    glm::mat4 m_Model = glm::identity<glm::mat4>();

    std::string m_CubeShader = "SandBox/10_Material/BasicMaterial.shader";
    Material m_Material;
    Light m_Light;
    bool m_UseBreatheLight = true;
public:
    virtual void Setup() override
    {
        // ...创建Cube和灯的Buffer略

        m_Material.ambient = glm::vec3(1.0f, 0.5f, 0.31f);
        m_Material.diffuse = glm::vec3(1.0f, 0.5f, 0.31f);
        m_Material.specular = glm::vec3(0.5f, 0.5f, 0.5f);
        m_Material.shininess = 32.0f;

        m_Light.position = glm::vec3(1.2f, 1.0f, 2.0f);
        m_Light.ambient = glm::vec3(0.2f, 0.2f, 0.2f);
        m_Light.diffuse = glm::vec3(0.5f, 0.5f, 0.5f);
        m_Light.specular = glm::vec3(1.0f, 1.0f, 1.0f);
    }

    virtual void Clear() override
    {
        m_Renderer.Clear();
    }

    virtual void Update(float deltaTime) override
    {
        m_TestScene->Update(deltaTime);
    }

    virtual void Render() override
    {
        //m_TestScene->Render();

        if (m_UseBreatheLight)
        {
            glm::vec3 lightColor;
            lightColor.x = sin((float)glfwGetTime() * 2.0f);
            lightColor.y = sin((float)glfwGetTime() * 0.7f);
            lightColor.z = sin((float)glfwGetTime() * 1.3f);

            glm::vec3 diffuseColor = lightColor * glm::vec3(0.5f); // 降低影响
            glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f); // 很低的影响
            m_Light.ambient = ambientColor;
            m_Light.diffuse = diffuseColor;
        }

        this->m_Renderer.Draw(m_VA[0], 36, m_Shader[0]);
        m_Shader[0]->SetMat4f("u_Projection", m_Camera->GetProjectionMatrix());
        m_Shader[0]->SetMat4f("u_View", m_Camera->GetViewMatrix());
        m_Shader[0]->SetVec3("u_ViewPos", m_Camera->GetEye());

        m_Shader[0]->SetVec3("light.position", m_Light.position);
        m_Shader[0]->SetVec3("light.ambient", m_Light.ambient);
        m_Shader[0]->SetVec3("light.diffuse", m_Light.diffuse);
        m_Shader[0]->SetVec3("light.specular", m_Light.specular);

        m_Shader[0]->SetVec3("material.ambient", m_Material.ambient);
        m_Shader[0]->SetVec3("material.diffuse", m_Material.diffuse);
        m_Shader[0]->SetVec3("material.specular", m_Material.specular);
        m_Shader[0]->SetFloat("material.shininess", m_Material.shininess);


        this->m_Renderer.Draw(m_VA[1], 36, m_Shader[1]);
        m_Shader[1]->SetMat4f("u_Projection", m_Camera->GetProjectionMatrix());
        m_Shader[1]->SetMat4f("u_View", m_Camera->GetViewMatrix());
        m_Shader[1]->SetVec3("u_LightColor", 1.0f, 1.0f, 1.0f);
        m_Model = glm::identity<glm::mat4>();
        m_Model = glm::translate(m_Model, m_Light.position);
        m_Model = glm::scale(m_Model, glm::vec3(0.2f));
        m_Shader[1]->SetMat4f("u_Model", m_Model);
    }

    virtual void OnGUI() override
    {
        ImGui::Begin("LightTest");
        {
            if (ImGui::CollapsingHeader("Material", ImGuiTreeNodeFlags_DefaultOpen))
            {
                ImGui::ColorEdit3("Ambient##Material", (float*)&m_Material.ambient);
                ImGui::ColorEdit3("Diffuse##Material", (float*)&m_Material.diffuse);
                ImGui::ColorEdit3("Specular##Material", (float*)&m_Material.specular);
                ImGui::SliderFloat("Specular##Material", &m_Material.shininess, 1.0f, 256.0f);
            }
            ImGui::NewLine();
            if (ImGui::CollapsingHeader("Light", ImGuiTreeNodeFlags_DefaultOpen))
            {
                ImGui::SliderFloat3("Position##Light", (float*)&m_Light.position, -5.0f, 5.0f);
                ImGui::ColorEdit3("Ambient##Light", (float*)&m_Light.ambient);
                ImGui::ColorEdit3("Diffuse##Light", (float*)&m_Light.diffuse);
                ImGui::ColorEdit3("Specular##Light", (float*)&m_Light.specular);
            }
        }
        ImGui::End();
    }
};

点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像