首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用OpenGL 3.3和Qt5.6制作相机

用OpenGL 3.3和Qt5.6制作相机
EN

Stack Overflow用户
提问于 2016-03-26 04:47:26
回答 1查看 1.7K关注 0票数 0

我对OpenGL和Qt非常陌生,到目前为止我一直对此很在行。用OpenGL 3.3绘制一个简单的三角形并不困难,但是集成一个相机是很困难的。不知为什么,我的三角消失了!?我计算矩阵的数学错了吗?我使用这两个教程作为起点:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

Qt

我的代码(只有最重要的部分):

代码语言:javascript
复制
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 );
}

顶点着色器:

代码语言:javascript
复制
#version 330

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-26 12:44:33

你打了个电话给

代码语言:javascript
复制
shader.enableAttributeArray( "vertex" );

但它的名字是:

代码语言:javascript
复制
vertexPosition_modelspace

在着色器中,您需要更改名称以保持一致。

尝试将着色器中的变量重命名为“顶点”。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36231719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档