Exemplo n.º 1
0
void BasicUsageScene::init()
{
    vao_.create();
    vao_.bind();

    prepareShaderProgram();
    prepareVertexBuffers();
}
Exemplo n.º 2
0
void Scene::initialize()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    // Récupération des fonctions d'OpenGL 3.1
    m_funcs = m_context->versionFunctions<QOpenGLFunctions_3_1>();

    if ( ! m_funcs )
    {
        qFatal("Requires OpenGL >= 3.1");
        exit(1);
    }

    m_funcs->initializeOpenGLFunctions();

    // Charge, compile et link le Vertex et Fragment Shader
    prepareShaderProgram();

    // Initialisation des buffers
    prepareVertexBuffers();
    prepareIndexBuffer();
}
void TerrainTessellationScene::initialise()
{
    m_material = MaterialPtr( new Material );
    m_material->setShaders( ":/shaders/terraintessellation.vert",
                            ":/shaders/terraintessellation.tcs",
                            ":/shaders/terraintessellation.tes",
                            ":/shaders/terraintessellation.geom",
                            ":/shaders/terraintessellation.frag" );

    QImage heightMapImage( "../../../opengl/assets/textures/heightmap-1024x1024.png" );
    TexturePtr heightMap( new Texture );
    heightMap->create();
    heightMap->bind();
    heightMap->setImage( heightMapImage );

    SamplerPtr sampler( new Sampler );
    sampler->create();
    sampler->setMinificationFilter( GL_LINEAR );
    sampler->setMagnificationFilter( GL_LINEAR );
    sampler->setWrapMode( Sampler::DirectionS, GL_CLAMP_TO_EDGE );
    sampler->setWrapMode( Sampler::DirectionT, GL_CLAMP_TO_EDGE );

    m_material->setTextureUnitConfiguration( 0, heightMap, sampler, QByteArrayLiteral( "heightMap" ) );


    // Create a renderable object
    prepareVertexBuffers( heightMapImage.size() );
    prepareVertexArrayObject();

    // Enable depth testing
    glEnable( GL_DEPTH_TEST );

    m_funcs = m_context->versionFunctions<QOpenGLFunctions_4_0_Core>();
    if ( !m_funcs )
        qFatal( "Could not obtain required OpenGL context version" );
    m_funcs->initializeOpenGLFunctions();
}
void InstancedHistogramScene::initialise()
{
#if !defined(Q_OS_MAC)
    // Resolve the OpenGL 3.3 functions that we need for instanced rendering
    m_funcs = m_context->versionFunctions<QOpenGLFunctions_3_3_Compatibility>();
    if ( !m_funcs )
        qFatal( "Could not obtain required OpenGL context version" );
    m_funcs->initializeOpenGLFunctions();
#else
    m_funcs = m_context->versionFunctions<QOpenGLFunctions_2_1>();
    if ( !m_funcs )
        qFatal( "Could not obtain required OpenGL context version" );
    m_funcs->initializeOpenGLFunctions();

    m_instanceFuncs = new QOpenGLExtension_ARB_instanced_arrays();
    m_instanceFuncs->initializeOpenGLFunctions();

    m_drawInstanced = new QOpenGLExtension_ARB_draw_instanced();
    m_drawInstanced->initializeOpenGLFunctions();
#endif

    // Create a Material we can use to operate on instanced geometry
    MaterialPtr material( new Material );
#if !defined(Q_OS_MAC)
    material->setShaders( ":/shaders/instancedhistogram.vert",
                          ":/shaders/instancedhistogram.frag" );
#else
    material->setShaders( ":/shaders/instancedhistogram_2_1.vert",
                          ":/shaders/instancedhistogram_2_1.frag" );
#endif

    // Create a cube
    m_cube = new Cube;
    m_cube->setMaterial( material );
    m_cube->create();

    // Create a VBO ready to hold our data
    prepareVertexBuffers();

    // Tell OpenGL how to pass the data VBOs to the shader program
    prepareVertexArrayObject();

    // Use a sphere for the background - we'll render the scene
    // inside the sphere
    MaterialPtr noiseMaterial( new Material );
#if !defined(Q_OS_MAC)
    noiseMaterial->setShaders( ":/shaders/noise.vert",
                               ":/shaders/noise.frag" );
#else
    noiseMaterial->setShaders( ":/shaders/noise_2_1.vert",
                               ":/shaders/noise_2_1.frag" );
#endif

    m_sphere = new Sphere;
    m_sphere->setMaterial( noiseMaterial );
    m_sphere->create();

    m_sphereModelMatrix.setToIdentity();
    m_sphereModelMatrix.scale( 30.0f );

    // Enable depth testing to prevent artifacts
    glEnable( GL_DEPTH_TEST );
}
Exemplo n.º 5
0
vector< shared_ptr<ModelData> > ModelLoader::loadModel(const string& filename,
                                                       const QOpenGLShaderProgramPtr& shader)
{
    m_shader = shader;

    Assimp::Importer Importer;
    const aiScene* scene = Importer.ReadFile(filename.c_str(),
                                             aiProcessPreset_TargetRealtime_MaxQuality |
                                             aiProcess_FlipUVs);

    if(scene == nullptr)
    {
        qFatal(qPrintable(QObject::tr("Error parsing : %1 -> %2").arg(filename.c_str()).arg(Importer.GetErrorString())));
        exit(1);
    }
    else if(scene->HasTextures())
    {
        qFatal("Support for meshes with embedded textures is not implemented");
        exit(1);
    }

    vector<shared_ptr<ModelData>> modelData = vector<shared_ptr<ModelData>>();
    modelData.resize(scene->mNumMeshes);

    qDebug() << "Model has" << modelData.size() << "meshes";

    unsigned int numVertices = 0;
    unsigned int numIndices  = 0;

    for(unsigned int i = 0; i < modelData.size(); i++)
    {
        numVertices += scene->mMeshes[i]->mNumVertices;
        numIndices  += scene->mMeshes[i]->mNumFaces * 3;
    }

    m_positions.reserve(numVertices);
    m_colors.reserve(numVertices);
    m_normals.reserve(numVertices);
    m_texCoords.reserve(numVertices);
    m_tangents.reserve(numVertices);
    m_indices.reserve(numIndices);

    numVertices = 0;
    numIndices  = 0;

    for(unsigned int i = 0; i < modelData.size(); i++)
    {
        modelData[i] = make_shared<ModelData>();

        modelData[i]->meshData     = loadMesh(i, numVertices, numIndices, scene->mMeshes[i]);
        modelData[i]->textureData  = loadTexture(filename, scene->mMaterials[scene->mMeshes[i]->mMaterialIndex]);
        modelData[i]->materialData = loadMaterial(i, scene->mMaterials[scene->mMeshes[i]->mMaterialIndex]);

        numVertices += scene->mMeshes[i]->mNumVertices;
        numIndices  += scene->mMeshes[i]->mNumFaces * 3;
    }

    prepareVertexBuffers();

    qDebug() << "Model has" << numVertices << "vertices";

    return modelData;
}
Exemplo n.º 6
0
void SimpleTextureScene::initialise()
{
    m_funcs = m_context->functions();
    m_funcs->initializeOpenGLFunctions();

    // mplayer's output is definitely easier to parse than ffmpeg's...
    QProcess movieFileParameters;
    movieFileParameters.start("mplayer",
                              QStringList()
                              << "-identify"
                              << "-vo" << "null"
                              << "-ao" << "null"
                              << "-frames" << "0"
                              << "-vc" << "null"
                              << "--"
                              << m_movieFile,
                              QIODevice::ReadOnly);
    movieFileParameters.waitForFinished();
    QString mplayerOutput = QString::fromLocal8Bit(movieFileParameters.readAllStandardOutput());

    QRegularExpression widthRe("^ID_VIDEO_WIDTH=(.*)", QRegularExpression::MultilineOption);
    QRegularExpressionMatch widthMatch = widthRe.match(mplayerOutput);
    if (widthMatch.hasMatch())
        m_frameSize.setWidth(widthMatch.captured(1).toInt());

    QRegularExpression heightRe("^ID_VIDEO_HEIGHT=(.*)", QRegularExpression::MultilineOption);
    QRegularExpressionMatch heightMatch = heightRe.match(mplayerOutput);
    if (heightMatch.hasMatch())
        m_frameSize.setHeight(heightMatch.captured(1).toInt());

    if (m_frameSize.width() <= 0 || m_frameSize.height() <= 0)
        qFatal("Cannot determine the input file frame size!");

    qDebug() << "Got frame size:" << m_frameSize;

    // Set the clear color to black
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

    // Prepare a complete shader program...
    prepareShaderProgram( ":/shaders/simpletexture.vert",
                          ":/shaders/simpletexture.frag" );

    // Prepare the vertex, texture and index buffers
    prepareVertexBuffers();

    // Prepare the VAO
    prepareVertexArrayObject();

    // Prepare the texture data itself (textures and pixel unpack buffer objects)
    prepareTexture();

    // Link texture unit 0 to the "ySampler" variable in the fragment shader
    m_shader.setUniformValue( "ySampler", 0 );

    // Link texture unit 1 to the "uvSampler" variable in the fragment shader
    m_shader.setUniformValue( "uvSampler", 1 );

    m_videoDecoder.start("ffmpeg",
                         QStringList()
                         << "-i" << m_movieFile
                         << "-f" << "rawvideo"
                         << "-vcodec" << "rawvideo"
                         << "-pix_fmt" << "nv12"
                         << "-an"
                         << "-ss" << "180" // jump to 3m
                         << "-",
                         QIODevice::ReadOnly);

    m_videoDecoder.closeWriteChannel();
    m_videoDecoder.closeReadChannel(QProcess::StandardError);
}