Программа расчёта координат текстуры для произвольной 3D-геометрии
void CScene::InitGL()
{
glPolygonOffset(-2.0, -1.0);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glLineWidth(3.0);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
glClearStencil(0);
glClearDepth(1.0f);
glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
// texture setup
static GLfloat sPlane[4] = { 1.0, 0.0, 0.0, 0.0 };
static GLfloat tPlane[4] = { 0.0, 1.0, 0.0, 0.0 };
static GLfloat rPlane[4] = { 0.0, 0.0, 1.0, 0.0 };
static GLfloat qPlane[4] = { 0.0, 0.0, 0.0, 1.0 };
// Texgen that maps object coordinates directly to texture coordinates
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
glTexGenfv(GL_R, GL_OBJECT_PLANE, rPlane);
glTexGenfv(GL_Q, GL_OBJECT_PLANE, qPlane);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void CScene::MakeTextureMatrix()
{
static GLfloat slideX=0.0f, slideY=0.0f;
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(slideX/(GLfloat)objectTexture.width,
slideY/(GLfloat)objectTexture.height,
0.0f);
glScalef(cx/(GLfloat)objectTexture.width,
cy/(GLfloat)objectTexture.height,
1.0);
gluPerspective(fov, this->cx/this->cy, zNear, zFar); // projection matrix
glTranslatef(tx, ty, tz); // view matrix
glRotatef(35, 1, 0, 0);
if (rotate) glRotatef(curry, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
}