It turned out that my deductions were correct. Increasing the number of quads on the cube face improved it no end. The code below uses an 8x8 grid so there are 64 Squares and the diagonals are now an eighth of the original size reducing distortion.
I have modified your program so that it can easily be adapted so that you can change faces. It not the neatest code I have ever done but it is always harder to add 'unplanned features' to a program without some compromises.
You can try different grid sizes by changeing the gridSize variable.
The makeGrid function expects 4 integers representing the index values to use in your coordinate arrays.
Code:
import processing.opengl.*;
PImage img;
PFont font;
float[] xy = new float[2];
int grootte = 300;
Boolean enter = false;
float rotatex = 0;
float rotatey = 0;
float rotatez = 0;
float bokz = 0;
float[] current_x = new float[8];
float[] current_y = new float[8];
// Store texture coordinates for a face with multiple QUADS
int gridSize;
Point[][] gpoint;
class Point {
public float x;
public float y;
public void interpolate(Point start, Point end, float interp){
x = start.x + interp * (end.x - start.x);
y = start.y + interp * (end.y - start.y);
}
}
void setup() {
size(800, 533, P3D);
gridSize = 8; // Try different values
gpoint = new Point[gridSize + 1][gridSize + 1];
for(int i = 0; i < gridSize + 1; i++)
for(int j = 0; j < gridSize + 1; j++)
gpoint[i][j] = new Point();
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
float aspect = float(width)/float(height);
float zNear = cameraZ/10.0;
float zFar = cameraZ*10.0;
perspective(fov, aspect, zNear, zFar);
// img = loadImage("img.jpg");
img = loadImage("house.jpg");
font = createFont("Serif",12);
}
void draw() {
background(64);
if(mousePressed == true) {
xy[0] = mouseX;
xy[1] = mouseY;
}
drawBox(xy[0], xy[1], bokz, grootte, rotatez, rotatey, rotatex);
if (enter) {
drawShape();
}
tint(255, 128);
image(img, 0, 0);
}
void makeFaceGrid(int c0, int c1, int c2, int c3){
// Set the corners
gpoint[0][0].x = current_x[c0];
gpoint[0][0].y = current_y[c0];
gpoint[0][gridSize].x = current_x[c3];
gpoint[0][gridSize].y = current_y[c3];
gpoint[gridSize][gridSize].x = current_x[c2];
gpoint[gridSize][gridSize].y = current_y[c2];
gpoint[gridSize][0].x = current_x[c1];
gpoint[gridSize][0].y = current_y[c1];
float t = 1.0/gridSize;
int e , r, c;
// Calc the 4 grid edges
for(e = 0; e < gridSize + 1; e++){
gpoint[e][0].interpolate(gpoint[0][0], gpoint[gridSize][0], e * t);
gpoint[e][gridSize].interpolate(gpoint[0][gridSize], gpoint[gridSize][gridSize], e * t);
gpoint[0][e].interpolate(gpoint[0][0], gpoint[0][gridSize], e * t);
gpoint[gridSize][e].interpolate(gpoint[gridSize][0], gpoint[gridSize][gridSize], e * t);
}
// Calc interia
for(r = 1; r < gridSize; r++){
for(c = 0; c < gridSize; c++){
gpoint[c][r].interpolate(gpoint[0][r], gpoint[gridSize][r], c * t);
}
}
}
void drawShape() {
makeFaceGrid(3, 7, 4, 0);
beginShape(QUADS);
texture(img);
float delta = grootte/ (float)gridSize;
for(int r = 0; r < gridSize; r++){
for(int c = 0; c < gridSize; c++){
vertex(c * delta, r * delta, gpoint[c][r].x, gpoint[c][r].y);
vertex(c * delta, (r+1) * delta, gpoint[c][r+1].x, gpoint[c][r+1].y);
vertex((c+1) * delta, (r+1) * delta, gpoint[c+1][r+1].x, gpoint[c+1][r+1].y);
vertex((c+1) * delta, r * delta, gpoint[c+1][r].x, gpoint[c+1][r].y);
}
}
endShape();
}
void drawBox(float x, float y, float z, int boxsize, float rotz, float roty, float rotx) {
stroke(255);
//fill(255, 64);
noFill();
pushMatrix();
translate(x, y, z);
rotateZ(rotz);
rotateY(roty);
rotateX(rotx);
box(boxsize);
int n = boxsize/2;
fill(255);
translate(0, 0, -n);
textFont(font);
text("a, "+screenX(n, n, 0)+", "+screenY(n, n, 0), n, n);
current_x[0] = screenX(n, n, 0);
current_y[0] = screenY(n, n, 0);
text("b", -n, n);
current_x[1] = screenX(-n, n, 0);
current_y[1] = screenY(-n, n, 0);
text("c, "+screenX(-n,-n,0)+", "+screenY(-n,-n,0), -n, -n);
current_x[2] = screenX(-n, -n, 0);
current_y[2] = screenY(-n,-n,0);
text("d, "+screenX(n,-n,0)+", "+screenY(n, -n,0), n, -n);
current_x[3] = screenX(n, -n, 0);
current_y[3] = screenY(n, -n, 0);
translate(0,0, n*2); // boxsize
text("e", n, n);
current_x[4] = screenX(n, n, 0);
current_y[4] = screenY(n, n, 0);
text("f", -n, n);
current_x[5] = screenX(-n, n, 0);
current_y[5] = screenY(-n, n, 0);
text("g", -n, -n);
current_x[6] = screenX(-n, -n, 0);
current_y[6] = screenY(-n, -n, 0);
text("h, "+screenX(n,-n,0)+", "+screenY(n, -n,0), n, -n);
current_x[7] = screenX(n,-n,0);
current_y[7] = screenY(n,-n,0);
popMatrix();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
rotatex += PI/180;
}
else
if (keyCode == DOWN) {
rotatex -= PI/180;
}
else
if (keyCode == LEFT) {
rotatey -= PI/180;
}
else
if (keyCode == RIGHT) {
rotatey += PI/180;
}
else
if (keyCode == KeyEvent.VK_PAGE_DOWN) {
rotatez -= PI/180;
}
else
if (keyCode == KeyEvent.VK_PAGE_UP ) {
rotatez += PI/180;
}
}
else
if (key == '=' || key == '+') {
grootte += 5;
}
else
if (key == '-' || key == '_') {
grootte -= 5;
}
if (key == '[' || key == '{') {
bokz += 5;
}
else
if (key == ']' || key == '}') {
bokz -= 5;
}
else
if (key == ',' || key == '<') {
rotatez -= PI/180;
}
else
if (key == '.' || key == '>') {
rotatez += PI/180;
}
else
if (key == ENTER || key == RETURN ) {
enter = true;
}
}