5 * Revision 1.1 2002/09/07 02:58:19 curt
8 * Revision 1.3 2001/07/30 20:34:21 curt
11 * Revision 1.2 2001/06/27 02:48:01 curt
12 * Fixed a type conversion bug that could trip up some of the pickier compilers
15 * Revision 1.1 2001/06/26 15:19:39 curt
16 * Added tr.cxx / tr.h, Brian Paul's LGPL'd tiled rendering support libs for
17 * rendering ultra high res "tiled" screen shots.
19 * Revision 1.9 1998/01/29 16:56:54 brianp
20 * allow trOrtho() and trFrustum() to be called at any time, minor clean-up
22 * Revision 1.8 1998/01/28 19:47:39 brianp
23 * minor clean-up for C++
25 * Revision 1.7 1997/07/21 17:34:38 brianp
28 * Revision 1.6 1997/07/21 15:47:35 brianp
29 * renamed all "near" and "far" variables
31 * Revision 1.5 1997/04/26 21:23:25 brianp
32 * added trRasterPos3f function
34 * Revision 1.4 1997/04/26 19:59:36 brianp
35 * set CurrentTile to -1 before first tile and after last tile
37 * Revision 1.3 1997/04/22 23:51:15 brianp
38 * added WIN32 header stuff, removed tabs
40 * Revision 1.2 1997/04/19 23:26:10 brianp
43 * Revision 1.1 1997/04/18 21:53:05 brianp
50 * Tiled Rendering library
52 * Copyright (C) Brian Paul
56 #include <simgear/compiler.h>
71 #define DEFAULT_TILE_WIDTH 256
72 #define DEFAULT_TILE_HEIGHT 256
73 #define DEFAULT_TILE_BORDER 0
77 /* Final image parameters */
78 GLint ImageWidth, ImageHeight;
79 GLenum ImageFormat, ImageType;
83 GLint TileWidth, TileHeight;
84 GLint TileWidthNB, TileHeightNB;
86 GLenum TileFormat, TileType;
89 /* Projection parameters */
90 GLboolean Perspective;
102 GLint CurrentTileWidth, CurrentTileHeight;
103 GLint CurrentRow, CurrentColumn;
105 GLint ViewportSave[4];
111 * Misc setup including computing number of tiles (rows and columns).
113 static void Setup(TRcontext *tr)
118 tr->Columns = (tr->ImageWidth + tr->TileWidthNB - 1) / tr->TileWidthNB;
119 tr->Rows = (tr->ImageHeight + tr->TileHeightNB - 1) / tr->TileHeightNB;
122 assert(tr->Columns >= 0);
123 assert(tr->Rows >= 0);
128 TRcontext *trNew(void)
130 TRcontext *tr = (TRcontext *) calloc(1, sizeof(TRcontext));
132 tr->TileWidth = DEFAULT_TILE_WIDTH;
133 tr->TileHeight = DEFAULT_TILE_HEIGHT;
134 tr->TileBorder = DEFAULT_TILE_BORDER;
135 tr->RowOrder = TR_BOTTOM_TO_TOP;
136 tr->CurrentTile = -1;
138 return (TRcontext *) tr;
142 void trDelete(TRcontext *tr)
150 void trTileSize(TRcontext *tr, GLint width, GLint height, GLint border)
158 assert(width >= 2*border);
159 assert(height >= 2*border);
161 tr->TileBorder = border;
162 tr->TileWidth = width;
163 tr->TileHeight = height;
164 tr->TileWidthNB = width - 2 * border;
165 tr->TileHeightNB = height - 2 * border;
171 void trTileBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
176 tr->TileFormat = format;
178 tr->TileBuffer = image;
183 void trImageSize(TRcontext *tr, GLint width, GLint height)
188 tr->ImageWidth = width;
189 tr->ImageHeight = height;
194 void trImageBuffer(TRcontext *tr, GLenum format, GLenum type, GLvoid *image)
199 tr->ImageFormat = format;
200 tr->ImageType = type;
201 tr->ImageBuffer = image;
205 GLint trGet(TRcontext *tr, TRenum param)
211 return tr->TileWidth;
213 return tr->TileHeight;
215 return tr->TileBorder;
217 return tr->ImageWidth;
218 case TR_IMAGE_HEIGHT:
219 return tr->ImageHeight;
225 if (tr->CurrentTile<0)
228 return tr->CurrentRow;
229 case TR_CURRENT_COLUMN:
230 if (tr->CurrentTile<0)
233 return tr->CurrentColumn;
234 case TR_CURRENT_TILE_WIDTH:
235 return tr->CurrentTileWidth;
236 case TR_CURRENT_TILE_HEIGHT:
237 return tr->CurrentTileHeight;
239 return (GLint) tr->RowOrder;
245 GLdouble trGetD(TRcontext *tr, TRenum param)
267 void trRowOrder(TRcontext *tr, TRenum order)
272 if (order==TR_TOP_TO_BOTTOM || order==TR_BOTTOM_TO_TOP)
273 tr->RowOrder = order;
277 void trOrtho(TRcontext *tr,
278 GLdouble left, GLdouble right,
279 GLdouble bottom, GLdouble top,
280 GLdouble zNear, GLdouble zFar)
285 tr->Perspective = GL_FALSE;
295 void trFrustum(TRcontext *tr,
296 GLdouble left, GLdouble right,
297 GLdouble bottom, GLdouble top,
298 GLdouble zNear, GLdouble zFar)
303 tr->Perspective = GL_TRUE;
313 void trPerspective(TRcontext *tr,
314 GLdouble fovy, GLdouble aspect,
315 GLdouble zNear, GLdouble zFar )
317 GLdouble xmin, xmax, ymin, ymax;
318 ymax = zNear * tan(fovy * 3.14159265 / 360.0);
320 xmin = ymin * aspect;
321 xmax = ymax * aspect;
322 trFrustum(tr, xmin, xmax, ymin, ymax, zNear, zFar);
326 void trBeginTile(TRcontext *tr)
329 GLint tileWidth, tileHeight, border;
330 GLdouble left, right, bottom, top;
335 if (tr->CurrentTile <= 0) {
337 /* Save user's viewport, will be restored after last tile rendered */
338 glGetIntegerv(GL_VIEWPORT, tr->ViewportSave);
341 /* which tile (by row and column) we're about to render */
342 if (tr->RowOrder==TR_BOTTOM_TO_TOP) {
343 tr->CurrentRow = tr->CurrentTile / tr->Columns;
344 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
346 else if (tr->RowOrder==TR_TOP_TO_BOTTOM) {
347 tr->CurrentRow = tr->Rows - (tr->CurrentTile / tr->Columns) - 1;
348 tr->CurrentColumn = tr->CurrentTile % tr->Columns;
351 /* This should never happen */
354 assert(tr->CurrentRow < tr->Rows);
355 assert(tr->CurrentColumn < tr->Columns);
357 border = tr->TileBorder;
359 /* Compute actual size of this tile with border */
360 if (tr->CurrentRow < tr->Rows-1)
361 tileHeight = tr->TileHeight;
363 tileHeight = tr->ImageHeight - (tr->Rows-1) * (tr->TileHeightNB) + 2 * border;
365 if (tr->CurrentColumn < tr->Columns-1)
366 tileWidth = tr->TileWidth;
368 tileWidth = tr->ImageWidth - (tr->Columns-1) * (tr->TileWidthNB) + 2 * border;
370 /* Save tile size, with border */
371 tr->CurrentTileWidth = tileWidth;
372 tr->CurrentTileHeight = tileHeight;
374 glViewport(0, 0, tileWidth, tileHeight); /* tile size including border */
376 /* save current matrix mode */
377 glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
378 glMatrixMode(GL_PROJECTION);
381 /* compute projection parameters */
382 left = tr->Left + (tr->Right - tr->Left)
383 * (tr->CurrentColumn * tr->TileWidthNB - border) / tr->ImageWidth;
384 right = left + (tr->Right - tr->Left) * tileWidth / tr->ImageWidth;
385 bottom = tr->Bottom + (tr->Top - tr->Bottom)
386 * (tr->CurrentRow * tr->TileHeightNB - border) / tr->ImageHeight;
387 top = bottom + (tr->Top - tr->Bottom) * tileHeight / tr->ImageHeight;
389 ssgSetFrustum ( left, right, bottom, top, tr->Near, tr->Far );
391 /* restore user's matrix mode */
392 glMatrixMode( (GLenum)matrixMode );
397 int trEndTile(TRcontext *tr)
399 GLint prevRowLength, prevSkipRows, prevSkipPixels /*, prevAlignment */;
404 assert(tr->CurrentTile>=0);
406 /* be sure OpenGL rendering is finished */
409 /* save current glPixelStore values */
410 glGetIntegerv(GL_PACK_ROW_LENGTH, &prevRowLength);
411 glGetIntegerv(GL_PACK_SKIP_ROWS, &prevSkipRows);
412 glGetIntegerv(GL_PACK_SKIP_PIXELS, &prevSkipPixels);
413 /*glGetIntegerv(GL_PACK_ALIGNMENT, &prevAlignment);*/
415 if (tr->TileBuffer) {
416 GLint srcX = tr->TileBorder;
417 GLint srcY = tr->TileBorder;
418 GLint srcWidth = tr->TileWidthNB;
419 GLint srcHeight = tr->TileHeightNB;
420 glReadPixels(srcX, srcY, srcWidth, srcHeight,
421 tr->TileFormat, tr->TileType, tr->TileBuffer);
424 if (tr->ImageBuffer) {
425 GLint srcX = tr->TileBorder;
426 GLint srcY = tr->TileBorder;
427 GLint srcWidth = tr->CurrentTileWidth - 2 * tr->TileBorder;
428 GLint srcHeight = tr->CurrentTileHeight - 2 * tr->TileBorder;
429 GLint destX = tr->TileWidthNB * tr->CurrentColumn;
430 GLint destY = tr->TileHeightNB * tr->CurrentRow;
432 /* setup pixel store for glReadPixels */
433 glPixelStorei(GL_PACK_ROW_LENGTH, tr->ImageWidth);
434 glPixelStorei(GL_PACK_SKIP_ROWS, destY);
435 glPixelStorei(GL_PACK_SKIP_PIXELS, destX);
436 /*glPixelStorei(GL_PACK_ALIGNMENT, 1);*/
438 /* read the tile into the final image */
439 glReadPixels(srcX, srcY, srcWidth, srcHeight,
440 tr->ImageFormat, tr->ImageType, tr->ImageBuffer);
443 /* restore previous glPixelStore values */
444 glPixelStorei(GL_PACK_ROW_LENGTH, prevRowLength);
445 glPixelStorei(GL_PACK_SKIP_ROWS, prevSkipRows);
446 glPixelStorei(GL_PACK_SKIP_PIXELS, prevSkipPixels);
447 /*glPixelStorei(GL_PACK_ALIGNMENT, prevAlignment);*/
449 /* increment tile counter, return 1 if more tiles left to render */
451 if (tr->CurrentTile >= tr->Rows * tr->Columns) {
452 /* restore user's viewport */
453 glViewport(tr->ViewportSave[0], tr->ViewportSave[1],
454 tr->ViewportSave[2], tr->ViewportSave[3]);
455 tr->CurrentTile = -1; /* all done */
464 * Replacement for glRastePos3f() which avoids the problem with invalid
467 void trRasterPos3f(TRcontext *tr, GLfloat x, GLfloat y, GLfloat z)
469 if (tr->CurrentTile<0) {
470 /* not doing tile rendering right now. Let OpenGL do this. */
471 glRasterPos3f(x, y, z);
474 GLdouble modelview[16], proj[16];
476 GLdouble winX, winY, winZ;
478 /* Get modelview, projection and viewport */
479 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
480 glGetDoublev(GL_PROJECTION_MATRIX, proj);
483 viewport[2] = tr->CurrentTileWidth;
484 viewport[3] = tr->CurrentTileHeight;
486 /* Project object coord to window coordinate */
487 if (gluProject(x, y, z, modelview, proj, viewport, &winX, &winY, &winZ)){
489 /* set raster pos to window coord (0,0) */
490 glMatrixMode(GL_MODELVIEW);
493 glMatrixMode(GL_PROJECTION);
496 glOrtho(0.0, tr->CurrentTileWidth,
497 0.0, tr->CurrentTileHeight, 0.0, 1.0);
498 glRasterPos3f(0.0, 0.0, -winZ);
500 /* Now use empty bitmap to adjust raster position to (winX,winY) */
502 GLubyte bitmap[1] = {0};
503 glBitmap(1, 1, 0.0, 0.0, winX, winY, bitmap);
506 /* restore original matrices */
507 glPopMatrix(); /*proj*/
508 glMatrixMode(GL_MODELVIEW);
513 printf("GL error!\n");