cylindrical texture map doesn't fit
in
Core Library Questions
•
1 year ago
hi,
i've mapped an image to a cylinder in processing. the texture is mapped to the xyzuv coordinates, however, the texture overlaps. if i double the number of sides, there's an extra space between the beginning and end of the texture.
import processing.opengl.*;
PImage img;
void setup()
{
smooth();
img = loadImage("lines2.png");
size(1000, 1000, OPENGL);
}
void draw()
{
background(255);
noStroke();
lights();
int passedTime = millis() - savedTime;
println(passedTime/1000);
pushMatrix();
translate(width/2, height/2);
rotateY(map(mouseX, 0, width, -PI, PI));
drawCylinder(30, img.width/(2 * PI), img.height); //the texture overlaps at 30 sides and, at 60 leaves a gap
popMatrix();
}
void drawCylinder(int sides, float r, float h)
{
float angle = 360 / sides;
float halfHeight = h / 2;
// draw top of the tube
beginShape();
for (int i = 0; i < sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, -halfHeight, z);
}
endShape(CLOSE);
// draw bottom of the tube
beginShape();
for (int i = 0; i < sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, halfHeight, z);
}
endShape(CLOSE);
// first texture
beginShape(QUAD_STRIP);
texture(img);
for (int i = 0; i <= sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, -halfHeight, z, img.width/sides * i, 0);
vertex( x, halfHeight, z, img.width/sides * i, img.height);
}
endShape(CLOSE);
}
thanks,
destro
i've mapped an image to a cylinder in processing. the texture is mapped to the xyzuv coordinates, however, the texture overlaps. if i double the number of sides, there's an extra space between the beginning and end of the texture.
import processing.opengl.*;
PImage img;
void setup()
{
smooth();
img = loadImage("lines2.png");
size(1000, 1000, OPENGL);
}
void draw()
{
background(255);
noStroke();
lights();
int passedTime = millis() - savedTime;
println(passedTime/1000);
pushMatrix();
translate(width/2, height/2);
rotateY(map(mouseX, 0, width, -PI, PI));
drawCylinder(30, img.width/(2 * PI), img.height); //the texture overlaps at 30 sides and, at 60 leaves a gap
popMatrix();
}
void drawCylinder(int sides, float r, float h)
{
float angle = 360 / sides;
float halfHeight = h / 2;
// draw top of the tube
beginShape();
for (int i = 0; i < sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, -halfHeight, z);
}
endShape(CLOSE);
// draw bottom of the tube
beginShape();
for (int i = 0; i < sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, halfHeight, z);
}
endShape(CLOSE);
// first texture
beginShape(QUAD_STRIP);
texture(img);
for (int i = 0; i <= sides; i++) {
float x = cos( radians( i * angle ) ) * r;
float z = sin( radians( i * angle ) ) * r;
vertex( x, -halfHeight, z, img.width/sides * i, 0);
vertex( x, halfHeight, z, img.width/sides * i, img.height);
}
endShape(CLOSE);
}
thanks,
destro
1