1

I am using OpenGL to draw textures in my game.

glClear(GL_COLOR_BUFFER_BIT);

float w = 128;
float h = 128;

w *= scale;
h *= scale;

GLfloat vertices[] = {
    w, 0,
    w, h,
    0, 0,
    0, h
};

float u1 = 0;
float u2 = 1;
float v1 = 0;
float v2 = 1;

GLfloat texCoords[] = {
    u2, v1,
    u2, v2,
    u1, v1,
    u1, v2
};

glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

It is a simple example to explain my problem. I change scale variable, which impacts only on triangle vertices and should not alter texture coordinates. When scale is 1, i have this picture:

one http://dl.dropbox.com/u/21467/opengl/1.png

When I change scale to 0.99, I have this picture:

two http://dl.dropbox.com/u/21467/opengl/3.png

If you look carefully, you notice little stripe on the top of the texture. Why is it there and how can I avoid it?

In my real game I draw tiles and if I change scale, my tiles start having those stripes and in result I got this nasty grid of stripes in my game. Does anybody know, what is wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
psyched
  • 1,859
  • 1
  • 21
  • 28

2 Answers2

2

Call the following code when creating the texture buffer:

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

If you look carefully, you notice little stripe on the top of the texture. Why is it there and how can I avoid it?

What is happening is that at certain pixels, the texture coordinate at that screen position may be something like -0.0001, or 1.0002. The TEXTURE_WRAP_[S/T] texture parameter details what will happen when this occurs.

Keldon Alleyne
  • 2,103
  • 16
  • 23
  • Thanks a lot! By the way, I found that the constant names are GL_TEXTURE_WRAP_T and GL_TEXTURE_WRAP_S and for some reason I have only GL_CLAMP in my OpenGL. I found this question useful and it solved my problem http://stackoverflow.com/questions/5617250/gl-clamp-to-edge-was-not-declared-in-this-scope – psyched Dec 28 '11 at 08:04
  • I just noticed, that it works only with GL_NEAREST and in case of GL_LINEAR I still have borders around my image... – psyched Dec 28 '11 at 09:24
  • Yes, [glTexParameter notes](http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml#notes) states that GL_CLAMP_TO_EDGE is only available in OpenGL 1.2 or later. See [Moving Beyond OpenGL 1.1 for Windows](http://www.gamedev.net/page/resources/_/technical/opengl/moving-beyond-opengl-11-for-windows-r1929) for more information. – Keldon Alleyne Dec 28 '11 at 13:17
0

Second time today...

See this answer:

https://stackoverflow.com/a/8643282/524368


Oh boy, do you people never google for a problem? This has become some sort of FAQ:

I answered it here

https://stackoverflow.com/a/5879551/524368

and here

https://stackoverflow.com/a/7272871/524368

and in a few other places as well.

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I disagree with you. My texture coordinates are always (0.0, 0.0) bottom left and (1.0, 1.0) up right and that corresponds to edges of the pixels, because 0.0 is min value and 1.0 is max value for the whole texture, thus it is the edge. – psyched Dec 28 '11 at 09:18
  • @user1113852: The problem is, that you don't want to address the pixel edges, but the centers, so that the edge doesn't bleeds into. – datenwolf Dec 28 '11 at 10:07
  • If I understand you right, I have to make my S and T coordinates not (0.0, 0.0) and (1.0, 1.0), but (0.0 + 1.0/128.0, 0.0 + 1.0/128.0) and (1.0 - 1.0/128.0, 1.0 - 1.0/128.0). In this case white stripes goes away, but my textures are blured, meaning they are scaled. I don't want that effect. – psyched Dec 28 '11 at 13:02