Reference: Baidu experience
reading the above article, then go to the official documents and found that they are translated or copied someone else's translation of it, want to see authentic official document can point here: the point I point my .
Resources official website to see here: the point I point my
It must first be clear that, openGL is a standard, windows and linux support it, then it does not require installation you can use, just need a computer drive configuration only (I did not go to move these, but you can visually Modern general can, if not, check back), the configuration of the drive to see here: the point I point my .
Second, it is easy to openGL glut library program, go here to download: point I downloaded glut library , unpack five documents: glut.h, glut.dll, glut32.dll, glut.lib , glut32.lib.
Third, install the glut library. Assuming that your installation path for vs MY_VS_ROOT, then new in MY_VS_ROOT / VC / include / next folder GL, then copy glut.h to this folder, for example, I is D: \ soft \ vs2015 \ VC \ include \ GL \ glut.h.
Then copy glut.lib and glut32.lib to MY_VS_ROOT / VC / lib /, the last copy dll directory under glut.dll and glut32.dll to the system: C: \ Windows \ system32 folder (32-bit) or C: \ Windows \ SysWOW64 (64-bit systems).
Fourth, write code to test. Under the new project vs a cpp file, copy the following code into it, ctrl + F5 to run the mouse click on the screen, you can see * you guess ╮ (╯ ▽ ╰) ╭.
1 // source: http://jingyan.baidu.com/article/d5c4b52bca5005da560dc5d6.html 2 #include <GL/glut.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <stdio.h> 6 7 static int year = 0, spin = 0, day = 0; 8 static GLint fogMode; 9 const int n = 100; 10 const GLfloat R = 1.0f; 11 const GLfloat Pi = 3.1415926536f; 12 13 void DrawCircle() { 14 15 int i; 16 glClear(GL_COLOR_BUFFER_BIT); 17 glBegin(GL_LINE_LOOP); 18 19 for (i = 0; i < n; ++i) 20 { 21 glColor3f(1.0, 0.0, 0.0); 22 glVertex2f(R*cos(2 * Pi / n*i), R*sin(2 * Pi / n*i)); 23 } 24 25 glEnd(); 26 glFlush(); 27 } 28 29 void init(void) { 30 GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 }; 31 glEnable(GL_DEPTH_TEST); //防止遮挡 32 glLightfv(GL_LIGHT0, GL_POSITION, position); 33 glEnable(GL_LIGHTING); 34 glEnable(GL_LIGHT0); 35 36 { 37 GLfloat mat[3] = { 0.1745, 0.01175, 0.01175 }; 38 glMaterialfv(GL_FRONT, GL_AMBIENT, mat); 39 mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136; 40 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat); 41 mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959; 42 glMaterialfv(GL_FRONT, GL_SPECULAR, mat); 43 glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0); 44 } 45 46 glEnable(GL_FOG); 47 48 { 49 GLfloat fogColor[4] = { 0.5, 0.5, 0.5, 1.0 }; 50 fogMode = GL_EXP; 51 glFogi(GL_FOG_MODE, fogMode); 52 glFogfv(GL_FOG_COLOR, fogColor); 53 glFogf(GL_FOG_DENSITY, 0.35); 54 glHint(GL_FOG_HINT, GL_DONT_CARE); 55 glFogf(GL_FOG_START, 1.0); 56 glFogf(GL_FOG_END, 5.0); 57 } 58 59 glClearColor(0.5, 0.9, 0.9, 1.0); /* fog color */ 60 61 } 62 63 void display(void) { 64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 65 glColor3f(0.0, 1.0, 1.0); 66 glPushMatrix (); // remember their position 67 glutSolidSphere ( 1.0 , 20 , 16 ); / * draw solar radius, 20 longitude, latitude 16 * / 68 glRotatef (Spin, 0.0 , 1.0 , 0.0 ); // rotation, a vector around a given angle of rotation (counterclockwise positive) 69 the glTranslatef ( 2.0 , 1.0 , 0.0 ); 70 glRotatef (Spin, 1.0 , 0.0 , 0.0 ); // revolution 71 is glRectf ( 0.1 ,0.1 , 0.5 , 0.5 ); 72 glColor3f ( 0.0 , 0.0 , 1.0 ); 73 is glutWireSphere ( 0.2 , . 8 , . 8 ); / * Videos first asteroid * / 74 glColor3f ( 1.0 , 0.0 , 0.0 ); 75 the glTranslatef ( 2.0 , 1.0 , 0.0 ); 76 glRotatef ( 2 * Spin, 0.0 , 1.0 ,0.0 ); 77 glutSolidSphere ( 0.5 , 16 , . 8 ); 78 glPopMatrix (); // return to the original position 79 glutSwapBuffers (); 80 } 81 82 void spinDisplay ( void ) { 83 Spin Spin + = 2 ; 84 IF ( Spin> 360 ) 85 Spin Spin = - 360 ; 86 glutPostRedisplay (); 87 } 88 89 void Mouse (int button, int state, int x, int y) { 90 switch (button) 91 { 92 case GLUT_LEFT_BUTTON: 93 if (state == GLUT_DOWN) 94 glutIdleFunc(spinDisplay); 95 break; 96 97 case GLUT_MIDDLE_BUTTON: 98 if (state == GLUT_DOWN) 99 glutIdleFunc(NULL); 100 break; 101 102 default: 103 break; 104 } 105 106 } 107 108 void reshape(int w, int h) { 109 glViewport(0, 0, (GLsizei)w, (GLsizei)h); 110 glMatrixMode(GL_PROJECTION); 111 glLoadIdentity(); 112 gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 20.0); 113 glMatrixMode(GL_MODELVIEW); 114 glLoadIdentity(); 115 gluLookAt(0.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 116 } 117 118 void keyboard(unsigned char key, int x, int y) { 119 switch (key) { 120 case 'd': 121 day = (day + 10) % 360; 122 glutPostRedisplay(); 123 break; 124 case 'D': 125 day = (day - 10) % 360; 126 glutPostRedisplay(); 127 break; 128 case 'y': 129 year = (year + 5) % 360; 130 glutPostRedisplay(); 131 break; 132 case 'Y': 133 year = (year - 5) % 360; 134 glutPostRedisplay(); 135 break; 136 case 27: 137 exit(0); 138 break; 139 default: 140 break; 141 } 142 } 143 144 int main(int argc, char** argv) { 145 glutInit(&argc, argv); 146 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 147 glutInitWindowSize(400, 400 ); 148 glutInitWindowPosition ( 100 , 100 ); 149 glutCreateWindow ( " OpengGL programming - I copied this code is " ); 150 the init (); 151 // glutDisplayFunc (DrawCircle); 152 glutDisplayFunc (Run the display); 153 glutReshapeFunc (the RESHAPE); 154 // glutKeyboardFunc (Keyboard); 155 glutMouseFunc (Mouse); 156 glutMainLoop (); 157 158 return 0 ; 159 }
Original link: https: //blog.csdn.net/Jacketinsysu/article/details/49563139