7
votes

Hi i am quite new to OpenGL.

My task is to implement the GDI brushes in OpenGL for drawing on MAC OS X. Thus I turned to textures in OpenGL to first draw the pattern and then this needs to be mapped to drawing primitives such as a polygon (rectangle a case in point). I am creating a 8x8 texels texture and then mapping it on a polygon with co ordinates below on a 500x500 pixels window.

glVertex3f(0.35, 0.35, 0.0);

glVertex3f(0.35, 0.65, 0.0);

glVertex3f(0.65, 0.65, 0.0);

glVertex3f(0.65, 0.35, 0.0);

I since I need a brush effect the pattern must be repeated along the rectangle. The rectangle/square is 0.3 x 0.3 and the whole window is 500 x 500 then in terms of pixels the polygon is 150 x 150.

therefore repetitions required are 150/8

I therefore set the texture coordinates as follows:

glTexCoord2f(0.0,       0.0);   glVertex3f(0.35, 0.35, 0.0);
glTexCoord2f(0.0,       150/8); glVertex3f(0.35, 0.65, 0.0);
glTexCoord2f(150/8,     150/8); glVertex3f(0.65, 0.65, 0.0);
glTexCoord2f(150/8,     0.0);   glVertex3f(0.65, 0.35, 0.0);

I am having a problem that the vertical hatching (one texel transparent the other coloured) pattern I have created as a texture is not being appropriately mapped in the sense that some vertical lines are getting wider than others ( a sort of aliasing problem). Is it like this that I have to set the texture coordinates while mapping?

Thanks for any replies

al

2

2 Answers

17
votes

If I'm understanding you correctly you're not treating the texture coordinates correctly. When using glTexCoord2f() the texture extends from 0.0, 0.0 to 1.0, 1.0. So if you want to have a texture that fills a quad you use the following:

glTexCoord2f(0.0f, 0.0f);    glVertex3f(0.35f, 0.35f, 0.0f);
glTexCoord2f(0.0f, 1.0f);    glVertex3f(0.35f, 0.65f, 0.0f);
glTexCoord2f(1.0f, 1.0f);    glVertex3f(0.65f, 0.65f, 0.0f);
glTexCoord2f(1.0f, 0.0f);    glVertex3f(0.65f, 0.35f, 0.0f);

And if you want to repeat the texture 8 times across the surface of the quad use 8.0f in place of the 1.0fs in the texCoords call. You'll also need to make sure that the texture is set to repeat when it is set once you've bound the texture.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

That's probably your problem, but if you're still having aliasing issues, try looking into glTexParameteri with GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MAG_FILTER and mip-mapping.

It may be over a decade old, but this old Flipcode tutorial is still relevant and worth a look.

EDIT: Had vertex calls before texcoord calls

3
votes

If the code you wrote is exactly what you have in your code, then 150/8 is your problem. This is an integer division, and will not return the floating point 18.75, but the integer 18.

Change your code to floating point values.

 150.f/8.f