我需要一个程序生成的OpenGL ES球体 - 我用Google搜索并发现this OGL code,并认为它看起来整洁,所以我将其修改为OGLES。我删除了仅使用OGL的glBegin(),glEnd()并将原始的drawtri和drawsphere修改为generatetri和generateSpherePoints。这些新函数将GLfloat值分配给球体法线和顶点的静态数组,这意味着我可以随意调用一个新函数drawSphere(),并且不需要重新计算这些点。我还在generateSpherePoints中添加了x,y,z参数,允许指定非零球体中心。
问题是我的OGLES版本在glDrawArrays行的XCode中给出了一个EXC_BAD_ACCESS。通过调试器,我检查了'sphereNormals'和'sphereVertices'数组,它们看起来很好。如果有人可以提出问题的原因,我会非常感激,因为我现在对此一无所知!
#define SX .525731112119133606
#define SZ .850650808352039932
/* Drawing Sphere */
static GLfloat vdata[12][3] = {
{-SX, 0.0, SZ}, {SX, 0.0, SZ}, {-SX, 0.0, -SZ}, {SX, 0.0, -SZ},
{0.0, SZ, SX}, {0.0, SZ, -SX}, {0.0, -SZ, SX}, {0.0, -SZ, -SX},
{SZ, SX, 0.0}, {-SZ, SX, 0.0}, {SZ, -SX, 0.0}, {-SZ, -SX, 0.0}
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
void normalize(GLfloat *a) {
GLfloat d=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
a[0]/=d; a[1]/=d; a[2]/=d;
}
static const int ndiv = 5;
static const int vecSize = 3;
static const int isoVertexCount = 3;
static const int spherePointsSectionCount = 20;
static const int pointSectionSize = 9216;// value MUST be updated to be result of calcPointSectionSize(ndiv)
static const int pointCount = 184320;// value MUST be updated to be spherePointsSectionCount*pointSectionSize;
static const int sphereVertsCount = 61440; // value MUST be updated to be pointCount/vecSize;
static GLfloat spherePoints[184320]; // size MUST be updated to be pointCount value
static GLfloat sphereNormals[184320]; // size MUST be updated to be pointCount value
int calcPointSectionSize(int div) {
return vecSize*isoVertexCount*powf(4.0f,(float)(div));
}
// OpenGL ES doesn't support glBegin(), glEnd() so must use glDrawArrays() instead
void generatetri(GLfloat *a, GLfloat *b, GLfloat *c, int div, float r, int pos) {
if (div<=0) {
int X = 0, Y = 1, Z = 2;
sphereNormals[pos+X] = a[X];
sphereNormals[pos+Y] = a[Y];
sphereNormals[pos+Z] = a[Z];
spherePoints[pos+X] = a[X]*r;
spherePoints[pos+Y] = a[Y]*r;
spherePoints[pos+Z] = a[Z]*r;
sphereNormals[pos+vecSize+X] = b[X];
sphereNormals[pos+vecSize+Y] = b[Y];
sphereNormals[pos+vecSize+Z] = b[Z];
spherePoints[pos+vecSize+X] = b[X]*r;
spherePoints[pos+vecSize+Y] = b[Y]*r;
spherePoints[pos+vecSize+Z] = b[Z]*r;
sphereNormals[pos+2*vecSize+X] = c[X];
sphereNormals[pos+2*vecSize+Y] = c[Y];
sphereNormals[pos+2*vecSize+Z] = c[Z];
spherePoints[pos+2*vecSize+X] = c[X]*r;
spherePoints[pos+2*vecSize+Y] = c[Y]*r;
spherePoints[pos+2*vecSize+Z] = c[Z]*r;
/*
glNormal3fv(a); glVertex3f(a[0]*r, a[1]*r, a[2]*r);
glNormal3fv(b); glVertex3f(b[0]*r, b[1]*r, b[2]*r);
glNormal3fv(c); glVertex3f(c[0]*r, c[1]*r, c[2]*r);
*/
} else {
GLfloat ab[3], ac[3], bc[3];
for (int i=0;i<3;i++) {
ab[i]=(a[i]+b[i])/2;
ac[i]=(a[i]+c[i])/2;
bc[i]=(b[i]+c[i])/2;
}
normalize(ab); normalize(ac); normalize(bc);
const int pointSectionSize = calcPointSectionSize(div-1);
generatetri(a, ab, ac, div-1, r, pos+0*pointSectionSize);
generatetri(b, bc, ab, div-1, r, pos+1*pointSectionSize);
generatetri(c, ac, bc, div-1, r, pos+2*pointSectionSize);
generatetri(ab, bc, ac, div-1, r, pos+3*pointSectionSize);
}
}
void generateSpherePoints(float x, float y, float z, float radius) {
for (int i=0;i<20;i++) {
GLfloat *va = vdata[tindices[i][0]];
GLfloat *vb = vdata[tindices[i][1]];
GLfloat *vc = vdata[tindices[i][2]];
GLfloat a[3] = {va[0]+x,va[1]+y,va[2]+z};
GLfloat b[3] = {vb[0]+x,vb[1]+y,vb[2]+z};
GLfloat c[3] = {vc[0]+x,vc[1]+y,vc[2]+z};
generatetri(a, b, c, ndiv, radius, i*pointSectionSize);
}
}
void drawSphere() {
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, sphereNormals);
glVertexPointer(3, GL_FLOAT, 0, spherePoints);
glDrawArrays(GL_TRIANGLES, 0, sphereVertsCount);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
答案 0 :(得分:0)
EXC_BAD_ACCESS表示您的代码正在尝试访问其不拥有的内存地址。造成这种情况的最常见原因是内存管理代码中的错误导致对象在仍在使用时被释放。例如,您的代码可能无法保留所需的对象,或者可能过早地释放了对象。
在这种情况下,我猜你正在绘制的视图可能已经过早释放。但是,您应该使用NSZombieEnabled工具来确定代码尝试访问哪个解除分配的对象,而不是猜测。请参阅iOS Development Guide section on Debugging Applications。