5 * Revision 1.2 2004/11/18 19:10:34 curt
6 * Abstract out location of gl.h, glut.h, and glu.h includes so that we can
7 * make the Mac platform happy since they put these in a different place compared
8 * to the rest of the world.
10 * Revision 1.1.1.1 2002/09/07 02:58:19 curt
11 * Initial revsion of Simgear-0.3.0
13 * Revision 1.3 2001/07/30 20:34:21 curt
16 * Revision 1.2 2001/06/27 02:48:01 curt
17 * Fixed a type conversion bug that could trip up some of the pickier compilers
20 * Revision 1.1 2001/06/26 15:19:39 curt
21 * Added tr.cxx / tr.h, Brian Paul's LGPL'd tiled rendering support libs for
22 * rendering ultra high res "tiled" screen shots.
24 * Revision 1.9 1998/01/29 16:56:54 brianp
25 * allow trOrtho() and trFrustum() to be called at any time, minor clean-up
27 * Revision 1.8 1998/01/28 19:47:39 brianp
28 * minor clean-up for C++
30 * Revision 1.7 1997/07/21 17:34:38 brianp
33 * Revision 1.6 1997/07/21 15:47:35 brianp
34 * renamed all "near" and "far" variables
36 * Revision 1.5 1997/04/26 21:23:25 brianp
37 * added trRasterPos3f function
39 * Revision 1.4 1997/04/26 19:59:36 brianp
40 * set CurrentTile to -1 before first tile and after last tile
42 * Revision 1.3 1997/04/22 23:51:15 brianp
43 * added WIN32 header stuff, removed tabs
45 * Revision 1.2 1997/04/19 23:26:10 brianp
48 * Revision 1.1 1997/04/18 21:53:05 brianp
55 * Tiled Rendering library
57 * Copyright (C) Brian Paul
61 #include <simgear/compiler.h>
77 #define DEFAULT_TILE_WIDTH 256
78 #define DEFAULT_TILE_HEIGHT 256
79 #define DEFAULT_TILE_BORDER 0
83 /* Final image parameters */
84 GLint ImageWidth, ImageHeight;
85 GLenum ImageFormat, ImageType;
89 GLint TileWidth, TileHeight;
90 GLint TileWidthNB, TileHeightNB;
92 GLenum TileFormat, TileType;
95 /* Projection parameters */
96 GLboolean Perspective;
108 GLint CurrentTileWidth, CurrentTileHeight;
109 GLint CurrentRow, CurrentColumn;
111 GLint ViewportSave[4];
117 * Misc setup including computing number of tiles (rows and columns).
119 static void Setup(TRcontext *tr)
124 tr->Columns = (tr->ImageWidth + tr->TileWidthNB - 1) / tr->TileWidthNB;
125 tr->Rows = (tr->ImageHeight + tr->TileHeightNB - 1) / tr->TileHeightNB;
128 assert(tr->Columns >= 0);
129 assert(tr->Rows >= 0);
134 TRcontext *trNew(void)
136 TRcontext *tr = (TRcontext *) calloc(1, sizeof(TRcontext));
138 tr->TileWidth = DEFAULT_TILE_WIDTH;
139 tr->TileHeight = DEFAULT_TILE_HEIGHT;
140 tr->TileBorder = DEFAULT_TILE_BORDER;
141 tr->RowOrder = TR_BOTTOM_TO_TOP;
142 tr->CurrentTile = -1;
144 return (TRcontext *) tr;
148 void trDelete(TRcontext *tr)
156 void trTileSize(TRcontext *tr, GLint width, GLint height, GLint border)
164 assert(width >= 2*border);
165 assert(height >= 2*border);
167 tr->TileBorder = border;
168 tr->TileWidth = width;
169 tr->TileHeight = height;
170 tr->TileWidthNB = width - 2 * border;
171 tr->TileHeightNB = height - 2 * border;
177 void trTileBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
182 tr->TileFormat = format;
184 tr->TileBuffer = image;
189 void trImageSize(TRcontext *tr, GLint width, GLint height)
194 tr->ImageWidth = width;
195 tr->ImageHeight = height;
200 void trImageBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
205 tr->ImageFormat = format;
206 tr->ImageType = type;
207 tr->ImageBuffer = image;
211 GLint trGet(TRcontext *tr, TRenum param)
217 return tr->TileWidth;
219 return tr->TileHeight;
221 return tr->TileBorder;
223 return tr->ImageWidth;
224 case TR_IMAGE_HEIGHT:
225 return tr->ImageHeight;
231 if (tr->CurrentTile<0)
234 return tr->CurrentRow;
235 case TR_CURRENT_COLUMN:
236 if (tr->CurrentTile<0)
239 return tr->CurrentColumn;
240 case TR_CURRENT_TILE_WIDTH:
241 return tr->CurrentTileWidth;
242 case TR_CURRENT_TILE_HEIGHT:
243 return tr->CurrentTileHeight;
245 return (GLint) tr->RowOrder;
251 GLdouble trGetD(TRcontext *tr, TRenum param)
273 void trRowOrder(TRcontext *tr, TRenum order)
278 if (order==TR_TOP_TO_BOTTOM || order==TR_BOTTOM_TO_TOP)
279 tr->RowOrder = order;
283 void trOrtho(TRcontext *tr,
284 GLdouble left, GLdouble right,
285 GLdouble bottom, GLdouble top,
286 GLdouble zNear, GLdouble zFar)
291 tr->Perspective = GL_FALSE;
301 void trFrustum(TRcontext *tr,
302 GLdouble left, GLdouble right,
303 GLdouble bottom, GLdouble top,
304 GLdouble zNear, GLdouble zFar)
309 tr->Perspective = GL_TRUE;
319 void trPerspective(TRcontext *tr,
320 GLdouble fovy, GLdouble aspect,
321 GLdouble zNear, GLdouble zFar )
323 GLdouble xmin, xmax, ymin, ymax;
324 ymax = zNear * tan(fovy * 3.14159265 / 360.0);
326 xmin = ymin * aspect;
327 xmax = ymax * aspect;
328 trFrustum(tr, xmin, xmax, ymin, ymax, zNear, zFar);
332 void trBeginTile(TRcontext *tr)
335 GLint tileWidth, tileHeight, border;
336 GLdouble left, right, bottom, top;
341 if (tr->CurrentTile <= 0) {
343 /* Save user's viewport, will be restored after last tile rendered */
344 glGetIntegerv(GL_VIEWPORT, tr->ViewportSave);
347 /* which tile (by row and column) we're about to render */
348 if (tr->RowOrder==TR_BOTTOM_TO_TOP) {
349 tr->CurrentRow = tr->CurrentTile / tr->Columns;
350 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
352 else if (tr->RowOrder==TR_TOP_TO_BOTTOM) {
353 tr->CurrentRow = tr->Rows - (tr->CurrentTile / tr->Columns) - 1;
354 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
357 /* This should never happen */
360 assert(tr->CurrentRow < tr->Rows);
361 assert(tr->CurrentColumn < tr->Columns);
363 border = tr->TileBorder;
365 /* Compute actual size of this tile with border */
366 if (tr->CurrentRow < tr->Rows-1)
367 tileHeight = tr->TileHeight;
369 tileHeight = tr->ImageHeight - (tr->Rows-1) * (tr->TileHeightNB) + 2 * border;
371 if (tr->CurrentColumn < tr->Columns-1)
372 tileWidth = tr->TileWidth;
374 tileWidth = tr->ImageWidth - (tr->Columns-1) * (tr->TileWidthNB) + 2 * border;
376 /* Save tile size, with border */
377 tr->CurrentTileWidth = tileWidth;
378 tr->CurrentTileHeight = tileHeight;
380 glViewport(0, 0, tileWidth, tileHeight); /* tile size including border */
382 /* save current matrix mode */
383 glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
384 glMatrixMode(GL_PROJECTION);
387 /* compute projection parameters */
388 left = tr->Left + (tr->Right - tr->Left)
389 * (tr->CurrentColumn * tr->TileWidthNB - border) / tr->ImageWidth;
390 right = left + (tr->Right - tr->Left) * tileWidth / tr->ImageWidth;
391 bottom = tr->Bottom + (tr->Top - tr->Bottom)
392 * (tr->CurrentRow * tr->TileHeightNB - border) / tr->ImageHeight;
393 top = bottom + (tr->Top - tr->Bottom) * tileHeight / tr->ImageHeight;
395 ssgSetFrustum ( left, right, bottom, top, tr->Near, tr->Far );
397 /* restore user's matrix mode */
398 glMatrixMode( (GLenum)matrixMode );
403 int trEndTile(TRcontext *tr)
405 GLint prevRowLength, prevSkipRows, prevSkipPixels /*, prevAlignment */;
410 assert(tr->CurrentTile>=0);
412 /* be sure OpenGL rendering is finished */
415 /* save current glPixelStore values */
416 glGetIntegerv(GL_PACK_ROW_LENGTH, &prevRowLength);
417 glGetIntegerv(GL_PACK_SKIP_ROWS, &prevSkipRows);
418 glGetIntegerv(GL_PACK_SKIP_PIXELS, &prevSkipPixels);
419 /*glGetIntegerv(GL_PACK_ALIGNMENT, &prevAlignment);*/
421 if (tr->TileBuffer) {
422 GLint srcX = tr->TileBorder;
423 GLint srcY = tr->TileBorder;
424 GLint srcWidth = tr->TileWidthNB;
425 GLint srcHeight = tr->TileHeightNB;
426 glReadPixels(srcX, srcY, srcWidth, srcHeight,
427 tr->TileFormat, tr->TileType, tr->TileBuffer);
430 if (tr->ImageBuffer) {
431 GLint srcX = tr->TileBorder;
432 GLint srcY = tr->TileBorder;
433 GLint srcWidth = tr->CurrentTileWidth - 2 * tr->TileBorder;
434 GLint srcHeight = tr->CurrentTileHeight - 2 * tr->TileBorder;
435 GLint destX = tr->TileWidthNB * tr->CurrentColumn;
436 GLint destY = tr->TileHeightNB * tr->CurrentRow;
438 /* setup pixel store for glReadPixels */
439 glPixelStorei(GL_PACK_ROW_LENGTH, tr->ImageWidth);
440 glPixelStorei(GL_PACK_SKIP_ROWS, destY);
441 glPixelStorei(GL_PACK_SKIP_PIXELS, destX);
442 /*glPixelStorei(GL_PACK_ALIGNMENT, 1);*/
444 /* read the tile into the final image */
445 glReadPixels(srcX, srcY, srcWidth, srcHeight,
446 tr->ImageFormat, tr->ImageType, tr->ImageBuffer);
449 /* restore previous glPixelStore values */
450 glPixelStorei(GL_PACK_ROW_LENGTH, prevRowLength);
451 glPixelStorei(GL_PACK_SKIP_ROWS, prevSkipRows);
452 glPixelStorei(GL_PACK_SKIP_PIXELS, prevSkipPixels);
453 /*glPixelStorei(GL_PACK_ALIGNMENT, prevAlignment);*/
455 /* increment tile counter, return 1 if more tiles left to render */
457 if (tr->CurrentTile >= tr->Rows * tr->Columns) {
458 /* restore user's viewport */
459 glViewport(tr->ViewportSave[0], tr->ViewportSave[1],
460 tr->ViewportSave[2], tr->ViewportSave[3]);
461 tr->CurrentTile = -1; /* all done */
470 * Replacement for glRastePos3f() which avoids the problem with invalid
473 void trRasterPos3f(TRcontext *tr, GLfloat x, GLfloat y, GLfloat z)
475 if (tr->CurrentTile<0) {
476 /* not doing tile rendering right now. Let OpenGL do this. */
477 glRasterPos3f(x, y, z);
480 GLdouble modelview[16], proj[16];
482 GLdouble winX, winY, winZ;
484 /* Get modelview, projection and viewport */
485 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
486 glGetDoublev(GL_PROJECTION_MATRIX, proj);
489 viewport[2] = tr->CurrentTileWidth;
490 viewport[3] = tr->CurrentTileHeight;
492 /* Project object coord to window coordinate */
493 if (gluProject(x, y, z, modelview, proj, viewport, &winX, &winY, &winZ)){
495 /* set raster pos to window coord (0,0) */
496 glMatrixMode(GL_MODELVIEW);
499 glMatrixMode(GL_PROJECTION);
502 glOrtho(0.0, tr->CurrentTileWidth,
503 0.0, tr->CurrentTileHeight, 0.0, 1.0);
504 glRasterPos3f(0.0, 0.0, -winZ);
506 /* Now use empty bitmap to adjust raster position to (winX,winY) */
508 GLubyte bitmap[1] = {0};
509 glBitmap(1, 1, 0.0, 0.0, winX, winY, bitmap);
512 /* restore original matrices */
513 glPopMatrix(); /*proj*/
514 glMatrixMode(GL_MODELVIEW);
519 printf("GL error!\n");