仅显示 OpenGL 最近的顶点绘制
OpenGL only most recent vertex draw displayed
OpenGL的新手,尝试使用矩形绘制函数分别绘制2个矩形。 使用 openGL 3、GLFW 和 GLEW。 我做了一个名为drawBox的函数来绘制一个矩形。我的问题是,如果我在主循环中多次调用它,则只显示绘制的最后一个矩形。
void drawBox(float x, float y, float w, float h, float r, float g, float b) {
GLfloat colours[15], vertices[10] = {
x, y,
x, y + h,
x + w, y + h,
x + w, y,
x, y
};
for(int i = 0; i < 5; ++i) {
colours[3*i] = (GLfloat)r;
colours[3*i + 1] = (GLfloat)g;
colours[3*i + 2] = (GLfloat)b;
}
GLuint buff[2];
glGenBuffers(2, buff);
glBindBuffer(GL_ARRAY_BUFFER, buff[1]);
glBufferData(GL_ARRAY_BUFFER, 15*sizeof(GLfloat), colours, GL_STREAM_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, buff[0]);
glBufferData(GL_ARRAY_BUFFER, 10*sizeof(GLfloat), vertices, GL_STREAM_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram(defaultShader);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 5);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDeleteBuffers(2, buff);
}
我需要更改什么才能使此函数显示矩形,而不管过去的绘制如何。 假设所有 z 指数均为 0,并且永远不会发生重叠。 假设我可能必须对drawBox进行drawCircle调用或drawTriangle或介于2次调用之间的东西。
您应该在所有draw*()
调用之前只调用glClear( GL_COLOR_BUFFER_BIT );
一次。
相关文章:
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 使用 std::list 存储顶点并使用 SFML 绘制它们
- MoveToEx和LineTo不使用存储在顶点(push_back)win32 c++中的MAKEPOINTS进行绘制
- 如何绘制具有随机顶点数量的多边形并旋转它们
- DirectX11只绘制填充索引和顶点缓冲区中的一半顶点
- 在版本 440 核心中绘制具有纹理的立方体的顶点和片段着色器应该是什么
- 在 OpenGL 中,我可以访问我在顶点着色器中绘制的缓冲区吗?
- 在 OpenGL 中,为什么除非我再次重新指定顶点属性,否则我的缓冲区对象不会绘制?
- 为什么我的 openGL 顶点缓冲区对象不会绘制任何内容?
- 未绘制每个顶点处的C++OpenGL颜色
- 如何将顶点位置传递给顶点着色器.我写了一个着色器,它不会在屏幕上绘制任何内容
- 仅显示 OpenGL 最近的顶点绘制
- OpenGL 3.2+ 围绕现有顶点绘制立方体
- 顶点未在 OpenGL 中绘制
- 绘制具有不同颜色顶点的OpenGL颜色矩形
- OpenGL顶点阵列,根据用户输入绘制
- 绘制如何合并两个顶点/收缩边的图形
- 尝试将着色器与 Win32 OpenGL 一起使用,但顶点着色器不绘制任何内容
- OpenGL只从顶点数组中绘制一些索引
- 如何在顶点缓冲区中以另一种颜色绘制顶点