所以我跟随“OpenGL superbible 5”并遇到了另一个问题: 我正在尝试使用GLTools.h,GLShaderManager,math3d.h等来获取该书的示例代码,这些代码来自谷歌代码,但是 我得到的是一个空白的白色窗口。
这是我正在运行的代码:
// Objects.cpp
// OpenGL SuperBible, Chapter 4
// Demonstrates GLTools built-in objects
// Program by Richard S. Wright Jr.
#include <GL/glew.h>
#include <GLTools.h> // OpenGL toolkit
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLGeometryTransform.h>
#include <math.h>
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif
/////////////////////////////////////////////////////////////////////////////////
// An assortment of needed classes
GLShaderManager shaderManager;
GLMatrixStack modelViewMatrix;
GLMatrixStack projectionMatrix;
GLFrame cameraFrame;
GLFrame objectFrame;
GLFrustum viewFrustum;
GLTriangleBatch sphereBatch;
GLTriangleBatch torusBatch;
GLTriangleBatch cylinderBatch;
GLTriangleBatch coneBatch;
GLTriangleBatch diskBatch;
GLGeometryTransform transformPipeline;
M3DMatrix44f shadowMatrix;
GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
// Keep track of effects step
int nStep = 0;
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Black background
glClearColor(0.7f, 0.7f, 0.7f, 1.0f );
shaderManager.InitializeStockShaders();
glEnable(GL_DEPTH_TEST);
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
cameraFrame.MoveForward(-15.0f);
// Sphere
gltMakeSphere(sphereBatch, 3.0, 10, 20);
// Torus
gltMakeTorus(torusBatch, 3.0f, 0.75f, 15, 15);
// Cylinder
gltMakeCylinder(cylinderBatch, 2.0f, 2.0f, 3.0f, 13, 2);
// Cone
gltMakeCylinder(coneBatch, 2.0f, 0.0f, 3.0f, 13, 2);
// Disk
gltMakeDisk(diskBatch, 1.5f, 3.0f, 13, 3);
};
/////////////////////////////////////////////////////////////////////////
void DrawWireFramedBatch(GLTriangleBatch* pBatch)
{
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vGreen);
pBatch->Draw();
// Draw black outline
glPolygonOffset(-1.0f, -1.0f);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(2.5f);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);
pBatch->Draw();
// Restore polygon mode and depht testing
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_POLYGON_OFFSET_LINE);
glLineWidth(1.0f);
glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH);
};
///////////////////////////////////////////////////////////////////////////////
// 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);
modelViewMatrix.PushMatrix();
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.MultMatrix(mCamera);
M3DMatrix44f mObjectFrame;
objectFrame.GetMatrix(mObjectFrame);
modelViewMatrix.MultMatrix(mObjectFrame);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);
switch(nStep) {
case 0:
DrawWireFramedBatch(&sphereBatch);
break;
case 1:
DrawWireFramedBatch(&torusBatch);
break;
case 2:
DrawWireFramedBatch(&cylinderBatch);
break;
case 3:
DrawWireFramedBatch(&coneBatch);
break;
case 4:
DrawWireFramedBatch(&diskBatch);
break;
}
modelViewMatrix.PopMatrix();
// Flush drawing commands
glutSwapBuffers();
};
// Respond to arrow keys by moving the camera frame of reference
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_DOWN)
objectFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);
if(key == GLUT_KEY_LEFT)
objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);
if(key == GLUT_KEY_RIGHT)
objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);
glutPostRedisplay();
};
///////////////////////////////////////////////////////////////////////////////
// A normal ASCII key has been pressed.
// In this case, advance the scene when the space bar is pressed
void KeyPressFunc(unsigned char key, int x, int y)
{
if(key == 32)
{
nStep++;
if(nStep > 4)
nStep = 0;
}
switch(nStep)
{
case 0:
glutSetWindowTitle("Sphere");
break;
case 1:
glutSetWindowTitle("Torus");
break;
case 2:
glutSetWindowTitle("Cylinder");
break;
case 3:
glutSetWindowTitle("Cone");
break;
case 4:
glutSetWindowTitle("Disk");
break;
}
glutPostRedisplay();
};
///////////////////////////////////////////////////////////////////////////////
// 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);
viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
modelViewMatrix.LoadIdentity();
};
///////////////////////////////////////////////////////////////////////////////
// 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("Sphere");
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(KeyPressFunc);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
以下是VS2010输出文本框中弹出的错误: 很多PDB文件没有发现(但我怀疑这是问题)
'TriangleBatch shapes.exe': Loaded 'C:\Windows\SysWOW64\ig4icd32.dll', Cannot find or open the PDB file
'TriangleBatch shapes.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file
'TriangleBatch shapes.exe': Loaded 'C:\Windows\SysWOW64\oleacc.dll', Cannot find or open the PDB file
在更改窗口的大小或对窗口执行任何操作后出现了一堆错误..
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
First-chance exception at 0x02993890 in TriangleBatch shapes.exe: 0xC0000005: Access violation reading location 0x05bda35c.
感谢任何输入!! =) 的
答案 0 :(得分:1)
首先,在使用之前,您没有将FinalMatrix
设置为任何内容。我不熟悉GLTools,但是有一些设置需要你丢失吗?
答案 1 :(得分:0)
您可能没有可能解释白色渲染窗口的投影矩阵集。
在应用程序初始化期间,使用以下或类似方法生成投影矩阵:
glMatrixMode(GL_PROJECTION); //changes the current matrix to the projection matrix
//sets up the projection matrix for a perspective transform
gluPerspective(45, //view angle
1.0, //aspect ratio
10.0, //near clip
200.0); //far clip
glMatrixMode(GL_MODELVIEW); //changes the current matrix to the modelview matrix
(来自GameDev.net)
如果您愿意,可以通过在窗口宽度超过高度来计算纵横比。视角,近和远剪裁平面取决于您的应用。
为了使用glu函数,你需要使用glu.h并将glu.lib作为库添加(虽然快速查看glew.h标题会显示Glu头文件应该已经包含在内)。
另一个问题可能是尚未设置视图矩阵。 GLFrame标题似乎包含一个相机系统,可以在设置相机位置后,查看向量(或点),向上方向用于生成视图矩阵(我认为是GetCameraMatrix())。
我目前还不确定可以用什么函数来设置你目前使用的库的视图矩阵