[英]Drawing from two array buffers in OpenGL
I am trying to create a line graph of a cosine curve along with the axes. 我正在尝试创建余弦曲线和轴的折线图。 The problem I am having is that the line_strip will continue drawing the axes after it has drawn the line (ie i expected the line to draw, stop, then for the axes to start drawing separately. What happens now is the line draws the line and the axes all as one line_strip).
我遇到的问题是line_strip将在绘制线后继续绘制轴(即我预期线要绘制,停止,然后轴开始分别绘制。现在发生的是线绘制线和轴全部作为一个line_strip)。 An even stranger thing which i down't understand is that even if i remove the lines:
一个更奇怪的事情,我不明白,即使我删除线:
//Draw axes
glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, 2); // x axis
glDrawArrays(GL_LINE_STRIP, 2, 2); // y axis
glDrawArrays(GL_LINE_STRIP, 4, 2); // z axis
glDisableVertexAttribArray(0);
which i thought would stop drawing the axes altogether, they are still drawn!! 我认为它会完全停止绘制轴,它们仍然被绘制!
The relevant code is shown below: 相关代码如下所示:
//Vertices for the axes in 3 dimesions
const float axesPositions[] = {
-1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -1.0, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, -1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
//calculates cosine line vertices
std::vector<float> fillPositions()
{
std::vector<float> arr;
for (float x = -1.0f; x < 1.0f; x += 0.01f)
{
float y;
if (x == 0) y = 1; //divide by zero check
y = cos(x);
arr.push_back(x);
arr.push_back(y);
arr.push_back(0.0f);
arr.push_back(1.0f);
}
return arr;
}
GLuint positionBufferObject;
GLuint axesBufferObject;
GLuint vao;
std::vector<float> linePositions;
void InitializeVertexBuffers()
{
//genereate line graph vertex buffer
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * linePositions.size(), &linePositions[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//generate axes vertex buffer
glGenBuffers(1, &axesBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(axesPositions), axesPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
void init()
{
InitializeProgram();
linePositions = fillPositions();
InitializeVertexBuffers();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}
//Called to update the display.
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(theProgram);
// Draw line
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, linePositions.size());
glDisableVertexAttribArray(0);
//Draw axes
glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, 2); // x axis
glDrawArrays(GL_LINE_STRIP, 2, 2); // y axis
glDrawArrays(GL_LINE_STRIP, 4, 2); // z axis
glDisableVertexAttribArray(0);
glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();
}
linePositions.size() is the number of floats in your array, not the number of vertices you want to draw. linePositions.size()是数组中的浮点数,而不是要绘制的顶点数。
Change this line: 改变这一行:
glDrawArrays(GL_LINE_STRIP, 0, linePositions.size());
to this: 对此:
glDrawArrays(GL_LINE_STRIP, 0, linePositions.size()/4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.