opengl中2d和3d线的区别

时间:2013-06-27 23:20:04

标签: opengl 3d 2d

我正在尝试使用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);
}

1 个答案:

答案 0 :(得分:1)

  

与绘制2条协调线相比,我获得了不同的线

glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
                                 ^ whaaaat?

在你的“3D”情况下,你告诉OpenGL你的线坐标是这样的:

( -1.0f, 0.2f )
( 0.0f, 0.0f )

尝试在2来电中将3更改为glVertexAttribPointer()