Ubuntu/VS OpenGL配置

Ubuntu14.04

sudo apt-get install freeglut3 freeglut3-dev binutils-gold
g++ -lGL -lglut test.cpp -o test

VS

OpenGL安装和配置

GLUT3.7下载地址:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

下载好以后解压,里面可以得到5个文件

其中的.h文件放到VS的安装目录下include文件里:
\Visual Studio\VC\include

.lib文件放在和include同级的lib目录下:
\Visual Studio\VC

把两个dll文件都放到C:\Windows\System32下
如果是64位系统的话还要放在C:\Windows\SysWOW64下一份

测试程序

test.cpp

#if defined(_WIN32) || defined(WIN32)
#include <windows.h> 
#endif
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
static int year = 0, spin = 0, day = 0;
static GLint fogMode;
const int n = 100;
const GLfloat R = 1.0f;
const GLfloat Pi = 3.1415926536f;
void DrawCircle()
{
    int  i;
    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();
}
void init(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[3] = { 0.1745, 0.01175, 0.01175 };
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
        mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
        mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
        glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
    }
    glEnable(GL_FOG);
    {
        GLfloat fogColor[4] = { 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 */
}
void display(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();
}
void spinDisplay(void)
{
    spin = spin + 2;
    if (spin > 360)
        spin = spin - 360;
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    switch (button)
    {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(spinDisplay);
        break;
    case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN)
            glutIdleFunc(NULL);
        break;
    default:
        break;
    }
}
void reshape(int w, int h)
{
    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);
}
void keyboard(unsigned char key, int x, int y)
{
    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;
    case 27:
        exit(0);
        break;
    default:
        break;
    }
}
int main(int argc, 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();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值