/* Simplest demo of texture mapping using static arrays instead files. There are two arrays, one black&white and one color. Toggle between them with the 'u' callback. Scott D. Anderson Fall 2004 */ #include #include #include bool BW = true; // which flag to show void toggleFlag(unsigned char key, int x, int y) { BW = !BW; glutPostRedisplay(); } /* ================================================================ Texture arrays */ // The dimensions of the black and white texture array. Note that they // are both powers of 2. const int BWwidth = 4; const int BWheight = 4; // The array elements are unsigned bytes: grayscale values from 0 to 255. GLubyte bwTexture[BWwidth][BWheight] = { { 0, 0, 0, 255 }, { 0, 0, 255, 0 }, { 255, 0, 255, 255 }, { 0, 255, 255, 255 } }; // The dimensions of the color texture array. const int ColorWidth = 2; const int ColorHeight = 2; // We add a extra value to handle the issue of rows being aligned on a // four-byte boundary. By using RGBA instead of RGB, the rows necessarily // are aligned on a four-byte boundary, since each element is 4 bytes. GLubyte colorTexture[2][2][4] = { { {0,0,0,255}, {255,0,0,255} }, { {0,255,0,255}, {0,0,255,255} } }; /* ================================================================ */ void display() { twDisplayInit(); twCamera(); glPushAttrib(GL_ALL_ATTRIB_BITS); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 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_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glPixelStorei(GL_UNPACK_ALIGNMENT,1); if(BW) { glTexImage2D(GL_TEXTURE_2D, 0, 3, BWwidth, BWheight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, bwTexture); } else { glTexImage2D(GL_TEXTURE_2D, 0, 3, ColorWidth, ColorHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorTexture); } twError(); glEnable(GL_TEXTURE_2D); glutSolidTeapot(1); glPopAttrib(); glFlush(); glutSwapBuffers(); } /* ================================================================ */ int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500, 500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(0,10,0,5,-1,1); twMainInit(); twKeyCallback('u',toggleFlag,"toggle which flag to show"); glutMainLoop(); }