好吧,我刚刚买了这本opengl超级软件书(第5版)。不幸的是,我在第一章中偶然发现了如何设置opengl本身。这是我做的步骤。
(1) 首先,我安装了Microsoft Visual Studio 2010 C++学习版。
(2) 我下载了所需的库,为此我直接从opengl superible的官方网站下载:http://www.starstonesoftware.com/files/SB5.zip.然后我将这些文件提取到本地文件夹中。
(3) 然后我创建了一个新的空项目
文件->新建->项目->Visual C++安装的模板->常规->空项目
(4) 为freeglut和gltools文件头添加额外的includedirectories路径
项目->属性->配置属性->(然后我设置配置=所有配置)->C/C++->常规->其他包含目录
在这里我添加了两个目录:
(a) freeglut:[我的本地路径]\SB5\freeglut-2.60\include
(b) gltools:[我的本地路径]\SB5\Src\gltools\include
(5) 为freeglut和gltools文件头添加额外的依赖项路径
项目->属性->配置属性->(然后我设置配置=所有配置)->链接器->输入->其他依赖项
(a) freeglut_static.lib
(b) GLTools.lib
(6) 正在添加库目录
项目->属性->配置属性->(然后我设置配置=所有配置)->VC++目录->库目录。在这里,我设置了freeglut_static.lib和GLTools.lib的目录位置
(a) freeglut_static.lib:[my local path]\SB5\freeglut-2.60\VisualStudio2008Static\Release
(b) GLTools.lib:[my local path]\SB5\VisualStudio2008\GLTools\Release
(7) 之后,我复制了源代码(Triangle.cpp),但我没有忘记在第一行包含windows.h。
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <Windows.h>
#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 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: %sn", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
(8) 在第7步之后,我构建了这个项目,一切仍然顺利。以下是构建结果:
1>------ Build started: Project: superbible1, Configuration: Debug Win32 ------
1> main.cpp
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>GLTools.lib(GLBatch.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'GLTools.lib(GLBatch.obj)' or at 'C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugvc90.pdb'; linking object as if no debug info
1>GLTools.lib(glew.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'GLTools.lib(glew.obj)' or at 'C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugvc90.pdb'; linking object as if no debug info
1>GLTools.lib(GLShaderManager.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'GLTools.lib(GLShaderManager.obj)' or at 'C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugvc90.pdb'; linking object as if no debug info
1>GLTools.lib(GLTools.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'GLTools.lib(GLTools.obj)' or at 'C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugvc90.pdb'; linking object as if no debug info
1>GLTools.lib(GLTriangleBatch.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'GLTools.lib(GLTriangleBatch.obj)' or at 'C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugvc90.pdb'; linking object as if no debug info
1> superbible1.vcxproj -> C:Documents and SettingsTonyMy DocumentsVisual Studio 2010Projectssuperbible1Debugsuperbible1.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
(9) 我运行程序时出现了问题。我得到运行时错误。有一个警报对话框,显示以下消息:
Unhandled exception at 0x00000000 in superbible1.exe: 0xC0000005: Access violation reading location 0x00000000.
当我选择中断程序时,光标停在这一行:
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
如果我用另一个简单的代码(来自opengl红皮书)来更改这个代码。例如:
#define FREEGLUT_STATIC
#include <Windows.h>
#include <GLglut.h>
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
}
void init()
{
/* set clear color to black */
/* glClearColor (0.0, 0.0, 0.0, 0.0); */
/* set fill color to white */
/* glColor3f(1.0, 1.0, 1.0); */
/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
/* glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); */
}
int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
/* You must call glutInit before any other OpenGL/GLUT calls */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
glutDisplayFunc(display);
init();
glutMainLoop();
}
该应用程序可以运行良好。所以我想可能是代码出了问题,或者可能是我错过了什么。所以有人可以帮忙吗?
我也刚刚开始这方面的工作。
如果你要学习所有的例子,最好将所有的freeglut/GLTools.h和.lib文件复制到VC++2010的相关目录中。
您仍然需要在源代码中包含windows.h,并且仍然需要特别告诉VC++2010使用GLTools.lib(Linker,Input,Additional Dependencies)。
这个显然解释了如果pdb错误困扰你,如何消除它们。
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, ...
不会成为问题。但是Visual的绿色调试光标显示了将要执行的NEXT行,所以我怀疑它是shaderManager.InitializeStockShaders();崩溃了。
我不知道这是怎么回事,但它可能会读取文件。。。确保它能找到它们。特别是,根据zip的内容,从一个似乎有意义的目录直接从资源管理器运行.exe。