0
我有以下代码,但找不到该错误。这似乎是一个与记忆有关的问题。原始代码取自OpenGL超级自动化附带的3d库,并且我正在尝试使其适用于openGL es。任何关于为什么它一直在进行分割的想法?所以你需要分配在OpenGL ES中绘制球体的帮助
sizeof(GLfloat) * iStacks * (iSlices + 1) * 3 * 2
字节vertexPointer
- (void)drawSphereOfRadius:(GLfloat)fRadius nbSlices:(GLint)iSlices nbStacks:(GLint)iStacks
{
GLfloat *vertexPointer = malloc(sizeof(GLfloat) * iStacks * iSlices * 3 * 2);
GLfloat drho = (GLfloat)(3.141592653589)/(GLfloat) iStacks;
GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589)/(GLfloat) iSlices;
GLfloat ds = 1.0f/(GLfloat) iSlices;
GLfloat dt = 1.0f/(GLfloat) iStacks;
GLfloat t = 1.0f;
GLfloat s = 0.0f;
GLint i, j; // Looping variables
int idx = 0;
for (i = 0; i < iStacks; i++)
{
GLfloat rho = (GLfloat)i * drho;
GLfloat srho = (GLfloat)(sin(rho));
GLfloat crho = (GLfloat)(cos(rho));
GLfloat srhodrho = (GLfloat)(sin(rho + drho));
GLfloat crhodrho = (GLfloat)(cos(rho + drho));
s = 0.0f;
for (j = 0; j <= iSlices; j++)
{
GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
GLfloat stheta = (GLfloat)(-sin(theta));
GLfloat ctheta = (GLfloat)(cos(theta));
GLfloat x = stheta * srho;
GLfloat y = ctheta * srho;
GLfloat z = crho;
glNormal3f(x, y, z);
vertexPointer[idx] = x * fRadius;
vertexPointer[idx + 1] = y * fRadius;
vertexPointer[idx + 2] = z * fRadius;
x = stheta * srhodrho;
y = ctheta * srhodrho;
z = crhodrho;
s += ds;
glNormal3f(x, y, z);
vertexPointer[idx + 3] = x * fRadius;
vertexPointer[idx + 4] = y * fRadius;
vertexPointer[idx + 5] = z * fRadius;
idx += 6;
}
t -= dt;
}
glVertexPointer(3, GL_FLOAT, 0, vertexPointer);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, iStacks * iSlices * 2);
free(vertexPointer);
//glPopMatrix();
}
那么它停止了segfaulting,但它仍然画错了。我会努力的。 – 2009-09-27 15:43:00
对于开始您对'glNormal3f'的调用,请不断更改当前的正常值,但您不会等到'glDrawArrays'命令。它将使用您为所有顶点设置的最后一个正常值。至于你似乎试图生成三角形条的顶点,但这不是你设置顶点缓冲区的方式。与其试图解决这个问题,您可能想要为三角形条生成一个索引缓冲区,以节省重复前一个堆栈迭代中的所有顶点,即创建一个只保存一次顶点的顶点缓冲区。索引缓冲区是指向该顶点缓冲区的索引数组。 – Troubadour 2009-09-27 20:40:05
对不起,最后一条评论应该显示为“...但是,直到'glDrawArrays'命令不会_drawing_”。 – Troubadour 2009-09-27 21:47:02