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

使用 GLUT 的简单 OpenGL 窗口 EP_OpenGL_001

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.31/5 (9投票s)

2008 年 1 月 30 日

CPOL

2分钟阅读

viewsIcon

46696

downloadIcon

1047

使用 GLUT 的简单 OpenGL 窗口和 Win32 控制台应用程序

OpenGLBasicWindowImage.jpg

引言

这是一个使用 GLUT 库创建 OpenGL 窗口的简单 C++ 控制台应用程序。代码来自“OpenGL 编程指南(第三版)”。我尝试展示如何下载和包含 GLUT 库文件。包含 GLUT 文件后,编译和运行代码非常简单。如果您没有 GLUT 库,源代码将无法工作。请下载 GLUT 库并将其包含到特定位置(如下所述)。

背景

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

Using the Code

步骤

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

步骤 1

在开始新项目之前,您必须先下载 GLUT 库。您可以从以下链接获取最新版本: Windows 平台的 GLUT 3.7.6
点击此链接 (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 main(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;
}

关注点

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

历史

  • 上传于 2008/01/29
© . All rights reserved.