在Visual Studio 2015中配置openGL
的有关信息介绍如下:
在Visual Studio 2015中配置openGL
1.打开vs2015,运行—devenv
在Visual C++下新建一个win32控制台程序
确定—下一步—完成
项目—管理Nuget程序包
浏览——在搜索栏输入NupenGL,安装这两个文件包贪财
程序测试
#include
#include
#include
#include
staticintyear = 0, spin = 0, day = 0;staticGLint fogMode;constintn = 100;constGLfloat R = 1.0f;constGLfloat Pi = 3.1415926536f;
voidDrawCircle() {
inti;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(i = 0; i < n; ++i)
{
glColor3f(1.0, 0.0, 0.0);
glVertex2f(R*cos(2* Pi / n*i), R*sin(2* Pi / n*i));
}
glEnd();
glFlush();
}
voidinit(void) {
GLfloat position[] = { 0.5, 0.5, 3.0, 0.0};
glEnable(GL_DEPTH_TEST); //防止遮挡glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat = { 0.1745, 0.01175, 0.01175};
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat = 0.61424; mat = 0.04136; mat = 0.04136;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
茄睡mat = 0.727811; mat = 0.626959; mat = 0.626959;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
}
glEnable(GL_FOG);
{
羞总销GLfloat fogColor = { 0.5, 0.5, 0.5, 1.0};
fogMode = GL_EXP;
glFogi(GL_FOG_MODE, fogMode);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.35);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 1.0);
glFogf(GL_FOG_END, 5.0);
}
glClearColor(0.5, 0.9, 0.9, 1.0); /* fog color */
}
voiddisplay(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);
glPushMatrix(); //记住自己的位置
glutSolidSphere(1.0, 20, 16); /* 画太阳半径、 20经度、16纬度*/
glRotatef(spin, 0.0, 1.0, 0.0); //自转,绕着一个向量以给定角度旋转(正的为逆时针)
glTranslatef(2.0, 1.0, 0.0);
glRotatef(spin, 1.0, 0.0, 0.0); //公转
glRectf(0.1, 0.1, 0.5, 0.5);
glColor3f(0.0, 0.0, 1.0);
glutWireSphere(0.2, 8, 8); /* 画第一颗小行星 */
glColor3f(1.0, 0.0, 0.0);
glTranslatef(2.0, 1.0, 0.0);
glRotatef(2* spin, 0.0, 1.0, 0.0);
glutSolidSphere(0.5, 16, 8);
glPopMatrix();//回到原来的位置glutSwapBuffers();
}
voidspinDisplay(void) {
spin = spin + 2;
if(spin > 360)
spin = spin - 360;
glutPostRedisplay();
}
voidmouse(intbutton, intstate, intx, inty) {
switch(button)
{
caseGLUT_LEFT_BUTTON:
if(state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
caseGLUT_MIDDLE_BUTTON:
if(state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
voidreshape(intw, inth) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
voidkeyboard(unsigned charkey, intx, inty) {
switch(key) {
case'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
case27:
exit(0);
break;
default:
break;
}
}
intmain(intargc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpengGL 程序设计测试");
init();
//glutDisplayFunc(DrawCircle);glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);glutMouseFunc(mouse);
glutMainLoop();
return0;
}



