GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
nviz/render.c
Go to the documentation of this file.
1 
15 #include <grass/glocale.h>
16 #include <grass/nviz.h>
17 
24 struct render_window *Nviz_new_render_window(void)
25 {
26  struct render_window *rwin;
27 
28  /* G_malloc() calls G_fatal_error() on failure */
29  rwin = (struct render_window *)G_malloc(sizeof(struct render_window));
30 
31  return rwin;
32 }
33 
39 void Nviz_init_render_window(struct render_window *rwin)
40 {
41 #if defined(OPENGL_X11)
42  rwin->displayId = NULL;
43  rwin->contextId = NULL;
44  rwin->pixmap = 0;
45  rwin->windowId = 0;
46 #elif defined(OPENGL_AQUA)
47  rwin->pixelFmtId = NULL;
48  rwin->contextId = NULL;
49  rwin->windowId = NULL;
50 #elif defined(OPENGL_WINDOWS)
51  rwin->displayId = NULL;
52  rwin->contextId = NULL;
53  rwin->bitmapId = NULL;
54 #endif
55 }
56 
62 void Nviz_destroy_render_window(struct render_window *rwin)
63 {
64 #if defined(OPENGL_X11)
65  glXDestroyContext(rwin->displayId, rwin->contextId);
66  glXDestroyGLXPixmap(rwin->displayId, rwin->windowId);
67  XFreePixmap(rwin->displayId, rwin->pixmap);
68 #elif defined(OPENGL_AQUA)
69  aglDestroyPixelFormat(rwin->pixelFmtId);
70  aglDestroyContext(rwin->contextId);
71  aglDestroyPBuffer(rwin->windowId);
72  /* TODO FreePixMap */
73 #elif defined(OPENGL_WINDOWS)
74  wglDeleteContext(rwin->contextId);
75  DeleteDC(rwin->displayId);
76  DeleteObject(rwin->bitmapId);
77 #endif
78 
79  G_free((void *)rwin);
80 }
81 
92 int Nviz_create_render_window(struct render_window *rwin, void *display,
93  int width, int height)
94 {
95 #if defined(OPENGL_X11)
96  int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1,
97  GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
98  GLX_DEPTH_SIZE, 1, None
99  };
100  XVisualInfo *v;
101 
102  rwin->displayId = XOpenDisplay((char *)display);
103  if (!rwin->displayId) {
104  G_fatal_error(_("Bad server connection"));
105  }
106 
107  v = glXChooseVisual(rwin->displayId,
108  DefaultScreen(rwin->displayId), attributeList);
109 
110  rwin->contextId = glXCreateContext(rwin->displayId, v, NULL, GL_FALSE);
111 
112  if (!rwin->contextId) {
113  G_fatal_error(_("Unable to create rendering context"));
114  }
115 
116  /* create win pixmap to render to (same depth as RootWindow) */
117  rwin->pixmap = XCreatePixmap(rwin->displayId,
118  RootWindow(rwin->displayId, v->screen),
119  width, height, v->depth);
120 
121  /* create an off-screen GLX rendering area */
122  rwin->windowId = glXCreateGLXPixmap(rwin->displayId, v, rwin->pixmap);
123 
124  if (v) {
125  XFree(v);
126  }
127 #elif defined(OPENGL_AQUA)
128  int attributeList[] = { AGL_RGBA, AGL_RED_SIZE, 1,
129  AGL_GREEN_SIZE, 1, AGL_BLUE_SIZE, 1,
130  AGL_DEPTH_SIZE, 1, AGL_NONE
131  };
132  /* TODO: open mac display */
133 
134  /* TODO: dev = NULL, ndev = 0 ? */
135  rwin->pixelFmtId = aglChoosePixelFormat(NULL, 0, attributeList);
136 
137  rwin->contextId = aglCreateContext(rwin->pixelFmtId, NULL);
138 
139  /* create an off-screen AGL rendering area */
140  aglCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGBA, 0, &(rwin->windowId));
141 #elif defined(OPENGL_WINDOWS)
142  PIXELFORMATDESCRIPTOR pfd = {
143  sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
144  1, /* version number */
145  PFD_DRAW_TO_WINDOW | /* support window */
146  PFD_SUPPORT_OPENGL | /* support OpenGL */
147  PFD_DOUBLEBUFFER, /* double buffered */
148  PFD_TYPE_RGBA, /* RGBA type */
149  24, /* 24-bit color depth */
150  0, 0, 0, 0, 0, 0, /* color bits ignored */
151  0, /* no alpha buffer */
152  0, /* shift bit ignored */
153  0, /* no accumulation buffer */
154  0, 0, 0, 0, /* accum bits ignored */
155  32, /* 32-bit z-buffer */
156  0, /* no stencil buffer */
157  0, /* no auxiliary buffer */
158  PFD_MAIN_PLANE, /* main layer */
159  0, /* reserved */
160  0, 0, 0 /* layer masks ignored */
161  };
162  int iPixelFormat;
163 
164  rwin->displayId = CreateCompatibleDC(NULL);
165  iPixelFormat = ChoosePixelFormat(rwin->displayId, &pfd);
166  SetPixelFormat(rwin->displayId, iPixelFormat, &pfd);
167  rwin->bitmapId = CreateCompatibleBitmap(rwin->displayId, width, height);
168  SelectObject(rwin->displayId, rwin->bitmapId);
169  rwin->contextId = wglCreateContext(rwin->displayId);
170  /* TODO */
171 #endif
172  return 1;
173 }
174 
183 int Nviz_make_current_render_window(const struct render_window *rwin)
184 {
185 #if defined(OPENGL_X11)
186  if (!rwin->displayId || !rwin->contextId)
187  return 0;
188 
189  if (rwin->contextId == glXGetCurrentContext())
190  return 1;
191 
192  glXMakeCurrent(rwin->displayId, rwin->windowId, rwin->contextId);
193 #elif defined(OPENGL_AQUA)
194  if (!rwin->contextId)
195  return 0;
196 
197  if (rwin->contextId == aglGetCurrentContext())
198  return 1;
199 
200  aglSetCurrentContext(rwin->contextId);
201  aglSetPBuffer(rwin->contextId, rwin->windowId, 0, 0, 0);
202 #elif defined(OPENGL_WINDOWS)
203  if (!rwin->displayId || !rwin->contextId)
204  return 0;
205 
206  wglMakeCurrent(rwin->displayId, rwin->contextId);
207 #endif
208 
209  return 1;
210 }