GRASS Programmer's Manual  6.4.2(2012)
nviz/render.c
Go to the documentation of this file.
00001 
00015 #include <grass/glocale.h>
00016 #include <grass/nviz.h>
00017 
00024 struct render_window *Nviz_new_render_window(void)
00025 {
00026     struct render_window *rwin;
00027 
00028     /* G_malloc() calls G_fatal_error() on failure */
00029     rwin = (struct render_window *)G_malloc(sizeof(struct render_window));
00030 
00031     return rwin;
00032 }
00033 
00039 void Nviz_init_render_window(struct render_window *rwin)
00040 {
00041 #if defined(OPENGL_X11)
00042     rwin->displayId = NULL;
00043     rwin->contextId = NULL;
00044     rwin->pixmap = 0;
00045     rwin->windowId = 0;
00046 #elif defined(OPENGL_AQUA)
00047     rwin->pixelFmtId = NULL;
00048     rwin->contextId = NULL;
00049     rwin->windowId = NULL;
00050 #elif defined(OPENGL_WINDOWS)
00051     rwin->displayId = NULL;
00052     rwin->contextId = NULL;
00053     rwin->bitmapId = NULL;
00054 #endif
00055 }
00056 
00062 void Nviz_destroy_render_window(struct render_window *rwin)
00063 {
00064 #if defined(OPENGL_X11)
00065     glXDestroyContext(rwin->displayId, rwin->contextId);
00066     glXDestroyGLXPixmap(rwin->displayId, rwin->windowId);
00067     XFreePixmap(rwin->displayId, rwin->pixmap);
00068 #elif defined(OPENGL_AQUA)
00069     aglDestroyPixelFormat(rwin->pixelFmtId);
00070     aglDestroyContext(rwin->contextId);
00071     aglDestroyPBuffer(rwin->windowId);
00072     /* TODO FreePixMap */
00073 #elif defined(OPENGL_WINDOWS)
00074     wglDeleteContext(rwin->contextId);
00075     DeleteDC(rwin->displayId);
00076     DeleteObject(rwin->bitmapId);
00077 #endif
00078 
00079     G_free((void *)rwin);
00080 }
00081 
00092 int Nviz_create_render_window(struct render_window *rwin, void *display,
00093                               int width, int height)
00094 {
00095 #if defined(OPENGL_X11)
00096     int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1,
00097         GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
00098         GLX_DEPTH_SIZE, 1, None
00099     };
00100     XVisualInfo *v;
00101 
00102     rwin->displayId = XOpenDisplay((char *)display);
00103     if (!rwin->displayId) {
00104         G_fatal_error(_("Bad server connection"));
00105     }
00106 
00107     v = glXChooseVisual(rwin->displayId,
00108                         DefaultScreen(rwin->displayId), attributeList);
00109 
00110     rwin->contextId = glXCreateContext(rwin->displayId, v, NULL, GL_FALSE);
00111 
00112     if (!rwin->contextId) {
00113         G_fatal_error(_("Unable to create rendering context"));
00114     }
00115 
00116     /* create win pixmap to render to (same depth as RootWindow) */
00117     rwin->pixmap = XCreatePixmap(rwin->displayId,
00118                                  RootWindow(rwin->displayId, v->screen),
00119                                  width, height, v->depth);
00120 
00121     /* create an off-screen GLX rendering area */
00122     rwin->windowId = glXCreateGLXPixmap(rwin->displayId, v, rwin->pixmap);
00123 
00124     if (v) {
00125         XFree(v);
00126     }
00127 #elif defined(OPENGL_AQUA)
00128     int attributeList[] = { AGL_RGBA, AGL_RED_SIZE, 1,
00129         AGL_GREEN_SIZE, 1, AGL_BLUE_SIZE, 1,
00130         AGL_DEPTH_SIZE, 1, AGL_NONE
00131     };
00132     /* TODO: open mac display */
00133 
00134     /* TODO: dev = NULL, ndev = 0 ? */
00135     rwin->pixelFmtId = aglChoosePixelFormat(NULL, 0, attributeList);
00136 
00137     rwin->contextId = aglCreateContext(rwin->pixelFmtId, NULL);
00138 
00139     /* create an off-screen AGL rendering area */
00140     aglCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGBA, 0, &(rwin->windowId));
00141 #elif defined(OPENGL_WINDOWS)
00142     PIXELFORMATDESCRIPTOR pfd = {
00143         sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd 
00144         1,                      /* version number           */
00145         PFD_DRAW_TO_WINDOW |    /* support window           */
00146             PFD_SUPPORT_OPENGL |        /* support OpenGL           */
00147             PFD_DOUBLEBUFFER,   /* double buffered          */
00148         PFD_TYPE_RGBA,          /* RGBA type                */
00149         24,                     /* 24-bit color depth       */
00150         0, 0, 0, 0, 0, 0,       /* color bits ignored       */
00151         0,                      /* no alpha buffer          */
00152         0,                      /* shift bit ignored        */
00153         0,                      /* no accumulation buffer   */
00154         0, 0, 0, 0,             /* accum bits ignored       */
00155         32,                     /* 32-bit z-buffer          */
00156         0,                      /* no stencil buffer        */
00157         0,                      /* no auxiliary buffer      */
00158         PFD_MAIN_PLANE,         /* main layer               */
00159         0,                      /* reserved                 */
00160         0, 0, 0                 /* layer masks ignored      */
00161     };
00162     int iPixelFormat;
00163 
00164     rwin->displayId = CreateCompatibleDC(NULL);
00165     iPixelFormat = ChoosePixelFormat(rwin->displayId, &pfd);
00166     SetPixelFormat(rwin->displayId, iPixelFormat, &pfd);
00167     rwin->bitmapId = CreateCompatibleBitmap(rwin->displayId, width, height);
00168     SelectObject(rwin->displayId, rwin->bitmapId);
00169     rwin->contextId = wglCreateContext(rwin->displayId);
00170     /* TODO */
00171 #endif
00172     return 1;
00173 }
00174 
00183 int Nviz_make_current_render_window(const struct render_window *rwin)
00184 {
00185 #if defined(OPENGL_X11)
00186     if (!rwin->displayId || !rwin->contextId)
00187         return 0;
00188 
00189     if (rwin->contextId == glXGetCurrentContext())
00190         return 1;
00191 
00192     glXMakeCurrent(rwin->displayId, rwin->windowId, rwin->contextId);
00193 #elif defined(OPENGL_AQUA)
00194     if (!rwin->contextId)
00195         return 0;
00196 
00197     if (rwin->contextId == aglGetCurrentContext())
00198         return 1;
00199 
00200     aglSetCurrentContext(rwin->contextId);
00201     aglSetPBuffer(rwin->contextId, rwin->windowId, 0, 0, 0);
00202 #elif defined(OPENGL_WINDOWS)
00203     if (!rwin->displayId || !rwin->contextId)
00204         return 0;
00205 
00206     wglMakeCurrent(rwin->displayId, rwin->contextId);
00207 #endif
00208 
00209     return 1;
00210 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines