2011-04-19 32 views
0

我开始阅读OpenGL超级第五版和我被困在第一个程序。由于我使用的是vs2008,因此我遵循本书提供的所有说明来设置vs2008来运行三角形程序,但在编译过程后,它显示一个错误,我不知道为什么会发生这种情况。该方案是以下几点:的Opengl OpenGL超级第五版代码问题

// 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; 
} 

并显示错误是这样

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" 

可能有人请帮我解决这个问题?谢谢。

+0

有你确信你D:\的OpenGL \ SB5 \ freeglut-2.6.0 \ VisualStudio2008Static \发布\ freeglut_static.lib? – joce 2011-04-19 02:28:47

+0

你使用的是LIB还是DLL?我输入了错误的是几十遍,我已经下载了它导致这一点的问题(你的错误列出了LIB,但你确定它实际上是,和正确的文件,等等?) – ssube 2011-04-19 03:09:32

+0

LIB在书中 – Dgenxron 2011-04-20 02:21:46

回答

0

您收到此错误信息表明freeglut_static.lib文件已损坏。您最好重新安装库以确保文件被正确复制,或者您可以尝试从源重建库。

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

+0

指示freeblut from superbible官方网站 – Dgenxron 2011-04-19 02:48:32

+0

仍然不能正常工作 – Dgenxron 2011-04-19 02:56:47

+0

那么,为什么不从图书馆的主页http://freeglut.sf.net下载图书馆,并自己编译它。这样你可以确保链接的库和编译器匹配。 – datenwolf 2011-04-19 07:24:36

1

我认为你的问题不在代码中。我一直追溯到什么

#define FREEGLUT_STATIC 

做这个弹出:

# 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 Development Libraries下载准备包,你只能freeglut.lib。所以要么你必须从freeglut为自己构建“freeglut_static.lib”,要么不使用静态宏,你应该没问题。链接器只是说它正在经历什么 - 无法找到你的文件,所以无法阅读...

和BTW:什么书指示是一件事,但也许你跳过一些东西,或者他们只是没有包括它,但当你下载干净的freeglut时,VisualStudio2008Static文件夹不包含任何库,只是另一个文件夹和Visual Studio项目。您需要打开该项目,在MSVC(build-> configuration manager)中选择发布版本并构建项目。您可以在发布文件夹中找到freeglut_static.lib。