简体   繁体   English

OpenGL Superbible 5th Edition代码问题

[英]Opengl superbible 5th edition code problem

I've started reading superbible 5th edition and I'm stuck at the first program. 我已经开始阅读精湛的第五版,而我被困在第一个程序中。 As I'm using xp & vs2008, I've followed all the instruction provided by the book to setup the vs2008 to run the triangle program, but after compilation process it shows an error and I've no idea why is this happenning. 当我使用xp和vs2008时,我遵循了本书提供的所有说明来设置vs2008以运行三角程序,但是在编译过程后它显示了错误,我不知道为什么会这样。 The program is following: 该程序如下:

// 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: %s\n", glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}

And the error displaying is this 错误显示是这个

1>d:\opengl\SB5\freeglut-2.6.0\VisualStudio2008Static\Release\freeglut_static.lib : fatal error LNK1107: invalid or corrupt file: cannot read at 0x3
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\dfdf\Debug\BuildLog.htm"

Could somebody please help me with this problem? 有人可以帮我解决这个问题吗? Thank you. 谢谢。

I think your problem lays somewhere else than code. 我认为您的问题不在代码之外。 I've traced back what is 我追溯了什么

#define FREEGLUT_STATIC 

doing and this popped out: 这样做,然后弹出:

#   ifdef FREEGLUT_STATIC

#       define FGAPI
#       define FGAPIENTRY

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

However if you downloaded prepared package from freeglut Windows Development Libraries , you get only freeglut.lib. 但是,如果您从freeglut Windows Development Libraries下载了准备好的软件包, 只会得到freeglut.lib。 So either you have to build "freeglut_static.lib" for yourself from freeglut, or don't use static macro and you should be fine. 因此,要么必须从freeglut自己构建“ freeglut_static.lib”,要么不使用静态宏,就可以了。 Linker is just saying what it's experiencing - can't find your file so not able to read... 链接器只是在说它在经历什么-无法找到您的文件,因此无法读取...

and BTW: What book instructs is one thing but maybe you skipped something or they just didn't included it, but when you download clean freeglut, VisualStudio2008Static folder doesn't contain any library just another folders and visual studio project. 和BTW:这本书的内容是一回事,但也许您跳过了某些内容,或者它们只是没有包含其中,但是当您下载干净的freeglut时,VisualStudio2008Static文件夹不包含任何库,而仅包含另一个文件夹和Visual Studio项目。 You need to open that project, select release version in MSVC (build-> configuration manager) and build project. 您需要打开该项目,在MSVC(构建->配置管理器)中选择发行版本并构建项目。 You get you freeglut_static.lib in release folder. 您可以在release文件夹中获得freeglut_static.lib。

The error message you're getting indicates that the freeglut_static.lib file is corrupt. 您收到的错误消息表明freeglut_static.lib文件已损坏。 You would be best off to reinstall the library to ensure the file was copied correctly, or you could try rebuilding the library from source. 您最好重新安装该库以确保文件已正确复制,或者您可以尝试从源代码重建该库。

It might be worth trying a prebuilt version from another website, maybe you'll have more luck with the Visual Studio version available here . 可能值得尝试从另一个网站获得预构建的版本,也许您可​​以从这里的Visual Studio版本获得更多的运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM