[英]Opengl superbible 5th edition code problem
我已經開始閱讀精湛的第五版,而我被困在第一個程序中。 當我使用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: %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"
有人可以幫我解決這個問題嗎? 謝謝。
我認為您的問題不在代碼之外。 我追溯了什么
#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(構建->配置管理器)中選擇發行版本並構建項目。 您可以在release文件夾中獲得freeglut_static.lib。
您收到的錯誤消息表明freeglut_static.lib文件已損壞。 您最好重新安裝該庫以確保文件已正確復制,或者您可以嘗試從源代碼重建該庫。
可能值得嘗試從另一個網站獲得預構建的版本,也許您可以從這里的Visual Studio版本獲得更多的運氣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.