4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): 2007 Blender Foundation (refactor)
25 * ***** END GPL LICENSE BLOCK *****
28 * Subwindow opengl handling.
29 * BTW: subwindows open/close in X11 are way too slow, tried it, and choose for my own system... (ton)
35 #include "MEM_guardedalloc.h"
37 #include "DNA_windowmanager_types.h"
38 #include "DNA_screen_types.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
43 #include "BKE_context.h"
44 #include "BKE_global.h"
47 #include "BIF_glutil.h"
50 #include "wm_subwindow.h"
51 #include "wm_window.h"
53 /* wmSubWindow stored in wmWindow... but not exposed outside this C file */
54 /* it seems a bit redundant (area regions can store it too, but we keep it
55 because we can store all kind of future opengl fanciness here */
57 /* we use indices and array because:
58 - index has safety, no pointers from this C file hanging around
59 - fast lookups of indices with array, list would give overhead
60 - old code used it this way...
61 - keep option open to have 2 screens using same window
64 typedef struct wmSubWindow {
65 struct wmSubWindow *next, *prev;
70 float viewmat[4][4], winmat[4][4];
71 float viewmat1[4][4], winmat1[4][4];
75 /* ******************* open, free, set, get data ******************** */
77 /* not subwindow itself */
78 static void wm_subwindow_free(wmSubWindow *swin)
80 /* future fancy stuff */
83 void wm_subwindows_free(wmWindow *win)
87 for(swin= win->subwindows.first; swin; swin= swin->next)
88 wm_subwindow_free(swin);
90 BLI_freelistN(&win->subwindows);
94 int wm_subwindow_get(wmWindow *win)
97 return win->curswin->swinid;
101 static wmSubWindow *swin_from_swinid(wmWindow *win, int swinid)
105 for(swin= win->subwindows.first; swin; swin= swin->next)
106 if(swin->swinid==swinid)
111 void wm_subwindow_getsize(wmWindow *win, int swinid, int *x, int *y)
113 wmSubWindow *swin= swin_from_swinid(win, swinid);
116 *x= swin->winrct.xmax - swin->winrct.xmin + 1;
117 *y= swin->winrct.ymax - swin->winrct.ymin + 1;
121 void wm_subwindow_getorigin(wmWindow *win, int swinid, int *x, int *y)
123 wmSubWindow *swin= swin_from_swinid(win, swinid);
126 *x= swin->winrct.xmin;
127 *y= swin->winrct.ymin;
131 void wm_subwindow_getmatrix(wmWindow *win, int swinid, float mat[][4])
133 wmSubWindow *swin= swin_from_swinid(win, swinid);
136 Mat4MulMat4(mat, swin->viewmat, swin->winmat);
139 /* always sets pixel-precise 2D window/view matrices */
140 /* coords is in whole pixels. xmin = 15, xmax= 16: means window is 2 pix big */
141 int wm_subwindow_open(wmWindow *win, rcti *winrct)
147 for(swin= win->subwindows.first; swin; swin= swin->next)
148 if(freewinid <= swin->swinid)
149 freewinid= swin->swinid+1;
151 win->curswin= swin= MEM_callocN(sizeof(wmSubWindow), "swinopen");
152 BLI_addtail(&win->subwindows, swin);
154 if(G.f & G_DEBUG) printf("swin %d added\n", freewinid);
155 swin->swinid= freewinid;
156 swin->winrct= *winrct;
158 Mat4One(swin->viewmat);
159 Mat4One(swin->winmat);
161 /* and we appy it all right away */
162 wmSubWindowSet(win, swin->swinid);
165 wm_subwindow_getsize(win, swin->swinid, &width, &height);
166 wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
173 void wm_subwindow_close(wmWindow *win, int swinid)
175 wmSubWindow *swin= swin_from_swinid(win, swinid);
178 if (swin==win->curswin)
180 wm_subwindow_free(swin);
181 BLI_remlink(&win->subwindows, swin);
185 printf("wm_subwindow_close: Internal error, bad winid: %d\n", swinid);
190 /* pixels go from 0-99 for a 100 pixel window */
191 void wm_subwindow_position(wmWindow *win, int swinid, rcti *winrct)
193 wmSubWindow *swin= swin_from_swinid(win, swinid);
198 swin->winrct= *winrct;
200 /* CRITICAL, this clamping ensures that
201 * the viewport never goes outside the screen
202 * edges (assuming the x, y coords aren't
203 * outside). This caused a hardware lock
204 * on Matrox cards if it happens.
206 * Really Blender should never _ever_ try
207 * to do such a thing, but just to be safe
208 * clamp it anyway (or fix the bScreen
209 * scaling routine, and be damn sure you
210 * fixed it). - zr (2001!)
213 if (swin->winrct.xmax > win->sizex)
214 swin->winrct.xmax= win->sizex;
215 if (swin->winrct.ymax > win->sizey)
216 swin->winrct.ymax= win->sizey;
219 wmSubWindowSet(win, swinid);
220 wm_subwindow_getsize(win, swinid, &width, &height);
221 wmOrtho2(-0.375, (float)width-0.375, -0.375, (float)height-0.375);
224 printf("wm_subwindow_position: Internal error, bad winid: %d\n", swinid);
228 /* ---------------- WM versions of OpenGL calls, using glBlah() syntax ------------------------ */
229 /* ----------------- exported in WM_api.h ------------------------------------------------------ */
231 /* internal state, no threaded opengl! XXX */
232 static wmWindow *_curwindow= NULL;
233 static wmSubWindow *_curswin= NULL;
235 void wmSubWindowScissorSet(wmWindow *win, int swinid, rcti *srct)
238 _curswin= swin_from_swinid(win, swinid);
241 printf("wmSubWindowSet %d: doesn't exist\n", swinid);
245 win->curswin= _curswin;
248 width= _curswin->winrct.xmax - _curswin->winrct.xmin + 1;
249 height= _curswin->winrct.ymax - _curswin->winrct.ymin + 1;
250 glViewport(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
253 width= srct->xmax - srct->xmin + 1;
254 height= srct->ymax - srct->ymin + 1;
255 glScissor(srct->xmin, srct->ymin, width, height);
258 glScissor(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
260 glMatrixMode(GL_PROJECTION);
261 glLoadMatrixf(&_curswin->winmat[0][0]);
262 glMatrixMode(GL_MODELVIEW);
263 glLoadMatrixf(&_curswin->viewmat[0][0]);
269 /* enable the WM versions of opengl calls */
270 void wmSubWindowSet(wmWindow *win, int swinid)
272 wmSubWindowScissorSet(win, swinid, NULL);
275 void wmLoadMatrix(float mat[][4])
277 if(_curswin==NULL) return;
281 if (glaGetOneInteger(GL_MATRIX_MODE)==GL_MODELVIEW)
282 Mat4CpyMat4(_curswin->viewmat, mat);
284 Mat4CpyMat4(_curswin->winmat, mat);
287 void wmGetMatrix(float mat[][4])
289 if(_curswin==NULL) return;
291 if (glaGetOneInteger(GL_MATRIX_MODE)==GL_MODELVIEW) {
292 Mat4CpyMat4(mat, _curswin->viewmat);
294 Mat4CpyMat4(mat, _curswin->winmat);
298 void wmMultMatrix(float mat[][4])
300 if(_curswin==NULL) return;
302 glMultMatrixf((float*) mat);
304 if (glaGetOneInteger(GL_MATRIX_MODE)==GL_MODELVIEW)
305 glGetFloatv(GL_MODELVIEW_MATRIX, (float *)_curswin->viewmat);
307 glGetFloatv(GL_MODELVIEW_MATRIX, (float *)_curswin->winmat);
310 static int debugpush= 0;
312 void wmPushMatrix(void)
314 if(_curswin==NULL) return;
317 printf("wmPushMatrix error already pushed\n");
320 Mat4CpyMat4(_curswin->viewmat1, _curswin->viewmat);
321 Mat4CpyMat4(_curswin->winmat1, _curswin->winmat);
324 void wmPopMatrix(void)
326 if(_curswin==NULL) return;
329 printf("wmPopMatrix error nothing popped\n");
332 Mat4CpyMat4(_curswin->viewmat, _curswin->viewmat1);
333 Mat4CpyMat4(_curswin->winmat, _curswin->winmat1);
335 glMatrixMode(GL_PROJECTION);
336 glLoadMatrixf(&_curswin->winmat[0][0]);
337 glMatrixMode(GL_MODELVIEW);
338 glLoadMatrixf(&_curswin->viewmat[0][0]);
342 void wmGetSingleMatrix(float mat[][4])
345 Mat4MulMat4(mat, _curswin->viewmat, _curswin->winmat);
348 void wmScale(float x, float y, float z)
350 if(_curswin==NULL) return;
354 if (glaGetOneInteger(GL_MATRIX_MODE)==GL_MODELVIEW)
355 glGetFloatv(GL_MODELVIEW_MATRIX, (float *)_curswin->viewmat);
357 glGetFloatv(GL_MODELVIEW_MATRIX, (float *)_curswin->winmat);
361 void wmLoadIdentity(void)
363 if(_curswin==NULL) return;
365 if (glaGetOneInteger(GL_MATRIX_MODE)==GL_MODELVIEW)
366 Mat4One(_curswin->viewmat);
368 Mat4One(_curswin->winmat);
373 void wmFrustum(float x1, float x2, float y1, float y2, float n, float f)
377 glMatrixMode(GL_PROJECTION);
379 glFrustum(x1, x2, y1, y2, n, f);
381 glGetFloatv(GL_PROJECTION_MATRIX, (float *)_curswin->winmat);
382 glMatrixMode(GL_MODELVIEW);
386 void wmOrtho(float x1, float x2, float y1, float y2, float n, float f)
390 glMatrixMode(GL_PROJECTION);
393 glOrtho(x1, x2, y1, y2, n, f);
395 glGetFloatv(GL_PROJECTION_MATRIX, (float *)_curswin->winmat);
396 glMatrixMode(GL_MODELVIEW);
400 void wmOrtho2(float x1, float x2, float y1, float y2)
402 /* prevent opengl from generating errors */
405 wmOrtho(x1, x2, y1, y2, -100, 100);
409 /* *************************** Framebuffer color depth, for selection codes ********************** */
411 static int wm_get_colordepth(void)
413 static int mainwin_color_depth= 0;
415 if(mainwin_color_depth==0) {
418 glGetIntegerv(GL_RED_BITS, &r);
419 glGetIntegerv(GL_GREEN_BITS, &g);
420 glGetIntegerv(GL_BLUE_BITS, &b);
422 mainwin_color_depth= r + g + b;
424 printf("Color depth r %d g %d b %d\n", (int)r, (int)g, (int)b);
425 glGetIntegerv(GL_AUX_BUFFERS, &r);
426 printf("Aux buffers: %d\n", (int)r);
429 return mainwin_color_depth;
435 /* apple seems to round colors to below and up on some configs */
437 static unsigned int index_to_framebuffer(int index)
439 unsigned int i= index;
441 switch(wm_get_colordepth()) {
443 i= ((i & 0xF00)<<12) + ((i & 0xF0)<<8) + ((i & 0xF)<<4);
444 /* sometimes dithering subtracts! */
449 i= ((i & 0x7C00)<<9) + ((i & 0x3E0)<<6) + ((i & 0x1F)<<3);
454 default: // 18 bits...
455 i= ((i & 0x3F000)<<6) + ((i & 0xFC0)<<4) + ((i & 0x3F)<<2);
465 /* this is the old method as being in use for ages.... seems to work? colors are rounded to lower values */
467 static unsigned int index_to_framebuffer(int index)
469 unsigned int i= index;
471 switch(wm_get_colordepth()) {
473 i= ((i & 48)<<18) + ((i & 12)<<12) + ((i & 3)<<6);
477 i= ((i & 0xF00)<<12) + ((i & 0xF0)<<8) + ((i & 0xF)<<4);
478 /* sometimes dithering subtracts! */
483 i= ((i & 0x7C00)<<9) + ((i & 0x3E0)<<6) + ((i & 0x1F)<<3);
488 default: // 18 bits...
489 i= ((i & 0x3F000)<<6) + ((i & 0xFC0)<<4) + ((i & 0x3F)<<2);
499 void WM_set_framebuffer_index_color(int index)
501 cpack(index_to_framebuffer(index));
504 int WM_framebuffer_to_index(unsigned int col)
506 if (col==0) return 0;
508 switch(wm_get_colordepth()) {
510 return ((col & 0xC00000)>>18) + ((col & 0xC000)>>12) + ((col & 0xC0)>>6);
512 return ((col & 0xF00000)>>12) + ((col & 0xF000)>>8) + ((col & 0xF0)>>4);
515 return ((col & 0xF80000)>>9) + ((col & 0xF800)>>6) + ((col & 0xF8)>>3);
517 return col & 0xFFFFFF;
518 default: // 18 bits...
519 return ((col & 0xFC0000)>>6) + ((col & 0xFC00)>>4) + ((col & 0xFC)>>2);
524 /* ********** END MY WINDOW ************** */
526 #if 0 // XXX not used...
528 static int is_a_really_crappy_nvidia_card(void) {
529 static int well_is_it= -1;
531 /* Do you understand the implication? Do you? */
533 well_is_it= (strcmp((char*) glGetString(GL_VENDOR), "NVIDIA Corporation") == 0);
538 #endif // XXX not used...