00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00033
00034 #ifndef GROOVX_GFX_AGLWRAPPER_CC_UTC20051112002830_DEFINED
00035 #define GROOVX_GFX_AGLWRAPPER_CC_UTC20051112002830_DEFINED
00036
00037 #ifdef GVX_GL_PLATFORM_AGL
00038
00039 #define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_3
00040
00041 #include "gfx/aglwrapper.h"
00042
00043 #include "gfx/glxopts.h"
00044
00045 #include "rutz/error.h"
00046
00047 #include "rutz/debug.h"
00048 GVX_DBG_REGISTER
00049 #include "rutz/trace.h"
00050
00051 namespace
00052 {
00053
00054 class AttribList
00055 {
00056 private:
00057 enum { MAXSIZE = 1000 };
00058
00059 void push(GLint v)
00060 {
00061 GVX_ASSERT(next < MAXSIZE);
00062 data[next++] = v;
00063 }
00064
00065 void cap()
00066 {
00067 data[next] = AGL_NONE;
00068 }
00069
00070 GLint data[MAXSIZE];
00071 int next;
00072
00073 public:
00075 AttribList(const GlxOpts& opts) : next(0)
00076 {
00077 if (opts.rgbaFlag) this->rgba(opts.rgbaRed, opts.rgbaGreen, opts.rgbaBlue,
00078 opts.alphaFlag ? opts.alphaSize : -1);
00079
00080 else this->colorIndex( opts.colorIndexSize );
00081
00082 if (opts.depthFlag) this->depthBuffer( opts.depthSize );
00083
00084 if (opts.doubleFlag) this->doubleBuffer();
00085
00086 if (opts.stencilFlag) this->stencilBuffer( opts.stencilSize );
00087
00088 if (opts.accumFlag) this->accum(opts.accumRed, opts.accumGreen, opts.accumBlue,
00089 opts.alphaFlag ? opts.accumAlpha : -1);
00090
00091 if (opts.auxNumber > 0) this->auxBuffers( opts.auxNumber );
00092
00093 if (opts.level != 0) this->level( opts.level );
00094
00095 if (opts.transparent) this->transparent();
00096
00097 if (!opts.indirect) this->accelerated();
00098 }
00099
00101 GLint* get() { cap(); return data; }
00102
00104 void rgba(int rbits, int gbits, int bbits, int abits = -1)
00105 {
00106 push( AGL_RGBA );
00107 push( AGL_RED_SIZE );
00108 push( rbits ); dbg_eval(3, rbits);
00109 push( AGL_GREEN_SIZE );
00110 push( gbits ); dbg_eval(3, gbits);
00111 push( AGL_BLUE_SIZE );
00112 push( bbits ); dbg_eval_nl(3, bbits);
00113 if (abits > 0)
00114 {
00115 push( AGL_ALPHA_SIZE );
00116 push( abits ); dbg_eval_nl(3, abits);
00117 }
00118 }
00119
00121 void level(int lev)
00122 {
00123 push( AGL_LEVEL );
00124 push( lev );
00125 }
00126
00128 void colorIndex(int bits)
00129 {
00130 push( AGL_BUFFER_SIZE );
00131 push( bits );
00132 }
00133
00135 void depthBuffer(int bits)
00136 {
00137 push( AGL_DEPTH_SIZE );
00138 push( bits );
00139 }
00140
00142 void doubleBuffer()
00143 {
00144 push( AGL_DOUBLEBUFFER );
00145 }
00146
00148 void stencilBuffer(int bits)
00149 {
00150 push( AGL_STENCIL_SIZE );
00151 push( bits );
00152 }
00153
00155 void accum(int rbits, int gbits, int bbits, int abits = -1)
00156 {
00157 push( AGL_ACCUM_RED_SIZE );
00158 push( rbits );
00159 push( AGL_ACCUM_GREEN_SIZE );
00160 push( gbits );
00161 push( AGL_ACCUM_BLUE_SIZE );
00162 push( bbits );
00163 if (abits > 0)
00164 {
00165 push( AGL_ACCUM_ALPHA_SIZE );
00166 push( abits );
00167 }
00168 }
00169
00171 void auxBuffers(int n)
00172 {
00173 push( AGL_AUX_BUFFERS );
00174 push( n );
00175 }
00176
00178 void accelerated()
00179 {
00180 push( AGL_ACCELERATED );
00181 }
00182
00184 void transparent()
00185 {
00186
00187 }
00188 };
00189 }
00190
00191 AglWrapper::AglWrapper(GlxOpts& opts) :
00192 itsPixFormat(0),
00193 itsContext(0),
00194 itsDrawable(0)
00195 {
00196 GVX_TRACE("AglWrapper::AglWrapper");
00197
00198
00199 AGLDevice gdev = GetMainDevice();
00200 const GLint ndev = 1;
00201
00202 AttribList attribs(opts);
00203
00204 itsPixFormat = aglChoosePixelFormat(&gdev, ndev, attribs.get());
00205
00206 if (itsPixFormat == 0)
00207 throw rutz::error("couldn't choose Apple-OpenGL pixel format",
00208 SRC_POS);
00209
00210 AGLContext share = 0;
00211
00212 itsContext = aglCreateContext(itsPixFormat, share);
00213
00214 if (itsContext == 0)
00215 throw rutz::error("couldn't create Apple-OpenGL graphics context",
00216 SRC_POS);
00217 }
00218
00219 AglWrapper* AglWrapper::make(GlxOpts& opts)
00220 {
00221 GVX_TRACE("AglWrapper::make");
00222 return new AglWrapper(opts);
00223 }
00224
00225 AglWrapper::~AglWrapper()
00226 {
00227 GVX_TRACE("AglWrapper::~AglWrapper");
00228
00229 aglDestroyPixelFormat(itsPixFormat);
00230 itsPixFormat = 0;
00231
00232 aglDestroyContext(itsContext);
00233 itsContext = 0;
00234 }
00235
00236 bool AglWrapper::isDirect() const
00237 {
00238 GVX_TRACE("AglWrapper::isDirect");
00239
00240 GLint value = 0;
00241 int status = aglDescribePixelFormat(itsPixFormat, AGL_ACCELERATED, &value);
00242
00243 if (status == GL_FALSE)
00244 throw rutz::error("couldn't get Apple-OpenGL accelerated attribute",
00245 SRC_POS);
00246
00247 return (value == GL_TRUE);
00248 }
00249
00250 bool AglWrapper::isDoubleBuffered() const
00251 {
00252 GVX_TRACE("AglWrapper::isDoubleBuffered");
00253
00254 GLint value = 0;
00255 int status = aglDescribePixelFormat(itsPixFormat, AGL_DOUBLEBUFFER, &value);
00256
00257 if (status == GL_FALSE)
00258 throw rutz::error("couldn't get Apple-OpenGL doublebuffer attribute",
00259 SRC_POS);
00260
00261 return (value == GL_TRUE);
00262 }
00263
00264 unsigned int AglWrapper::bitsPerPixel() const
00265 {
00266 GVX_TRACE("AglWrapper::bitsPerPixel");
00267
00268 GLint value = 0;
00269 int status = aglDescribePixelFormat(itsPixFormat, AGL_PIXEL_SIZE, &value);
00270
00271 if (status == GL_FALSE)
00272 throw rutz::error("couldn't get Apple-OpenGL pixel size attribute",
00273 SRC_POS);
00274
00275 GVX_ASSERT(value > 0);
00276
00277 return static_cast<unsigned int>(value);
00278 }
00279
00280 void AglWrapper::makeCurrent()
00281 {
00282 GVX_TRACE("AglWrapper::makeCurrent");
00283
00284 GVX_ASSERT(itsDrawable != 0);
00285
00286 if (GVX_DBG_LEVEL() >= 3)
00287 {
00288 WindowRef macWindow = GetWindowFromPort(itsDrawable);
00289 Rect rectPort;
00290 GetWindowPortBounds(macWindow, &rectPort);
00291 dbg_eval(0, rectPort.right);
00292 dbg_eval_nl(0, rectPort.left);
00293 dbg_eval(0, rectPort.bottom);
00294 dbg_eval_nl(0, rectPort.top);
00295 }
00296
00297 int status1 = aglSetDrawable(itsContext, itsDrawable);
00298
00299 if (status1 == GL_FALSE)
00300 throw rutz::error("couldn't set Apple-OpenGL drawable", SRC_POS);
00301
00302 int status2 = aglSetCurrentContext(itsContext);
00303
00304 if (status2 == GL_FALSE)
00305 throw rutz::error("couldn't set current Apple-OpenGL context", SRC_POS);
00306
00307 int status3 = aglUpdateContext(itsContext);
00308
00309 if (status3 == GL_FALSE)
00310 throw rutz::error("couldn't update Apple-OpenGL context", SRC_POS);
00311 }
00312
00313 void AglWrapper::onReshape(int , int )
00314 {
00315 GVX_TRACE("AglWrapper::onReshape");
00316
00317 int status = aglUpdateContext(itsContext);
00318
00319 if (status == GL_FALSE)
00320 throw rutz::error("couldn't update Apple-OpenGL context", SRC_POS);
00321 }
00322
00323 void AglWrapper::swapBuffers() const
00324 {
00325 GVX_TRACE("AglWrapper::swapBuffers");
00326 aglSwapBuffers(itsContext);
00327 }
00328
00329 #endif // GVX_GL_PLATFORM_AGL
00330
00331 static const char __attribute__((used)) vcid_groovx_gfx_aglwrapper_cc_utc20051112002830[] = "$Id: aglwrapper.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00332 #endif // !GROOVX_GFX_AGLWRAPPER_CC_UTC20051112002830DEFINED