#include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 #define TEST_WIDTH 32 #define TEST_HEIGHT 32 GLubyte *generated; GLfloat *expected; GLfloat *results; void GenerateTexture() { int totalComponents = TEST_WIDTH * TEST_HEIGHT; int i; generated = malloc(totalComponents * 2 * sizeof(GLubyte)); expected = malloc(totalComponents * 4 * sizeof(GLfloat)); for (i = 0; i < totalComponents; i++) { generated[i*2] = i % 255; generated[i*2+1] = i % 255; expected[i*4 + 0] = ((float)generated[i*2]) / 256; expected[i*4 + 1] = ((float)generated[i*2]) / 256; expected[i*4 + 2] = ((float)generated[i*2]) / 256; expected[i*4 + 3] = ((float)generated[i*2+1]) / 256; } } //////////////////////////////////////////////////////////////////////// static void ClearAll() { void *tmp = malloc(TEST_WIDTH * TEST_HEIGHT * 4 * sizeof(GLfloat)); memset(tmp, 0, TEST_WIDTH * TEST_HEIGHT * 4 * sizeof(GLfloat)); glTexImage2D(GL_TEXTURE_2D, 0, 1, TEST_WIDTH, TEST_HEIGHT, 0, GL_RGBA, GL_FLOAT, tmp); free(tmp); } static void Draw2D(int x, int y) { int w = TEST_WIDTH; int h = TEST_HEIGHT; glBegin(GL_POLYGON); glTexCoord2f(0.0, 0.0); glVertex2f(x+0, y+0); glTexCoord2f(0.0, 1.0); glVertex2f(x+0, y+h); glTexCoord2f(1.0, 1.0); glVertex2f(x+w, y+h); glTexCoord2f(1.0, 0.0); glVertex2f(x+w, y+0); glEnd(); } int Compare() { int i; for (i=0; i< TEST_WIDTH * TEST_HEIGHT; i++){ if (fabs(results[i*4] - expected[i]) > 0.001 || fabs(results[i*4+1] - expected[i*4+1]) > 0.001 || fabs(results[i*4+2] - expected[i*4+2]) > 0.001 || fabs(results[i*4+3] - expected[i*4+3]) > 0.001){ printf ("at: %d\n", i); printf ("result rgba: %f, %f, %f, %f\n", results[i*4], results[i*4+1], results[i*4+2], results[i*4+3]); printf ("expected rgba: %f, %f, %f, %f\n", expected[i*4], expected[i*4+1], expected[i*4+2], expected[i*4+3]); return -1; } } return 0; } long TestAll() { glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_TEXTURE_2D); Draw2D(0,10); glDisable(GL_TEXTURE_2D); results = malloc (TEST_WIDTH * TEST_HEIGHT * 4 * sizeof(GLfloat)); // Compare the 2D test results with the expected results. glReadPixels(0, 10, TEST_WIDTH, TEST_HEIGHT, GL_RGBA, GL_FLOAT, results); // return Compare(); return 0; } void init(void) { glDisable(GL_DITHER); glClearColor(1,1,1,1); glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } static void test(void) { GenerateTexture(); ClearAll(); glTexImage2D(GL_TEXTURE_2D, 0, 1, //internalFormat TEST_WIDTH, TEST_HEIGHT, 0, GL_LUMINANCE_ALPHA, //format GL_UNSIGNED_BYTE, //type generated); if (TestAll() == -1) { ; //exit(1); } } 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; }