我尝试使用GL_QUADS,GL_TRIANGLES和GL_POLYGON扭曲飞机:
glBegin(GL_QUADS);
for (int i = 0; i < squareIn.size(); i++) {
glNormal3f(0,0,1);
glTexCoord2f(squareIn[i]->v1->x,squareIn[i]->v1->y);
glVertex2f(squareOut[i]->v1->x,squareOut[i]->v1->y);
glTexCoord2f(squareIn[i]->v2->x,squareIn[i]->v2->y);
glVertex2f(squareOut[i]->v2->x,squareOut[i]->v2->y);
glTexCoord2f(squareIn[i]->v3->x,squareIn[i]->v3->y);
glVertex2f(squareOut[i]->v3->x,squareOut[i]->v3->y);
glTexCoord2f(squareIn[i]->v4->x,squareIn[i]->v4->y);
glVertex2f(squareOut[i]->v4->x,squareOut[i]->v4->y);
glTexCoord2f(squareIn[i]->v1->x,squareIn[i]->v1->y);
glVertex2f(squareOut[i]->v1->x,squareOut[i]->v1->y);
}
glEnd();
但是当我做出极端失真时,所有这些都会给出这个结果:
预期结果:
我现在看到了两种可能的解决方案,使用glNormal或glMultMatrixf。
glNormal看起来很优雅,而glMultMatrixf看起来很复杂。 但是我找不到任何解释如何计算glNormals以纹理平面的代码。
这是使用glMultMatrif的a good example。
答案 0 :(得分:2)
我处理的OpenFrameworks代码将图像细分为部分adapted from here:
//----------------------------------------- setup
#define GRID_X 8
#define GRID_Y 8
float * compL = new float[GRID_Y];
float * compR = new float[GRID_Y];
memset(compL, 0, GRID_Y * sizeof(float));
memset(compR, 0, GRID_Y * sizeof(float));
ofPoint * grid = new ofPoint[GRID_X * GRID_Y];
ofPoint * coor = new ofPoint[GRID_X * GRID_Y];
int width = imageGrid.width;
int height = imageGrid.height;
ofPoint quad[4];
ofPoint utQuad[4];
//----------------------------------------- update
quad[0].set(0,0,0);
quad[1].set(width,0,0);
quad[2].set(mouseX,mouseY,0);
quad[3].set(0,height,0);
utQuad[0].set(0,0,0);
utQuad[1].set(1,0,0);
utQuad[2].set(1,1,0);
utQuad[3].set(0,1,0);
int gridSizeX = GRID_X;
int gridSizeY = GRID_Y;
float xRes = 1.0/(gridSizeX-1);
float yRes = 1.0/(gridSizeY-1);
for(int y = 0; y < gridSizeY; y++){
for(int x = 0; x < gridSizeX; x++){
int index = y*gridSizeX + x;
float pctx = (float)x * xRes;
float pcty = (float)y * yRes;
float pctyL = pcty + yRes*compL[y];
float pctyR = pcty + yRes*compR[y];
float linePt0x = (1-pctyL)*quad[0].x + pctyL * quad[3].x;
float linePt0y = (1-pctyL)*quad[0].y + pctyL * quad[3].y;
float linePt1x = (1-pctyR)*quad[1].x + pctyR * quad[2].x;
float linePt1y = (1-pctyR)*quad[1].y + pctyR * quad[2].y;
float ptx = (1-pctx) * linePt0x + pctx * linePt1x;
float pty = (1-pctx) * linePt0y + pctx * linePt1y;
float utPt0x = (1-pcty)*utQuad[0].x + pcty * utQuad[3].x;
float utPt0y = (1-pcty)*utQuad[0].y + pcty * utQuad[3].y;
float utPt1x = (1-pcty)*utQuad[1].x + pcty * utQuad[2].x;
float utPt1y = (1-pcty)*utQuad[1].y + pcty * utQuad[2].y;
float tt = (1-pctx) * utPt0x + pctx * utPt1x;
float uu = (1-pctx) * utPt0y + pctx * utPt1y;
grid[index].set(ptx, pty, 0);
coor[index].set( (tt * imageGrid.getTextureReference().texData.tex_t), imageGrid.getTextureReference().texData.bFlipTexture ? imageGrid.getTextureReference().texData.tex_u - (uu * imageGrid.getTextureReference().texData.tex_u) : (uu * imageGrid.getTextureReference().texData.tex_u), 0);
}
}
//----------------------------------------- draw
ofSetColor(255, 255, 255);
ofFill();
glEnable(imageGrid.getTextureReference().texData.textureTarget);
glBindTexture( imageGrid.getTextureReference().texData.textureTarget, (GLuint)imageGrid.getTextureReference().texData.textureID);
for(int y = 0; y < gridSizeY-1; y++){
for(int x = 0; x < gridSizeX-1; x++){
glBegin(GL_QUADS);
int pt0 = x + y*gridSizeX;
int pt1 = (x+1) + y*gridSizeX;
int pt2 = (x+1) + (y+1)*gridSizeX;
int pt3 = x + (y+1)*gridSizeX;
glTexCoord2f(coor[pt0].x, coor[pt0].y);
glVertex2f( grid[pt0].x, grid[pt0].y);
glTexCoord2f(coor[pt1].x, coor[pt1].y);
glVertex2f( grid[pt1].x, grid[pt1].y);
glTexCoord2f(coor[pt2].x, coor[pt2].y);
glVertex2f( grid[pt2].x, grid[pt2].y);
glTexCoord2f(coor[pt3].x, coor[pt3].y);
glVertex2f( grid[pt3].x, grid[pt3].y);
glEnd();
}
}
glDisable(imageGrid.getTextureReference().texData.textureTarget);
//Draw grid
ofSetColor(255, 0, 0);
ofNoFill();
for(int y = 0; y < gridSizeY-1; y++){
for(int x = 0; x < gridSizeX-1; x++){
glBegin(GL_LINE_LOOP);
int pt0 = x + y*gridSizeX;
int pt1 = (x+1) + y*gridSizeX;
int pt2 = (x+1) + (y+1)*gridSizeX;
int pt3 = x + (y+1)*gridSizeX;
glVertex2f( grid[pt0].x, grid[pt0].y);
glVertex2f( grid[pt1].x, grid[pt1].y);
glVertex2f( grid[pt2].x, grid[pt2].y);
glVertex2f( grid[pt3].x, grid[pt3].y);
glEnd();
}
}
结果很棒: