C++ 如何在渲染器中使用多重着色器?

C++ 如何在渲染器中使用多重着色器?,c++,opengl,shader,renderer,C++,Opengl,Shader,Renderer,我试图使我的渲染器能够使用默认或自定义着色器 class Renderer { public: void Create() { //Setup vao, vbo, ebo, quad indices, add data to ebo, //create default Shader //set camera projection } void Map() { vertexData = (V

我试图使我的渲染器能够使用默认或自定义着色器

class Renderer
{
public:
    void Create()
    {
        //Setup vao, vbo, ebo, quad indices, add data to ebo, 
        //create default Shader
        //set camera projection
    }

    void Map()
    {
        vertexData = (VertexData*)glMapBufferRange(...);
    }

    void Draw(Rectangle destRect, Rectangle texCoordRect, Texture* texture)
    {
        //4 times for 4 different corners
        float textureSlot = FindTexture(texture);
        AddDataToMappedBuffer(position, texCoords, textureSlot, color);

        indexCount += 6;
    }

    void Unmap()
    {
        glUnmapBuffer(...);
    }

    void Render()
    {
        Unmap();

        shader.UniformMatrix4fv("projection", camera.getProjection());

        for(uint i = 0; i < texture.size(); ++i)
        {
            texture[i]->Bind(i);
        }

        BindVAO();
        DrawElements(TRIANGELS, indexCount, UNSIGNED_INT, nullptr);
        UnbindVAO();

        For(uint i = 0; i < texture.size(); ++i)
        {
            texture[i]->Unbind(i);
        }

        indexCount = 0;
        texture.clear();
    }

    float Renderer2D::FindTexture(Texture* t)
    {
        float result = 0.0f;
        bool found = false;
        for (uint i = 0; i < texture.size(); ++i) {
            if (texture[i] == t) {
                result = static_cast<float>(i + 1);
                found = true;
                break;
            }
        }

        if (!found) {
            if (texture.size() >= MAX_TEXTURES) {
                Render();
                Map();
            }
            texture.push_back(t);
            result = static_cast<float>(texture.size());
        }

        return result;
    }
private:
    std::vector<Texture*> texture;
    uint vao, vbo, ebo;
    VertexData* vertexData;
    float FindTexture(Texture*);
    Shader shader;
    Camera2D camera;
};


int main()
{
    Renderer renderer;
    renderer.Create();

    while(!quit)
    {
        //Clear color etc.

        renderer.Map();
        renderer.Draw(Rectangle(0.0f, 0.0f, 500.0f, 500.0f), Rectangle(0.0f, 0.0f, 1.0f, 1.0f), texture("sometexture.png"));
        renderer.Render();
    }
}
我还尝试在未找到着色器时查找着色器而不执行另一个绘制调用(仅将其添加到数组中),并在渲染方法中通过所有着色器循环并执行绘制调用。它也没有起到很好的作用


我应该怎么做?

那么,您只想在未找到着色器的情况下进行渲染?这对我来说没有什么意义。不,如果着色器是默认着色器,我想使用1个绘制调用,或者如果有多个着色器,我想使用1个绘制调用,但仍然将其保留在一个类中。@应该在每个对象(网格)上应用一个着色器吗?或者应该对同一对象应用多个着色器?@rabbi76着色器每个精灵/对象-或者如果该着色器已被使用,或者是默认着色器-将其附加到精灵以避免调用另一个绘制-当然,如果可能的话。
std::vector<Shader*> shader;

void Renderer2D::FindShader(Shader* sh)
{
    bool found = false;
    for (uint i = 0; i < shader.size(); ++i) {
        if (shader[i] == sh) {
            found = true;
            return;
        }
    }

    if (!found) {
        shader.push_back(sh);
        Render();
        Map();
    }
}

void Render()
{
    Unmap();

    for(const auto& shader : shader) 
    {
        shader->UseProgram();
        shader.UniformMatrix4fv("projection", camera.getProjection());   
    }

    for(uint i = 0; i < texture.size(); ++i)
    {
        texture[i]->Bind(i);
    }

    BindVAO();
    DrawElements(TRIANGELS, indexCount, UNSIGNED_INT, nullptr);
    UnbindVAO();

    For(uint i = 0; i < texture.size(); ++i)
    {
        texture[i]->Unbind(i);
    }

    indexCount = 0;
    texture.clear();
}

void Draw(Rectangle destRect, Rectangle texCoordRect, Texture* texture, Shader* shader)
{
    FindShader(shader);
    float textureSlot = FindTexture(texture);
    AddDataToMappedBuffer(position, texCoords, textureSlot, color);

    indexCount += 6;
}