RenderObject.hh 5.73 KB
Newer Older
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6 7
#ifndef ACGL_OPENGL_OBJECTS_RENDEROBJECT_HH
#define ACGL_OPENGL_OBJECTS_RENDEROBJECT_HH
8

Robert Menzel's avatar
Robert Menzel committed
9 10 11 12 13 14 15 16
/*
 * A RenderObject combines a FrameBuffer to draw to, a ShaderProgramObject
 * to draw with and a VertexBufferObject to name what to draw.
 *
 * Instead of setting those objects individually and hoping that they match
 * a RenderObject can take care of that.
 */

17 18 19 20 21 22 23 24 25 26
#include <vector>
#include <string>
#include <tr1/memory>
#include <iostream>

#include <ACGL/ACGL.hh>

#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>

27 28
#include <ACGL/OpenGL/Objects/VertexBufferObject.hh>
#include <ACGL/OpenGL/Objects/FrameBufferObject.hh>
29
#include <ACGL/OpenGL/Objects/ShaderProgramObject.hh>
30
#include <ACGL/OpenGL/Objects/Viewport.hh>
31 32 33 34

namespace ACGL{
namespace OpenGL{

35
class RenderObject
36
{
37
    ACGL_NOT_COPYABLE(RenderObject)
38 39 40 41 42 43
    // ==================================================================================================== \/
    // ============================================================================================ STRUCTS \/
    // ==================================================================================================== \/
private:
    struct AttributeMapping
    {
44 45 46 47
        //! The attributeID stores the attribute index within the vbo.
        int_t attributeID;
        //! The attributeLocation comes from the shader
        int_t attributeLocation;
48 49 50 51 52 53 54 55 56 57 58 59
    };

    // ===================================================================================================== \/
    // ============================================================================================ TYPEDEFS \/
    // ===================================================================================================== \/
private:
    typedef std::vector< AttributeMapping > AttributeMappingVec;

    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
60
    RenderObject(ConstSharedVertexBufferObject _vertexBufferObject,
61
          ConstSharedShaderProgramObject _shaderProgram,
62
          ConstSharedFrameBufferObject _frameBufferObject)
63
    :   mpVertexBufferObject(_vertexBufferObject),
64
        mpShaderProgramObject(_shaderProgram),
65
        mpFrameBufferObject(_frameBufferObject),
66
        mAttributeMappings(),
67
        mpDrawBuffers(NULL)
68 69 70 71
    {
        updateMappings();
    }

72 73
    RenderObject(ConstSharedVertexBufferObject _vertexBufferObject,
          ConstSharedShaderProgramObject _shaderProgram)
74
    :   mpVertexBufferObject(_vertexBufferObject),
75
        mpShaderProgramObject(_shaderProgram),
76
        mpFrameBufferObject(),
77
        mAttributeMappings(),
78
        mpDrawBuffers(NULL)
79
    {
80
        updateMappings();
81 82
    }

83
    virtual ~RenderObject(void)
84
    {
Robert Menzel's avatar
Robert Menzel committed
85
        delete[](mpDrawBuffers);
86 87 88 89 90 91
    }

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
92 93
    inline ConstSharedVertexBufferObject  getVertexBufferObject  (void) const { return mpVertexBufferObject;  }
    inline ConstSharedFrameBufferObject   getFrameBufferObject   (void) const { return mpFrameBufferObject;   }
94
    inline ConstSharedShaderProgramObject getShaderProgramObject (void) const { return mpShaderProgramObject; }
95 96 97 98 99

    // ==================================================================================================== \/
    // ============================================================================================ METHODS \/
    // ==================================================================================================== \/
public:
100
    void updateMappings (void);
101 102

    void bindFrameBufferObject     (void) const;
103
    void useShaderProgramObject    (void) const;
104 105
    void enableVertexBufferObject  (void) const;
    void disableVertexBufferObject (void) const;
106

107
    inline void enable(void) const
108
    {
109 110 111 112 113 114 115 116
        bindFrameBufferObject();
        useShaderProgramObject();
        enableVertexBufferObject();
    }

    inline void disable(void) const
    {
        disableVertexBufferObject();
117 118
    }

119
    inline void draw(void) const
120 121 122 123
    {
        mpVertexBufferObject->draw();
    }

124
    inline void render(void) const
125
    {
126
        enable();
127
        draw();
128 129 130 131 132 133 134
        disable();
    }

    inline void validate (void) const
    {
        mpFrameBufferObject->validate();
        mpVertexBufferObject->validate();
135
    }
136 137 138 139 140

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
141
    ConstSharedVertexBufferObject  mpVertexBufferObject;
142
    ConstSharedShaderProgramObject mpShaderProgramObject;
143
    ConstSharedFrameBufferObject   mpFrameBufferObject;
144

Robert Menzel's avatar
Robert Menzel committed
145 146
    AttributeMappingVec            mAttributeMappings;
    GLuint*                        mpDrawBuffers;
147 148
};

149
ACGL_SHARED_TYPEDEF(RenderObject)
150 151 152 153

} // OpenGL
} // ACGL

154
#endif // ACGL_OPENGL_OBJECTS_RENDEROBJECT_HH