其他分享
首页 > 其他分享> > 如何在ANSI C中使用glDrawVertex()函数

如何在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();

解决方法:

从规格:

count : Specifies the number of indices to be rendered.

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

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

标签:c-3,linux,opengl,segmentation-fault
来源: https://codeday.me/bug/20191105/1998307.html