#include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 #define TEXSIZEX 8 #define TEXSIZEY 64 GLfloat *buf; GLfloat colorMap[][3] = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 1.0, 0.0 }, { 1.0, 0.0, 1.0 }, { 0.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 } }; static void makeMipmap(GLsizei sizeX, GLsizei sizeY) { GLfloat *ptr; GLint level, i; level = 0; while (sizeX > 0 || sizeY > 0) { ptr = buf; for (i = 0; i < ((sizeX==0)?1:sizeX) * ((sizeY==0)?1:sizeY); i++) { *ptr++ = colorMap[level][0]; *ptr++ = colorMap[level][1]; *ptr++ = colorMap[level][2]; } glTexImage2D(GL_TEXTURE_2D, level++, GL_RGB, (sizeX==0)?1:sizeX, (sizeY==0)?1:sizeY, 0, GL_RGB, GL_FLOAT, (unsigned char *)buf); sizeX /= 2; sizeY /= 2; } } void Draw(GLfloat sizeX, GLfloat sizeY) { GLfloat x, y; x = (1.0 / sizeX); y = (1.0 / sizeY) / 16; printf ("x, y: %f %f\n", x, y); glBegin(GL_POLYGON); glTexCoord2f(0.0, 0.0); glVertex2f(0, 0); glTexCoord2f(x, 0.0); glVertex2f(0+1.0, 0); glTexCoord2f(x, y); glVertex2f(0+1.0, 1.0); glTexCoord2f(0.0, y); glVertex2f(0, 1.0); glEnd(); } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glDisable(GL_DITHER); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); buf = (GLfloat *)malloc(WINDSIZEX*WINDSIZEY*3*sizeof(GLfloat)); makeMipmap(TEXSIZEX, TEXSIZEY); } static void test(void) { int drawCount; // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.5, 0.5, 0.5); Draw(TEXSIZEX, TEXSIZEY); glReadPixels(0, 0, 1, 1, GL_RGB, GL_FLOAT, buf); printf("buf: %f, %f, %f\n", buf[0], buf[1], buf[2]); printf("it should be: 1.0, 0.0, 0.0\n"); } 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; }