65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 GLUT 库的简单 OpenGL 窗口

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (8投票s)

2008年2月1日

CPOL

2分钟阅读

viewsIcon

90571

downloadIcon

2966

一个简单的 C++ 控制台应用程序,使用 GLUT 库创建 OpenGL 窗口

OpenGLBasicWindowImage.jpg

引言

这是一个非常简单的 C++ 控制台应用程序,用于创建一个简单的 OpenGL 窗口。源代码来自书籍“OpenGL 编程指南(第三版)”。本文将解释如何下载和包含 GLUT 库。一旦包含 GLUT 库,构建和执行代码就非常简单。它还解释了该程序中使用的 OpenGL 代码。

背景

您应该能够使用 Visual Studio 2005 和 C++。您也应该对 OpenGL 有一些了解。

Using the Code

步骤

  1. GLUT 安装(下载 GLUT 库)
  2. 将 GLUT 库文件复制并粘贴到特定位置
  3. 创建一个 C++ 控制台应用程序
  4. 输入代码,构建并执行

步骤 1

在开始新项目之前,您需要下载 GLUT 库。您可以从此链接获取最新版本:GLUT 3.7.6 for Windows。点击此链接(glut-3.7.6-bin.zip (117 KB) )并将 ZIP 文件下载到您的计算机。

第二步

解压缩文件夹,将 glut32.dll 文件复制并粘贴到 C:\WINDOWS\system。将 glut32.lib 文件复制并粘贴到此目录:C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib。最后一步是将 glut.h 文件复制并粘贴到此目录:C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\gl。GLUT 库文件的安装完成。但是,如果您使用的是不同的 Windows 或不同的 Visual Studio 版本,目录可能会有所不同。我假设您使用的是 Windows Vista 和 Visual Studio 2005。

步骤 3

创建一个 C++ 控制台应用程序。您不需要 Windows 应用程序。从 Visual Studio 2005 中,选择 文件 -> 新建 -> 项目。选择项目类型 Visual C++ -> Win32 并创建一个 Win32 控制台应用程序。

步骤 4

输入代码,构建并执行。确保包含 <GL/glut.h >

//Include files
#include "stdafx.h"
#include <GL/glut.h >

函数

函数 display(void) 显示一个多边形

void display(void)
{
    //Clear all pixels
    glClear(GL_COLOR_BUFFER_BIT);

    //draw white polygon (rectangle) with corners at
    // (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_POLYGON);
        glVertex3f(0.25, 0.25, 0.0);
        glVertex3f(0.75, 0.25, 0.0);
        glVertex3f(0.75, 0.75, 0.0);
        glVertex3f(0.25, 0.75, 0.0);
    glEnd();
    
    // Don't wait start processing buffered OpenGL routines
    glFlush();
}

函数 init() 初始化 GLUT(设置状态)

void init(void)
{
    //select clearing (background) color
    glClearColor(0.0, 0.0, 0.0, 0.0);

    //initialize viewing values 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

}

主函数:确保将命令行参数 _TCHAR* 更改为 char**

int _tmain(int argc, char** argv)
{
    //Initialise GLUT with command-line parameters. 
    glutInit(&argc, argv);
    
    //Set Display Mode
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    
    //Set the window size
    glutInitWindowSize(250,250);
    
    //Set the window position
    glutInitWindowPosition(100,100);
    
    //Create the window
    glutCreateWindow("A Simple OpenGL Windows Application with GLUT");
    
    //Call init (initialise GLUT
    init();
    
    //Call "display" function
    glutDisplayFunc(display);
    
    //Enter the GLUT event loop
    glutMainLoop();

    return 0;
}

变量或类名应包含在 <code> 标签中,例如 this

关注点

实际上,这个程序是一个非常简单的 OpenGL 程序。但是,如果您是新手,并且不知道如何初始化 GLUT 库,编译代码可能需要更多一点时间。

历史

上传日期:2008/01/29

© . All rights reserved.