I'm trying to render a tetrahedron that I could expand to other objects.
Using static arrays works fine, draws the tetrahedron.
But when I read in an OFF file into a dynamic array, nothing appears. No errors appear when I compile.
GLfloat *tetra_vertices = NULL; //This is declared at the start
The following is used in the function that grabs the data from the OFF file. mod->faces grabs the number of faces from the OFF file. In this case it is 4. I checked reading in the vertices from the OFF file and that is accurate. Even compared it to the static array.
tetra_vertices = (GLfloat *)malloc(sizeof(GLfloat) * mod->faces * 3 * 3);
for(i=0; i< (mod->faces * 3); i++){
tetra_vertices[i*3]=mod->verts[(mod->face[i])*3];
tetra_vertices[i*3+1]=mod->verts[(mod->face[i])*3+1];
tetra_vertices[i*3+2]=mod->verts[(mod->face[i])*3+2];
}
In init():
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tetra_vertices), tetra_vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tetra_colors), tetra_colors, GL_STATIC_DRAW);
program = initshader( "a4a_vs.glsl", "a4a_fs.glsl" );
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
pos = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(pos);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, NULL);
color = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(color);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 0, NULL);
ModelView = glGetUniformLocation(program, "modelview");
Projection = glGetUniformLocation(program, "projection");
glEnable (GL_DEPTH_TEST);
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glewExperimental = GL_TRUE;
glewInit();
And finally in my main():
GLFWwindow *window = glfwCreateWindow (512, 512, "Hello Cube", NULL, NULL);
glfwMakeContextCurrent (window);
setup(argc, argv); //This creates reads in the OFF file and creates the arrays.
init();
reshape(window, 512, 512);
glfwSetKeyCallback(window, keyboard);
glfwSetWindowSizeCallback(window, reshape);
while (!glfwWindowShouldClose (window)) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
identity(transform);
lookat(transform, eye, at, up);
glUseProgram(program);
glBindVertexArray(vao);
glUniformMatrix4fv(ModelView, 1, GL_FALSE, transform);
glUniformMatrix4fv(Projection, 1, GL_FALSE, projection);
glDrawArrays(GL_TRIANGLES, 0, (mod->faces * 3));
glfwSwapBuffers (window);
if (animation)
update();
glfwPollEvents ();
}
Eventually I want to store the array into a list so I can cycle throw different objects from different OFF files. But for now I just want it to draw something. Any assistance would be greatly appreciated. Thank you.
The
sizeof
operator queries the size of the object or type, it returns the size in bytes of the type that would be returned by the expression you pass to it. Since the type oftetra_vertices
isGLfloat*
,sizeof(tetra_vertices)
returns the size of the pointer type, which probably is 4 or 8 depending on the hardware.The 2nd parameter of
glBufferData
is the size of the data buffer in bytes.This means you have to change your code like this:
You have to do the same for the color attribute buffer
tetra_colors
.