Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreRenderTexture.cpp @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 4.1 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
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
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30
31#include "OgreRenderTexture.h"
32#include "OgreException.h"
33#include "OgreHardwarePixelBuffer.h"
34#include "OgreImage.h"
35#include "OgreImageCodec.h"
36
37namespace Ogre
38{
39
40    //-----------------------------------------------------------------------------
41        RenderTexture::RenderTexture(HardwarePixelBuffer *buffer, size_t zoffset):
42                mBuffer(buffer), mZOffset(zoffset)
43    {
44        mPriority = OGRE_REND_TO_TEX_RT_GROUP;
45                mWidth = static_cast<unsigned int>(mBuffer->getWidth());
46                mHeight = static_cast<unsigned int>(mBuffer->getHeight());
47        mColourDepth = static_cast<unsigned int>(
48                        Ogre::PixelUtil::getNumElemBits(mBuffer->getFormat()));
49    }
50    RenderTexture::~RenderTexture()
51    {
52                mBuffer->_clearSliceRTT(0);
53    }
54
55        void RenderTexture::writeContentsToFile( const String & filename )
56    {
57                // copyToMemory
58        ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
59       
60        imgData->width = mWidth;
61        imgData->height = mHeight;
62                imgData->depth = 1;
63        imgData->format = PF_BYTE_RGBA;
64                size_t size = imgData->width * imgData->height * 4;
65
66        // Allocate buffer
67        uchar* pBuffer = new uchar[size];
68
69        // Read pixels
70        mBuffer->blitToMemory(
71                        Box(0,0,mZOffset,mWidth,mHeight,mZOffset+1), 
72                        PixelBox(mWidth, mHeight, 1, imgData->format, pBuffer)
73                );
74
75        // Wrap buffer in a chunk
76        MemoryDataStreamPtr stream(new MemoryDataStream(
77            pBuffer, size, false));
78
79        // Get codec
80        size_t pos = filename.find_last_of(".");
81            String extension;
82        if( pos == String::npos )
83            OGRE_EXCEPT(
84                Exception::ERR_INVALIDPARAMS, 
85            "Unable to determine image type for '" + filename + "' - invalid extension.",
86                "GLRenderTexture::writeContentsToFile" );
87
88        while( pos != filename.length() - 1 )
89            extension += filename[++pos];
90
91        // Get the codec
92        Codec * pCodec = Codec::getCodec(extension);
93
94        // Write out
95        Codec::CodecDataPtr codecDataPtr(imgData);
96        pCodec->codeToFile(stream, filename, codecDataPtr);
97
98                delete [] pBuffer;
99    }
100        //-----------------------------------------------------------------------------
101        MultiRenderTarget::MultiRenderTarget(const String &name)
102    {
103        mPriority = OGRE_REND_TO_TEX_RT_GROUP;
104                mName = name;
105                /// Width and height is unknown with no targets attached
106                mWidth = mHeight = 0;
107    }
108        //-----------------------------------------------------------------------------
109        void MultiRenderTarget::writeContentsToFile( const String & filename )
110        {
111                OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, 
112                                "Cannot write MultiRenderTargets to disk",
113                "MultiRenderTarget::writeContentsToFile");
114        }
115
116}
Note: See TracBrowser for help on using the repository browser.