GUI drawing S32 or F32
by Demolishun · in Torque Game Engine · 01/27/2007 (9:33 am) · 8 replies
Okay, I am using the following function to draw lines for my GUI widget:
void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color);
The dglDrawLine function takes ints so that if you draw a line that is not on an int boundary say 2.x it will either be 2 or 3. I am currently drawing a grid and have to calculate the position of the line using floats and then convert to S32 to get it to properly fill the widget area for all the different resolutions. I use relative flag to make the widget resize with the screen resolution. However, the round off tends to make some of the grid locations smaller or bigger because of numbers converging or diverging. With just the grid it is not so noticable, but with putting mini bitmaps in each location it really stands out.
What I want to know is if there is a drawback to creating a line drawing routine that uses floats?
I imagine that if I use floats for drawing lines: glBegin(GL_LINES) or something similar that I may have to turn blending on for those operations?
I can't imagine there would be a performance hit.
Thanks,
Frank
void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color);
The dglDrawLine function takes ints so that if you draw a line that is not on an int boundary say 2.x it will either be 2 or 3. I am currently drawing a grid and have to calculate the position of the line using floats and then convert to S32 to get it to properly fill the widget area for all the different resolutions. I use relative flag to make the widget resize with the screen resolution. However, the round off tends to make some of the grid locations smaller or bigger because of numbers converging or diverging. With just the grid it is not so noticable, but with putting mini bitmaps in each location it really stands out.
What I want to know is if there is a drawback to creating a line drawing routine that uses floats?
I imagine that if I use floats for drawing lines: glBegin(GL_LINES) or something similar that I may have to turn blending on for those operations?
I can't imagine there would be a performance hit.
Thanks,
Frank
About the author
I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67
#2
It is already using F32 to draw with!
I just changed the function into: dglDrawLineF(F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color)
and removed the 0.5 (round off adjust?) and it draws the exact same way. I wonder if I have to enable antialiasing to get it draw mid pixel.
01/27/2007 (10:02 am)
Here is something that I didn't think to look at:void dglDrawLine(S32 x1, S32 y1, S32 x2, S32 y2, const ColorI &color)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glColor4ub(color.red, color.green, color.blue, color.alpha);
glBegin(GL_LINES);
glVertex2f((F32)x1 + 0.5, (F32)y1 + 0.5);
glVertex2f((F32)x2 + 0.5, (F32)y2 + 0.5);
glEnd();
glBegin(GL_POINTS);
glVertex2f((F32)x2 + 0.5, (F32)y2 + 0.5);
glEnd();
}It is already using F32 to draw with!
I just changed the function into: dglDrawLineF(F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color)
and removed the 0.5 (round off adjust?) and it draws the exact same way. I wonder if I have to enable antialiasing to get it draw mid pixel.
#3
They work pretty well.
01/27/2007 (2:45 pm)
Here are a couple of modified functions:namespace {
ColorI sg_bitmapModulation(255, 255, 255, 255);
ColorI sg_textAnchorColor(255, 255, 255, 255);
ColorI sg_stackColor(255, 255, 255, 255);
RectI sgCurrentClipRect;
} // namespace {}
void dglDrawLineF(F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color)
{
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glColor4ub(color.red, color.green, color.blue, color.alpha);
glBegin(GL_LINES);
// glVertex2f(x1 + 0.5, y1 + 0.5);
// glVertex2f(x2 + 0.5, y2 + 0.5);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
// glBegin(GL_POINTS);
// glVertex2f((F32)x2 + 0.5, (F32)y2 + 0.5);
// glEnd();
glDisable(GL_LINE_SMOOTH);
}
void dglDrawBitmapStretchSRF(TextureObject* texture,
const RectF& dstRect,
const RectF& srcRect,
const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapStretchSR: NULL Handle");
if(!dstRect.isValidRect())
return;
AssertFatal(srcRect.isValidRect() == true,
"GSurface::drawBitmapStretchSR: routines assume normal rects");
glDisable(GL_LIGHTING);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture->texGLName);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
F32 texLeft = F32(srcRect.point.x) / F32(texture->texWidth);
F32 texRight = F32(srcRect.point.x + srcRect.extent.x) / F32(texture->texWidth);
F32 texTop = F32(srcRect.point.y) / F32(texture->texHeight);
F32 texBottom = F32(srcRect.point.y + srcRect.extent.y) / F32(texture->texHeight);
F32 screenLeft = dstRect.point.x;
F32 screenRight = dstRect.point.x + dstRect.extent.x;
F32 screenTop = dstRect.point.y;
F32 screenBottom = dstRect.point.y + dstRect.extent.y;
if(in_flip & GFlip_X)
{
F32 temp = texLeft;
texLeft = texRight;
texRight = temp;
}
if(in_flip & GFlip_Y)
{
F32 temp = texTop;
texTop = texBottom;
texBottom = temp;
}
glColor4ub(sg_bitmapModulation.red,
sg_bitmapModulation.green,
sg_bitmapModulation.blue,
sg_bitmapModulation.alpha);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(texLeft, texBottom);
glVertex2f(screenLeft, screenBottom);
glTexCoord2f(texRight, texBottom);
glVertex2f(screenRight, screenBottom);
glTexCoord2f(texRight, texTop);
glVertex2f(screenRight, screenTop);
glTexCoord2f(texLeft, texTop);
glVertex2f(screenLeft, screenTop);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisable(GL_POLYGON_SMOOTH);
}
void dglDrawBitmapStretchF(TextureObject* texture, const RectF& dstRect, const U32 in_flip)
{
AssertFatal(texture != NULL, "GSurface::drawBitmapStretch: NULL Handle");
AssertFatal(dstRect.isValidRect() == true,
"GSurface::drawBitmapStretch: routines assume normal rects");
RectF subRegion(0, 0,
texture->bitmapWidth,
texture->bitmapHeight);
dglDrawBitmapStretchSRF(texture,
dstRect,
subRegion,
in_flip);
}They work pretty well.
#4
These are really simple to change. The important parts are the
Have fun!
Frank
01/27/2007 (2:48 pm)
Here is the header file stuff for them:void dglDrawLineF(F32 x1, F32 y1, F32 x2, F32 y2, const ColorI &color);
void dglDrawBitmapStretchSRF(TextureObject* texture,
const RectF& dstRect,
const RectF& srcRect,
const U32 in_flip = GFlip_None);
void dglDrawBitmapStretchF(TextureObject* texture,
const RectF& dstRect,
const U32 in_flip = GFlip_None);These are really simple to change. The important parts are the
glEnable(GL_LINE_SMOOTH); // for lines glEnable(GL_POLYGON_SMOOTH); // for polygons
Have fun!
Frank
#5
01/27/2007 (3:36 pm)
Dang it there is a strange problem. For some reason chaning the values to floats (F32) on the inputs to the functions will disable GL_MODULATE. I don't know why just yet, but this is certainly a strange problem. I tried removing the glEnable(GL_POLYGON_SMOOTH) and it did not help. So I am thinking it has something to do with the floats. For the life of me I cannot think what as the original function was using floats for the final render.
#6
So I was using a different set of variables than the dgl source file was. The question is, how can I get around this? Is there a way to link anonymouse namespaces? I guess I could put these variables in a named namespace, but that is a bigger change to the original libraries. Perhaps a function to get/set the variables?
01/27/2007 (6:39 pm)
Okay, found my problem. I had duplicated this in my own source file:namespace dgl {
extern ColorI sg_bitmapModulation;
extern ColorI sg_textAnchorColor;
extern ColorI sg_stackColor;
extern RectI sgCurrentClipRect;
} // namespace {}So I was using a different set of variables than the dgl source file was. The question is, how can I get around this? Is there a way to link anonymouse namespaces? I guess I could put these variables in a named namespace, but that is a bigger change to the original libraries. Perhaps a function to get/set the variables?
#7
Replace this:
01/27/2007 (6:48 pm)
Okay, I am retarded here is the code to fix the color issue.Replace this:
glColor4ub(sg_bitmapModulation.red,
sg_bitmapModulation.green,
sg_bitmapModulation.blue,
sg_bitmapModulation.alpha);With this:ColorF c; dglGetBitmapModulation(&c); glColor4f(c.red,c.green,c.blue,c.alpha);
#8
tdn.garagegames.com/wiki/User:Frank_Carney
It is a link under the code section and ends with "dglExt".
01/28/2007 (12:01 pm)
I put this code as of the most recent changes here:tdn.garagegames.com/wiki/User:Frank_Carney
It is a link under the code section and ends with "dglExt".
Torque Owner Stefan Lundmark
There will be, but if it's noticable or not is another story and I have no clue. Let us know if you decide to test! :)