参考资料:百度经验
看完上面文章,再去看官方文档,发现他们都是翻译或抄别人的翻译而已,想看原汁原味的官方文档可以点这里:点我点我。
资源官网看这里:点我点我
首先要明确的是,openGL是一个标准,windows和linux都支持它,然后它不需要安装就可以用了,需要的只是电脑的驱动配置而已(我都没去动过这些,不过可以,目测现代一般的都可以,如果不行的话,再回来检查),配置的驱动看这里:点我点我。
第二,glut库很便于openGL编程,去这里下载:点我下载glut库,解压得到5个文件:glut.h,glut.dll,glut32.dll,glut.lib,glut32.lib。
第三,安装glut库。假设你的vs的安装路径为MY_VS_ROOT,那么在MY_VS_ROOT/VC/include/下新建一个文件夹GL,然后复制glut.h到这个文件夹下,比如我的就是D:\soft\vs2015\VC\include\GL\glut.h。
然后复制glut.lib和glut32.lib到MY_VS_ROOT/VC/lib/下,最后复制glut.dll和glut32.dll到系统的dll目录下:C:\Windows\system32文件夹内(32位系统)或C:\Windows\SysWOW64(64位系统)。
第四,写个代码测试一下。在vs工程下新建一个cpp文件,复制下面的代码进去,ctrl + F5运行,鼠标点击画面,就可以看到*你猜╮(╯▽╰)╭。
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(); //记住自己的位置
67 glutSolidSphere(1.0, 20, 16); /* 画太阳半径、 20经度、16纬度*/
68 glRotatef(spin, 0.0, 1.0, 0.0); //自转,绕着一个向量以给定角度旋转(正的为逆时针)
69 glTranslatef(2.0, 1.0, 0.0);
70 glRotatef(spin, 1.0, 0.0, 0.0); //公转
71 glRectf(0.1, 0.1, 0.5, 0.5);
72 glColor3f(0.0, 0.0, 1.0);
73 glutWireSphere(0.2, 8, 8); /* 画第一颗小行星 */
74 glColor3f(1.0, 0.0, 0.0);
75 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();//回到原来的位置
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 程序设计--这段代码是我抄的");
150 init();
151 //glutDisplayFunc(DrawCircle);
152 glutDisplayFunc(display);
153 glutReshapeFunc(reshape);
154 //glutKeyboardFunc(keyboard);
155 glutMouseFunc(mouse);
156 glutMainLoop();
157
158 return 0;
159 }
原文链接:https://blog.csdn.net/Jacketinsysu/article/details/49563139