【问题标题】:GLKit: [drawing pixels] initializing GLfloat arrayGLKit:[绘制像素] 初始化 GLfloat 数组
【发布时间】:2012-04-06 08:44:58
【问题描述】:

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

【问题讨论】:

    标签: ios opengl-es glkit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多