I’m trying to draw a sphere using vertex array…
This is my Creating Code…
int CSampleRendererDoc::CreateSphere(double dRadius, int nSlice, int nStack,
float **ppOutTexCoords, float **ppOutNormals, float **ppOutVertices)
{
// check params
if (!ppOutNormals || !ppOutVertices || nSlice <= 0 || nStack <= 0) return -2;
//###############
//## Fill Here ##
//###############
// Longitude, Latitude.
float fLon, fLat;
float fLonStep = M_PI/static_cast<float>(nStack);
float fLatStep = M_PI/static_cast<float>(nSlice);
int iCntVertices = 0;
int iTotalVertex = ((M_2PI/fLonStep) * ((M_PI+fLatStep)/fLatStep)) * 2 * 3 + 1;
(*ppOutNormals) = new float[iTotalVertex];
(*ppOutVertices) = new float[iTotalVertex];
memset((*ppOutNormals), 0, iTotalVertex*sizeof(float));
memset((*ppOutVertices), 0, iTotalVertex*sizeof(float));
for (fLon = 0.0; fLon <= M_2PI; fLon += (fLonStep))
{
for (fLat = 0.0; fLat <= M_PI + fLatStep; fLat += (fLatStep))
{
(*ppOutNormals)[iCntVertices + 0] = dRadius * cosf(fLon)*sinf(fLat);
(*ppOutNormals)[iCntVertices + 1] = dRadius * sinf(fLon)*sinf(fLat);
(*ppOutNormals)[iCntVertices + 2] = dRadius * cosf(fLat);
(*ppOutVertices)[iCntVertices + 0] = dRadius * cosf(fLon)*sinf(fLat);
(*ppOutVertices)[iCntVertices + 1] = dRadius * sinf(fLon)*sinf(fLat);
(*ppOutVertices)[iCntVertices + 2] = dRadius * cosf(fLat);
iCntVertices += 3;
(*ppOutNormals)[iCntVertices + 0] = dRadius * cosf(fLon + fLonStep)*sinf(fLat);
(*ppOutNormals)[iCntVertices + 1] = dRadius * sinf(fLon + fLonStep)*sinf(fLat);
(*ppOutNormals)[iCntVertices + 2] = dRadius * cosf(fLat);
(*ppOutVertices)[iCntVertices + 0] = dRadius * cosf(fLon + fLonStep)*sinf(fLat);
(*ppOutVertices)[iCntVertices + 1] = dRadius * sinf(fLon + fLonStep)*sinf(fLat);
(*ppOutVertices)[iCntVertices + 2] = dRadius * cosf(fLat);
iCntVertices += 3;
}
}
return iCntVertices;
}
I’m pretty sure my creating code is not bad…
however, on the drawing codes, my application crashes at glArrayElement()
void CSampleRendererDoc::DrawSphereVA(int iSSID)
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
{
glVertexPointer(3, GL_FLOAT, 0, m_aSunAndPlanets[iSSID].m_pVertexArray);
glNormalPointer(GL_FLOAT, 0, m_aSunAndPlanets[iSSID].m_pNormalArray);
glBegin(GL_TRIANGLE_STRIP);
{
for(GLint iCnt=0; iCnt<m_aSunAndPlanets[iSSID].m_iVertices; iCnt+=3)
{
glArrayElement(iCnt+0);
glArrayElement(iCnt+1);
glArrayElement(iCnt+2);
}
}
glEnd();
//glDrawArrays(GL_QUAD_STRIP, 0, m_aSunAndPlanets[iSSID].m_iVertices);
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
I can’t understand why does my app crash…
I need your help…
Thank you.