GLKit:[绘制像素]初始化GLfloat数组

时间:2012-04-06 08:44:58

标签: ios opengl-es glkit

我正在使用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;
}

1 个答案:

答案 0 :(得分:0)

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