# OpenGL getting viewport size

## 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 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.
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0, window_width, 0, window_height, -1, 1);glTranslated(0.5, 0.5, 0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();

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 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 )		return;	// 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 on other sites
Question: does the size of the window include the title bar?

##### Share on other sites
You can use glOrtho(0, Width, Height, 0, 0, 1) in Ortho mode to get Windows-same coordinate system, where (0,0) is top left.

##### 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.

## 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

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978518
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
Thank you in advance!
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13