好吧,我刚刚买了这本opengl精美图书(第5版)。不幸的是,我在第一章偶然发现了如何设置opengl本身。这是我所做的步骤。
(1)首先,我安装了 Microsoft Visual Studio 2010 C++ Express 。
(2)我下载了所需的库,为此我直接从opengl superbible的官方网站http://www.starstonesoftware.com/files/SB5.zip下载了。然后,我将文件提取到本地文件夹中。
(3)然后我创建一个新的空项目。
文件->新建->项目-> Visual C++安装的模板->常规->空项目
(4)为freeglut和gltools文件头添加其他包含目录路径。
项目->属性->配置属性->(然后设置配置=所有配置)-> C / C++->常规->其他包含目录。
在这里我添加两个目录:
(a)freeglut:[我的本地路径] \ SB5 \ freeglut-2.6.0 \ 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:[我的本地路径] \ SB5 \ freeglut-2.6.0 \ VisualStudio2008Static \ Release
(b)GLTools.lib:[我的本地路径] \ 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: %s\n", 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 Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\vc90.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 Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\vc90.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 Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\vc90.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 Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\vc90.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 Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\vc90.pdb'; linking object as if no debug info
1> superbible1.vcxproj -> C:\Documents and Settings\Tony\My Documents\Visual Studio 2010\Projects\superbible1\Debug\superbible1.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 <GL\glut.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();
}
该应用程序可以正常运行。所以我认为代码可能有问题,或者我想念一些东西。所以有人可以帮忙吗?
最佳答案
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, ...
可以不是问题。但是VisualVisual的绿色调试光标显示将要执行的NEXT行,因此我怀疑它是shaderManager.InitializeStockShaders();。崩溃了。
我不知道这是做什么的,但是它可能读取文件...确保找到了文件。特别是,根据zip的内容,从似乎合理的目录中直接从资源管理器中运行.exe。
关于c++ - OpenGL SuperBible第5版的另一个设置问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9990513/