1

我正在使用 GLKit 绘制像素。如果我有,我可以成功地在 (10, 10) 坐标处绘制像素:

glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Prepare the effect for rendering 
[self.effect prepareToDraw];

GLfloat points[] =
{
    10.0f, 10.0f,
};

glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

GLuint bufferObjectNameArray;
glGenBuffers(1, &bufferObjectNameArray);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);

glBufferData(
             GL_ARRAY_BUFFER,
             sizeof(points),
             points,
             GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);

glVertexAttribPointer(
                      GLKVertexAttribPosition,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      2*4,
                      NULL);
glDrawArrays(GL_POINTS, 0, 1);

但是我想在运行时决定要绘制像素的数量和确切位置,所以我尝试了这个,但它在 (10, 0) 处绘制像素,这里出了点问题:

glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Prepare the effect for rendering 
[self.effect prepareToDraw];

GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2);
for (int i=0; i<2; i++) {
    points[i] = 10.0f;
}

glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

GLuint bufferObjectNameArray;
glGenBuffers(1, &bufferObjectNameArray);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);

glBufferData(
             GL_ARRAY_BUFFER,
             sizeof(points),
             points,
             GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);

glVertexAttribPointer(
                      GLKVertexAttribPosition,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      2*4,
                      NULL);
glDrawArrays(GL_POINTS, 0, 1);

请帮帮我。

编辑: 问题 实际上问题是:我不知道有什么区别:

GLfloat points[] =
{
    10.0f, 10.0f,
};

GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2);
for (int i=0; i<2; i++) {
    points[i] = 10.0f;
}
4

1 回答 1

0

我敢打赌,问题出sizeof(points)glBufferData数据调用中:在这种情况下,它将返回指针的大小,即一个字,即 4 个字节(或类似的东西)。您应该传递数组的实际大小,这与您在malloc代码中计算的大小相同。

于 2012-04-06T12:59:01.017 回答