5 * Revision 1.3 2001/07/30 20:34:21 curt
8 * Revision 1.2 2001/06/27 02:48:01 curt
9 * Fixed a type conversion bug that could trip up some of the pickier compilers
12 * Revision 1.1 2001/06/26 15:19:39 curt
13 * Added tr.cxx / tr.h, Brian Paul's LGPL'd tiled rendering support libs for
14 * rendering ultra high res "tiled" screen shots.
16 * Revision 1.9 1998/01/29 16:56:54 brianp
17 * allow trOrtho() and trFrustum() to be called at any time, minor clean-up
19 * Revision 1.8 1998/01/28 19:47:39 brianp
20 * minor clean-up for C++
22 * Revision 1.7 1997/07/21 17:34:38 brianp
25 * Revision 1.6 1997/07/21 15:47:35 brianp
26 * renamed all "near" and "far" variables
28 * Revision 1.5 1997/04/26 21:23:25 brianp
29 * added trRasterPos3f function
31 * Revision 1.4 1997/04/26 19:59:36 brianp
32 * set CurrentTile to -1 before first tile and after last tile
34 * Revision 1.3 1997/04/22 23:51:15 brianp
35 * added WIN32 header stuff, removed tabs
37 * Revision 1.2 1997/04/19 23:26:10 brianp
40 * Revision 1.1 1997/04/18 21:53:05 brianp
47 * Tiled Rendering library
49 * Copyright (C) Brian Paul
53 #include <simgear/compiler.h>
68 #define DEFAULT_TILE_WIDTH 256
69 #define DEFAULT_TILE_HEIGHT 256
70 #define DEFAULT_TILE_BORDER 0
74 /* Final image parameters */
75 GLint ImageWidth, ImageHeight;
76 GLenum ImageFormat, ImageType;
80 GLint TileWidth, TileHeight;
81 GLint TileWidthNB, TileHeightNB;
83 GLenum TileFormat, TileType;
86 /* Projection parameters */
87 GLboolean Perspective;
99 GLint CurrentTileWidth, CurrentTileHeight;
100 GLint CurrentRow, CurrentColumn;
102 GLint ViewportSave[4];
108 * Misc setup including computing number of tiles (rows and columns).
110 static void Setup(TRcontext *tr)
115 tr->Columns = (tr->ImageWidth + tr->TileWidthNB - 1) / tr->TileWidthNB;
116 tr->Rows = (tr->ImageHeight + tr->TileHeightNB - 1) / tr->TileHeightNB;
119 assert(tr->Columns >= 0);
120 assert(tr->Rows >= 0);
125 TRcontext *trNew(void)
127 TRcontext *tr = (TRcontext *) calloc(1, sizeof(TRcontext));
129 tr->TileWidth = DEFAULT_TILE_WIDTH;
130 tr->TileHeight = DEFAULT_TILE_HEIGHT;
131 tr->TileBorder = DEFAULT_TILE_BORDER;
132 tr->RowOrder = TR_BOTTOM_TO_TOP;
133 tr->CurrentTile = -1;
135 return (TRcontext *) tr;
139 void trDelete(TRcontext *tr)
147 void trTileSize(TRcontext *tr, GLint width, GLint height, GLint border)
155 assert(width >= 2*border);
156 assert(height >= 2*border);
158 tr->TileBorder = border;
159 tr->TileWidth = width;
160 tr->TileHeight = height;
161 tr->TileWidthNB = width - 2 * border;
162 tr->TileHeightNB = height - 2 * border;
168 void trTileBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
173 tr->TileFormat = format;
175 tr->TileBuffer = image;
180 void trImageSize(TRcontext *tr, GLint width, GLint height)
185 tr->ImageWidth = width;
186 tr->ImageHeight = height;
191 void trImageBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
196 tr->ImageFormat = format;
197 tr->ImageType = type;
198 tr->ImageBuffer = image;
202 GLint trGet(TRcontext *tr, TRenum param)
208 return tr->TileWidth;
210 return tr->TileHeight;
212 return tr->TileBorder;
214 return tr->ImageWidth;
215 case TR_IMAGE_HEIGHT:
216 return tr->ImageHeight;
222 if (tr->CurrentTile<0)
225 return tr->CurrentRow;
226 case TR_CURRENT_COLUMN:
227 if (tr->CurrentTile<0)
230 return tr->CurrentColumn;
231 case TR_CURRENT_TILE_WIDTH:
232 return tr->CurrentTileWidth;
233 case TR_CURRENT_TILE_HEIGHT:
234 return tr->CurrentTileHeight;
236 return (GLint) tr->RowOrder;
242 GLdouble trGetD(TRcontext *tr, TRenum param)
264 void trRowOrder(TRcontext *tr, TRenum order)
269 if (order==TR_TOP_TO_BOTTOM || order==TR_BOTTOM_TO_TOP)
270 tr->RowOrder = order;
274 void trOrtho(TRcontext *tr,
275 GLdouble left, GLdouble right,
276 GLdouble bottom, GLdouble top,
277 GLdouble zNear, GLdouble zFar)
282 tr->Perspective = GL_FALSE;
292 void trFrustum(TRcontext *tr,
293 GLdouble left, GLdouble right,
294 GLdouble bottom, GLdouble top,
295 GLdouble zNear, GLdouble zFar)
300 tr->Perspective = GL_TRUE;
310 void trPerspective(TRcontext *tr,
311 GLdouble fovy, GLdouble aspect,
312 GLdouble zNear, GLdouble zFar )
314 GLdouble xmin, xmax, ymin, ymax;
315 ymax = zNear * tan(fovy * 3.14159265 / 360.0);
317 xmin = ymin * aspect;
318 xmax = ymax * aspect;
319 trFrustum(tr, xmin, xmax, ymin, ymax, zNear, zFar);
323 void trBeginTile(TRcontext *tr)
326 GLint tileWidth, tileHeight, border;
327 GLdouble left, right, bottom, top;
332 if (tr->CurrentTile <= 0) {
334 /* Save user's viewport, will be restored after last tile rendered */
335 glGetIntegerv(GL_VIEWPORT, tr->ViewportSave);
338 /* which tile (by row and column) we're about to render */
339 if (tr->RowOrder==TR_BOTTOM_TO_TOP) {
340 tr->CurrentRow = tr->CurrentTile / tr->Columns;
341 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
343 else if (tr->RowOrder==TR_TOP_TO_BOTTOM) {
344 tr->CurrentRow = tr->Rows - (tr->CurrentTile / tr->Columns) - 1;
345 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
348 /* This should never happen */
351 assert(tr->CurrentRow < tr->Rows);
352 assert(tr->CurrentColumn < tr->Columns);
354 border = tr->TileBorder;
356 /* Compute actual size of this tile with border */
357 if (tr->CurrentRow < tr->Rows-1)
358 tileHeight = tr->TileHeight;
360 tileHeight = tr->ImageHeight - (tr->Rows-1) * (tr->TileHeightNB) + 2 * border;
362 if (tr->CurrentColumn < tr->Columns-1)
363 tileWidth = tr->TileWidth;
365 tileWidth = tr->ImageWidth - (tr->Columns-1) * (tr->TileWidthNB) + 2 * border;
367 /* Save tile size, with border */
368 tr->CurrentTileWidth = tileWidth;
369 tr->CurrentTileHeight = tileHeight;
371 glViewport(0, 0, tileWidth, tileHeight); /* tile size including border */
373 /* save current matrix mode */
374 glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
375 glMatrixMode(GL_PROJECTION);
378 /* compute projection parameters */
379 left = tr->Left + (tr->Right - tr->Left)
380 * (tr->CurrentColumn * tr->TileWidthNB - border) / tr->ImageWidth;
381 right = left + (tr->Right - tr->Left) * tileWidth / tr->ImageWidth;
382 bottom = tr->Bottom + (tr->Top - tr->Bottom)
383 * (tr->CurrentRow * tr->TileHeightNB - border) / tr->ImageHeight;
384 top = bottom + (tr->Top - tr->Bottom) * tileHeight / tr->ImageHeight;
386 ssgSetFrustum ( left, right, bottom, top, tr->Near, tr->Far );
388 /* restore user's matrix mode */
389 glMatrixMode( (GLenum)matrixMode );
394 int trEndTile(TRcontext *tr)
396 GLint prevRowLength, prevSkipRows, prevSkipPixels /*, prevAlignment */;
401 assert(tr->CurrentTile>=0);
403 /* be sure OpenGL rendering is finished */
406 /* save current glPixelStore values */
407 glGetIntegerv(GL_PACK_ROW_LENGTH, &prevRowLength);
408 glGetIntegerv(GL_PACK_SKIP_ROWS, &prevSkipRows);
409 glGetIntegerv(GL_PACK_SKIP_PIXELS, &prevSkipPixels);
410 /*glGetIntegerv(GL_PACK_ALIGNMENT, &prevAlignment);*/
412 if (tr->TileBuffer) {
413 GLint srcX = tr->TileBorder;
414 GLint srcY = tr->TileBorder;
415 GLint srcWidth = tr->TileWidthNB;
416 GLint srcHeight = tr->TileHeightNB;
417 glReadPixels(srcX, srcY, srcWidth, srcHeight,
418 tr->TileFormat, tr->TileType, tr->TileBuffer);
421 if (tr->ImageBuffer) {
422 GLint srcX = tr->TileBorder;
423 GLint srcY = tr->TileBorder;
424 GLint srcWidth = tr->CurrentTileWidth - 2 * tr->TileBorder;
425 GLint srcHeight = tr->CurrentTileHeight - 2 * tr->TileBorder;
426 GLint destX = tr->TileWidthNB * tr->CurrentColumn;
427 GLint destY = tr->TileHeightNB * tr->CurrentRow;
429 /* setup pixel store for glReadPixels */
430 glPixelStorei(GL_PACK_ROW_LENGTH, tr->ImageWidth);
431 glPixelStorei(GL_PACK_SKIP_ROWS, destY);
432 glPixelStorei(GL_PACK_SKIP_PIXELS, destX);
433 /*glPixelStorei(GL_PACK_ALIGNMENT, 1);*/
435 /* read the tile into the final image */
436 glReadPixels(srcX, srcY, srcWidth, srcHeight,
437 tr->ImageFormat, tr->ImageType, tr->ImageBuffer);
440 /* restore previous glPixelStore values */
441 glPixelStorei(GL_PACK_ROW_LENGTH, prevRowLength);
442 glPixelStorei(GL_PACK_SKIP_ROWS, prevSkipRows);
443 glPixelStorei(GL_PACK_SKIP_PIXELS, prevSkipPixels);
444 /*glPixelStorei(GL_PACK_ALIGNMENT, prevAlignment);*/
446 /* increment tile counter, return 1 if more tiles left to render */
448 if (tr->CurrentTile >= tr->Rows * tr->Columns) {
449 /* restore user's viewport */
450 glViewport(tr->ViewportSave[0], tr->ViewportSave[1],
451 tr->ViewportSave[2], tr->ViewportSave[3]);
452 tr->CurrentTile = -1; /* all done */
461 * Replacement for glRastePos3f() which avoids the problem with invalid
464 void trRasterPos3f(TRcontext *tr, GLfloat x, GLfloat y, GLfloat z)
466 if (tr->CurrentTile<0) {
467 /* not doing tile rendering right now. Let OpenGL do this. */
468 glRasterPos3f(x, y, z);
471 GLdouble modelview[16], proj[16];
473 GLdouble winX, winY, winZ;
475 /* Get modelview, projection and viewport */
476 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
477 glGetDoublev(GL_PROJECTION_MATRIX, proj);
480 viewport[2] = tr->CurrentTileWidth;
481 viewport[3] = tr->CurrentTileHeight;
483 /* Project object coord to window coordinate */
484 if (gluProject(x, y, z, modelview, proj, viewport, &winX, &winY, &winZ)){
486 /* set raster pos to window coord (0,0) */
487 glMatrixMode(GL_MODELVIEW);
490 glMatrixMode(GL_PROJECTION);
493 glOrtho(0.0, tr->CurrentTileWidth,
494 0.0, tr->CurrentTileHeight, 0.0, 1.0);
495 glRasterPos3f(0.0, 0.0, -winZ);
497 /* Now use empty bitmap to adjust raster position to (winX,winY) */
499 GLubyte bitmap[1] = {0};
500 glBitmap(1, 1, 0.0, 0.0, winX, winY, bitmap);
503 /* restore original matrices */
504 glPopMatrix(); /*proj*/
505 glMatrixMode(GL_MODELVIEW);
510 printf("GL error!\n");