CmWin32RenderTexture.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmRenderSystem.h"
  25. #include "OgreException.h"
  26. #include "OgreStringConverter.h"
  27. #include "CmWin32RenderTexture.h"
  28. #include "CmWin32GLSupport.h"
  29. #include "CmWin32Context.h"
  30. namespace CamelotEngine {
  31. Win32PBuffer::Win32PBuffer(PixelComponentType format, size_t width, size_t height):
  32. GLPBuffer(format, width, height),
  33. mContext(0)
  34. {
  35. createPBuffer();
  36. // Create context
  37. mContext = new Win32Context(mHDC, mGlrc);
  38. #if 0
  39. if(mUseBind)
  40. {
  41. // Bind texture
  42. glBindTextureEXT(GL_TEXTURE_2D, static_cast<GLTexture*>(mTexture.get())->getGLID());
  43. wglBindTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
  44. }
  45. #endif
  46. }
  47. Win32PBuffer::~Win32PBuffer()
  48. {
  49. #if 0
  50. if(mUseBind)
  51. {
  52. // Unbind texture
  53. glBindTextureEXT(GL_TEXTURE_2D,
  54. static_cast<GLTexture*>(mTexture.get())->getGLID());
  55. glBindTextureEXT(GL_TEXTURE_2D,
  56. static_cast<GLTexture*>(mTexture.get())->getGLID());
  57. wglReleaseTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
  58. }
  59. #endif
  60. // Unregister and destroy mContext
  61. delete mContext;
  62. // Destroy pbuffer
  63. destroyPBuffer();
  64. }
  65. void Win32PBuffer::createPBuffer()
  66. {
  67. // Process format
  68. int bits=0;
  69. bool isFloat=false;
  70. #if 0
  71. bool hasAlpha=true;
  72. #endif
  73. switch(mFormat)
  74. {
  75. case PCT_BYTE:
  76. bits=8; isFloat=false;
  77. break;
  78. case PCT_SHORT:
  79. bits=16; isFloat=false;
  80. break;
  81. case PCT_FLOAT16:
  82. bits=16; isFloat=true;
  83. break;
  84. case PCT_FLOAT32:
  85. bits=32; isFloat=true;
  86. break;
  87. default: break;
  88. };
  89. HDC old_hdc = wglGetCurrentDC();
  90. HGLRC old_context = wglGetCurrentContext();
  91. // Bind to RGB or RGBA texture
  92. int bttype = 0;
  93. #if 0
  94. if(mUseBind)
  95. {
  96. // Only provide bind type when actually binding
  97. bttype = PixelUtil::hasAlpha(mInternalFormat)?
  98. WGL_BIND_TO_TEXTURE_RGBA_ARB : WGL_BIND_TO_TEXTURE_RGB_ARB;
  99. }
  100. int texformat = hasAlpha?
  101. WGL_TEXTURE_RGBA_ARB : WGL_TEXTURE_RGB_ARB;
  102. #endif
  103. // Make a float buffer?
  104. int pixeltype = isFloat?
  105. WGL_TYPE_RGBA_FLOAT_ARB: WGL_TYPE_RGBA_ARB;
  106. int attrib[] = {
  107. WGL_RED_BITS_ARB,bits,
  108. WGL_GREEN_BITS_ARB,bits,
  109. WGL_BLUE_BITS_ARB,bits,
  110. WGL_ALPHA_BITS_ARB,bits,
  111. WGL_STENCIL_BITS_ARB,1,
  112. WGL_DEPTH_BITS_ARB,15,
  113. WGL_DRAW_TO_PBUFFER_ARB,true,
  114. WGL_SUPPORT_OPENGL_ARB,true,
  115. WGL_PIXEL_TYPE_ARB,pixeltype,
  116. //WGL_DOUBLE_BUFFER_ARB,true,
  117. //WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB, // Make sure it is accelerated
  118. bttype,true, // must be last, as bttype can be zero
  119. 0
  120. };
  121. int pattrib_default[] = {
  122. 0
  123. };
  124. #if 0
  125. int pattrib_bind[] = {
  126. WGL_TEXTURE_FORMAT_ARB, texformat,
  127. WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB,
  128. WGL_PBUFFER_LARGEST_ARB, true,
  129. 0
  130. };
  131. #endif
  132. int format;
  133. unsigned int count;
  134. // Choose suitable pixel format
  135. wglChoosePixelFormatARB(old_hdc,attrib,NULL,1,&format,&count);
  136. if(count == 0)
  137. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglChoosePixelFormatARB() failed", " Win32PBuffer::createPBuffer");
  138. // Analyse pixel format
  139. const int piAttributes[]={
  140. WGL_RED_BITS_ARB,WGL_GREEN_BITS_ARB,WGL_BLUE_BITS_ARB,WGL_ALPHA_BITS_ARB,
  141. WGL_DEPTH_BITS_ARB,WGL_STENCIL_BITS_ARB
  142. };
  143. int piValues[sizeof(piAttributes)/sizeof(const int)];
  144. wglGetPixelFormatAttribivARB(old_hdc,format,0,sizeof(piAttributes)/sizeof(const int),piAttributes,piValues);
  145. mPBuffer = wglCreatePbufferARB(old_hdc,format,mWidth,mHeight,pattrib_default);
  146. if(!mPBuffer)
  147. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglCreatePbufferARB() failed", " Win32PBuffer::createPBuffer");
  148. mHDC = wglGetPbufferDCARB(mPBuffer);
  149. if(!mHDC) {
  150. wglDestroyPbufferARB(mPBuffer);
  151. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglGetPbufferDCARB() failed", " Win32PBuffer::createPBuffer");
  152. }
  153. mGlrc = wglCreateContext(mHDC);
  154. if(!mGlrc) {
  155. wglReleasePbufferDCARB(mPBuffer,mHDC);
  156. wglDestroyPbufferARB(mPBuffer);
  157. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglCreateContext() failed", " Win32PBuffer::createPBuffer");
  158. }
  159. if(!wglShareLists(old_context,mGlrc)) {
  160. wglDeleteContext(mGlrc);
  161. wglReleasePbufferDCARB(mPBuffer,mHDC);
  162. wglDestroyPbufferARB(mPBuffer);
  163. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglShareLists() failed", " Win32PBuffer::createPBuffer");
  164. }
  165. // Query real width and height
  166. int iWidth, iHeight;
  167. wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_WIDTH_ARB, &iWidth);
  168. wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_HEIGHT_ARB, &iHeight);
  169. mWidth = iWidth;
  170. mHeight = iHeight;
  171. }
  172. void Win32PBuffer::destroyPBuffer()
  173. {
  174. wglDeleteContext(mGlrc);
  175. wglReleasePbufferDCARB(mPBuffer,mHDC);
  176. wglDestroyPbufferARB(mPBuffer);
  177. }
  178. }