我想用z座標作爲深度,這樣當我繪製兩個物體時,一個可以出現在另一個的後面。我試圖用拉3個座標線,但它失敗了,相比2個協調行(我把Z座標爲0.0fopengl中2d和3d線的區別
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);
}
你的繪圖代碼是什麼樣的? –