Textured rounded box, last stretch
in
Programming Questions
•
2 months ago
Hello all, for a project I need a textured rounded box, because I couldn't find it I thought I'd make it myself, its nearly there I am just missing the last small bit, but because my brain is hurting due to all the trigionometry terror I just can't figure it out. I hope someone here can point it out :)
The script requires a texture named "tex" once you run the sketch you'll see that the rounded corners are now round and therefore do not fit right, I think I'm missing some kind of sinus cosinus tangent thingy at the rounded corners vertex part.
Thank you!
void roundedCube(PImage tex, float xSize, float ySize, float zSize, float radR) {
int tesVal = 2;
float rad = radR;
pushMatrix();
boxFace(xSize, ySize, zSize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(xSize, ySize, zSize, radR, tesVal);
rotate(PI/2, 1, 0, 0);
boxFace(xSize, zSize, ySize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(xSize, zSize, ySize, radR, tesVal);
rotate(PI/2, 0, 1, 0);
boxFace(ySize, zSize, xSize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(ySize, zSize, xSize, radR, tesVal);
popMatrix();
}
void boxFace(float a, float b, float c, float rad, int tes) {
int tesVal = tes;
float xVal= a-rad;
float yVal = b-rad;
float zVal = c-rad;
beginShape(QUADS);
texture(tex);
vertex(-xVal, -yVal, c, (-xVal/(2*a))+0.5, (-yVal/(2*b))+0.5);
vertex( xVal, -yVal, c, (xVal/(2*a))+0.5, (-yVal/(2*b))+0.5);
vertex( xVal, yVal, c, (xVal/(2*a))+0.5, (yVal/(2*b))+0.5);
vertex(-xVal, yVal, c, (-xVal/(2*a))+0.5, (yVal/(2*b))+0.5);
endShape();
//half a round edge two times
for (int i = 0;i<2;i++) {
rotate(PI, 0, 0, 1);
beginShape(QUAD_STRIP);
texture(tex);
for (int j=0;j<=tesVal;j++) {
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float xRot = rad*cos(angle);
//float zRot = rad*sin(angle);
vertex(xVal +xRot, yVal, zVal+rad*sin(angle), (xVal+xRot)/(2*a)+0.5, (yVal/b)+0.5);
vertex(xVal +xRot, -yVal, zVal+rad*sin(angle), (xVal+xRot)/(2*a)+0.5, (-yVal/b)+0.5);
}
endShape();
}
//half a round edge two times again
for (int i = 0;i<2;i++) {
rotate(PI, 0, 0, 1);
beginShape(QUAD_STRIP);
texture(tex);
for (int j=0;j<=tesVal;j++) {
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float yRot = rad*cos(angle);
vertex(xVal, yVal+yRot, zVal+rad*sin(angle), (xVal/(2*a))+0.5, (yVal+yRot)/(2*b)+0.5 );
vertex(-xVal, yVal+yRot, zVal+rad*sin(angle), (-xVal/(2*a))+0.5, (yVal+yRot)/(2*b)+0.5);
}
endShape();
}
//Round corners
float xMul =1;
float yMul = 1;
for (int i = 0;i<4;i++) {
if (i==1) {
yMul=-1;
}
else if (i==2) {
xMul = -1;
}
else if (i==3) {
yMul = 1;
}
else {
}
for (int j=0;j<tesVal;j++) {
beginShape(QUAD_STRIP);
texture(tex);
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float angle3 =PI/4+ (PI/4)*(float(j+1)/tesVal);
for (int k=0;k<=tesVal;k++) {
float angle2 = (PI/2)*(float(k)/tesVal);
float angle4 = (PI/2)*(float(k+1)/tesVal);
float xRot = rad*cos(angle);
float yRot = rad*sin(angle2);
//I guess I am missing some thing at the z vertex
vertex(xMul*xVal +xMul*rad*cos(angle)*cos(angle2), yMul*yVal+yMul*rad*sin(angle2)*cos(angle), zVal+rad*sin(angle), (xMul*xVal +xMul*rad*cos(angle)*cos(angle2))/(2*a)+0.5, (yMul*yVal+yMul*rad*sin(angle2)*cos(angle))/(2*b)+0.5);
vertex(xMul*xVal +xMul*rad*cos(angle3)*cos(angle2), yMul*yVal+yMul* rad*sin(angle2)*cos(angle3), zVal+rad*sin(angle3), (xMul*xVal +xMul*rad*cos(angle3)*cos(angle2))/(2*a)+0.5, (yMul*yVal+yMul*rad*sin(angle2)*cos(angle3))/(2*b)+0.5);
}
endShape();
}
}
}
The script requires a texture named "tex" once you run the sketch you'll see that the rounded corners are now round and therefore do not fit right, I think I'm missing some kind of sinus cosinus tangent thingy at the rounded corners vertex part.
Thank you!
void roundedCube(PImage tex, float xSize, float ySize, float zSize, float radR) {
int tesVal = 2;
float rad = radR;
pushMatrix();
boxFace(xSize, ySize, zSize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(xSize, ySize, zSize, radR, tesVal);
rotate(PI/2, 1, 0, 0);
boxFace(xSize, zSize, ySize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(xSize, zSize, ySize, radR, tesVal);
rotate(PI/2, 0, 1, 0);
boxFace(ySize, zSize, xSize, radR, tesVal);
rotate(PI, 1, 0, 0);
boxFace(ySize, zSize, xSize, radR, tesVal);
popMatrix();
}
void boxFace(float a, float b, float c, float rad, int tes) {
int tesVal = tes;
float xVal= a-rad;
float yVal = b-rad;
float zVal = c-rad;
beginShape(QUADS);
texture(tex);
vertex(-xVal, -yVal, c, (-xVal/(2*a))+0.5, (-yVal/(2*b))+0.5);
vertex( xVal, -yVal, c, (xVal/(2*a))+0.5, (-yVal/(2*b))+0.5);
vertex( xVal, yVal, c, (xVal/(2*a))+0.5, (yVal/(2*b))+0.5);
vertex(-xVal, yVal, c, (-xVal/(2*a))+0.5, (yVal/(2*b))+0.5);
endShape();
//half a round edge two times
for (int i = 0;i<2;i++) {
rotate(PI, 0, 0, 1);
beginShape(QUAD_STRIP);
texture(tex);
for (int j=0;j<=tesVal;j++) {
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float xRot = rad*cos(angle);
//float zRot = rad*sin(angle);
vertex(xVal +xRot, yVal, zVal+rad*sin(angle), (xVal+xRot)/(2*a)+0.5, (yVal/b)+0.5);
vertex(xVal +xRot, -yVal, zVal+rad*sin(angle), (xVal+xRot)/(2*a)+0.5, (-yVal/b)+0.5);
}
endShape();
}
//half a round edge two times again
for (int i = 0;i<2;i++) {
rotate(PI, 0, 0, 1);
beginShape(QUAD_STRIP);
texture(tex);
for (int j=0;j<=tesVal;j++) {
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float yRot = rad*cos(angle);
vertex(xVal, yVal+yRot, zVal+rad*sin(angle), (xVal/(2*a))+0.5, (yVal+yRot)/(2*b)+0.5 );
vertex(-xVal, yVal+yRot, zVal+rad*sin(angle), (-xVal/(2*a))+0.5, (yVal+yRot)/(2*b)+0.5);
}
endShape();
}
//Round corners
float xMul =1;
float yMul = 1;
for (int i = 0;i<4;i++) {
if (i==1) {
yMul=-1;
}
else if (i==2) {
xMul = -1;
}
else if (i==3) {
yMul = 1;
}
else {
}
for (int j=0;j<tesVal;j++) {
beginShape(QUAD_STRIP);
texture(tex);
float angle =PI/4+ (PI/4)*(float(j)/tesVal);
float angle3 =PI/4+ (PI/4)*(float(j+1)/tesVal);
for (int k=0;k<=tesVal;k++) {
float angle2 = (PI/2)*(float(k)/tesVal);
float angle4 = (PI/2)*(float(k+1)/tesVal);
float xRot = rad*cos(angle);
float yRot = rad*sin(angle2);
//I guess I am missing some thing at the z vertex
vertex(xMul*xVal +xMul*rad*cos(angle)*cos(angle2), yMul*yVal+yMul*rad*sin(angle2)*cos(angle), zVal+rad*sin(angle), (xMul*xVal +xMul*rad*cos(angle)*cos(angle2))/(2*a)+0.5, (yMul*yVal+yMul*rad*sin(angle2)*cos(angle))/(2*b)+0.5);
vertex(xMul*xVal +xMul*rad*cos(angle3)*cos(angle2), yMul*yVal+yMul* rad*sin(angle2)*cos(angle3), zVal+rad*sin(angle3), (xMul*xVal +xMul*rad*cos(angle3)*cos(angle2))/(2*a)+0.5, (yMul*yVal+yMul*rad*sin(angle2)*cos(angle3))/(2*b)+0.5);
}
endShape();
}
}
}
1