Sign in to follow this  
Jengu

OpenGL Tiled rendering math hurts my brain

Recommended Posts

I'm trying to do tiled rendering so that I can do high resolution screenshots. I found this page that describes how to do it: http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node30.html But I can't wrap my head around the math, esp. for calculating xoffset and yoffset (the equations look totally arbitrary to me). Can anyone explain or point to a more detailed explanation? Thanks :)

Share this post


Link to post
Share on other sites
Oh, forgot to include the code I'm using so far. The problem I'm getting is gaps between the tiles. I'm not sure how to debug it because I'm just copying the math from the link.


double multiplier = double(dpi) / 72.0; // DPI chosen by user

int sceneWidth = m_primarySelection->GetWidth() * multiplier;
int sceneHeight = m_primarySelection->GetHeight() * multiplier;

// We're using glReadPixels so we can't have tiles bigger than the
// the framebuffer. Pick biggest size that works.
int canvasWidth, canvasHeight;
GetClientSize(&canvasWidth, &canvasHeight);
int tileWidth, tileHeight;
for(int i = 1; i < 1000; ++i) {
tileWidth = std::ceil(double(sceneWidth) / double(i));
tileHeight = std::ceil(double(sceneHeight) / double(i));
if( tileWidth < canvasWidth &&
tileHeight < canvasHeight)
break;
}

float xscale = sceneWidth / tileWidth;
float yscale = sceneHeight / tileHeight;

wxBitmap composite_shot(sceneWidth, sceneHeight);
wxMemoryDC compositor;
compositor.SelectObject(composite_shot);

int nTilesX = std::ceil(double(sceneWidth) / double(tileWidth));
int nTilesY = std::ceil(double(sceneHeight) / double(tileHeight));

for(int y = 0; y < nTilesY; ++y) {
for(int x = 0; x < nTilesX; ++x) {
float xOffset = (-2.0f * float(x) * float(tileWidth) / float(sceneWidth)) + (1.0f - 1.0f / float(nTilesX));
float yOffset = (-2.0f * float(y) * float(tileHeight) / float(sceneHeight)) + (1.0f - 1.0f / float(nTilesY));

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// The scale and offset values are applied to the projection matrix before glOrtho is called
m_primarySelection->ContentsRender(xscale, yscale, xOffset, yOffset);

compositor.DrawBitmap(*Screenshot(0, 0, tileWidth, tileHeight), x * tileWidth, sceneHeight - (y+1) * tileHeight);
}
}

composite_shot.SaveFile(fileDlg.GetFilename(), wxBITMAP_TYPE_PNG);


(the wxwidgets stuff is just for dumping the data from glReadPixels, which is called by Screenshot(), to disk)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this