我最近开始通过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: %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。
答案 0 :(得分:2)
由于您使用的是Windows,因此必须在windows.h
之前添加gl.h
。我不确定你包含的其他标题,但请确保检查。不同的订单通常会导致出现这些警告和错误。