TrivPants
TrivPants

Reputation: 89

Reshape Function For OpenGL

I am drawing squares to the screen using the mouse function, and the coordinates I gathered from the screen. The coordinates are within the range of the pixels of the window which the OpenGL program is running. So, to avoid having to tranform the pixels to the opengl coordinate system of -1 to 1 for both x and y axes, I used:

glOrtho(0,g_width,0,g_height,0,2200);

Where

0,g_width

are the left and right values of the drawing coordinates,

0,g_height

are the bottom and top values of the drawing coordinates, and I believe that

0,2200

are the minimum and maximum values of the coordinate to be drawn to.

Here is my reshape function:

void resize(GLsizei w, GLsizei h){
  cout << "Resize..." << endl
       << "W: " << w << endl
       << "H: " << h << endl;
  g_width = w;
  g_height = h;

  glViewport(0,0,g_width,g_height);
  glOrtho(0,g_width,0,g_height,0,2200);
  cout << "Ortho called" << endl;
}

I take the current width and height from the function, I tested that this works with the print statements I left in, and it works as I shrink or stretch the window, but the effects dont seem to be changing area which I can draw squares in. In this instance of the code, when I stretch the screen any direction I can no longer draw squares to the screen, but If I leave it at the original 700*700 that I defined it will work as I defined. But the reshape method isn't working for new widths and heights.

Any ideas?

Upvotes: 0

Views: 934

Answers (1)

derhass
derhass

Reputation: 45322

glOrtho will multiply the current top matrix of the currently selected matrix stack by the ortho matrix with the given parameters. But you don't want that product, you want just this ortho matrix, so you should call glLoadIdentity() before. However, since the ortho matrix is supposed to be the projection matrix, you should switch the matrix mode to GL_PROJECTION before, and switch back to GL_MODELVIEW after.

But you should be aware that all of that is totally deprecated. Modern GL does not have matrix stacks and glOrtho. You are supposed to use your own matrix functions (or use some library like glm), and use a vertex shader to apply whatever transformations you like to your vertex data.

Upvotes: 1

Related Questions