OpenGL转换不会生效

OpenGL translations do not take effect

本文关键字:转换 OpenGL      更新时间:2023-10-16

这些问题可能很基本,请耐心听我说。

我已经构建了一个非常小的Qt程序来处理OpenGL,并尝试学习渲染方法,并且已经有了一个彩色三角形(扩展了本教程http://qt-project.org/wiki/How_to_use_OpenGL_Core_Profile_with_Qt)。我现在的问题是,我试图应用于三角形的任何变换都无济于事。最重要的是,将参数更改为glOrtho似乎对显示没有任何影响,而且我不知道将参数设置为glViewport应该如何工作:使它们变小似乎会使3D世界的渲染区域变大。

代码:

这是我的顶点/索引数组(vec3位置,vec3颜色)

static GLfloat tri[] = { -0.5f, -0.5f, 0.0f,  0.0f, 0.0f, 1.0f,
                        0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,
                        0.0f,  0.5f, 0.0f,  1.0f, 0.0f, 0.0f };
static GLuint tri_ind[] = { 0, 1, 2 };
static int SIZEOF_TRI = 2 * 3 * 3 * sizeof(GLfloat);
static int SIZEOF_TRI_IND = 3 * sizeof(GLuint);
static int NUM_TRI_VERTS = 3;

我的OpenGL小部件将Qt的缓冲区对象用于顶点和索引缓冲区:

class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    GLWidget( const QGLFormat& format, QWidget* parent = 0 );
protected:
    virtual void initializeGL();
    virtual void resizeGL( int w, int h );
    virtual void paintGL();
    virtual void keyPressEvent( QKeyEvent* e );
private:
    bool prepareShaderProgram( const QString& vertexShaderPath,
                               const QString& fragmentShaderPath );
    QGLShaderProgram m_shader;
    QGLBuffer m_vertexBuffer;
    QGLBuffer m_indexBuffer;
};

构造函数初始化缓冲区:

GLWidget::GLWidget( const QGLFormat& format, QWidget* parent )
    : QGLWidget( format, parent ),
      m_vertexBuffer( QGLBuffer::VertexBuffer ),
      m_indexBuffer( QGLBuffer::IndexBuffer )
{
}

initializeGL初始化其余部分:

void GLWidget::initializeGL()
{
    QGLFormat glFormat = QGLWidget::format();
    if ( !glFormat.sampleBuffers() )
        qWarning() << "Could not enable sample buffers";
    // Set the clear color to black
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    // Prepare a complete shader program...
    QString vert = QCoreApplication::applicationDirPath() + "/simple.vert";
    QString frag = QCoreApplication::applicationDirPath() + "/simple.frag";
    if ( !prepareShaderProgram( vert, frag ) )
        return;
    // Bind the shader program so that we can associate variables from
    // our application to the shaders
    if ( !m_shader.bind() )
    {
        qWarning() << "Could not bind shader program to context";
        return;
    }
    // We need us some vertex data. Start simple with a triangle ;-)
    GLfloat* points = tri;
    m_vertexBuffer.create();
    m_vertexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
    if ( !m_vertexBuffer.bind() )
    {
        qWarning() << "Could not bind vertex buffer to the context";
        return;
    }
    m_vertexBuffer.allocate( points, SIZEOF_TRI );
    // Indices
    GLuint* indices = tri_ind;
    m_indexBuffer.create();
    m_indexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
    if ( !m_indexBuffer.bind() )
    {
        qWarning() << "Could not bind vertex buffer to the context";
        return;
    }
    m_indexBuffer.allocate( indices, SIZEOF_TRI_IND );

    // Enable the "vertex" attribute to bind it to our currently bound
    // vertex buffer.
    m_shader.setAttributeBuffer( "vertex", GL_FLOAT, 0, 3, 6 * sizeof(GLfloat) );
    m_shader.enableAttributeArray( "vertex" );
    m_shader.setAttributeBuffer( "color", GL_FLOAT, 3 * sizeof(GLfloat), 3, 6 * sizeof(GLfloat) );
    m_shader.enableAttributeArray( "color" );
}

paintGL进行平移并绘制顶点:

void GLWidget::paintGL()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    // Draw stuff
    glLoadIdentity();
    glTranslatef(10.0, 10.0, 0.0);
    glDrawArrays( GL_TRIANGLES, 0, NUM_TRI_VERTS );
}

这个三角形看起来和我添加翻译之前完全一样。

此外,在resizeGL中(调整小部件的大小时),将参数更改为glOrtho对显示没有影响。在+-5和+-10之间没有差异。如果我将glViewport的宽度和高度分别设置为100,然后分别设置为200,则当值为200时,三角形(在左下角)看起来比值为100时更大。我本以为视口大小越大,里面的对象就越小,但也许我错过了什么。

void GLWidget::resizeGL( int w, int h )
{
    // Set the viewport to window dimensions
    glViewport( 0, 0, w, qMax( h, 1 ) );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0, 10.0, -10.0, 10.0, -5.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

有人能给我一些正确的方向吗?

谢谢。

编辑:顶点着色器:

#version 330
in vec3 vertex;
in vec3 color;
out vec3 Color;
void main( void )
{
    Color = color;
    gl_Position = vec4(vertex, 1.0);
}

片段着色器:

#version 330
 
layout(location = 0, index = 0) out vec4 fragColor;
in vec3 Color;
 
void main( void )
{
    fragColor = vec4(Color, 1.0);
}

编写着色器是为了在OpenGL-3.3或更高版本的核心配置文件中工作。您使用的是不推荐使用的固定函数管道方法(glMatrixMode、glLoadIdentity、glTranslate等),这些方法在核心配置文件中不起作用。使用像GLM、Eigen或linmath.h这样的实矩阵数学库来计算矩阵,然后将它们传递给着色器统一。