arrays - glDrawArrays 无法绘制动态数组

标签 arrays ubuntu opengl gldrawarrays

我正在尝试渲染一个可以扩展到其他对象的四面体。
使用静态数组可以很好地绘制四面体。
但是当我将 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 文件中循环抛出不同的对象。但现在我只想让它画一些东西。任何帮助将不胜感激。谢谢你。

最佳答案

sizeof 运算符查询对象或类型的大小,它返回将由您传递给它的表达式返回的类型的大小(以字节为单位)。由于 tetra_vertices 的类型是 GLfloat* , sizeof(tetra_vertices)返回指针类型的大小,可能是 4 或 8,具体取决于硬件。

glBufferData 的第二个参数是数据缓冲区的大小,以字节为单位。

这意味着您必须像这样更改代码:

size_t vertex_size = sizeof(GLfloat) * mod->faces * 3 * 3;
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)vertex_size, tetra_vertices, GL_STATIC_DRAW);

您必须对颜色属性缓冲区执行相同操作 tetra_colors .

关于arrays - glDrawArrays 无法绘制动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732745/

相关文章:

apache - Phabricator 重写规则配置问题

c - 包含 glad.h 时出现段错误

c++ - 在 Windows 上创建并行离屏 OpenGL 上下文

java - 在 Map Java 中转换嵌套 JsonArray

javascript - 为php表单动态生成文本框,将它们放入数组,内爆为字符串并将字符串写入数据库

python - 无法在 Ubuntu 上安装 TA-Lib

ruby-on-rails - Rails 在 ubuntu 中失败。谁能说为什么?

c++ - 镜面反射分量不正确的法线贴图和 phong 着色

php - 数字对具有混合类型值的数组进行排序

php - 检查数组 PHP 中特定值的次数