1 #define GLUT_DISABLE_ATEXIT_HACK 2 #include 3 #include 4 #include 5 #pragma comment(lib,"glaux.lib") 6 7 8 AUX_RGBImageRec* Image; 9 GLint iWidth,iHeight,iMode=1; 10 GLubyte* pImage; 11 12 void Initialization(void); 13 void MenuCreate(void); 14 void OnDisplay(void); 15 void OnReshape(int,int); 16 void OnMenu(int); 17 18 void main(int argc,char* argv[]) 19 { 20 glutInit(&argc,argv); 21 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 22 glutInitWindowSize(400,400); 23 glutCreateWindow("EXAM703"); 24 25 glutCreateMenu(OnMenu); 26 glutDisplayFunc(OnDisplay); 27 glutReshapeFunc(OnReshape); 28 29 Initialization(); 30 glutMainLoop(); 31 } 32 33 void Initialization(void) 34 { 35 glClearColor(0,0,0,1); 36 37 MenuCreate(); 38 39 glPixelStorei(GL_UNPACK_ALIGNMENT,1); 40 Image=auxDIBImageLoad("11.bmp"); 41 iWidth=Image->sizeX; 42 iHeight=Image->sizeY; 43 pImage=Image->data; 44 } 45 46 void OnDisplay(void) 47 { 48 glClear(GL_COLOR_BUFFER_BIT); 49 50 GLbyte *pModified=NULL; 51 GLfloat invert[256]; 52 GLint i; 53 54 glRasterPos2i(0,0); 55 56 switch(iMode){ 57 case 2: 58 glPixelZoom(-1.0f,-1.0f); 59 glRasterPos2i(iWidth-1,iHeight-1); 60 break; 61 62 case 3: 63 glPixelZoom(0.5f,0.5f); 64 break; 65 66 case 4: 67 glPixelTransferf(GL_RED_SCALE,1.0f); 68 glPixelTransferf(GL_GREEN_SCALE,0.0f); 69 glPixelTransferf(GL_BLUE_SCALE,0.0f); 70 break; 71 72 case 5: 73 glPixelTransferf(GL_RED_SCALE,0.0f); 74 glPixelTransferf(GL_GREEN_SCALE,1.0f); 75 glPixelTransferf(GL_BLUE_SCALE,0.0f); 76 break; 77 78 case 6: 79 glPixelTransferf(GL_RED_SCALE,0.0f); 80 glPixelTransferf(GL_GREEN_SCALE,0.0f); 81 glPixelTransferf(GL_BLUE_SCALE,1.0f); 82 break; 83 84 case 7: 85 glDrawPixels(iWidth,iHeight,GL_RGB,GL_UNSIGNED_BYTE,pImage); 86 pModified=(GLbyte*)new GLbyte[iWidth*iHeight]; 87 88 glPixelTransferf(GL_RED_SCALE,0.3f); 89 glPixelTransferf(GL_GREEN_SCALE,0.59f); 90 glPixelTransferf(GL_BLUE_BITS,0.11f); 91 92 glReadPixels(0,0,iWidth,iHeight,GL_LUMINANCE,GL_UNSIGNED_BYTE,pModified); 93 glPixelTransferf(GL_RED_SCALE,1.0f); 94 glPixelTransferf(GL_GREEN_SCALE,1.0f); 95 glPixelTransferf(GL_BLUE_SCALE,1.0f); 96 break; 97 98 case 8: 99 invert[0]=1.0f;100 for(i=1;i<256;i++)101 invert[i]=1.0f-(1.0f/255.0f*GLfloat(i));102 103 glPixelMapfv(GL_PIXEL_MAP_R_TO_R,255,invert);104 glPixelMapfv(GL_PIXEL_MAP_G_TO_G,255,invert);105 glPixelMapfv(GL_PIXEL_MAP_B_TO_B,255,invert);106 glPixelTransferf(GL_MAP_COLOR,GL_TRUE);107 break;108 109 case 1:110 default:111 break;112 113 }114 115 if(pModified==NULL)116 glDrawPixels(iWidth,iHeight,GL_RGB,GL_UNSIGNED_BYTE,pImage);117 else{118 glDrawPixels(iWidth,iHeight,GL_LUMINANCE,GL_UNSIGNED_BYTE,pModified);119 delete []pModified;120 }121 122 glPixelTransferi(GL_MAP_COLOR,GL_FALSE);123 glPixelTransferf(GL_RED_SCALE,1.0f);124 glPixelTransferf(GL_GREEN_SCALE,1.0f);125 glPixelTransferf(GL_BLUE_SCALE,1.0f);126 glPixelZoom(1.0f,1.0f);127 128 glutSwapBuffers();129 }130 131 void OnReshape(int w,int h)132 {133 GLfloat aspectRatio=(GLfloat)w/(GLfloat)h;//计算窗口的纵横比134 GLfloat winWidth,winHeight;135 136 137 glViewport(0,0,w,h);138 139 140 glMatrixMode(GL_PROJECTION);141 glLoadIdentity();142 143 144 if(w<=h){ //根据纵横比计算绘图区域的宽和高145 winWidth=400;146 winHeight=400*aspectRatio;147 }148 else{149 winWidth=400*aspectRatio;150 winHeight=400;151 }152 gluOrtho2D(0,winWidth,0,winHeight);153 154 155 glMatrixMode(GL_MODELVIEW);156 glLoadIdentity();157 158 }159 160 void OnMenu(int value)161 {162 iMode=value;163 164 glutPostRedisplay();165 }166 167 void MenuCreate(void)168 {169 glutAddMenuEntry("正常显示",1);170 glutAddMenuEntry("翻转图像",2);171 glutAddMenuEntry("缩小图像",3);172 glutAddMenuEntry("红色通道",4);173 glutAddMenuEntry("绿色通道",5);174 glutAddMenuEntry("蓝色通道",6);175 glutAddMenuEntry("灰度图",7);176 glutAddMenuEntry("反转颜色",8);177 178 glutAttachMenu(GLUT_RIGHT_BUTTON);179 180 }