Opengl超级圣经第5版代码问题

Opengl superbible 5th edition code problem

本文关键字:5版 代码 问题 Opengl      更新时间:2023-10-16

我已经开始读《超级圣经》第五版了,但是我在第一个程序上卡住了。我用的是xp &vs2008,我遵循了书中提供的所有指令来设置vs2008运行三角形程序,但在编译过程后,它显示了一个错误,我不知道为什么会发生这种情况。程序如下:

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
#endif
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
    glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    shaderManager.InitializeStockShaders();
    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
                          0.5f, 0.0f, 0.0f,
                          0.0f, 0.5f, 0.0f };
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
    // Perform the buffer swap to display the back buffer
    glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
    gltSetWorkingDirectory(argv[0]);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %sn", glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}

错误显示如下

1>d:openglSB5freeglut-2.6.0VisualStudio2008StaticReleasefreeglut_static.lib : fatal error LNK1107: invalid or corrupt file: cannot read at 0x3
1>Build log was saved at "file://c:Documents and SettingsAdministratorMy DocumentsVisual Studio 2008ProjectsdfdfDebugBuildLog.htm"
有人能帮我解决这个问题吗?谢谢你。

我认为你的问题不在代码上。我已经找到了

是什么
#define FREEGLUT_STATIC 

where and this弹出:

#   ifdef FREEGLUT_STATIC
#       define FGAPI
#       define FGAPIENTRY
        /* Link with Win32 static freeglut lib */
#       if FREEGLUT_LIB_PRAGMAS
#           pragma comment (lib, "freeglut_static.lib")
#       endif

然而,如果你从freeglut Windows开发库下载准备好的包,你只得到freeglut.lib。所以要么你必须构建"freeglut_static"。或者不使用静态宏,你应该没问题。链接器只是在说它正在经历什么-找不到你的文件,所以无法读取…

和BTW:什么书指示是一回事,但可能你跳过了一些东西,或者他们只是没有包括它,但当你下载干净的freeglut, VisualStudio2008Static文件夹不包含任何库,只是另一个文件夹和visual studio项目。您需要打开该项目,在MSVC(构建->配置管理器)中选择发布版本并构建项目。你会得到freeglut_static。

您得到的错误消息表明freeglut_static。库文件损坏。您最好重新安装库以确保文件被正确复制,或者您可以尝试从源代码重新构建库。

这可能值得尝试从其他网站预构建的版本,也许你会有更多的运气与Visual Studio版本在这里。