/* alpha_fade2.c uses jpegread to input a .jpg file, needs jpeg.h */ /* also needs files p1.jpg, p2.jpg, p3.jpg. p4.jpg */ #include #include static int fade=0; static GLubyte rgbpix1[4*480*400]; /* size bigger than p1.jpg */ static int width1 = 480; /* also read in */ static int height1 = 400; static int alpha1 = 255; static GLubyte rgbpix2[4*480*400]; /* size bigger than p2.jpg */ static int width2 = 480; /* also read in */ static int height2 = 400; static int alpha2 = 0; static GLubyte rgbpix3[4*480*400]; /* size bigger than p3.jpg */ static int width3 = 480; /* also read in */ static int height3 = 400; static int alpha3 = 0; static GLubyte rgbpix4[4*480*400]; /* size bigger than p4.jpg */ static int width4 = 480; /* also read in */ static int height4 = 400; static int alpha4 = 0; void display(void) { char *p; char msg[] = "press f for alpha fade"; /* clear window */ glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity (); glColor3f(0.0, 0.0, 0.0); /* draw .jpg files */ glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glRasterPos2f(0.0, 0.1); glDrawPixels(width1, height1, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix1); glRasterPos2f(0.0, 0.1); glDrawPixels(width2, height2, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix2); glRasterPos2f(0.0, 0.1); glDrawPixels(width3, height3, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix3); glRasterPos2f(0.0, 0.1); glDrawPixels(width4, height4, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix4); /* draw text */ glEnable(GL_LINE_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glLoadIdentity (); glTranslatef(0.02, 0.02, 0.0); glScalef(0.0005, 0.0005, 0.0); for(p=msg; *p; p++) glutStrokeCharacter(GLUT_STROKE_ROMAN, *p); glFlush(); glutSwapBuffers(); } void myreshape(int h, int w) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,h,w); } void init() { int status; printf("jpegread on file p1.jpg\n"); status = jpegread("p1.jpg", alpha1, &width1, &height1, rgbpix1); printf("jpegread returned status=%d, width1=%d, height1=%d, alpha1=%d \n", status, width1, height1, alpha1); printf("jpegread on file p2.jpg\n"); status = jpegread("p2.jpg", alpha2, &width2, &height2, rgbpix2); printf("jpegread returned status=%d, width2=%d, height2=%d, alpha3=%d \n", status, width2, height2, alpha2); printf("jpegread on file p3.jpg\n"); status = jpegread("p3.jpg", alpha3, &width3, &height3, rgbpix3); printf("jpegread returned status=%d, width3=%d, height3=%d, alpha3=%d \n", status, width3, height3, alpha3); printf("jpegread on file p4.jpg\n"); status = jpegread("p4.jpg", alpha4, &width4, &height4, rgbpix4); printf("jpegread returned status=%d, width4=%d, height4=%d, alpha4=%d \n", status, width4, height4, alpha4); /* set clear color to white */ glClearColor (1.0, 1.0, 1.0, 0.0); /* set fill color to black */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,480,400); } static void key(unsigned char k, int x, int y) { int i; switch (k) { case 'f': fade++; if(rgbpix2[3]<256-31) for(i=3; i<4*480*400; i+=4) rgbpix2[i]+=31; else if(rgbpix3[3]<256-63) for(i=3; i<4*480*400; i+=4) rgbpix3[i]+=63; else if(rgbpix4[3]<256-63) for(i=3; i<4*480*400; i+=4) rgbpix4[i]+=63; else for(i=3; i<4*480*400; i+=4) { rgbpix2[i]=0; rgbpix3[i]=0; rgbpix4[i]=0; } break; case 'q': exit(0); break; default: break; } printf("alpha2=%d, alpha3=%d, alpha4=%d\n", rgbpix2[3], rgbpix3[3], rgbpix4[3]); glutPostRedisplay(); } /* end key function */ int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(480,400); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); glutReshapeFunc(myreshape); glutKeyboardFunc(key); glutDisplayFunc(display); init(); glutMainLoop(); return 0; }