State.cc 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#include <ACGL/OpenGL/Objects/State.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/Base/StringOperations.hh>

#include <iostream>
#include <fstream>

using namespace ACGL::Base;
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;

17
void State::update (void)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
{
    ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders();

    for(ShaderProgram::ConstSharedShaderVec::size_type shader = 0; shader < shaders.size(); shader++)
    {
        const Shader::AttributeVec& attributeVec = shaders[shader]->getAttributes();

        for(Shader::AttributeVec::size_type k = 0; k < attributeVec.size(); k++)
        {
            AttributeMapping mapping;
            mapping.idInShaderProgram = mpShaderProgram->getAttributeLocation(attributeVec[k]);
            mapping.idInVertexBuffer  = mpVertexBuffer->getAttributeIndexByName(attributeVec[k]);

            mAttributeMappings.push_back(mapping);
        }

        const Shader::FragmentDataVec& fragmentDataVec = shaders[shader]->getFragmentData();

        for(Shader::FragmentDataVec::size_type k = 0; k < fragmentDataVec.size(); k++)
        {
            FragmentDataMapping mapping;
            mapping.idInShaderProgram = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
            mapping.idInFrameBuffer   = mpFrameBuffer->getColorAttachmentIndexByName(fragmentDataVec[k]);

            mFragmentDataMappings.push_back(mapping);
        }
    }

    mpBuffers = new GLuint[mFragmentDataMappings.size()];
    for(FragmentDataMappingVec::size_type k = 0; k < mFragmentDataMappings.size(); k++)
        mpBuffers[k] =  GL_COLOR_ATTACHMENT0 + mFragmentDataMappings[k].idInFrameBuffer;
}

void State::render(void)
{
    //Setting the draw buffers of the framebuffer
    mpFrameBuffer->bind();
    glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);

    if(mpVertexBuffer->getElementArrayBuffer())
        mpVertexBuffer->getElementArrayBuffer()->bind();

    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
        mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
        mpVertexBuffer->setAttributePointer(mAttributeMappings[k].idInVertexBuffer, mAttributeMappings[k].idInShaderProgram);
        glEnableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
        openGLRareError();
    }

    mpVertexBuffer->draw();

    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
        mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
        glDisableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
        openGLRareError();
    }
}