State.cc 4.31 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 59
void State::updateMappings (void)
{
    ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgramObject->getShaderProgram()->getShaders();

    for(ShaderProgram::ConstSharedShaderVec::size_type shader = 0; shader < shaders.size(); shader++)
    {
        for(Shader::AttributeNameVec::size_type k = 0; k < shaders[shader]->getAttributeNames().size(); k++)
        {
            AttributeMapping mapping;
            mapping.shaderProgramAttributeLocation = mpShaderProgramObject->getShaderProgram()->getAttributeLocation(shaders[shader]->getAttributeNames()[k]);
            mapping.vertexBufferObjectAttribute    = mpVertexBufferObject->getAttributeIndexByName(shaders[shader]->getAttributeNames()[k]);
            mapping.arrayBufferID                  = mpVertexBufferObject->getArrayBufferIDByAttributeIndex(mapping.vertexBufferObjectAttribute);

            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);
        }

        for(Shader::FragmentDataNameVec::size_type k = 0; k < shaders[shader]->getFragmentDataNames().size(); k++)
        {
            FragmentDataMapping mapping;
            mapping.shaderProgramFragmentDataLocation = mpShaderProgramObject->getShaderProgram()->getFragmentDataLocation(shaders[shader]->getFragmentDataNames()[k]);
            mapping.frameBufferObjectColorAttachment  = mpFrameBufferObject->getColorAttachmentIndexByName(shaders[shader]->getFragmentDataNames()[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].frameBufferObjectColorAttachment;
}

void State::useViewport (void) const
60
{
61
    mpViewport->use();
62 63
}

64 65 66 67 68 69
void State::useShaderProgramObject (void) const
{
    mpShaderProgramObject->use();
}

void State::bindFrameBufferObject (void) const
70
{
71
    mpFrameBufferObject->bind();
72 73 74
    glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
}

75
void State::enableVertexBufferObject (void) const
76
{
77 78
    if(mpVertexBufferObject->getElementArrayBuffer())
        mpVertexBufferObject->getElementArrayBuffer()->bind();
79 80 81 82 83 84 85

    int currentArrayBufferID = -1;

    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
        if(mAttributeMappings[k].arrayBufferID != currentArrayBufferID)
        {
86
            mpVertexBufferObject->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
87 88
            currentArrayBufferID = mAttributeMappings[k].arrayBufferID;
        }
89 90
        mpVertexBufferObject->setAttributePointer(mAttributeMappings[k].vertexBufferObjectAttribute, mAttributeMappings[k].shaderProgramAttributeLocation);
        glEnableVertexAttribArray(mAttributeMappings[k].shaderProgramAttributeLocation);
91 92 93 94
        openGLRareError();
    }
}

95
void State::disableVertexBufferObject (void) const
96
{
97
    int currentArrayBufferID = -1;
98 99
    for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
    {
100 101 102 103 104 105
        if(mAttributeMappings[k].arrayBufferID != currentArrayBufferID)
        {
            mpVertexBufferObject->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
            currentArrayBufferID = mAttributeMappings[k].arrayBufferID;
        }
        glDisableVertexAttribArray(mAttributeMappings[k].shaderProgramAttributeLocation);
106 107 108
        openGLRareError();
    }
}