We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm a bit of a novice with writing code and have been banging my head against this for hours. I'm simply attempting to map a texture to a cylinder and can not get this one bit of code right. It just maps the texture to one of the faces and stretches it out across all the rest.
Any help is greatly appreciated!
beginShape(TRIANGLE_STRIP);
texture(img);
for (int i = 0; i <= sides; i++) {
float u = i/sides;
float x = cos(i*angle)*r;
float y = sin(i*angle)*r;
vertex(x, halfHeight, y, u, 1);
vertex(x, -halfHeight, y, u, 0);
}
endShape(CLOSE);
Answers
I'm able to get it to work by changing textureMode() to IMAGE, but why doesn't it work when in NORMAL mode?
Integer division on line 4
float u = i/sides;
When i and sides are both integers the result of the division will always be rounded to a full number. I guess "u" will always have a value of 0.0
You can either turn one of the variables into a float or cast it for the division:
Not exactly rounded. Rather the fractional part is removed from the result.
It's equivalent to use
(int)
casting operator over the division's result.