source: OGRE/trunk/ogrenew/RenderSystems/GL/src/OgreGLPBRenderTexture.cpp @ 692

Revision 692, 5.6 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreGLPBRenderTexture.h"
26#include "OgreGLContext.h"
27#include "OgreGLPixelFormat.h"
28#include "OgreLogManager.h"
29#include "OgreStringConverter.h"
30#include "OgreRoot.h"
31#include "OgreGLHardwarePixelBuffer.h"
32
33namespace Ogre {
34//----------------------------------------------------------------------------- 
35    GLPBuffer::GLPBuffer(PixelComponentType format, size_t width, size_t height):
36        mFormat(format),
37        mWidth(width),
38        mHeight(height)
39    {
40    }
41    GLPBuffer::~GLPBuffer()
42    {
43    }
44
45//----------------------------------------------------------------------------- 
46    GLPBRenderTexture::GLPBRenderTexture(GLPBRTTManager *manager, const String &name, const GLSurfaceDesc &target):
47        GLRenderTexture(name, target),
48        mManager(manager)
49    {
50        mPBFormat = PixelUtil::getComponentType(target.buffer->getFormat());
51       
52        mManager->requestPBuffer(mPBFormat, mWidth, mHeight);
53    }
54    GLPBRenderTexture::~GLPBRenderTexture()
55    {
56        // Release PBuffer
57        mManager->releasePBuffer(mPBFormat);
58    }
59    void GLPBRenderTexture::getCustomAttribute(const String& name, void* pData)
60    {
61        if(name=="TARGET")
62        {
63                        GLSurfaceDesc &target = *static_cast<GLSurfaceDesc*>(pData);
64                        target.buffer = static_cast<GLHardwarePixelBuffer*>(mBuffer);
65                        target.zoffset = mZOffset;
66        }
67        else if(name=="GLCONTEXT")
68        {
69            // Get PBuffer for our internal format
70            *static_cast<GLContext**>(pData) = mManager->getContextFor(mPBFormat, mWidth, mHeight);
71        }
72    }
73//----------------------------------------------------------------------------- 
74    GLPBRTTManager::GLPBRTTManager(GLSupport *support, RenderTarget *mainwindow):
75        mSupport(support),
76                mMainWindow(mainwindow),
77                mMainContext(0)
78    {
79                mMainWindow->getCustomAttribute("GLCONTEXT", &mMainContext);
80    } 
81    GLPBRTTManager::~GLPBRTTManager()
82    {
83        // Delete remaining PBuffers
84        for(size_t x=0; x<PCT_COUNT; ++x)
85        {
86            delete mPBuffers[x].pb;
87        }
88    }
89
90    RenderTexture *GLPBRTTManager::createRenderTexture(const String &name, const GLSurfaceDesc &target)
91    {
92        return new GLPBRenderTexture(this, name, target);
93    }
94   
95    bool GLPBRTTManager::checkFormat(PixelFormat format)
96    {
97        return true;
98    }
99
100    void GLPBRTTManager::bind(RenderTarget *target)
101    {
102        // Nothing to do here
103        // Binding of context is done by GL subsystem, as contexts are also used for RenderWindows
104    }
105
106    void GLPBRTTManager::unbind(RenderTarget *target)
107    {
108                // Copy on unbind
109        GLSurfaceDesc surface;
110                surface.buffer = 0;
111        target->getCustomAttribute("TARGET", &surface);
112        if(surface.buffer)
113            static_cast<GLTextureBuffer*>(surface.buffer)->copyFromFramebuffer(surface.zoffset);
114    }
115   
116    void GLPBRTTManager::requestPBuffer(PixelComponentType ctype, size_t width, size_t height)
117    {
118        //Check size
119        if(mPBuffers[ctype].pb)
120        {
121            if(mPBuffers[ctype].pb->getWidth()<width || mPBuffers[ctype].pb->getHeight()<height)
122            {
123                // If the current PBuffer is too small, destroy it and create a new one
124                delete mPBuffers[ctype].pb;
125                mPBuffers[ctype].pb = 0;
126            }
127        }
128        if(!mPBuffers[ctype].pb)
129        {
130            // Create pbuffer via rendersystem
131            mPBuffers[ctype].pb = mSupport->createPBuffer(ctype, width, height);
132        }
133       
134        ++mPBuffers[ctype].refcount;
135    }
136   
137    void GLPBRTTManager::releasePBuffer(PixelComponentType ctype)
138    {
139        --mPBuffers[ctype].refcount;
140        if(mPBuffers[ctype].refcount == 0)
141        {
142            delete mPBuffers[ctype].pb;
143            mPBuffers[ctype].pb = 0;
144        }
145    }
146   
147    GLContext *GLPBRTTManager::getContextFor(PixelComponentType ctype, size_t width, size_t height)
148    {
149        // Faster to return main context if the RTT is smaller than the window size
150        // and ctype is PCT_BYTE. This must be checked every time because the window might have been resized
151                if(ctype == PCT_BYTE)
152                {
153                        if(width <= mMainWindow->getWidth() && height <= mMainWindow->getWidth())
154                                return mMainContext;
155                }
156        assert(mPBuffers[ctype].pb);
157        return mPBuffers[ctype].pb->getContext();
158    }
159//---------------------------------------------------------------------------------------------
160
161}
162
Note: See TracBrowser for help on using the repository browser.