- 理论基础
纹理组合器函数(glTexEnvf):简单点讲就是指定纹理贴图和材质混合的方式。固定管线时代,它只是告诉你,纹理的像素读取出来之后,其实还可以经过一些复杂变换,然后才拿去使用,这样可以搞出一些绚丽效果。不过现在shader可以进行更多更自由的变换,所以这东西现在就没什么用了。
#include "GLTools.h"
#ifdef __APPLE__
#include
#else
#define FREEGLUT_STATIC
#include
#endif
#define imageWidth 8
#define imageHeight 8
static GLubyte image0[imageHeight][imageWidth][4];
static GLubyte image1[imageHeight][imageWidth][4];
static GLuint texName[4];
void makeImages(void)
{
int i, j, c;
for (i = 0; i < imageHeight; i++) {
for (j = 0; j < imageWidth; j++) {
c = ((i&2)==0)*255;
image0[i][j][0] = (GLubyte) c;
image0[i][j][1] = (GLubyte) c;
image0[i][j][2] = (GLubyte) c;
image0[i][j][3] = (GLubyte) 255;
c = ((j&4)!=0)*128;
image1[i][j][0] = (GLubyte) 0;
image1[i][j][1] = (GLubyte) c;
image1[i][j][2] = (GLubyte) c;
image1[i][j][3] = (GLubyte) 255;
}
}
}
void init(void)
{
GLint numunits[1];
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
makeImages();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(4, texName);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, image0);
glBindTexture(GL_TEXTURE_2D, texName[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
glNewList (1, GL_COMPILE);
glBegin(GL_QUADS);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glColor3f (0.5, 1.0, 0.25);
glVertex3f(0.0, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 2.0);
glColor3f (1.0, 1.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 2.0, 2.0);
glColor3f (1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 2.0, 0.0);
glColor3f (1.0, 0.5, 0.25);
glVertex3f(1.0, 0.0, 0.0);
glEnd();
glEndList ();
}
void display(void)
{
static GLfloat constColor[4] = {0.0, 0.0, 0.0, 0.0};
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(0.0, 5.0, 0.0);
glCallList(1);
glPopMatrix();
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTranslatef(1.0, 5.0, 0.0);
glCallList(1);
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texName[1]);
glTranslatef(2.0, 5.0, 0.0);
glCallList(1);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glPushMatrix();
glTranslatef(1.0, 4.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glPushMatrix();
glTranslatef(2.0, 4.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD);
glPushMatrix();
glTranslatef(3.0, 4.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD_SIGNED_ARB);
glPushMatrix();
glTranslatef(4.0, 4.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_SUBTRACT_ARB);
glPushMatrix();
glTranslatef(5.0, 4.0, 0.0);
glCallList(1);
glPopMatrix();
constColor[3] = 0.2;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
glPushMatrix();
glTranslatef(1.0, 3.0, 0.0);
glCallList(1);
glPopMatrix();
constColor[3] = 0.4;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor);
glPushMatrix();
glTranslatef(2.0, 3.0, 0.0);
glCallList(1);
glPopMatrix();
constColor[3] = 0.6;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor);
glPushMatrix();
glTranslatef(3.0, 3.0, 0.0);
glCallList(1);
glPopMatrix();
constColor[4] = 0.8;
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constColor);
glPushMatrix();
glTranslatef(4.0, 3.0, 0.0);
glCallList(1);
glPopMatrix();
glActiveTextureARB (GL_TEXTURE0_ARB);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glActiveTextureARB (GL_TEXTURE1_ARB);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texName[1]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE);
glPushMatrix();
glTranslatef(1.0, 2.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glPushMatrix();
glTranslatef(2.0, 2.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD);
glPushMatrix();
glTranslatef(3.0, 2.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ADD_SIGNED_ARB);
glPushMatrix();
glTranslatef(4.0, 2.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_SUBTRACT_ARB);
glPushMatrix();
glTranslatef(5.0, 2.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 2.0);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_REPLACE);
glPushMatrix();
glTranslatef(1.0, 1.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
glPushMatrix();
glTranslatef(2.0, 1.0, 0.0);
glCallList(1);
glPopMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_SUBTRACT_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
glPushMatrix();
glTranslatef(5.0, 1.0, 0.0);
glCallList(1);
glPopMatrix();
glActiveTextureARB (GL_TEXTURE1_ARB);
glDisable (GL_TEXTURE_2D);
glActiveTextureARB (GL_TEXTURE0_ARB);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 7.0, 0.0, 7.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)glutGetProcAddress("glActiveTextureARB");
glMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)glutGetProcAddress("glMultiTexCoord2fARB");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}