配置OpenGL及第一个实例

Windows环境下安装GLUT的步骤:
1、将下载的压缩包解开,将得到5个文件
2、在“我的电脑”中搜索“gl.h”,并找到其所在文件夹(如果是VS,则应该是其安装目录下面的“VCPlatformSDKincludegl文件夹”)。把解压得到的glut.h放到这个文件夹。
3、把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(如果是VS,则应该是其安装目录下面的“VClib”文件夹)。
4、把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:WindowsSystem32)

进入VS,随便新建或打开一个win32控制台应用程序,随后:项目 -->选择属性 CC++-->preprocessor-->preprocessor definition添加GLUT_BUILDING_LIB,中间用分号隔开,然后点击linker-- >input--> additional dependencies添加glut32.lib Opengl32.lib Glu32.lib,此时OpenGL的开发环境就配置好了。(不过以后每新建一个项目我都需要重复这个步骤,肯定有其他方法,知道了大神们请留言哦)

下面是一个由中心点向四周发散射线的例子。

#include<GL/glut.h>
#include<math.h>

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;

///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
    {
        GLfloat x,y,z,angle; // Storeage for coordinates and angles

    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);

    // Save matrix state and do the rotation
    glPushMatrix();
    glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);

    // Call only once for all remaining points
    glBegin(GL_LINES);

    z = 0.0f;
    for(angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))
        {
        // Top half of the circle
        x = 50.0f*sin(angle);
        y = 50.0f*cos(angle);
        glVertex3f(x, y, z);

        // Bottom half of the circle
        x = 50.0f*sin(angle+GL_PI);
        y = 50.0f*cos(angle+GL_PI);
        glVertex3f(x, y, z);    
        }

    // Done drawing points
    glEnd();

    // Restore transformations
    glPopMatrix();

    // Flush drawing commands
    glutSwapBuffers();
    }

///////////////////////////////////////////////////////////
// This function does any needed initialization on the 
// rendering context. 
void SetupRC()
    {
    // Black background
    glClearColor(0, 0.0f, 0, 1.0f );

    // Set drawing color to green
    glColor3f(0, 1,0);
    }

///////////////////////////////////////////////////////////
// Respond to arrow keys
void SpecialKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP)
        xRot-= 5.0f;

    if(key == GLUT_KEY_DOWN)
        xRot += 5.0f;

    if(key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if(key == GLUT_KEY_RIGHT)
        yRot += 5.0f;

    if(key > 356.0f){
        xRot = 0.0f;
        yRot=0.0f;
    }
    if(key < -1.0f){
        xRot = 355.0f;
        yRot=355.0f;
    }

    // Refresh the Window
    glutPostRedisplay();
    }

///////////////////////////////////////////////////////////
// Window has changed size, recalculate projection
void ChangeSize(int w, int h)
    {
    GLfloat nRange = 100.0f;

    // Prevent a divide by zero
    if(h == 0)
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    if (w <= h) 
        glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
    else 
        glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

///////////////////////////////////////////////////////////
// Main Program Entry Point
int main(int argc, char* argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800,600);
    glutCreateWindow("Lines Example");
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(SpecialKeys);
    glutDisplayFunc(RenderScene);
    SetupRC();
    glutMainLoop();

    return 0;
    }
原文地址:https://www.cnblogs.com/zjzsky/p/3330389.html