Archived

This topic is now archived and is closed to further replies.

Setting the screen aspect ratio (using SDL)

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not sure how to set the screen aspect ratio in OpenGL, using C++. I am also using SDL to set up the window, as it cuts down the init code by a mile. I have tried some snippets with gluPerspective and stuff but none of them seem to do anything. I am just using verxtex2 to draw my polys, if that actually makes any difference (I don't think it does). Here is my initiation code if it would help anyone. I'd be really thankful for any help, thanks :D. p.s. I just saw that sticky and tried it... it didn't work. I'm not an idiot as that post implies, just I'm kind of stuck when snippets that should work actually don't work.
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);

  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

  SDL_Surface *screen;

  mode = SDL_OPENGL;
  resolution.width = 640;
  resolution.height = 480;
  resolution.depth = 32;

  screen = SDL_SetVideoMode(resolution.width, resolution.height, resolution.depth, mode);

  if ( screen == NULL )
  {
    printf("Unable to set %dx%dx%d video: %s\n",resolution.width, resolution.height, resolution.depth, SDL_GetError());
    exit(1);
  }

  SDL_ShowCursor(0) ;
  screen = SDL_GetVideoSurface();
  glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );  
[edited by - hpesoj on May 28, 2004 10:18:37 AM]

Share this post


Link to post
Share on other sites
That''s your SDL code... where''s the GL code? We need to see glOrtho/gluPerspective/frustum calls etc to say what''s wrong...

[cpp]
void myReshape(GLsizei w, GLsizei h)
{
glMatrixMode (GL_PROJECTION); /* prepare for and then */
glLoadIdentity (); /* define the projection */
glFrustum (-1.0, 1.0, -1.0, 1.0, /* transformation */
1.5, 20.0);
glMatrixMode (GL_MODELVIEW); /* back to modelview matrix */
glViewport (0, 0, w, h); /* define the viewport */
}
[/cpp]

From http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html

Share this post


Link to post
Share on other sites
Thanks for your help, but it still didn't work. That's my problem, I don't know how to use those commands to set up the screen aspect, I don't have them anywhere in my code. This is my resize window function that I am using, with that code that you gave me, I don't actually use any of the commands in my code . I'm new to C++ and OpenGL, so try not to be too harsh for any major mistakes I'm making. Thanks.

void screenClass::setScreenRes(int w, int h, int d)
{
if ((resolution.width != w) && (resolution.height != h) && (resolution.depth != d)) {
resolution.width = w;
resolution.height = h;
resolution.depth = d;

SDL_Surface *screen;

screen = SDL_SetVideoMode(resolution.width, resolution.height, resolution.depth , mode);
if ( screen == NULL ) {
printf("Unable to set %dx%dx%d video: %s\n",resolution.width, resolution.height, resolution.depth, SDL_GetError());
exit(1);
}
screen = SDL_GetVideoSurface();

glMatrixMode (GL_PROJECTION); /* prepare for and then */
glLoadIdentity (); /* define the projection */
glFrustum (-1.0, 1.0, -1.0, 1.0, /* transformation */
1.5, 20.0);
glMatrixMode (GL_MODELVIEW); /* back to modelview matrix */
glViewport (0, 0, w, h); /* define the viewport */

}
}


[edited by - hpesoj on May 28, 2004 11:57:52 AM]

[edited by - hpesoj on May 28, 2004 11:59:26 AM]

Share this post


Link to post
Share on other sites
Your resize code (at least the OpenGL portion) seems fine. Maybe I'm misunderstanding due to my own fault or your explanation, but I thought all you wanted to do was setup screen aspect? If that's the case and your window is working fine and you can draw something (test out drawing a 3D quad or something; see if perspective seems to be working), then all you need to do to setup the screen aspect is use the width (or height) to determine the height (or width) via some aspect ratio. What purpose do you have for doing this btw?

glFrustum (-1.0, 1.0, -1.0, 1.0, /* transformation */
1.5, 20.0);

gives you a perspective that goes from -1.0 to 1.0 left to right, -1.0 to 1.0 bottom to top and 1.5 to 20.0 start depth to end depth. [glFrustrum(...) depth values should always be positive, but the depth is really going from -1.5 to -20.0.] OpenGL deals with the screen aspect for you if you setup a perspective like this (or an ortho mode).

~Urayami

[edited by - urayami on May 28, 2004 2:22:33 PM]

Share this post


Link to post
Share on other sites
What would you suggest I used in place of those values then? I just copied Squirm's code. I have it working now by using this code...

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, resolution.width, 0, resolution.height, -1, 1);
glMatrixMode (GL_MODELVIEW);
glViewport (0, 0, resolution.width, resolution.height);

Am I doing this correctly, or is there anything else I need? It appears to be correct.

What does glFrustrum actually do? I doesn't seem to have any effect on my scene, is it just with 3D that it applies?

[edited by - hpesoj on May 29, 2004 11:52:06 AM]

[edited by - hpesoj on May 29, 2004 12:01:06 PM]

Share this post


Link to post
Share on other sites
glFrustrum is a function that will setup a _perspective_ projection (view) for you. Perspective is only noticed if you have a specified field of vision and depth - both are 3D qualities. (By field of vision, I meant angular, not width or something of that sort.) I do not _think_ it is possible to obtain a 2D (orthographic) view via glFrustrum() because of the field of view aspect. So, in case you haven''t noticed, I''m agreeing with you; glFrustrum() "applies to 3D".

Your code to setup your view looks fine.
Though, I''d add a glLoadIdentity() call after your call to

glMatrixMode(GL_MODELVIEW);

Hope this does it for ya.

~Urayami

Share this post


Link to post
Share on other sites