State.cc 3.79 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 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
void State::enableFrameBuffer()
{
    mpFrameBuffer->bind();
    glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
}

void State::enableVertexBuffer()
{
    if(mpVertexBuffer->getElementArrayBuffer())
        mpVertexBuffer->getElementArrayBuffer()->bind();

    int currentArrayBufferID = -1;

    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
        if(mAttributeMappings[k].arrayBufferID != currentArrayBufferID)
        {
            mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
            currentArrayBufferID = mAttributeMappings[k].arrayBufferID;
        }

        mpVertexBuffer->setAttributePointer(mAttributeMappings[k].vertexBufferAttribute, mAttributeMappings[k].shaderProgramAttribute);
        glEnableVertexAttribArray(mAttributeMappings[k].shaderProgramAttribute);
        openGLRareError();
    }
}

void State::disableVertexBuffer()
{
    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
        mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
        glDisableVertexAttribArray(mAttributeMappings[k].shaderProgramAttribute);
        openGLRareError();
    }
}

void State::render(void)
{
    mpVertexBuffer->draw();
}

59
void State::update (void)
60 61 62 63 64 65 66 67 68 69
{
    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;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            mapping.shaderProgramAttribute = mpShaderProgram->getAttributeLocation(attributeVec[k]);
            mapping.vertexBufferAttribute  = mpVertexBuffer->getAttributeIndexByName(attributeVec[k]);
            mapping.arrayBufferID          = mpVertexBuffer->getArrayBufferIDByAttributeIndex(mapping.vertexBufferAttribute);

            bool inserted = false;
            for (AttributeMappingVec::iterator it = mAttributeMappings.begin(); it < mAttributeMappings.end() && !inserted; it++)
            {
                if((*it).arrayBufferID > mapping.arrayBufferID)
                {
                    mAttributeMappings.insert(it, mapping);
                    inserted = true;
                }
            }

            if(!inserted)
                mAttributeMappings.push_back(mapping);
86 87 88 89 90 91 92
        }

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

        for(Shader::FragmentDataVec::size_type k = 0; k < fragmentDataVec.size(); k++)
        {
            FragmentDataMapping mapping;
93 94
            mapping.shaderProgramFragmentData  = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
            mapping.frameBufferColorAttachment = mpFrameBuffer->getColorAttachmentIndexByName(fragmentDataVec[k]);
95 96 97 98 99 100 101

            mFragmentDataMappings.push_back(mapping);
        }
    }

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