简体   繁体   English

使用OpenGL 3.3和Qt 5.6创建相机

[英]Creating a Camera with OpenGL 3.3 and Qt 5.6

I'm quite new to OpenGL and Qt and I've been so far so good with it. 我对OpenGL和Qt还是很陌生,到目前为止我对它一直很好。 Rendering a simple triangle with OpenGL 3.3 wasn't that hard but integrating a Camera is. 使用OpenGL 3.3渲染一个简单的三角形并不难,但集成Camera则很困难。 For some reason my triangle dissappears!? 由于某种原因,我的三角形消失了! Did I get the math wrong to calculate the matrices? 在计算矩阵时数学是否出错? I used these 2 tutorials as a starting point: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ 我以以下两个教程为起点: http : //www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

https://wiki.qt.io/How_to_use_OpenGL_Core_Profile_with_Qt https://wiki.qt.io/How_to_use_OpenGL_Core_Profile_with_Qt

My code(the most important parts only): 我的代码(仅最重要的部分):

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 );

    // Prepare a complete shader program…
    if ( !prepareShaderProgram( "A:/Projekte/Qt Workspace/Projects/CGOpenGL/simple.vert", "A:/Projekte/Qt Workspace/Projects/CGOpenGL/simple.frag" ) )
        return;

    /////Matrix calculations/////
    projection.perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    view.lookAt(QVector3D(4,3,3),QVector3D(0,0,0),QVector3D(0,1,0));
    model = QMatrix4x4();
    //////////


    // We need us some vertex data. Start simple with a triangle ;-)
    GLfloat points[] = {-0.5f, -0.5f, 0.0f,
                        1.0f, 0.5f, -0.5f,
                        0.0f, 1.0f, 0.0f,
                        0.5f, 0.0f, 1.0f};
    vertexBuffer.create();
    vertexBuffer.setUsagePattern( QGLBuffer::StaticDraw );
    if ( !vertexBuffer.bind() )
    {
        qWarning() << "Could not bind vertex buffer to the context";
        return;
    }
    vertexBuffer.allocate( points, 3 * 4 * sizeof( float ) );

    // Bind the shader program so that we can associate variables from
    // our application to the shaders
    if ( !shader.bind() )
    {
        qWarning() << "Could not bind shader program to context";
        return;
    }

    // Enable the "vertex" attribute to bind it to our currently bound
    // vertex buffer.
    shader.setAttributeBuffer( "vertex", GL_FLOAT, 0, 4 );
    shader.enableAttributeArray( "vertex" );
}

void GLWidget::paintGL()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Set the MVP variable in the shader
    shader.setUniformValue("MVP",projection * view * model);

    // Draw stuff
    glDrawArrays( GL_TRIANGLES, 0, 3 );
}

Vertex Shader: 顶点着色器:

#version 330

layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;

void main( void )
{
   gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
}

you made a call to 您打电话给

shader.enableAttributeArray( "vertex" );

yet named it: 尚未命名:

vertexPosition_modelspace

in the shader, you need to change the names to be consistent. 在着色器中,您需要更改名称以使其一致。

Try renaming the variable in the shader to "vertex" 尝试将着色器中的变量重命名为“顶点”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM