如何在 ANSI C 中使用 glDrawVertex() 函数
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据规范:
每个索引有 3 个坐标,所以
From the spec :
You have 3 coordinates per index, so