我在绘制球体方面遇到了一些麻烦。
std::vector<GLfloat> ballVerts;
for(int i = 0; i <= 40; i++)
{
double lat0 = M_PI * (-0.5 + (double) (i - 1) / 40);
double z0 = sin(lat0);
double zr0 = cos(lat0);
double lat1 = M_PI * (-0.5 + (double) i / 40);
double z1 = sin(lat1);
double zr1 = cos(lat1);
for(int j = 0; j <= 40; j++)
{
double lng = 2 * M_PI * (double) (j - 1) / 40;
double x = cos(lng);
double y = sin(lng);
ballVerts.push_back(x * zr0); //X
ballVerts.push_back(y * zr0); //Y
ballVerts.push_back(z0); //Z
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f);
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f); //R,G,B,A
ballVerts.push_back(x * zr1); //X
ballVerts.push_back(y * zr1); //Y
ballVerts.push_back(z1); //Z
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f);
ballVerts.push_back(0.0f);
ballVerts.push_back(1.0f); //R,G,B,A
}
}
glGenBuffers(1, &ballVbo);
glBindBuffer(GL_VERTEX_ARRAY, ballVbo);
GLuint sphereSize = 3200*7*4; //3200 vertixes * 7 floats
glBufferData(GL_VERTEX_ARRAY,sphereSize, &ballVerts, GL_STATIC_DRAW);
/*
Draw a ball
*/
glBindBuffer(GL_VERTEX_ARRAY, ballVbo);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 7*4, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 7*4, (void*)(3*4));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3200);
glBindBuffer(GL_ARRAY_BUFFER, 0);
生成球体的代码在立即模式下工作正常,但是当我把它放在VBO中时,我不断从glDrawArrays中获得访问冲突异常。有什么建议吗?
答案 0 :(得分:7)
glBufferData(GL_VERTEX_ARRAY,sphereSize, &ballVerts, GL_STATIC_DRAW);
ballVerts
不是数组。这是std::vector
。获取std::vector
的地址不会获得 vector
中包含的数组的地址。您需要使用&ballVerts[0]
;
答案 1 :(得分:0)
更好地预调整大小矢量并使用迭代器来扫描它 像这样的东西:
std::vector <GLfloat> vertices;
vertices.resize(count * 3);
...
std::vector <GLfloat>::iterator v = vertices.begin();
for( U32 r = 0; r < m_prec/2; ++r ) //широта
{
float const theta1 = -F_PI_BY_TWO + pi2p * r;
float const theta2 = theta1 + pi2p;
for( U32 s = 0; s < m_prec; ++s ) //долгота
{
float const theta3 = s * pi2p;
float ex = cosf(theta3) *cosf(theta2);
float ey = sinf(theta2);
float ez = sinf(theta3) * cosf(theta2);
*v++ = m_x + m_radius * ex;
*v++ = m_y + m_radius * ey;
*v++ = m_z + m_radius * ez;
等
:: glVertexPointer(3,GL_FLOAT,0,&amp; vertices [0]);
会起作用