我正在尝试渲染一个可以扩展到其他对象的四面体。使用静态数组可以很好地绘制四面体。
但是当我将 OFF 文件读入动态数组时,什么也没有出现。编译时没有出现错误。
GLfloat *tetra_vertices = NULL; //This is declared at the start
以下用于从 OFF 文件中获取数据的函数中。mod->faces 从 OFF 文件中获取面数。在这种情况下,它是 4。我检查了从 OFF 文件中读取的顶点,这是准确的。甚至将其与静态数组进行比较。
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];
}
在初始化()中:
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();
最后在我的 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 ();
}
最终我想将数组存储到一个列表中,这样我就可以从不同的 OFF 文件中循环抛出不同的对象。但现在我只想让它画一些东西。任何帮助将不胜感激。谢谢你。