我的理解是当我试图访问尚未考虑的内存时发生分段错误。我似乎无法找到错误。我已经测试了在此代码之外填充数组的过程并且它工作正常,我认为错误必须在我分配的可用空间中,但我已经分配了5倍的内存,但仍然会出现分段错误,所以我对这个很不确定。
#include <cmath>
#include <iostream>
#ifdef __APPLE__
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <OpenGL/glext.h>
#else
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <GL/glext.h>
#pragma comment(lib, "glew32.lib") // Compiler directive to include the GLEW library.
#endif
#define PI 3.14159265
using namespace std;
/*we have to draw a circular lampshade... USING VERTEX ARRs*/
static float R1 = 10.0;
static float R2 = 8.0;
static float h = 10;
static float H = 10;
static float Xangle = 0.0, Yangle = 0.0, Zangle = 0.0; // Angles to
static int p = 6; // number of subdiviions of 2 * PI
static float *verts = NULL;
static unsigned int *inds = NULL;
//there are 2*(p+1) vertices
//but verts has size, 6*(p+1)
void fillVer(void)
{
int i,j;
i = -1;
for(j = 0; j <= p; j++)
{
verts[i++] = R1 * cos(2.0 * PI * (j/p));
verts[i++] = R1 * sin(2.0 * PI * (j/p));
verts[i++] = h;
}
for(j = 0; j <= p; j++)
{
verts[i++] = R2 * cos(2.0 * PI * (j/p));
verts[i++] = R2 * sin(2.0 * PI * (j/p));
verts[i++] = H;
}
}
// there are 2*(p+1) vertices
void fillInd(void)
{
int j = 0;
for(int i = 0; i <= p; i++)
{
inds[j] = i;
inds[j++] = i+(p+1);
}
}
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glEnableClientState(GL_VERTEX_ARRAY);
}
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0,0,0);
verts = new float[6*(p + 1)];
inds = new unsigned int[2*(p+1)];
fillVer();
fillInd();
glVertexPointer(3,GL_FLOAT,0,verts);
glTranslatef(0.0, 0.0, -10.0);
glRotatef(Zangle, 0.0, 0.0, 1.0);
glRotatef(Yangle, 0.0, 1.0, 0.0);
glRotatef(Xangle, 1.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glDrawElements(GL_TRIANGLE_STRIP,2*(p+1),GL_UNSIGNED_INT, inds);
glFlush();
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'x':
Xangle += 5.0;
if (Xangle > 360.0) Xangle -= 360.0;
glutPostRedisplay();
break;
case 'X':
Xangle -= 5.0;
if (Xangle < 0.0) Xangle += 360.0;
glutPostRedisplay();
break;
case 'y':
Yangle += 5.0;
if (Yangle > 360.0) Yangle -= 360.0;
glutPostRedisplay();
break;
case 'Y':
Yangle -= 5.0;
if (Yangle < 0.0) Yangle += 360.0;
glutPostRedisplay();
break;
case 'z':
Zangle += 5.0;
if (Zangle > 360.0) Zangle -= 360.0;
glutPostRedisplay();
break;
case 'Z':
Zangle -= 5.0;
if (Zangle < 0.0) Zangle += 360.0;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitContextVersion(2, 1);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("hemisphereMultidraw.cpp");
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glewExperimental = GL_TRUE;
glewInit();
setup();
glutMainLoop();
}
EDIT 问题在于三角函数的论证!
2.0 * PI * (j/p)
应该是
2.0 * (float)(j/p) * PI
以及其他修改,代码完美,并绘制了可爱的灯罩。
答案 0 :(得分:2)
找到一个原因:
int i,j;
i = -1;
for(j = 0; j <= p; j++)
{
verts[i++] = R1 * cos(2.0 * PI * (j/p));
verts[i++] = R1 * sin(2.0 * PI * (j/p));
verts[i++] = h;
}
i
的值从-1开始,所以在这个for循环中执行的第一件事是
verts[-1] = R1 * cos(2.0 * PI * (j/p));
访问索引-1
处的数组必然会导致分段错误。您的OpenGL代码看起来很好,但您肯定需要重写fillVer()
和fillInd()
函数。