如何在 ANSI C 中使用 glDrawVertex() 函数

发布于 2024-09-15 15:59:55 字数 2429 浏览 7 评论 0原文

我正在尝试使用 glDrawArrays() 但在 glDrawArrays 调用时总是出现分段错误。我做错了什么吗?

/*MESH*/
struct Mesh
{
 GLsizei numVertices;
 GLfloat vertices[48];
} m;

static void meshCreate(struct Mesh *mesh)
{
 mesh->vertices[0] = 0.000000f;
 mesh->vertices[1] = -0.000000f;
 mesh->vertices[2] = -0.800000f;
 mesh->vertices[3] = 0.000000f;
 mesh->vertices[4] = 0.158333f;
 mesh->vertices[5] = -0.177084f;
 mesh->vertices[6] = 0.300000f;
 mesh->vertices[7] = -0.000000f;
 mesh->vertices[8] = -0.300000f;
 mesh->vertices[9] = -0.300000f;
 mesh->vertices[10] = -0.000000f;
 mesh->vertices[11] = -0.300000f;
 mesh->vertices[12] = 0.300000f;
 mesh->vertices[13] = 0.000000f;
 mesh->vertices[14] = 0.300000f;
 mesh->vertices[15] = -0.300000f;
 mesh->vertices[16] = 0.000000f;
 mesh->vertices[17] = 0.300000f;
 mesh->vertices[18] = 0.177084f;
 mesh->vertices[19] = 0.158333f;
 mesh->vertices[20] = -0.000000f;
 mesh->vertices[21] = 0.800000f;
 mesh->vertices[22] = -0.000000f;
 mesh->vertices[23] = -0.000000f;
 mesh->vertices[24] = -0.177083f;
 mesh->vertices[25] = 0.158333f;
 mesh->vertices[26] = -0.000000f;
 mesh->vertices[27] = -0.800000f;
 mesh->vertices[28] = -0.000000f;
 mesh->vertices[29] = -0.000000f;
 mesh->vertices[30] = 0.000000f;
 mesh->vertices[31] = 0.158333f;
 mesh->vertices[32] = 0.177083f;
 mesh->vertices[33] = 0.000000f;
 mesh->vertices[34] = 0.000000f;
 mesh->vertices[35] = 0.800000f;
 mesh->vertices[36] = 0.000000f;
 mesh->vertices[37] = -0.158333f;
 mesh->vertices[38] = -0.177084f;
 mesh->vertices[39] = 0.177084f;
 mesh->vertices[40] = -0.158333f;
 mesh->vertices[41] = -0.000000f;
 mesh->vertices[42] = -0.177083f;
 mesh->vertices[43] = -0.158333f;
 mesh->vertices[44] = -0.000000f;
 mesh->vertices[45] = 0.000000f;
 mesh->vertices[46] = -0.158333f;
 mesh->vertices[47] = 0.177083f;

 mesh->numVertices = 48;

 logAdd(&gameLog, "Info: Mesh loaded.");
}

static void meshRender(struct Mesh *mesh)
{
 glVertexPointer(3, GL_FLOAT, 0, mesh->vertices);
 glDrawArrays(GL_QUADS, 0, mesh->numVertices); //<- here segmentation fault
}

//edit

meshCreate(&m); //OK
printf("%f", m.vertices[47]);//OK - variable m exists and has good values

//begin of loop
glClear();
meshRender(&m); //<-segfault
glfwSwapBuffers();

I'm trying to use glDrawArrays() but I always get segmentation fault on glDrawArrays call. Am I doing something wrong?

/*MESH*/
struct Mesh
{
 GLsizei numVertices;
 GLfloat vertices[48];
} m;

static void meshCreate(struct Mesh *mesh)
{
 mesh->vertices[0] = 0.000000f;
 mesh->vertices[1] = -0.000000f;
 mesh->vertices[2] = -0.800000f;
 mesh->vertices[3] = 0.000000f;
 mesh->vertices[4] = 0.158333f;
 mesh->vertices[5] = -0.177084f;
 mesh->vertices[6] = 0.300000f;
 mesh->vertices[7] = -0.000000f;
 mesh->vertices[8] = -0.300000f;
 mesh->vertices[9] = -0.300000f;
 mesh->vertices[10] = -0.000000f;
 mesh->vertices[11] = -0.300000f;
 mesh->vertices[12] = 0.300000f;
 mesh->vertices[13] = 0.000000f;
 mesh->vertices[14] = 0.300000f;
 mesh->vertices[15] = -0.300000f;
 mesh->vertices[16] = 0.000000f;
 mesh->vertices[17] = 0.300000f;
 mesh->vertices[18] = 0.177084f;
 mesh->vertices[19] = 0.158333f;
 mesh->vertices[20] = -0.000000f;
 mesh->vertices[21] = 0.800000f;
 mesh->vertices[22] = -0.000000f;
 mesh->vertices[23] = -0.000000f;
 mesh->vertices[24] = -0.177083f;
 mesh->vertices[25] = 0.158333f;
 mesh->vertices[26] = -0.000000f;
 mesh->vertices[27] = -0.800000f;
 mesh->vertices[28] = -0.000000f;
 mesh->vertices[29] = -0.000000f;
 mesh->vertices[30] = 0.000000f;
 mesh->vertices[31] = 0.158333f;
 mesh->vertices[32] = 0.177083f;
 mesh->vertices[33] = 0.000000f;
 mesh->vertices[34] = 0.000000f;
 mesh->vertices[35] = 0.800000f;
 mesh->vertices[36] = 0.000000f;
 mesh->vertices[37] = -0.158333f;
 mesh->vertices[38] = -0.177084f;
 mesh->vertices[39] = 0.177084f;
 mesh->vertices[40] = -0.158333f;
 mesh->vertices[41] = -0.000000f;
 mesh->vertices[42] = -0.177083f;
 mesh->vertices[43] = -0.158333f;
 mesh->vertices[44] = -0.000000f;
 mesh->vertices[45] = 0.000000f;
 mesh->vertices[46] = -0.158333f;
 mesh->vertices[47] = 0.177083f;

 mesh->numVertices = 48;

 logAdd(&gameLog, "Info: Mesh loaded.");
}

static void meshRender(struct Mesh *mesh)
{
 glVertexPointer(3, GL_FLOAT, 0, mesh->vertices);
 glDrawArrays(GL_QUADS, 0, mesh->numVertices); //<- here segmentation fault
}

//edit

meshCreate(&m); //OK
printf("%f", m.vertices[47]);//OK - variable m exists and has good values

//begin of loop
glClear();
meshRender(&m); //<-segfault
glfwSwapBuffers();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-09-22 15:59:55

根据规范:

count : Specifies the number of indices to be rendered.

每个索引有 3 个坐标,所以

glDrawArrays(GL_QUADS, 0, mesh->numVertices / 3)

From the spec :

count : Specifies the number of indices to be rendered.

You have 3 coordinates per index, so

glDrawArrays(GL_QUADS, 0, mesh->numVertices / 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文