我正试图在iOS上用OpenGL绘制一个球体。我在下面创建了一个球类。然后我设置并绘制球体。但是,根本没有任何吸引力。
我试过不使用VBO,仍然无法正常工作。我检查了我的VBO是否已创建,它是。
没有崩溃或错误。它只是没有画。我做错了什么?
class Sphere
{
private:
std::vector<GLVertex> vertices;
GLuint vbo;
GLuint count;
public:
Sphere(float radius, float step)
{
#define DEGREES_TO_RADIANS(value) (value * M_PI / 180.0)
GLfloat uStep = DEGREES_TO_RADIANS(step);
GLfloat vStep = uStep;
for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep) {
for (GLfloat v = -M_PI_2; v <= M_PI_2; v += vStep) {
float x = radius * cosf(v) * cosf(u);
float y = radius * cosf(v) * sinf(u);
float z = radius * sinf(v);
vertices.push_back({{x, y, z}, {1, 0, 0, 1}});
x = radius * cosf(v) * cosf(u + uStep);
y = radius * cosf(v) * sinf(u + uStep);
z = radius * sinf(v);
vertices.push_back({{x, y, z}, {1, 0, 0, 1}});
x = radius * cosf(v + vStep) * cosf(u);
y = radius * cosf(v + vStep) * sinf(u);
z = radius * sinf(v + vStep);
vertices.push_back({{x, y, z}, {1, 0, 0, 1}});
x = radius * cosf(v + vStep) * cosf(u + uStep);
y = radius * cosf(v + vStep) * sinf(u + uStep);
z = radius * sinf(v + vStep);
vertices.push_back({{x, y, z}, {1, 0, 0, 1}});
}
}
count = vertices.size();
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLVertex), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
~Sphere()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vbo);
}
void draw(float x, float y, float z)
{
glPushMatrix();
glTranslatef(x, y, z);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_FLOAT, sizeof(GLVertex), 0);
glColor4f(1, 1, 1, 1);
glDrawArrays(GL_POINTS, 0, count);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}
};
然后我像这样设置和绘制:
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearStencil(0);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
GLKMatrix4 projection = GLKMatrix4MakePerspective(60.0, self.bounds.size.width / self.bounds.size.height, 1.0, 100);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&projection.m[0]);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
static Sphere sphere(20, 30);
sphere.draw(0, 0, -5);
[_context presentRenderbuffer:GL_RENDERBUFFER];