PDA

View Full Version : Smoothing of Circles using Texture Mapping



Aravi
07-08-2010, 02:31 PM
Hello guys,
I have been trying to solve this problem for a few days now.
I came across texture mapping as a way out to make the circles smooth. But I am not sure if I am doing it correctly. I generated a texture in an array with white colored pixels and used it. The code is below

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">


/***************************************/


void create_texture(void)
{
width = 128;
height = 128;

texture_data = (unsigned char *)malloc( width * height * 3 );

for(int i = 0 ; i < width * height * 3 ; i++)
{
texture_data[i] = 0Xff;
}


}

/*****************************************/


Then in the init() function I wrote the following


//Function that creates the white color texture to be used in smoothing the circle boundary
create_texture();

//Initialize the 2D texture parameters

// allocate a texture name
glGenTextures( 1, &amp;texture );


// select our current texture
glBindTexture( GL_TEXTURE_2D , texture );


// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );



// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);

// when texture area is large, bilinear filter the original
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );


// the texture wraps over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );



glNewList(texture = glGenLists(1),GL_COMPILE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data); //check these parrameters
glEndList();

free(texture_data);

/*************************************/
void drawCircle(float radius)
{
double k;


float x , y;



glLineWidth(0.05);


glEnable(GL_TEXTURE_2D);
glCallList(texture);
glBegin(GL_LINE_LOOP);


for ( k=0; k <= 360.0;)
{
float degInRad = k*DEG2RAD;

x = cos(degInRad)*radius;
y = sin(degInRad)*radius;

glTexCoord2f(0.5,0.5);
glVertex2f(x,y);


k = k + 5;
}



glEnd();
glDisable(GL_TEXTURE_2D);

glLineWidth(1.5);


}
[/QUOTE]</div>




This is the code which I use to smooth the circles. But somehow I am not getting the smooth edges. Kindly help me..


Thanks
ATN

dorbie
07-08-2010, 05:21 PM
There are several problems with the code.

It's difficult to see why anyone expected this to smooth lines of what is expected of this use of texture.

Can you explain your intent rather than throwing code up that really has no bearing on the stated problem.

Aravi
07-08-2010, 08:36 PM
I am linking the post in this forum which were posted a few days back.
Smooth rendering using texture map (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=280009&amp;Searchpa ge=1&amp;Main=53991&amp;Words=smooth+rendering&amp;Search=true #Post280009)

Here the guy "mhagain" had posted a way to use texturing to construct a circle efficiently and also in a smooth way. It wasn't clear to me. Hence I posted there. There were no replies. Hence I tried myself and thought if I can make it work. Thats the result of the dirty code above...

If it can't be done by texture mapping, is there any other way?

I have tried GL_LINE_SMOOTH with blending. It works. But, when the circle is very small, the outline look foggy/spongy. Hence I need to find some other method.

ZbuffeR
07-09-2010, 01:46 AM
Maybe just a depth test disable can solve your problem ?
Or adapt dynamically the number of sides to the radius, so that a smaller circle is drawn with fewer lines, to avoid subpixel precision issues.

Can you post a screenshot of your circles, both correct and too small/foggy ?

mhagain
07-09-2010, 03:42 AM
No no no no no. That is not the way to do it at all.

Forget about your display list, forget about your 360 degrees, this is all you need to draw a circle using a texture image (assume that "circletexture" is your texture object):
glBindTexture (GL_TEXTURE_2D, circletexture);
glBegin (GL_QUADS);
glTexCoord2f (0, 0);
glVertex2f (100, 100);
glTexCoord2f (1, 0);
glVertex2f (200, 100);
glTexCoord2f (1, 1);
glVertex2f (200, 200);
glTexCoord2f (0, 1);
glVertex2f (100, 200);
glEnd ();
I would strongly suggest that you look up some basic tutorials on texture mapping and alpha blending at this stage, and that you use them just as they are - don't go trying to jump too far ahead.

Aravi
07-09-2010, 09:41 AM
mhagain and others,
thanks for the response.
from what you have given, it seems to me like, we need to have the circle image as a texture and apply it on the square surface which would exactly enclose the circle which I need. Thereby, the MIN and MAG filter will do their work to reduce or increase the size of the circles. Am I correct.

To get a circle texture, should I try with some graphics tool and get a circle image file out of it?? Any other way??

Thanks in advance
ATN

Aravi
07-15-2010, 02:37 PM
I tried texture mapping a circle as mhagain told.
I get a good circles. But since the image file of a circle can only be a square (64 X 64 etc), when circles are close to each other, edges of the circle images which are a part of the square hide the neighboring circle image. It's evident in one of the circles in the image present in the link
hidden_circles (http://www.cs.utah.edu/~aravi/?C=N;O=A)

Please find the hidden_circles.bmp file in the page.

Is there a way to overcome this?

GL Starter
07-15-2010, 06:24 PM
I think this issue due to overlapping of two texture rendering.

Need to enable alpha function.

You should set Alpha value to 0.0 for the pixels that are not belong to circle area And change Alpha function for that.
Then outside area of circle will not drawn to screen and you will not have this issue.

glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER, 0.5 );

Aravi
07-16-2010, 03:39 PM
Hi all,
Thanks a lot for the replies. I am yet to try alpha blending. But before that, I found a way to prevent the texture overlapping without using alpha value.

In this method, instead of applying the texture on a square surface, I applied the texture on a octagonal surface. Hence when two circles were close to each other, their textures being octagonal, never overlap, as in the case of square.

I believe if we keep increasing the sides of the underlying polygon, we can get good results in terms of constructing a number of circles which touch each other.