Skip to content

Instantly share code, notes, and snippets.

@inactive123
Created October 13, 2011 18:49
Show Gist options
  • Save inactive123/1285109 to your computer and use it in GitHub Desktop.
Save inactive123/1285109 to your computer and use it in GitHub Desktop.
static void SetViewport(int x, int y, int w, int h, int outwidth, int outheight)
{
float device_aspect = psglGetDeviceAspectRatio(psgl_device);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat left = 0;
GLfloat right = outwidth;
GLfloat bottom = 0;
GLfloat top = outheight;
GLfloat zNear = -1.0;
GLfloat zFar = 1.0;
float desired_aspect = vidScrnAspect;
GLuint lowerleft_x, lowerleft_y, viewport_width, viewport_height;
GLuint real_width = w, real_height = h;
if(custom_aspect_ratio_mode)
{
lowerleft_x = x + nXOffset;
lowerleft_y = y + nYOffset;
real_width = w + nXScale;
real_height = h + nYScale;
left = x + nXOffset;
right = y + nYOffset;
bottom = gl_height + nYOffset;
}
else if ( (int)(device_aspect*1000) > (int)(desired_aspect*1000) )
{
float delta = (desired_aspect / device_aspect - 1.0) / 2.0 + 0.5;
lowerleft_x = w * (0.5 - delta);
lowerleft_y = 0;
real_width = (2.0 * w * delta);
real_height = h;
}
else if ( (int)(device_aspect*1000) < (int)(desired_aspect*1000) )
{
float delta = (device_aspect / desired_aspect - 1.0) / 2.0 + 0.5;
lowerleft_x = 0;
lowerleft_y = h * (0.5 - delta);
real_width = w;
real_height = (2.0 * h * delta);
}
else
{
lowerleft_x = 0;
lowerleft_y = 0;
real_width = w;
real_height = h;
}
glViewport(lowerleft_x, lowerleft_y, real_width, real_height);
cg_viewport_width = real_width;
cg_viewport_height = real_height;
cgGLSetStateMatrixParameter(ModelViewProj_cgParam, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
glOrthof(left, right, bottom, top, zNear, zFar);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment