#include #include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 #define TEX_SIZE 64 GLfloat colorTable[][3] = { { 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 1.0, 1.0 }, { 1.0, 1.0, 0.0 }, { 1.0, 0.0, 1.0 }, { 0.0, 1.0, 1.0 } }; void CreateTexture(GLint texEnum, GLfloat *buf) { GLfloat *ptr; GLint level, size, i, area; level = 0; for (size = TEX_SIZE; size > 0; size /= 2) { area = size; ptr = buf; for (i = 0; i < area; i++) { *ptr++ = colorTable[level][0]; *ptr++ = colorTable[level][1]; *ptr++ = colorTable[level][2]; } glTexImage1D(GL_TEXTURE_1D, level, 3, size, 0, GL_RGB, GL_FLOAT, (unsigned char *)buf); level++; } } GLuint texName[1]; GLfloat *buf; void init(void) { glGenTextures(1, texName); glDisable(GL_DITHER); glClearColor(0.0, 0.0, 0.0, 1.0); buf = (GLfloat *)malloc(WINDSIZEX*WINDSIZEY*3*sizeof(GLfloat)); glBindTexture(GL_TEXTURE_1D, texName[0]); CreateTexture(GL_TEXTURE_1D, buf); } void draw() { GLint size; GLfloat s, t; s = 1; t = 0; size = TEX_SIZE; glBegin(GL_POLYGON); glTexCoord2f(0.0, 0.0); glVertex2i(0, 0); glTexCoord2f(0.0, t); glVertex2i(0, size); glTexCoord2f(s, t); glVertex2i(size, size); glTexCoord2f(s, 0.0); glVertex2i(size, 0); glEnd(); glReadPixels(0, 0, size, size, GL_RGB, GL_FLOAT, buf); printf("get pixel at (0, 0): %f %f %f\n", buf[0], buf[1], buf[2]); // printf("should be: %f %f %f %f\n", expected[0], expected[1], expected[2], expected[3]); } void test(void) { glClear(GL_COLOR_BUFFER_BIT); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_1D, texName[0]); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_LOD_BIAS_EXT, 1); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); glTexEnvi(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 1); glEnable(GL_TEXTURE_1D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); draw(); glDisable(GL_TEXTURE_1D); } void display(void) { glViewport(0, 0, WINDSIZEX, WINDSIZEY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDSIZEX, 0, WINDSIZEY); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); test(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDSIZEX, WINDSIZEY); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }