source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/Hierarchical Systems Demo [OpenGL]/RESOURCES/include/RttRes/__RenderTexture.h @ 3255

Revision 3255, 2.0 KB checked in by szirmay, 15 years ago (diff)
Line 
1#pragma once
2
3#include "pbuffer.h"
4
5// simple wrapper to encapsulate a pbuffer using render-to-texture and its associated texture object
6// v1.1 - updated to work with multiple draw buffers
7
8class RenderTexture {
9public:
10        RenderTexture(char *strMode, int iWidth, int iHeight, GLenum target) : m_target(target)
11        {
12                m_pbuffer = new PBuffer(strMode);
13                m_pbuffer->Initialize(iWidth, iHeight, false, true);
14
15        for(int i=0; i<max_buffers; i++) {
16            m_tex[i] = 0;
17        }
18        };
19
20        ~RenderTexture()
21        {
22                delete m_pbuffer;
23        for(int i=0; i<max_buffers; i++) {
24            if (m_tex[i]) {
25                        glDeleteTextures(1, &m_tex[i]);
26            }
27        }
28        }
29
30        void Activate() { m_pbuffer->Activate(); }
31        void Deactivate() { m_pbuffer->Deactivate(); }
32
33        void Bind(int iBuffer=WGL_FRONT_LEFT_ARB)
34        {
35        int tex_no = iBuffer - WGL_FRONT_LEFT_ARB;
36        if (m_tex[tex_no] == 0) {
37            // lazily allocate texture objects on demand
38            CreateTexture(m_tex[tex_no]);
39        }
40                glBindTexture(m_target, m_tex[tex_no]);
41                m_pbuffer->Bind(iBuffer);
42        }
43
44        void Release(int iBuffer=WGL_FRONT_LEFT_ARB) {
45                glBindTexture(m_target, m_tex[iBuffer - WGL_FRONT_LEFT_ARB]);
46                m_pbuffer->Release(iBuffer);
47        }
48
49        GLuint GetTextureHandler(int iBuffer=WGL_FRONT_LEFT_ARB){return m_tex[iBuffer - WGL_FRONT_LEFT_ARB];}
50
51    inline int GetWidth() { return m_pbuffer->GetWidth(); }
52    inline int GetHeight() { return m_pbuffer->GetHeight(); }
53
54private:
55    void CreateTexture(GLuint &tex)
56    {
57                glGenTextures(1, &tex);
58                glBindTexture(m_target, tex);
59                glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
60                glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
61                glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
62                glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
63    }
64
65    const static int max_buffers = 14;  // WGL_FRONT_LEFT_ARB - WGL_AUX9_ARB
66        PBuffer *m_pbuffer;
67        GLuint m_tex[max_buffers];          // texture for each buffer
68        GLenum m_target;
69};
Note: See TracBrowser for help on using the repository browser.