views:

53

answers:

1

Hello :)

I'm learning OpenGL, and now usage of function glDrawArrays() but I always get Segmntation fault on glDrawArrays call, i'm 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();
+4  A: 

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)
Calvin1602
Unfortunately this does not solved problem. Segfault remains :]
BPS
Do you bind your vertex array ? glBindVertexArray(vertexArrayId);
Calvin1602
@Calvin glBindVertexArrays is to use VAOs, which are NOT required or even used here.
Matias Valdenegro
You're drawing quads, so it should be / 4, not /3.
Matias Valdenegro
@Matias Valdenego - /4 also not work :] It's could be a bug in driver? I use Linux with NVidia.
BPS
@BPS I doubt it, this is a "common" issue because you're not passing the correct data or correct sizes.
Matias Valdenegro
@Matias, @BPS : My mistake, I meant glEnableClientState(GL_VERTEX_ARRAY); But as a side note, the default VAO has been deprecated, so it is required (even if nobody does it)
Calvin1602
@Matias : Are you sure about the /4 ? this would mean that there is 48/4 indices...
Calvin1602