我有一个在CUDA上运行的波束形成程序,我必须在Opengl中显示光束的输出,我必须在Opengl中绘制一个矩形,它由24x12的小数组组成squares.I必须根据来自CUDA程序的输出进行波束形成,使用不同颜色对这些正方形进行着色。我已经能够使用VBO绘制矩形,我使用以下结构传递一个包含正方形顶点和每个顶点颜色的数组。我面临的问题的总体概述是我无法正确地将颜色分配给每个正方形。从代码的一些摘录:从Opengl中的不同颜色的方块绘制矩形
struct attributes {
GLfloat coords[2]; //co-ordinates of the vertices
GLfloat color[3]; //color of the vertices
};
glGenBuffers(1, &vbo_romanis); // vbo_romanis is the VBO for drawing the frame
glBindBuffer(GL_ARRAY_BUFFER, vbo_romanis);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STREAM_DRAW);
glShadeModel (GL_SMOOTH);
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord);
glEnableVertexAttribArray(attribute_color);
glBindBuffer(GL_ARRAY_BUFFER, vbo_romanis);
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
sizeof(struct attributes), // next coord2 appears every 5 floats
0 // offset of first element
);
glVertexAttribPointer(
attribute_color, // attribute
3, // number of elements per vertex, here (r,g,b)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
sizeof(struct attributes), // stride
(GLvoid*) offsetof(struct attributes, color) // offset
);
/* Push each element in buffer_vertices to the vertex shader */
glDrawArrays(GL_QUADS, 0, 4*NUM_SQRS);
所以我面临着2个问题,当我画数组:
的颜色没有出现,因为我希望他们。从我读到的有关Opengl的内容来看,曾经分配过的顶点的颜色不能改变。但是由于所有的正方形在它们之间共享顶点,颜色可能会混乱。如果我给所有的顶点赋予相同的颜色,它可以很好地工作,但是当我想绘制所有不同颜色的方块时不会。所以,如果有人能指出我可以给每个方格分配一种不同的颜色,那真的很有帮助。
如何更新每个帧的顶点颜色,我是否需要重新绘制整个框架,或者是否有方法仅更新顶点的颜色。
我对OpenGL编程完全陌生,任何帮助都将不胜感激。
非常感谢指针@derhass ...他们一直很有帮助 – anshu