GUI Images don't render if x position is negative.

Started by
6 comments, last by paulg568 12 years, 3 months ago
I wrote my own gui image renderer.

I am having an issue with images not rendering if there position is negative even if part of the image is still on screen.

Here is my code:


glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
glRasterPos2f(xPos, yPos);
glPixelZoom(xScale, yScale);
glDrawPixels(originalWidth, originalHeight, GL_RGBA, GL_UNSIGNED_BYTE, imageData.bits());
glPopMatrix();
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);


Anyone know how to fix this?

Thanks.
Advertisement
Basically this works without problems, maybe it just doesn't work with your code. The problem might be glRasterPos2f - maybe it just doesn't work with negative coordinates, which would make some kind of sense.

Anyway here is my texture drawing code that also works for negative positions:

// The parameters iLeft, iTop are the position (for example 0,0 or -20, -20 to draw it a bit in the negative area).
// fixedwidth, fixedheight are the texture width and height.
// m_height is the window height.

// ... bind texture etc.

// Draw quad (float format because its simply faster)
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2i(iLeft, m_height-iTop); // Top Left
glTexCoord2f(1.0f, 1.0f); glVertex2i(iLeft+fixedwidth, m_height-iTop); // Top Right
glTexCoord2f(1.0f, 0.0f); glVertex2i(iLeft+fixedwidth, m_height-iTop-fixedheight); // Bottom Right
glTexCoord2f(0.0f, 0.0f); glVertex2i(iLeft, m_height-iTop-fixedheight); // Bottom Left
glEnd();

// ... unbind texture etc.


the_visualist
I have never used raster position, and instead use what the other person suggested. Since you are explicitly setting a pixel x and y, it may be realizing that you are writing to a piece of memory that is not there for the image and it is deciding not to draw. Not sure.

from here:
http://msdn.microsoft.com/en-us/library/dd373990%28v=VS.85%29.aspx
"When the raster position coordinates are invalid, drawing commands that are based on the raster position are ignored (that is, they do not result in changes to the OpenGL state)." So clearly you can give it bad input and based on that input the draw doesn't happen.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

So how do you draw an image partially off the screen? I am going to try what I am about to say and let you know if it works.

If x is negative use glTranslafef to translate it partially offscreen.

In the mean time if someone else has an idea how to draw image partially on screen let me know.
Thanks.

-Paul
Translating it to a negative spot didn't work. It stopped it at zero.
If the raster position set by glRasterPos is clipped (outside the view volume), it is marked as invalid and any command relying on the raster position is ignored.

If you're specifying your coordinates in window space, then use glWindowPos instead of glRasterPos. The benefit is that you don't have to setup the modelview and the projection matrices for coordinate-to-pixel mappings, and the raster position is never invalid.

If you're specifying it in any other coordinate system, I suggest you project the point manually to determine what the screen space coordinates would be, and then set the raster position with glWindowPos.
So how do you draw an image partially off the screen?[/quote]

Use glOrhtho() with the window coordinates of your screen size. then call glVertex2f() where you want the image to be. If it is offscreen that is fine and it will render whatever part is on the screen. This is because clipping happens. When using glRasterPos, you are directly writing pixels and pixel location -1,-1 is not a part of memory for your screen, only 0,0 to width,height are valid pixels to write. glVertex2f() takes care and clips so that it only tries to write pixels that are in the image that will map in 0,0 to width,height and therefore never writes -1,-1 etc pixels.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Thanks for the replies.

Using glWindowPos2i did the trick.

I tried glVertex2f and the images showed up in the bottom left regardless of position. I am new to opengl and am going to make a guess as to why this didn't work. Don't you generally need veretex positions for polygons, not raster images or pixmaps?

Lastly, how do I mark a post as solved? I have made two posts and in both cases I need to mark the posts solved/complete/closed.

Thanks for everyones' help.

This topic is closed to new replies.

Advertisement