Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating Textures in OpenGL

Tags:

opengl

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

like image 372
user461426 Avatar asked Oct 06 '10 07:10

user461426


People also ask

What is texture repeating?

A repeating texture is created by tiling or repeating an image in a grid.

How do textures work in OpenGL?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.

How do you show textures in OpenGL?

You need to bind the texture, enable texturing (fixed function pipeline) or use a shader that does texturing (shader pipeline), and draw some shape like a quad. Don't forget the shape you draw needs texture coordinates.

What is Gl_repeat?

GL_REPEAT : The integer part of the coordinate will be ignored and a repeating pattern is formed. GL_MIRRORED_REPEAT : The texture will also be repeated, but it will be mirrored when the integer part of the coordinate is odd. GL_CLAMP_TO_EDGE : The coordinate will simply be clamped between 0 and 1 .


2 Answers

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

like image 88
Pike65 Avatar answered Oct 11 '22 15:10

Pike65


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
like image 33
Bahbar Avatar answered Oct 11 '22 15:10

Bahbar