C – How to use the glDrawVertex() function in ANSI C

How to use the glDrawVertex() function in ANSI C… here is a solution to the problem.

How to use the glDrawVertex() function in ANSI C

I’m trying to use glDrawArrays() but I keep getting a segfault on glDrawArrays calls. 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();

Solution

From the spec:

count : Specifies the number of indices to be rendered.

Each index has 3 coordinates, so

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

Related Problems and Solutions