Jump to content
  • Advertisement
Sign in to follow this  

OpenGL getting viewport size

This topic is 4586 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 attempting to get the viewport size so I can use it with glRasterPos, so I won't end up positioning text off the screen. My problem is that glGetFloatv with GL_VIEWPORT returns the size of the actual window. I want to be able to call a function that will return the bounds of the opengl coords that I'm looking at. Eg. If I want to draw text in the middle of the screen I use glRasterPos(0,0), when I get the viewport size, I get 800x600, which is my window size. If I then want to draw something half way from the left of the window to (0,0), I'd use glRasterPos(width/4,0), or something like that. But then that is equal to 200, and I'd get no text and a warning that it isn't a valid raster position. Is there a way to get what I'm looking for?

Share this post

Link to post
Share on other sites
You're approaching the problem from the wrong direction. If you want the raster position at a certain place on the screen, you should not calculate the raster position based on the current coordinate system. You should set the coordinate system so that the parameters in glRasterPos matches that of the screen.

glOrtho(0, window_width, 0, window_height, -1, 1);
glTranslated(0.5, 0.5, 0);

There you go, (0,0) in the lower left pixel and (window_width-1, window_height-1) in the upper right pixel. Just plug the coordinates into glRasterPos and you get it where you want it.

Or even better, forget about all those matrix functions and use glWindowPos directly.

Share this post

Link to post
Share on other sites
So, I'm using glWindowPos3f (I found that it's an extension). But I'm not seeing any text.

Am I using it right?

void COpenGLDriver::drawText(const char* text, float x, float y, int len = -1)
// no string == don't print anything
if( text == NULL )

// store the length of the string (either supplied or we count it using 'strlen')
int length = (len == -1) ? strlen(text) : len;

// move the window position to the position to draw the text
//glRasterPos3f(x, y, -1);
pglWindowPos3f(x,y, -1);

glPushAttrib(GL_LIST_BIT); // Pushes the display list bits
glListBase(m_fontList - 32); // Sets the base character to 32

glCallLists(length, GL_UNSIGNED_BYTE, text); // Draws the display list text
glPopAttrib(); // Pops the display list bits

All I did was replace the call to glRasterPos with a call to glWindowPos, and my query as to whether the raster position is valid says it is and the x,y components show that it is within the bounds of the window (18, 599).

Share this post

Link to post
Share on other sites
The coordinate system is relative to the client area where drawing occurs. OpenGL knows nothing about title bars or anything, only the area of the window/screen where it can draw.

The depth range for glWindowPos3 is [0, 1], where 0 is the near plane and 1 is the far plane. Although -1 would be clamped to 0 according to the specification, it's not really a valid value. Use glWindowPos2 unless you have a reason for depth awareness when doing text.

And there could be several problems why the text doesn't appear. Does the text rendering itself work at all? Assume it does judging by your first post, but anyway. Is the function pointer actually loaded properly? Noone says the function call must cause a crach just becuase it's a messed up function pointer.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!