2012-02-19 39 views
3

我最近开始通过SB第五版学习OpenGl,不幸的是他们在第一个例子中被卡住了。OpenGL superbible第5版。 Triangle.cpp

下面的代码:

#include <GLTools.h> 
#include <GLShaderManager.h> 
#include <GL/glew.h> 
#include <GL/glut.h> 


GLShaderManager shaderManager; 
GLBatch triangleBatch; 
void ChangeSize(GLsizei w, GLsizei h); 

void ChangeSize(int w, int h){ 
    glViewport(0,0,w,h); 
} 

void SetupRC(){ 
    //background 
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f); 


    shaderManager.InitializeStockShaders(); 

    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(); 

} 

void RenderScene(void){ 

    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(); 


    glutSwapBuffers(); 
} 


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

虽然,我不认为问题出在代码,因为我已经简单地复制从书粘贴它。我已经添加了本书内容中的库,并且还安装了freeglut。所以,现在他找到了我需要的所有标题,但是当我尝试构建时,存在与triangle.cpp本身无关的大量问题列表。这里有一些:

1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(183): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(183): error C2059: syntax error : ')' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(184): error C2146: syntax error : missing ')' before identifier 'width' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(184): error C2182: 'gltGenerateOrtho2DMat' : illegal use of type 'void' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(184): error C2059: syntax error : ')' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(234): error C2378: 'GLuint' : redefinition; symbol cannot be overloaded with a typedef 
1>   c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(177) : see declaration of 'GLuint' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(235): error C2378: 'GLint' : redefinition; symbol cannot be overloaded with a typedef 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(162) : see declaration of 'GLint' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(238): error C2378: 'GLbyte' : redefinition; symbol cannot be overloaded with a typedef 
1>   c:\users\whatever\desktop\sb5\src\gltools\include\gltools.h(153) : see declaration of 'GLbyte' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(804): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(804): error C2143: syntax error : missing ',' before '*' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(805): error C2146: syntax error : missing ')' before identifier 'i' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(805): warning C4229: anachronism used : modifiers on data are ignored 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(805): error C2182: 'glArrayElement' : illegal use of type 'void' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(805): error C2491: 'glArrayElement' : definition of dllimport data not allowed 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(805): error C2059: syntax error : ')' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(807): error C2061: syntax error : identifier 'GLuint' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(810): error C2146: syntax error : missing ')' before identifier 'list' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(810): warning C4229: anachronism used : modifiers on data are ignored 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(810): error C2182: 'glCallList' : illegal use of type 'void' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(810): error C2491: 'glCallList' : definition of dllimport data not allowed 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(810): error C2059: syntax error : ')' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(817): error C2146: syntax error : missing ')' before identifier 's' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(817): warning C4229: anachronism used : modifiers on data are ignored 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(817): error C2182: 'glClearStencil' : illegal use of type 'void' 
1>c:\users\whatever\desktop\sb5\src\gltools\include\gl\glew.h(817): error C2491:  'glClearStencil' : definition of dllimport data not allowed 

我真的不能做任何事情。它看起来好像错误是在头部本身。就像这些,还有其他几百个头文件。我希望对于我想要揭露的问题,我有足够的了解。如果有什么不清楚的地方,请随时询问。我会尽量少模糊

另外,我使用Microsoft Visual C++ 2010

+1

您是否试过[下载源代码](http://code.google.com/p/oglsuperbible5/)并直接编译它,而不是复制到自己的项目中? – 2012-02-19 16:17:58

+1

我有。奇怪的(或不是),即使在那个特定的文件中,我也遇到了同样的问题,其他项目的构建和运行都很好。 – 2012-02-19 17:34:49

回答

2

既然你在Windows上工作,你将有gl.h之前包括windows.h。我不确定你包含的其他标题,但一定要检查。不同的顺序通常会导致这些警告和错误出现。

+0

哇。就如此容易。人们会认为他们会在某处提到它。那么再说一遍,也许这本书不适合完成像我这样的业余爱好者。谢谢! 但是,一个新问题来取代刚才解决的问题: 1> triangle.obj:错误LNK2019:无法解析的外部符号“public:void __thiscall GLBatch :: End(void)”(?End @ GLBatch @@ QAEXXZ )在函数“void __cdecl SetupRC(void)”(?SetupRC @@ YAXXZ)中引用 12个与已经显示的错误类似的错误。我已经做了一个快速搜索,这是关于一些失踪的图书馆?我以为自己有了一切,显然不是。有任何想法吗? – 2012-02-19 17:13:44

+0

快速查看你的第二个问题,似乎之前已经有类似的问题了:http://stackoverflow.com/questions/4123082/linker-error-during-opengl-superbible-tutorial总之,你需要链接反对gltools.lib,我认为这是一本属于本书源代码的库... – Bart 2012-02-19 17:21:22

+0

不幸的是,该线程中的建议解决方案并不适合我。我已经完成了“在我的解决方案目录中包含glut32.dll”的所有内容,因为我认为这不在问题的范围之内。但LNK错误依然存在 – 2012-02-19 17:40:02