gpt4 book ai didi

ios - GLKit: [绘图像素] 初始化 GLfloat 数组

转载 作者:行者123 更新时间:2023-11-28 17:35:03 25 4
gpt4 key购买 nike

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

最佳答案

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

关于ios - GLKit: [绘图像素] 初始化 GLfloat 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com