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

Revision 692, 8.4 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://www.ogre3d.org/
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 "OgreStableHeaders.h"
26
27#include "OgreException.h"
28#include "OgreLogManager.h"
29#include "OgreRoot.h"
30#include "OgreStringConverter.h"
31
32#include "OgreGLRenderSystem.h"
33
34#include "OgreGLXRenderTexture.h"
35#include "OgreGLXContext.h"
36#include "OgreGLXUtils.h"
37
38#include <iostream>
39
40/// ATI: GLX_ATI_pixel_format_float
41#ifndef GLX_RGBA_FLOAT_ATI_BIT
42#define GLX_RGBA_FLOAT_ATI_BIT 0x00000100
43#endif
44
45/// ARB: GLX_ARB_fbconfig_float
46#ifndef GLX_RGBA_FLOAT_BIT
47#define GLX_RGBA_FLOAT_BIT 0x00000004
48#endif
49
50#ifndef GLX_RGBA_FLOAT_TYPE
51#define GLX_RGBA_FLOAT_TYPE 0x20B9
52#endif
53
54
55namespace Ogre
56{
57    GLXPBuffer::GLXPBuffer(PixelComponentType format, size_t width, size_t height):
58        GLPBuffer(format, width, height),
59        _hPBuffer(0),
60        mContext(0)
61    {
62        createPBuffer();
63        // Create context
64        mContext = new GLXContext(_pDpy, _hPBuffer, _hGLContext);
65    }
66       
67    GLContext *GLXPBuffer::getContext()
68    {
69        return mContext;
70    }
71
72    void GLXPBuffer::createPBuffer() {       
73        //LogManager::getSingleton().logMessage(
74        //"GLXPBuffer::Creating PBuffer"
75        //);
76        _pDpy = glXGetCurrentDisplay();
77        ::GLXContext context = glXGetCurrentContext();
78        int screen = DefaultScreen(_pDpy);
79        int attribs[50], ideal[50];
80        int attrib;
81       
82        // Process format
83        int bits=0;
84        bool isFloat=false;
85        switch(mFormat)
86        {
87            case PCT_BYTE:
88                bits=8; isFloat=false;
89                break;
90            case PCT_SHORT:
91                bits=16; isFloat=false;
92                break;
93            case PCT_FLOAT16:
94                bits=16; isFloat=true;
95                break;
96            case PCT_FLOAT32:
97                bits=32; isFloat=true;
98                break;
99            default: break;
100        };
101        RTFType floatBuffer = RTF_NONE;
102        if(isFloat)
103        {
104            floatBuffer = detectRTFType();
105            if(floatBuffer == RTF_NONE || floatBuffer == RTF_NV)
106            {
107                OGRE_EXCEPT(Exception::UNIMPLEMENTED_FEATURE, "Floating point PBuffers not supported on this hardware",  "GLRenderTexture::createPBuffer");
108            }
109        }
110
111        // Create base required format description
112        attrib = 0;
113        if (floatBuffer == RTF_ATI) {
114            attribs[attrib++] = GLX_RENDER_TYPE;
115            attribs[attrib++] = GLX_RGBA_FLOAT_ATI_BIT;
116        }
117        else if (floatBuffer == RTF_ARB)
118        {
119            attribs[attrib++] = GLX_RENDER_TYPE;
120            attribs[attrib++] = GLX_RGBA_FLOAT_BIT;
121        }
122        else
123        {
124            attribs[attrib++] = GLX_RENDER_TYPE;
125            attribs[attrib++] = GLX_RGBA_BIT;
126        }     
127        attribs[attrib++] = GLX_DRAWABLE_TYPE;
128        attribs[attrib++] = GLX_PBUFFER_BIT;
129        attribs[attrib++] = GLX_DOUBLEBUFFER;
130        attribs[attrib++] = 0;
131        /*
132        if (floatBuffer == RTF_NV) {
133                    attribs[attrib++] = GLX_FLOAT_COMPONENTS_NV;
134                    attribs[attrib++] = 1;
135            }
136        */
137        attribs[attrib++] = None;
138       
139        // Create "ideal" format description
140        attrib = 0;
141        ideal[attrib++] = GLX_RED_SIZE;
142        ideal[attrib++] = bits;       
143        ideal[attrib++] = GLX_GREEN_SIZE;
144        ideal[attrib++] = bits;
145        ideal[attrib++] = GLX_BLUE_SIZE;
146        ideal[attrib++] = bits;       
147        ideal[attrib++] = GLX_ALPHA_SIZE;
148        ideal[attrib++] = bits;
149        ideal[attrib++] = GLX_DEPTH_SIZE;
150        ideal[attrib++] = 24;
151        ideal[attrib++] = GLX_STENCIL_SIZE;
152        ideal[attrib++] = 8;
153        ideal[attrib++] = GLX_ACCUM_RED_SIZE;
154        ideal[attrib++] = 0;    // Accumulation buffer not used
155        ideal[attrib++] = GLX_ACCUM_GREEN_SIZE;
156        ideal[attrib++] = 0;    // Accumulation buffer not used
157        ideal[attrib++] = GLX_ACCUM_BLUE_SIZE;
158        ideal[attrib++] = 0;    // Accumulation buffer not used
159        ideal[attrib++] = GLX_ACCUM_ALPHA_SIZE;
160        ideal[attrib++] = 0;    // Accumulation buffer not used
161        ideal[attrib++] = None;
162
163        // Create vector of existing config data formats       
164        GLXFBConfig config = GLXUtils::findBestMatch(_pDpy, screen, attribs, ideal);
165
166        // Create the pbuffer in the best matching format
167        attrib = 0;
168        attribs[attrib++] = GLX_PBUFFER_WIDTH;
169        attribs[attrib++] = mWidth; // Get from texture?
170        attribs[attrib++] = GLX_PBUFFER_HEIGHT;
171        attribs[attrib++] = mHeight; // Get from texture?
172        attribs[attrib++] = GLX_PRESERVED_CONTENTS;
173        attribs[attrib++] = 1;
174        attribs[attrib++] = None;
175
176        FBConfigData configData(_pDpy, config);
177        LogManager::getSingleton().logMessage(
178                LML_NORMAL,
179                "GLXPBuffer::PBuffer chose format "+configData.toString());                   
180
181        _hPBuffer = glXCreatePbuffer(_pDpy, config, attribs);
182        if (!_hPBuffer)
183            OGRE_EXCEPT(Exception::UNIMPLEMENTED_FEATURE, "glXCreatePbuffer() failed", "GLRenderTexture::createPBuffer");
184
185        _hGLContext = glXCreateNewContext(_pDpy, config, GLX_RGBA_TYPE, context, True);
186        if (!_hGLContext)
187            OGRE_EXCEPT(Exception::UNIMPLEMENTED_FEATURE, "glXCreateContext() failed", "GLRenderTexture::createPBuffer");       
188
189        // Query real width and height
190        GLuint iWidth, iHeight;
191        glXQueryDrawable(_pDpy, _hPBuffer, GLX_WIDTH, &iWidth);
192        glXQueryDrawable(_pDpy, _hPBuffer, GLX_HEIGHT, &iHeight);
193
194        LogManager::getSingleton().logMessage(
195             LML_NORMAL,
196                "GLXPBuffer::PBuffer created -- Real dimensions "+
197                StringConverter::toString(iWidth)+"x"+StringConverter::toString(iHeight)+
198                ", number of bits is "+
199                StringConverter::toString(bits)+
200                ", floating point is "+
201                StringConverter::toString(isFloat)
202        );
203        mWidth = iWidth; 
204        mHeight = iHeight;
205    }
206
207    GLXPBuffer::~GLXPBuffer()
208    {
209        // Destroy and unregister context
210        delete mContext;
211        // Destroy GL context
212        glXDestroyContext(_pDpy, _hGLContext);
213        _hGLContext = 0;
214        glXDestroyPbuffer(_pDpy, _hPBuffer);
215        _hPBuffer = 0;
216        LogManager::getSingleton().logMessage(
217             LML_NORMAL,
218                "GLXPBuffer::PBuffer destroyed");
219    }
220   
221    GLXPBuffer::RTFType GLXPBuffer::detectRTFType()
222    {
223        RTFType floatBuffer = RTF_NONE;
224        /// Query supported float buffer extensions
225        /// Choose the best one
226        std::stringstream ext;
227        std::string instr;
228        ext << glXQueryExtensionsString(_pDpy, DefaultScreen(_pDpy)) << " " << glXGetClientString(_pDpy, GLX_EXTENSIONS);
229        while(ext >> instr)
230        {
231            if(instr == "GLX_NV_float_buffer" && floatBuffer<RTF_NV)
232                floatBuffer = RTF_NV;
233            if(instr == "GLX_ATI_pixel_format_float" && floatBuffer<RTF_ATI)
234                floatBuffer = RTF_ATI;
235            if(instr == "GLX_ARB_fbconfig_float" && floatBuffer<RTF_ARB)
236                floatBuffer = RTF_ARB;
237        }
238        return floatBuffer;
239    }
240 
241}
Note: See TracBrowser for help on using the repository browser.