我正在尝试使用 z 坐标作为深度,这样当我绘制两个对象时,一个对象可以出现在另一个对象的后面。我尝试使用 3 个坐标绘制线,但失败了,与绘制 2 个协调线相比,我得到不同的线(我将 z 坐标设置为 0.0f
GLfloat crossLine[] = {
-1.0f, 0.2f, // Vertex 1 (X, Y)
0.0f, 0.2f, // Vertex 2 (X, Y)
};
和
GLfloat crossLine[] = {
-1.0f, 0.2f, 0.0f, // Vertex 1 (X, Y, Z)
0.0f, 0.2f, 0.0f // Vertex 2 (X, Y, Z)
};
这是快照:http: //img4.imageshack.us/img4/78/thwc.png
这是代码:
void myWidget::initialize()
{
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_mycolor = m_program->uniformLocation("mycolor");
m_source = m_program->uniformLocation("sourcePoint");
m_matrixUniform = m_program->uniformLocation("matrix");
glViewport(0, 0, width(), height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60, 4.0/3.0, 0.1, 100.0);
matrix.translate(0, 0, -2);
matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);
m_program->setUniformValue(m_matrixUniform, matrix);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc( GL_LEQUAL );
glEnable(GL_COLOR);
}
void myWidget::drawLine(GLfloat vertices[], GLint verticesNumber, GLfloat color[], GLfloat z)
{
glUniform4fv(m_mycolor, 1, color);
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays( GL_LINE_STRIP , 0, verticesNumber);
}