GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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  glXDestroyGLXPixmap(rwin->displayId, rwin->windowId);
66  XFreePixmap(rwin->displayId, rwin->pixmap);
67  glXDestroyContext(rwin->displayId, rwin->contextId);
68  XCloseDisplay(rwin->displayId);
69 #elif defined(OPENGL_AQUA)
70  aglDestroyPixelFormat(rwin->pixelFmtId);
71  aglDestroyContext(rwin->contextId);
72  aglDestroyPBuffer(rwin->windowId);
73  /* TODO FreePixMap */
74 #elif defined(OPENGL_WINDOWS)
75  wglDeleteContext(rwin->contextId);
76  DeleteDC(rwin->displayId);
77  DeleteObject(rwin->bitmapId);
78 #endif
79 
80  G_free((void *)rwin);
81 }
82 
93 int Nviz_create_render_window(struct render_window *rwin, void *display,
94  int width, int height)
95 {
96 #if defined(OPENGL_X11)
97  int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1,
98  GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
99  GLX_DEPTH_SIZE, 1, None
100  };
101  XVisualInfo *v;
102 
103  rwin->displayId = XOpenDisplay((char *)display);
104  if (!rwin->displayId) {
105  G_fatal_error(_("Bad server connection"));
106  }
107 
108  v = glXChooseVisual(rwin->displayId,
109  DefaultScreen(rwin->displayId), attributeList);
110 
111  rwin->contextId = glXCreateContext(rwin->displayId, v, NULL, GL_FALSE);
112 
113  if (!rwin->contextId) {
114  G_fatal_error(_("Unable to create rendering context"));
115  }
116 
117  /* create win pixmap to render to (same depth as RootWindow) */
118  rwin->pixmap = XCreatePixmap(rwin->displayId,
119  RootWindow(rwin->displayId, v->screen),
120  width, height, v->depth);
121 
122  /* create an off-screen GLX rendering area */
123  rwin->windowId = glXCreateGLXPixmap(rwin->displayId, v, rwin->pixmap);
124 
125  if (v) {
126  XFree(v);
127  }
128 #elif defined(OPENGL_AQUA)
129  int attributeList[] = { AGL_RGBA, AGL_RED_SIZE, 1,
130  AGL_GREEN_SIZE, 1, AGL_BLUE_SIZE, 1,
131  AGL_DEPTH_SIZE, 1, AGL_NONE
132  };
133  /* TODO: open mac display */
134 
135  /* TODO: dev = NULL, ndev = 0 ? */
136  rwin->pixelFmtId = aglChoosePixelFormat(NULL, 0, attributeList);
137 
138  rwin->contextId = aglCreateContext(rwin->pixelFmtId, NULL);
139 
140  /* create an off-screen AGL rendering area */
141  aglCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGBA, 0, &(rwin->windowId));
142 #elif defined(OPENGL_WINDOWS)
143  PIXELFORMATDESCRIPTOR pfd = {
144  sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
145  1, /* version number */
146  PFD_DRAW_TO_WINDOW | /* support window */
147  PFD_SUPPORT_OPENGL | /* support OpenGL */
148  PFD_DOUBLEBUFFER, /* double buffered */
149  PFD_TYPE_RGBA, /* RGBA type */
150  24, /* 24-bit color depth */
151  0, 0, 0, 0, 0, 0, /* color bits ignored */
152  0, /* no alpha buffer */
153  0, /* shift bit ignored */
154  0, /* no accumulation buffer */
155  0, 0, 0, 0, /* accum bits ignored */
156  32, /* 32-bit z-buffer */
157  0, /* no stencil buffer */
158  0, /* no auxiliary buffer */
159  PFD_MAIN_PLANE, /* main layer */
160  0, /* reserved */
161  0, 0, 0 /* layer masks ignored */
162  };
163  int iPixelFormat;
164 
165  rwin->displayId = CreateCompatibleDC(NULL);
166  iPixelFormat = ChoosePixelFormat(rwin->displayId, &pfd);
167  SetPixelFormat(rwin->displayId, iPixelFormat, &pfd);
168  rwin->bitmapId = CreateCompatibleBitmap(rwin->displayId, width, height);
169  SelectObject(rwin->displayId, rwin->bitmapId);
170  rwin->contextId = wglCreateContext(rwin->displayId);
171  /* TODO */
172 #endif
173  return 1;
174 }
175 
184 int Nviz_make_current_render_window(const struct render_window *rwin)
185 {
186 #if defined(OPENGL_X11)
187  if (!rwin->displayId || !rwin->contextId)
188  return 0;
189 
190  if (rwin->contextId == glXGetCurrentContext())
191  return 1;
192 
193  glXMakeCurrent(rwin->displayId, rwin->windowId, rwin->contextId);
194 #elif defined(OPENGL_AQUA)
195  if (!rwin->contextId)
196  return 0;
197 
198  if (rwin->contextId == aglGetCurrentContext())
199  return 1;
200 
201  aglSetCurrentContext(rwin->contextId);
202  aglSetPBuffer(rwin->contextId, rwin->windowId, 0, 0, 0);
203 #elif defined(OPENGL_WINDOWS)
204  if (!rwin->displayId || !rwin->contextId)
205  return 0;
206 
207  wglMakeCurrent(rwin->displayId, rwin->contextId);
208 #endif
209 
210  return 1;
211 }
int Nviz_make_current_render_window(const struct render_window *rwin)
Make window current for rendering.
Definition: nviz/render.c:184
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
tuple width
void Nviz_destroy_render_window(struct render_window *rwin)
Free render window.
Definition: nviz/render.c:62
struct render_window * Nviz_new_render_window(void)
Allocate memory for render window.
Definition: nviz/render.c:24
return NULL
Definition: dbfopen.c:1394
int Nviz_create_render_window(struct render_window *rwin, void *display, int width, int height)
Create render window.
Definition: nviz/render.c:93
tuple display
Definition: tools.py:1387
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int height
void Nviz_init_render_window(struct render_window *rwin)
Initialize render window.
Definition: nviz/render.c:39