我最近开始通过SB第五版学习OpenGl,不幸的是,在他们提供的第一个例子中就被卡住了。
这是代码:
#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: %sn", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
不过,我不认为问题出在代码上,因为我只是从书中复制粘贴了它。我添加了这本书内容中包含的库,还安装了免费供过于求。所以现在他找到了我需要的所有标题,但当我尝试构建时,有一大堆与triangle.cpp本身无关的问题。以下是一些:
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(183): warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(183): error C2059: syntax error : ')'
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(184): error C2146: syntax error : missing ')' before identifier 'width'
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(184): error C2182: 'gltGenerateOrtho2DMat' : illegal use of type 'void'
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(184): error C2059: syntax error : ')'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(234): error C2378: 'GLuint' : redefinition; symbol cannot be overloaded with a typedef
1> c:userswhateverdesktopsb5srcgltoolsincludegltools.h(177) : see declaration of 'GLuint'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(235): error C2378: 'GLint' : redefinition; symbol cannot be overloaded with a typedef
1>c:userswhateverdesktopsb5srcgltoolsincludegltools.h(162) : see declaration of 'GLint'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(238): error C2378: 'GLbyte' : redefinition; symbol cannot be overloaded with a typedef
1> c:userswhateverdesktopsb5srcgltoolsincludegltools.h(153) : see declaration of 'GLbyte'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(804): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(804): error C2143: syntax error : missing ',' before '*'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(805): error C2146: syntax error : missing ')' before identifier 'i'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(805): warning C4229: anachronism used : modifiers on data are ignored
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(805): error C2182: 'glArrayElement' : illegal use of type 'void'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(805): error C2491: 'glArrayElement' : definition of dllimport data not allowed
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(805): error C2059: syntax error : ')'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(807): error C2061: syntax error : identifier 'GLuint'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(810): error C2146: syntax error : missing ')' before identifier 'list'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(810): warning C4229: anachronism used : modifiers on data are ignored
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(810): error C2182: 'glCallList' : illegal use of type 'void'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(810): error C2491: 'glCallList' : definition of dllimport data not allowed
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(810): error C2059: syntax error : ')'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(817): error C2146: syntax error : missing ')' before identifier 's'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(817): warning C4229: anachronism used : modifiers on data are ignored
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(817): error C2182: 'glClearStencil' : illegal use of type 'void'
1>c:userswhateverdesktopsb5srcgltoolsincludeglglew.h(817): error C2491: 'glClearStencil' : definition of dllimport data not allowed
我真的对此无能为力。看起来错误就好像在标头中。像这样,还有数百个来自其他头文件的头文件。我希望我对我试图揭露的问题足够明确。如果有什么不清楚的地方,请随时询问。我会尽量不那么含糊
此外,我正在使用Microsoft Visual C++2010。
由于您使用的是Windows,因此必须在gl.h
之前包含windows.h
。我不确定你包含的其他标题,但一定要检查一下。不同的顺序通常会导致出现这些警告和错误。