#include "GL/glut.h" #include #include #include #include #include #define WIN_WIDTH 100 #define WIN_HEIGHT 100 #define TEX_WIDTH 8 #define TEX_HEIGHT 4 #define EPSILON_R 0.4 #define EPSILON_G 0.4 #define EPSILON_B 0.4 #define EPSILON_A 0.4 GLfloat read_buf[4 * TEX_WIDTH * TEX_HEIGHT]; GLfloat tex_image2d[TEX_WIDTH][TEX_HEIGHT][3]; GLint tex_name = 1; void init() { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-1.0, -1.0, 0.0); glScalef(2.0/WIN_WIDTH, 2.0/WIN_HEIGHT, 1.0); glClearColor(0.0, 0.0, 0.0, 0.3); glClear(GL_COLOR_BUFFER_BIT); glDisable (GL_DITHER); } int check_pixels(GLint llx, GLint lly, GLint w, GLint h, GLfloat exp_r, GLfloat exp_g, GLfloat exp_b, GLfloat exp_a) { GLfloat *buf = NULL, color_r, color_g, color_b, color_a; int x, y, pixel; buf = (GLfloat *)malloc(w * h * sizeof(GLfloat) * 4); if (buf == NULL) return -2; glReadPixels(llx, lly, w, h, GL_RGBA, GL_FLOAT, buf); for (y = 0; y < h; y ++) { for (x = 0; x < w; x ++) { pixel = x + y * w; color_r = buf[pixel*4]; color_g = buf[pixel*4+1]; color_b = buf[pixel*4+2]; color_a = buf[pixel*4+3]; if (fabsf(color_r - exp_r) > EPSILON_R || fabsf(color_g - exp_g) > EPSILON_G || fabsf(color_b - exp_b) > EPSILON_B || fabsf(color_a - exp_a) > EPSILON_A) { printf("pixel[%d, %d]: %f %f %f %f\n", x, y, buf[pixel*4], buf[pixel*4+1], buf[pixel*4+2], buf[pixel*4+3]); } } } printf("\ncheck pixels done.\n"); if (buf) free(buf); return 0; } void display() { GLint i, read_w = 0, read_h = 0, x = 0, y = 0, pixel; glClear(GL_COLOR_BUFFER_BIT); if (glIsEnabled(GL_ALPHA_TEST) == GL_FALSE) printf("Alpha test is disabled \n"); glColor4f(0.0, 0.1, 1.0, 0.8); glBegin(GL_QUAD_STRIP); glVertex2i(10, 10); glVertex2i(10, 60); glVertex2i(60, 10); glVertex2i(60, 60); glEnd(); check_pixels(10, 10, 50, 50, 0.0, 0.1, 1.0, 0.8); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init(); glutDisplayFunc(display); glutMainLoop(); }