1 1. Program 1 OpenCV (OpenCV Sample001) 1 /* 2 - > - > - >VC++ 3 ( ) 4 C:\opencv\build\include 5 ( ) 6 C:\opencv\build\x86\vc10\lib 7 - > - > - > - > 8 (240 O p e n C V ) 9 opencv_core240d.lib 10 opencv_imgproc240d.lib 11 opencv_highgui240d.lib 12 opencv_video240d.lib 13 - > - > - > 14 ( ) 15 PATH=C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10;%PATH% 16 */ 17 #include <stdio.h> 18 #include <opencv2/opencv.hpp > 19 20 int main(void) 21 { 22 // image 23 IplImage *image = cvcreateimage(cvsize(640,480), IPL_DEPTH_8U, 3); 24 25 // p i c t u r e "sample.jpg" 26 IplImage *picture = cvloadimage("sample.jpg", CV_LOAD_IMAGE_COLOR); 27 28 // p i c t u r e c I m a g e R e s u l t 29 cvresize(picture, image, CV_INTER_LINEAR); 30 31 // "My Window" 32 cvnamedwindow ("My Window", CV_WINDOW_AUTOSIZE); 33 34 cvshowimage("my Window", image); // image "My Window" 35 36 cvwaitkey (0); // 37 38 cvdestroyallwindows (); // 39 40 return 0; 41 } 1
2. Program 2 (OpenGL Sample012) 1 /* 2 - > - > - >VC++ 3 ( ) 4 C:\opencv\build\include 5 ( ) 6 C:\opencv\build\x86\vc10\lib 7 - > - > - > - > 8 (240 O p e n C V ) 9 opencv_core240d.lib 10 opencv_imgproc240d.lib 11 opencv_highgui240d.lib 12 opencv_video240d.lib 13 - > - > - > 14 ( ) 15 PATH=C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10;%PATH% 16 */ 17 #include <stdio.h> 18 19 #include <opencv2/opencv.hpp > 20 21 #include <GL/glut.h> 22 23 #define SQUARE_TEXTURE 1 24 #define SPHERE_TEXTURE 2 25 26 // 27 static int MouseX = 0; // X 28 static int MouseY = 0; // Y 29 static float SpinX = 0; // X 30 static float SpinY = 0; // Y 31 static float Scale = 1.0; // 32 33 static int MouseLB_ON=0; // 34 static int MouseRB_ON=0; // 35 36 void mouse(int button, int state, int x, int y) 37 { 38 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){ 39 MouseLB_ON = 1; printf("(%3d,%3d) \ n", x, y); 40 }else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){ 41 MouseLB_ON = 0; printf("(%3d,%3d) \ n", x, y); 42 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){ 43 MouseRB_ON = 1; printf("(%3d,%3d) \ n", x, y); 44 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP){ 45 MouseRB_ON = 0; printf("(%3d,%3d) \ n", x, y); 46 } 47 } 48 49 void dragmotion(int x, int y) 50 { 51 if (MouseLB_ON == 1){ 52 printf("(%3d,%3d)...\ n", x, y); 53 // 54 SpinX += x - MouseX; 55 SpinY += y - MouseY; 56 // 57 MouseX = x; 58 MouseY = y; 59 glutpostredisplay (); 60 } 61 else if (MouseRB_ON == 1){ 62 printf("(%3d,%3d)...\ n", x, y); 63 // 64 Scale += (float)(y - MouseY )/100; 65 // 66 MouseX = x; 67 MouseY = y; 68 glutpostredisplay (); 69 } 70 } 71 72 void reshape(int w, int h) // R e s h a p e 73 { 74 glviewport(0, 0, w, h); 75 76 glmatrixmode(gl_projection); 77 glloadidentity (); 78 gluperspective (30.0, (double)w/h, 1.0, 100.0); // 79 80 glmatrixmode(gl_modelview); 81 glloadidentity (); 82 glulookat(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // 83 } 84 2
85 void display(void) // D i s p l a y 86 { 87 glclearcolor(0.0, 0.0, 0.0, 1.0); // 88 glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // 89 glenable(gl_depth_test); // 90 91 // 92 glpushmatrix (); 93 glrotatef(spinx, 1.0, 0.0, 0.0); // X S p i n X 94 glrotatef(spiny, 0.0, 1.0, 0.0); // Y S p i n Y 95 glscalef(scale, Scale, Scale); // S c a l e 96 97 glcalllist(square_texture); // A 98 //glcalllist(sphere_texture); // B 99 glpopmatrix(); 100 101 // 102 glpushmatrix (); // X 103 GLfloat mat1diff[] = { 0.6, 0.2, 0.2, 1.0 }; // 104 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat1diff); 105 glnormal3f(0.0, 1.0, 0.0); // 106 glbegin(gl_lines); 107 glvertex3f (0.0f, 0.0f, 0.0f); 108 glvertex3f (2.0f, 0.0f, 0.0f); 109 glend(); 110 glpopmatrix(); 111 glpushmatrix (); // Y 112 GLfloat mat2diff[] = { 0.2, 0.6, 0.2, 1.0 }; // 113 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat2diff); 114 glnormal3f(0.0, 1.0, 0.0); // 115 glbegin(gl_lines); 116 glvertex3f (0.0f, 0.0f, 0.0f); 117 glvertex3f (0.0f, 2.0f, 0.0f); 118 glend(); 119 glpopmatrix(); 120 glpushmatrix (); // Z 121 GLfloat mat3diff[] = { 0.2, 0.2, 0.6, 1.0 }; // 122 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat3diff); 123 glnormal3f(0.0, 1.0, 0.0); // 124 glbegin(gl_lines); 125 glvertex3f (0.0f, 0.0f, 0.0f); 126 glvertex3f (0.0f, 0.0f, 2.0f); 127 glend(); 128 glpopmatrix(); 129 130 glutswapbuffers (); // 131 } 132 133 void lightinit(void) // ( ) 134 { 135 glenable(gl_lighting); // 136 glenable(gl_light0); //0 (8 ) 137 glenable(gl_normalize); // 138 139 GLfloat light0pos[] = { 0.0, 5.0, 0.0, 1.0 }; 140 gllightfv(gl_light0, GL_POSITION, light0pos); // 0 141 142 GLfloat light0ambi[] = { 0.2, 0.2, 0.2, 1.0 }; 143 gllightfv(gl_light0, GL_AMBIENT, light0ambi); // 0 144 GLfloat light0diff[] = { 0.8, 0.8, 0.8, 1.0 }; 145 gllightfv(gl_light0, GL_DIFFUSE, light0diff); // 0 146 GLfloat light0spec[] = { 0.5, 0.5, 0.5, 1.0 }; 147 gllightfv(gl_light0, GL_SPECULAR, light0spec); // 0 148 149 glshademodel(gl_smooth); // 150 } 151 152 // A( ) 153 void makemodela(unsigned char *image, int size_x, int size_y) 154 { 155 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; // 156 157 GLuint texture; 158 glgentextures(1, &texture); // 159 glbindtexture(gl_texture_2d, texture); // ( ) 160 161 gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); // ( ) 162 gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); // ( ) 163 // 164 glubuild2dmipmaps(gl_texture_2d, 3, size_x, size_y, GL_RGB, GL_UNSIGNED_BYTE, image); 165 166 /* ( ) */ 167 glnewlist(square_texture, GL_COMPILE); 168 glpushmatrix (); 169 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, white); 170 glenable(gl_texture_2d); 171 glbegin(gl_polygon); 172 glnormal3f(0.0, 1.0, 0.0); // 3
173 gltexcoord2f(0.0, 1.0); glvertex3f( 1.0, 0.0, 1.0); 174 gltexcoord2f(0.0, 0.0); glvertex3f(-1.0, 0.0, 1.0); 175 gltexcoord2f(1.0, 0.0); glvertex3f(-1.0, 0.0,-1.0); 176 gltexcoord2f(1.0, 1.0); glvertex3f( 1.0, 0.0,-1.0); 177 glend(); 178 gldisable(gl_texture_2d); 179 glpopmatrix(); 180 glendlist(); 181 182 return; 183 } 184 185 // B 186 void makemodelb(unsigned char *image, int size_x, int size_y) 187 { 188 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; // 189 190 GLuint texture; 191 glgentextures(1, &texture); // 192 glbindtexture(gl_texture_2d, texture); // ( ) 193 194 gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); // ( ) 195 gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); // ( ) 196 // 197 glubuild2dmipmaps(gl_texture_2d, 3, size_x, size_y, GL_RGB, GL_UNSIGNED_BYTE, image); 198 199 GLUquadricObj* sphere; 200 sphere = glunewquadric (); 201 gluquadricdrawstyle(sphere, GLU_FILL); 202 gluquadricnormals(sphere, GLU_SMOOTH); 203 gluquadrictexture(sphere, GL_TRUE); 204 205 /* ( ) */ 206 glnewlist(sphere_texture, GL_COMPILE); 207 glpushmatrix (); 208 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, white); 209 glenable(gl_texture_2d); 210 glusphere(sphere, 1.0, 32, 32); 211 gldisable(gl_texture_2d); 212 glpopmatrix(); 213 glendlist(); 214 215 return; 216 } 217 218 int main(int argc, char *argv[]) 219 { 220 // OpenCV Initialize 221 // 222 IplImage *teximage = cvloadimage("texture.bmp",1); 223 unsigned char image [128][128][3]; 224 for(int x=0;x<128;x++){ 225 for(int y=0;y<128;y++){ 226 image[x][y][2]=teximage ->imagedata [128*y*3+x*3+0];/* B */ 227 image[x][y][1]=teximage ->imagedata [128*y*3+x*3+1];/* G */ 228 image[x][y][0]=teximage ->imagedata [128*y*3+x*3+2];/* R */ 229 } } 230 231 // 232 cvnamedwindow("opencv Window", CV_WINDOW_AUTOSIZE ); 233 cvshowimage("opencv Window", teximage); 234 cvwaitkey (1); 235 236 // OpenGL Initialize 237 glutinit(&argc, argv); // G L U T 238 glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH); 239 glutinitwindowsize(640, 480); // 240 glutcreatewindow("window"); // 241 glutreshapefunc(reshape); // R e s h a p e 242 glutdisplayfunc(display); // D i s p l a y 243 244 glutmousefunc(mouse); // 245 glutmotionfunc(dragmotion); // 246 247 lightinit(); // ( ) 248 249 makemodela((unsigned char *)image, 128, 128); // C G 250 makemodelb((unsigned char *)image, 128, 128); // C G 251 252 // ( g l u t m y I d l e ) 253 glutmainloop (); // 254 255 // OpenCV Finalize 256 cvdestroywindow( "OpenCV Window" ); 257 258 return 0; 259 } 4
Program 3 (OpenGL Sample013) 1 /* 2 - > - > - >VC++ 3 ( ) 4 C:\opencv\build\include 5 ( ) 6 C:\opencv\build\x86\vc10\lib 7 - > - > - > - > 8 (240 O p e n C V ) 9 opencv_core240d.lib 10 opencv_imgproc240d.lib 11 opencv_highgui240d.lib 12 opencv_video240d.lib 13 - > - > - > 14 ( ) 15 PATH=C:\opencv\build\x86\vc10\bin;C:\opencv\build\common\tbb\ia32\vc10;%PATH% 16 */ 17 #include <stdio.h> 18 19 #include <opencv2/opencv.hpp > 20 21 #include <GL/glut.h> 22 23 // ID 24 #define TEXTURE_STARS 101 25 #define TEXTURE_EARTH 102 26 27 // ID 28 #define SQUARE_TEXTURE 1 29 #define SPHERE_TEXTURE 2 30 31 // 32 static int MouseX = 0; // X 33 static int MouseY = 0; // Y 34 static float SpinX = 0; // X 35 static float SpinY = 0; // Y 36 static float Scale = 1.0; // 37 38 static int MouseLB_ON=0; // 39 static int MouseRB_ON=0; // 40 41 void mouse(int button, int state, int x, int y) 42 { 43 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){ 44 MouseLB_ON = 1; printf("(%3d,%3d) \ n", x, y); 45 }else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){ 46 MouseLB_ON = 0; printf("(%3d,%3d) \ n", x, y); 47 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){ 48 MouseRB_ON = 1; printf("(%3d,%3d) \ n", x, y); 49 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP){ 50 MouseRB_ON = 0; printf("(%3d,%3d) \ n", x, y); 51 } 52 } 53 54 void dragmotion(int x, int y) 55 { 56 if (MouseLB_ON == 1){ 57 printf("(%3d,%3d)...\ n", x, y); 58 // 59 SpinX += x - MouseX; 60 SpinY += y - MouseY; 61 // 62 MouseX = x; 63 MouseY = y; 64 glutpostredisplay (); 65 } 66 else if (MouseRB_ON == 1){ 67 printf("(%3d,%3d)...\ n", x, y); 68 // 69 Scale += (float)(y - MouseY )/100; 70 // 71 MouseX = x; 72 MouseY = y; 73 glutpostredisplay (); 74 } 75 } 76 77 void reshape(int w, int h) // R e s h a p e 78 { 79 glviewport(0, 0, w, h); 80 glmatrixmode(gl_projection); 81 glloadidentity (); 82 gluperspective (30.0, (double)w/h, 1.0, 100.0); // 83 84 glmatrixmode(gl_modelview); 85 glloadidentity (); 86 glulookat(5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // 87 } 5
88 89 void display(void) // D i s p l a y 90 { 91 glclearcolor(0.0, 0.0, 0.0, 1.0); // 92 glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // 93 glenable(gl_depth_test); // 94 95 // 96 glpushmatrix (); 97 gltranslatef(-1.0,0.0, -1.0); 98 glrotatef(45.0, 0.0, 1.0, 0.0); // Y S p i n Y 99 glscalef(3.0, 3.0, 3.0); // S c a l e 100 glbindtexture(gl_texture_2d, TEXTURE_STARS); // ( ) 101 glcalllist(square_texture); // A 102 glpopmatrix(); 103 104 // 105 glpushmatrix (); 106 glrotatef(spinx, 1.0, 0.0, 0.0); // X S p i n X 107 glrotatef(spiny, 0.0, 1.0, 0.0); // Y S p i n Y 108 glscalef(scale, Scale, Scale); // S c a l e 109 glbindtexture(gl_texture_2d, TEXTURE_EARTH); // ( ) 110 glcalllist(sphere_texture); // B 111 glpopmatrix(); 112 113 // 114 glpushmatrix (); // X 115 GLfloat mat1diff[] = { 0.6, 0.2, 0.2, 1.0 }; // 116 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat1diff); 117 glnormal3f(0.0, 1.0, 0.0); // 118 glbegin(gl_lines); 119 glvertex3f (0.0f, 0.0f, 0.0f); 120 glvertex3f (2.0f, 0.0f, 0.0f); 121 glend(); 122 glpopmatrix(); 123 glpushmatrix (); // Y 124 GLfloat mat2diff[] = { 0.2, 0.6, 0.2, 1.0 }; // 125 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat2diff); 126 glnormal3f(0.0, 1.0, 0.0); // 127 glbegin(gl_lines); 128 glvertex3f (0.0f, 0.0f, 0.0f); 129 glvertex3f (0.0f, 2.0f, 0.0f); 130 glend(); 131 glpopmatrix(); 132 glpushmatrix (); // Z 133 GLfloat mat3diff[] = { 0.2, 0.2, 0.6, 1.0 }; // 134 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat3diff); 135 glnormal3f(0.0, 1.0, 0.0); // 136 glbegin(gl_lines); 137 glvertex3f (0.0f, 0.0f, 0.0f); 138 glvertex3f (0.0f, 0.0f, 2.0f); 139 glend(); 140 glpopmatrix(); 141 142 glutswapbuffers (); // 143 } 144 145 void lightinit(void) // ( ) 146 { 147 glenable(gl_lighting); // 148 glenable(gl_light0); //0 (8 ) 149 glenable(gl_normalize); // 150 151 GLfloat light0pos[] = { 0.0, 5.0, 0.0, 1.0 }; 152 gllightfv(gl_light0, GL_POSITION, light0pos); // 0 153 154 GLfloat light0ambi[] = { 0.2, 0.2, 0.2, 1.0 }; 155 gllightfv(gl_light0, GL_AMBIENT, light0ambi); // 0 156 GLfloat light0diff[] = { 0.8, 0.8, 0.8, 1.0 }; 157 gllightfv(gl_light0, GL_DIFFUSE, light0diff); // 0 158 GLfloat light0spec[] = { 0.5, 0.5, 0.5, 1.0 }; 159 gllightfv(gl_light0, GL_SPECULAR, light0spec); // 0 160 161 glshademodel(gl_smooth); // 162 } 163 164 void loadtexture(void) 165 { 166 // O p e n C V 167 // 168 IplImage *_teximage1 = cvloadimage("texture1.jpg", 1); 169 // (( n )*( n ) ) 170 IplImage *teximage1 = cvcreateimage(cvsize(768,512), IPL_DEPTH_8U,3); 171 cvresize(_teximage1, teximage1); 172 cvflip(teximage1, teximage1, 0);// 173 cvcvtcolor(teximage1, teximage1, CV_BGR2RGB); // B G R R G B 174 cvnamedwindow("opencv Window 1", CV_WINDOW_AUTOSIZE ); 175 cvshowimage("opencv Window 1", _teximage1); cvwaitkey (1); 6
176 // 177 glbindtexture(gl_texture_2d, TEXTURE_STARS); // 178 gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); // ( ) 179 gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); // ( ) 180 // 181 glubuild2dmipmaps(gl_texture_2d, 3, 768, 512, GL_RGB, GL_UNSIGNED_BYTE, teximage1 ->imagedata); 182 // 183 cvreleaseimage(&teximage1); 184 185 // 186 IplImage *_teximage2 = cvloadimage("texture2.jpg", 1); 187 // (( n )*( n ) ) 188 IplImage *teximage2 = cvcreateimage(cvsize(1024,512), IPL_DEPTH_8U,3); 189 cvresize(_teximage2, teximage2); 190 cvflip(teximage2, teximage2, 0); // 191 cvcvtcolor(teximage2, teximage2, CV_BGR2RGB); // B G R R G B 192 cvnamedwindow("opencv Window 2", CV_WINDOW_AUTOSIZE ); 193 cvshowimage("opencv Window 2", _teximage2); cvwaitkey (1); 194 // 195 glbindtexture(gl_texture_2d, TEXTURE_EARTH); // 196 gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); // ( ) 197 gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); // ( ) 198 // 199 glubuild2dmipmaps(gl_texture_2d, 3, 1024, 512, GL_RGB, GL_UNSIGNED_BYTE, teximage2 ->imagedata); 200 // 201 cvreleaseimage(&teximage2); 202 203 return; 204 } 205 206 // A( ) 207 void makemodela(void) 208 { 209 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; // 210 211 /* ( ) */ 212 glnewlist(square_texture, GL_COMPILE); 213 glpushmatrix (); 214 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, white); 215 glenable(gl_texture_2d); 216 glbegin(gl_polygon); 217 glnormal3f(0.0, 1.0, 0.0); // 218 gltexcoord2f(1.0, 1.0); glvertex3f( 1.0, 1.0, 0.0); 219 gltexcoord2f(0.0, 1.0); glvertex3f(-1.0, 1.0, 0.0); 220 gltexcoord2f(0.0, 0.0); glvertex3f(-1.0, -1.0,0.0); 221 gltexcoord2f(1.0, 0.0); glvertex3f( 1.0, -1.0,0.0); 222 glend(); 223 gldisable(gl_texture_2d); 224 glpopmatrix(); 225 glendlist(); 226 227 return; 228 } 229 230 // B 231 void makemodelb(void) 232 { 233 GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 }; // 234 235 GLUquadricObj* sphere; 236 sphere = glunewquadric (); 237 gluquadricdrawstyle(sphere, GLU_FILL); 238 gluquadricnormals(sphere, GLU_SMOOTH); 239 gluquadrictexture(sphere, GL_TRUE); 240 241 /* ( ) */ 242 glnewlist(sphere_texture, GL_COMPILE); 243 glpushmatrix (); 244 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, white); 245 glenable(gl_texture_2d); 246 glusphere(sphere, 1.0, 32, 32); 247 gldisable(gl_texture_2d); 248 glpopmatrix(); 249 glendlist(); 250 251 return; 252 } 253 254 int main(int argc, char *argv[]) 255 { 256 257 //************************************************************* 258 // OpenGL Initialize 259 //************************************************************* 260 glutinit(&argc, argv); // G L U T 261 glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH); 262 glutinitwindowsize(640, 480); // 263 glutcreatewindow("window"); // 7
264 glutreshapefunc(reshape); // R e s h a p e 265 glutdisplayfunc(display); // D i s p l a y 266 267 glutmousefunc(mouse); // 268 glutmotionfunc(dragmotion); // 269 270 lightinit(); // ( ) 271 272 loadtexture(); // 273 makemodela(); // C G 274 makemodelb(); // C G 275 276 //************************************************************* 277 // ( g l u t m y I d l e ) 278 //************************************************************* 279 glutmainloop (); // 280 //--- 281 282 //************************************************************* 283 // OpenCV Finalize 284 //************************************************************* 285 cvdestroyallwindows (); 286 287 return 0; 288 } 8
3. 1 #include <stdio.h> 2 #include <GL/glut.h> 3 4 // 5 static int MouseX = 0; // X 6 static int MouseY = 0; // Y 7 static float SpinX = 0; // X 8 static float SpinY = 0; // Y 9 static float MoveX = 0; // X 10 static float MoveZ = 0; // Y 11 static float Scale = 1.0; // 12 Program 4 (OpenGL Sample014) 13 void specialkey(int key, int x, int y) 14 { 15 switch (key) { 16 case GLUT_KEY_UP: 17 printf("(%3d,%3d) [ ] \ n", x, y); 18 MoveZ += 0.1; 19 break; 20 case GLUT_KEY_DOWN: 21 printf("(%3d,%3d) [ ] \ n", x, y); 22 MoveZ -= 0.1; 23 break; 24 case GLUT_KEY_LEFT: 25 printf("(%3d,%3d) [ ] \ n", x, y); 26 MoveX -= 0.1; 27 break; 28 case GLUT_KEY_RIGHT: 29 printf("(%3d,%3d) [ ] \ n", x, y); 30 MoveX += 0.1; 31 break; 32 } 33 glutpostredisplay (); 34 } 35 36 static int MouseLB_ON=0; // 37 static int MouseRB_ON=0; // 38 39 void mouse(int button, int state, int x, int y) 40 { 41 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){ 42 MouseLB_ON = 1; printf("(%3d,%3d) \ n", x, y); 43 }else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP){ 44 MouseLB_ON = 0; printf("(%3d,%3d) \ n", x, y); 45 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){ 46 MouseRB_ON = 1; printf("(%3d,%3d) \ n", x, y); 47 }else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP){ 48 MouseRB_ON = 0; printf("(%3d,%3d) \ n", x, y); 49 } 50 } 51 52 void dragmotion(int x, int y) 53 { 54 if (MouseLB_ON == 1){ 55 printf("(%3d,%3d)...\ n", x, y); 56 // 57 SpinX += x - MouseX; 58 SpinY += y - MouseY; 59 // 60 MouseX = x; 61 MouseY = y; 62 glutpostredisplay (); 63 } 64 else if (MouseRB_ON == 1){ 65 printf("(%3d,%3d)...\ n", x, y); 66 // 67 Scale += (float)(y - MouseY )/100; 68 // 69 MouseX = x; 70 MouseY = y; 71 glutpostredisplay (); 72 } 73 } 74 75 void reshape(int w, int h) // R e s h a p e 76 { 77 glviewport(0, 0, w, h); 78 79 glmatrixmode(gl_projection); 80 glloadidentity (); 81 gluperspective (30.0, (double)w/h, 1.0, 100.0); // 82 83 glmatrixmode(gl_modelview); 84 glloadidentity (); 9
85 glulookat(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // 86 } 87 88 void display(void) // D i s p l a y 89 { 90 glclearcolor(0.0, 0.0, 0.0, 1.0); // 91 glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // 92 glenable(gl_depth_test); // 93 94 // ( ) 95 glpushmatrix (); 96 GLfloat silver_ambient[] = {0.19225, 0.19225, 0.19225, 1.0}; 97 GLfloat silver_diffuse[] = {0.50754, 0.50754, 0.50754, 1.0}; 98 GLfloat silver_specular[] = {0.508273, 0.508273, 0.508273,1.0}; 99 GLfloat silver_shininess []= {51.2}; 100 glmaterialfv(gl_front_and_back, GL_AMBIENT, silver_ambient); 101 glmaterialfv(gl_front_and_back, GL_DIFFUSE, silver_diffuse); 102 glmaterialfv(gl_front_and_back, GL_SPECULAR, silver_specular); 103 glmaterialfv(gl_front_and_back, GL_SHININESS,silver_shininess); 104 glmaterialfv(gl_front_and_back, GL_EMISSION, silver_diffuse);// 105 gltranslatef(0.0, 2.0, 0.0); // Y 2.0 106 glutsolidsphere(0.2, 20.0, 20.0); // 107 GLfloat silver_emission_nomal[] = {0.0, 0.0, 0.0, 1.0}; 108 glmaterialfv(gl_front_and_back, GL_EMISSION, silver_emission_nomal); // 109 glpopmatrix(); 110 111 // 112 glpushmatrix (); // X 113 GLfloat mat1diff[] = { 0.6, 0.2, 0.2, 1.0 }; 114 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat1diff); 115 glnormal3f(0.0, 1.0, 0.0); // 116 glbegin(gl_lines); 117 glvertex3f (0.0f, 0.0f, 0.0f); 118 glvertex3f (2.0f, 0.0f, 0.0f); 119 glend(); 120 glpopmatrix(); 121 glpushmatrix (); // Y 122 GLfloat mat2diff[] = { 0.2, 0.6, 0.2, 1.0 }; 123 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat2diff); 124 glnormal3f(0.0, 1.0, 0.0); // 125 glbegin(gl_lines); 126 glvertex3f (0.0f, 0.0f, 0.0f); 127 glvertex3f (0.0f, 2.0f, 0.0f); 128 glend(); 129 glpopmatrix(); 130 glpushmatrix (); // Z 131 GLfloat mat3diff[] = { 0.2, 0.2, 0.6, 1.0 }; 132 glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, mat3diff); 133 glnormal3f(0.0, 1.0, 0.0); // 134 glbegin(gl_lines); 135 glvertex3f (0.0f, 0.0f, 0.0f); 136 glvertex3f (0.0f, 0.0f, 2.0f); 137 glend(); 138 glpopmatrix(); 139 140 // 141 glenable(gl_blend); 142 // 143 glblendfunc(gl_src_alpha, GL_ONE); 144 145 // ( ) 146 glpushmatrix (); 147 GLfloat green_plastic_ambient[] = {0.0, 0.0, 0.0, 1.0}; 148 GLfloat green_plastic_diffuse[] = {0.1, 0.35, 0.1, 1.0}; 149 GLfloat green_plastic_specular[] = {0.45, 0.55, 0.45, 1.0}; 150 GLfloat green_plastic_shininess []= {32}; 151 glmaterialfv(gl_front_and_back, GL_AMBIENT, green_plastic_ambient); 152 glmaterialfv(gl_front_and_back, GL_DIFFUSE, green_plastic_diffuse); 153 glmaterialfv(gl_front_and_back, GL_SPECULAR, green_plastic_specular); 154 glmaterialfv(gl_front_and_back, GL_SHININESS,green_plastic_shininess); 155 gltranslatef(0.0, 0.0, 1.0); // Z 1.0 156 glutsolidsphere(0.5, 20.0, 20.0); // 157 glpopmatrix(); 158 159 // ( ) 160 glpushmatrix (); 161 GLfloat ruby_ambient[] = {0.1745, 0.01175, 0.01175, 1.0}; 162 GLfloat ruby_diffuse[] = {0.61424, 0.04136, 0.04136, 1.0}; 163 GLfloat ruby_specular[] = {0.727811, 0.626959, 0.626959, 1.0}; 164 GLfloat ruby_shininess []= {76.8 }; 165 glmaterialfv(gl_front_and_back, GL_AMBIENT, ruby_ambient); 166 glmaterialfv(gl_front_and_back, GL_DIFFUSE, ruby_diffuse); 167 glmaterialfv(gl_front_and_back, GL_SPECULAR, ruby_specular); 168 glmaterialfv(gl_front_and_back, GL_SHININESS,ruby_shininess); 169 gltranslatef(1.0, 0.0, 0.0); // X 1.0 170 glutsolidsphere(0.5, 20.0, 20.0); // 171 glpopmatrix(); 172 10
173 // ( ) 174 glpushmatrix (); 175 GLfloat brass_ambient[] ={0.329412, 0.223529, 0.027451, 0.5};// (0.5) 176 GLfloat brass_diffuse[] ={0.780392, 0.568627, 0.113725, 0.5};// (0.5) 177 GLfloat brass_specular[] ={0.992157, 0.941176, 0.807843, 0.5};// (0.5) 178 GLfloat brass_shininess []={27.89743616}; 179 glmaterialfv(gl_front_and_back, GL_AMBIENT, brass_ambient); 180 glmaterialfv(gl_front_and_back, GL_DIFFUSE, brass_diffuse); 181 glmaterialfv(gl_front_and_back, GL_SPECULAR, brass_specular); 182 glmaterialfv(gl_front_and_back, GL_SHININESS,brass_shininess); 183 gltranslatef(movex, 0.0, MoveZ); // X M o v e X Z M o v e Z 184 glrotatef(spinx, 1.0, 0.0, 0.0); // X S p i n X 185 glrotatef(spiny, 0.0, 1.0, 0.0); // Y S p i n Y 186 glscalef(scale, Scale, Scale); // S c a l e 187 // 188 glutsolidcube (2.0); 189 glpopmatrix(); 190 191 gldisable(gl_blend); // 192 193 glutswapbuffers (); // 194 } 195 196 void lightinit(void) // ( ) 197 { 198 glenable(gl_lighting); // 199 glenable(gl_light0); //0 (8 ) 200 glenable(gl_normalize); // 201 202 GLfloat light0pos[] = { 0.0, 2.0, 0.0, 1.0 };// Y 2.0 ( ) 203 gllightfv(gl_light0, GL_POSITION, light0pos); // 0 204 205 GLfloat light0ambi[] = { 0.2, 0.2, 0.2, 1.0 }; 206 gllightfv(gl_light0, GL_AMBIENT, light0ambi); // 0 207 GLfloat light0diff[] = { 0.8, 0.8, 0.8, 1.0 }; 208 gllightfv(gl_light0, GL_DIFFUSE, light0diff); // 0 209 GLfloat light0spec[] = { 0.5, 0.5, 0.5, 1.0 }; 210 gllightfv(gl_light0, GL_SPECULAR, light0spec); // 0 211 212 glshademodel(gl_smooth); // 213 } 214 215 int main(int argc, char *argv[]) 216 { 217 glutinit(&argc, argv); // G L U T 218 glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH); 219 glutinitwindowsize(640, 480); // 220 glutcreatewindow("window"); // 221 glutreshapefunc(reshape); // R e s h a p e 222 glutdisplayfunc(display); // D i s p l a y 223 224 glutmousefunc(mouse); // 225 glutmotionfunc(dragmotion); // 226 glutspecialfunc(specialkey); // ( ) 227 //glutignorekeyrepeat(gl_true); // 228 229 lightinit(); // ( ) 230 231 glutmainloop (); // 232 233 return 0; 234 } 11