wglgears: add srgb-mode to usage
[mesa-demos.git] / src / wgl / rtotex / pbuffer.cpp
bloba37966d297a477b26b6292f6bb9a8959b8d03855
1 //////////////////////////////////////////////////////////////////////////////////////////
2 // PBUFFER.cpp
3 // functions to set up a pbuffer
4 // Downloaded from: www.paulsprojects.net
5 // Created: 9th September 2002
6 //
7 // Copyright (c) 2006, Paul Baker
8 // Distributed under the New BSD Licence. (See accompanying file License.txt or copy at
9 // http://www.paulsprojects.net/NewBSDLicense.txt)
10 //////////////////////////////////////////////////////////////////////////////////////////
12 #include <windows.h>
13 #include <GL/glew.h>
14 #include <GL/wglew.h>
15 #include <GL/glu.h>
16 #include "log.h"
17 #include "pbuffer.h"
19 extern LOG errorLog;
21 bool PBUFFER::Init( int newWidth, int newHeight,
22 int newColorBits, int newDepthBits, int newStencilBits,
23 int numExtraIAttribs, int * extraIAttribList, int * flags)
25 //Check for pbuffer support
26 if( !WGLEW_ARB_extensions_string ||
27 !WGLEW_ARB_pixel_format ||
28 !WGLEW_ARB_pbuffer)
30 errorLog.OutputError("Extension required for pbuffer unsupported");
31 return false;
34 //set class's member variables
35 width=newWidth;
36 height=newHeight;
37 colorBits=newColorBits;
38 depthBits=newDepthBits;
39 stencilBits=newStencilBits;
41 //Get the current device context
42 HDC hCurrentDC=wglGetCurrentDC();
43 if(!hCurrentDC)
45 errorLog.OutputError("Unable to get current Device Context");
46 return false;
50 //choose pixel format
51 GLint pixelFormat;
53 const int standardIAttribList[]={ WGL_DRAW_TO_PBUFFER_ARB, 1,
54 WGL_COLOR_BITS_ARB, colorBits,
55 WGL_ALPHA_BITS_ARB, colorBits==32 ? 8 : 0,
56 WGL_DEPTH_BITS_ARB, depthBits,
57 WGL_STENCIL_BITS_ARB, stencilBits};
58 const float fAttribList[]={
59 0};
61 //add the extraIAttribList to the standardIAttribList
62 int * iAttribList=new int[sizeof(standardIAttribList)/sizeof(int)+numExtraIAttribs*2+1];
63 if(!iAttribList)
65 errorLog.OutputError("Unable to allocate space for iAttribList");
66 return false;
69 memcpy( iAttribList, standardIAttribList, sizeof(standardIAttribList));
70 memcpy( iAttribList+sizeof(standardIAttribList)/sizeof(int),
71 extraIAttribList, numExtraIAttribs*2*sizeof(int)+sizeof(int));
73 //Choose pixel format
74 unsigned int numFormats;
75 if(!wglChoosePixelFormatARB(hCurrentDC, iAttribList, fAttribList, 1,
76 &pixelFormat, &numFormats))
78 errorLog.OutputError("Unable to find a pixel format for the pbuffer");
79 return false;
82 //Create the pbuffer
83 hBuffer=wglCreatePbufferARB(hCurrentDC, pixelFormat, width, height, flags);
84 if(!hBuffer)
86 errorLog.OutputError("Unable to create pbuffer");
87 return false;
90 //Get the pbuffer's device context
91 hDC=wglGetPbufferDCARB(hBuffer);
92 if(!hDC)
94 errorLog.OutputError("Unable to get pbuffer's device context");
95 return false;
98 //Create a rendering context for the pbuffer
99 hRC=wglCreateContext(hDC);
100 if(!hRC)
102 errorLog.OutputError("Unable to create pbuffer's rendering context");
103 return false;
106 //Set and output the actual pBuffer dimensions
107 wglQueryPbufferARB(hBuffer, WGL_PBUFFER_WIDTH_ARB, &width);
108 wglQueryPbufferARB(hBuffer, WGL_PBUFFER_HEIGHT_ARB, &height);
109 errorLog.OutputSuccess("Pbuffer Created: (%d x %d)", width, height);
111 return TRUE; //success!
114 void PBUFFER::Shutdown(void)
116 if(hRC) //have a rendering context?
118 if(!wglDeleteContext(hRC)) //try to delete RC
120 errorLog.OutputError("Release of Pbuffer Rendering Context Failed.");
123 hRC=NULL; //set RC to NULL
126 if(hDC && !wglReleasePbufferDCARB(hBuffer, hDC)) //Are we able to release DC?
128 errorLog.OutputError("Release of Pbuffer Device Context Failed.");
129 hDC=NULL;
132 if(!wglDestroyPbufferARB(hBuffer))
134 errorLog.OutputError("Unable to destroy pbuffer");
139 bool PBUFFER::MakeCurrent()
141 if(!wglMakeCurrent(hDC, hRC))
143 errorLog.OutputError("Unable to change current context");
144 return false;
147 return true;