void HeightMap3DRenderer::initialize()
{
    //initialize opengl stuff

    //TODO initialize opengl functions? glewish?
    recompileProgram();
    //prepare textures?
    prepareVertexBuffer();
    prepareVertexArrayObject();

    m_initialized = true;
}
/** @brief Iterate Functional Test cases.
 *
 *  @return Iteration result.
 */
tcu::TestCase::IterateResult TextureCubeMapArrayETC2Support::iterate(void)
{
	prepareFramebuffer();
	prepareProgram();
	prepareVertexArrayObject();
	prepareTexture();
	draw();

	if (isRenderedImageValid())
		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
	else
		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");

	clean();
	return STOP;
}
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 );
}
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);
}