Example #1
0
static void uploadTextures(QOpenGLContext* context, SharedFrame& frame, GLuint texture[])
{
    int width = frame.get_image_width();
    int height = frame.get_image_height();
    const uint8_t* image = frame.get_image();
    QOpenGLFunctions* f = context->functions();

    // Upload each plane of YUV to a texture.
    if (texture[0])
        f->glDeleteTextures(3, texture);
    check_error(f);
    f->glGenTextures(3, texture);
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[0]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[1]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0,
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height);
    check_error(f);

    f->glBindTexture  (GL_TEXTURE_2D, texture[2]);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    check_error(f);
    f->glTexImage2D   (GL_TEXTURE_2D, 0, GL_LUMINANCE, width/2, height/2, 0,
                    GL_LUMINANCE, GL_UNSIGNED_BYTE, image + width * height + width/2 * height/2);
    check_error(f);
}
/**
 * @brief initShader Initiate a shader.
 * @return the compiled shader program or 0 if the shader file does not exist
 */
GLuint loadShader(string shader_name)
{
    QOpenGLFunctions* qf = new QOpenGLFunctions(QOpenGLContext::currentContext());
    char* error;

    QFile* file = new QFile((":/shader/" + shader_name + ".vsh").c_str());
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
        return 0;

    string code = "";
    QTextStream* in = new QTextStream(file);
    while (!in->atEnd()) {
        QString line = in->readLine();
        code = code + line.toStdString() + "\n";
    }
    file->close();

    GLuint vs = qf->glCreateShader(GL_VERTEX_SHADER);
    const char* v_str = code.c_str();
    qf->glShaderSource(vs, 1, &v_str, NULL);
    qf->glCompileShader(vs);
    qf->glGetShaderInfoLog(vs, NULL, NULL, error);
    cout << error << "\n";

    code = "";

    delete file;
    delete in;
    file = new QFile((":/shader/" + shader_name + ".fsh").c_str());
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
        return 0;

    in = new QTextStream(file);
    while (!in->atEnd()) {
        QString line = in->readLine();
        code = code + line.toStdString() + "\n";
    }
    file->close();

    GLuint fs = qf->glCreateShader(GL_FRAGMENT_SHADER);
    const char *f_str = code.c_str();
    qf->glShaderSource(fs, 1, &f_str, NULL);
    qf->glCompileShader(fs);
    qf->glGetShaderInfoLog(fs, NULL, NULL, error);
    cout << error << endl;

    GLuint program = qf->glCreateProgram();
    qf->glAttachShader(program, vs);
    qf->glAttachShader(program, fs);
    qf->glLinkProgram(program);
    qf->glGetProgramInfoLog(program, NULL, NULL, error);
    cout << error << endl;
    qf->glUseProgram(program);

    return program;
}