Exemplo n.º 1
0
void Painter::paint_3_4(float timef)
{
    paint_3_3_shadowmap(timef);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, m_depthTex);

    glActiveTexture(GL_TEXTURE0);

    QOpenGLShaderProgram * program(m_programs[PaintMode1]);


    // Task_3_3 - ToDo Begin
    QVector3D light = m_light.normalized();
    QMatrix4x4 L;
//    L.lookAt(light, center ,up);
L.perspective(camera()->fovy(), camera()->viewport().width()/static_cast<float>(camera()->viewport().height()), camera()->zNear(), camera()->zFar());
    L.lookAt(m_light,QVector3D(0.0,0.0,0.0),QVector3D(0.0,0.1,0.0));
    // QMatrix4x4 ...

    program->bind();
    program->setUniformValue("light", m_light);
    program->setUniformValue("viewProjection", L);
    program->release();

    // Task_3_3 - ToDo End


    paint_3_1_scene(true, timef);
    paint_3_2_label(camera()->viewProjection(), timef);

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, 0);
    glActiveTexture(GL_TEXTURE0);
}
Exemplo n.º 2
0
void Widget::paintGL()
{
	glClearColor(.3, .4, .5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

	myShader.bind();

	QMatrix4x4 modelview;
	modelview.perspective(70, 1.0 * this->width() / this->height(), 0.1, 100);

	QVector3D to = QVector3D(sin(yaw) * cos(pitch), cos(yaw) * cos(pitch), sin(pitch));
    move();
	modelview.lookAt(from, from + to, QVector3D(0, 0, 1));

	myShader.setUniformValue("mvp", modelview);
	groundMesh.draw(this, &myShader);

	modelview.scale(2);
	myShader.setUniformValue("mvp", modelview);
	torusMesh.draw(this, &myShader);

    myShader.release();

	// TODO: use QOpenGLDebugLogger
}
Exemplo n.º 3
0
/*!
    Returns the transformation matrix to apply to the projection matrix
    to present the scene as viewed from the camera position.

    The \a aspectRatio specifies the aspect ratio of the window the
    camera view is being displayed in.  An \a aspectRatio of 1 indicates that
    the window is square.  An \a aspectRatio greater than 1 indicates that
    the window is wider than it is high.  An \a aspectRatio less than 1
    indicates that the window is higher than it is wide.

    \sa apply(), modelViewMatrix()
*/
QMatrix4x4 Camera::projectionMatrix(qreal aspectRatio) const
{
    Q_D(const Camera);
    QMatrix4x4 m;
    if (!d->adjustForAspectRatio)
        aspectRatio = 1.0f;
    if (d->screenRotation != 0) {
        m.rotate((qreal)(d->screenRotation), 0.0f, 0.0f, 1.0f);
        if (d->screenRotation == 90 || d->screenRotation == 270) {
            if (aspectRatio != 0.0f)
                aspectRatio = 1.0f / aspectRatio;
        }
    }
    if (d->projectionType == Perspective && d->fieldOfView != 0.0f) {
        m.perspective(d->fieldOfView, aspectRatio,
                      d->nearPlane, d->farPlane);
    } else {
        qreal halfWidth = d->viewSize.width() / 2.0f;
        qreal halfHeight = d->viewSize.height() / 2.0f;
        if (aspectRatio > 1.0f) {
            halfWidth *= aspectRatio;
        } else if (aspectRatio > 0.0f && aspectRatio < 1.0f) {
            halfHeight /= aspectRatio;
        }
        if (d->projectionType == Perspective) {
            m.frustum(-halfWidth, halfWidth, -halfHeight, halfHeight,
                      d->nearPlane, d->farPlane);
        } else {
            m.ortho(-halfWidth, halfWidth, -halfHeight, halfHeight,
                    d->nearPlane, d->farPlane);
        }
    }
    return m;
}
Exemplo n.º 4
0
void Dragon2Widget::paintGL()
{
    qglClearColor(Qt::white);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_ALPHA_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // use the OpenGL shader program for painting
    glUseProgram(_program);

    // set model matrix
    glUniformMatrix4fv(_modelMatrixLocation, 1, GL_FALSE, _modelMatrix.data());

    // set view matrix
    QMatrix4x4 viewMatrix;
    viewMatrix.setToIdentity();
    viewMatrix.lookAt(QVector3D(0, 0, -1000), QVector3D(0, 0, 0), QVector3D(0, -1, 0));
    glUniformMatrix4fv(_viewMatrixLocation, 1, GL_FALSE, viewMatrix.data());

    // set projection matrix
    QMatrix4x4 projectionMatrix;
    projectionMatrix.setToIdentity();
    projectionMatrix.perspective(30, (float)width()/height(), 0.01f, 1e5f);
    glUniformMatrix4fv(_projectionMatrixLocation, 1, GL_FALSE, projectionMatrix.data());

    // bind ArrayBuffer to _vertBuffer
    glBindBuffer(GL_ARRAY_BUFFER, _vertBuffer);
    // enable vertex attribute "position" (bound to 0 already)
    glEnableVertexAttribArray(0); 
    // set the data of vertex attribute "position" using current ArrayBuffer
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(_vertices.first()), 0);
    // enable vertex attribute "normal" (bound to 1 already)
    glEnableVertexAttribArray(1);
    // set the data of vertex attribute "normal" using current ArrayBuffer
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(_vertices.first()), (void*)(3 * sizeof(float)));

    // bind ElementArrayBuffer to _triangleIndicesBuffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _triangleIndicesBuffer);
    // draw mesh using the indices stored in ElementArrayBuffer
    glDrawElements(GL_TRIANGLES, _triangleIndices.size(), GL_UNSIGNED_INT, 0);

    // disable vertex attributes
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    // unbind buffers
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // restore states
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_ALPHA_TEST);
    glDisable(GL_BLEND);
}
void BaseScene::setViewport(QSize viewport)
{
    glViewport(0, 0, viewport.width(), viewport.height());

    QMatrix4x4 proj;
    proj.perspective(90, float(viewport.width()) / viewport.height(), Z_NEAR, Z_FAR);
    GLHelper::setProjectionMatrix(proj);

    m_camera.setViewport(viewport);
    GLHelper::dumpIfError();
}
Exemplo n.º 6
0
void Tutorial03::initializeGL(void)
{
  // initialize OpenGL
  initializeOpenGLFunctions();

  bool success;
  // load and compile vertex shader
  success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/SimpleTransform.vert");

  // load and compile fragment shader
  success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/SingleColor.frag");

  // link the shader program
  shaderProgram.link();

  GLuint programID = shaderProgram.programId();

  // Get a handle for our buffers
  vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");

  // Get a handle to the colors
  color_location = glGetAttribLocation(programID, "v_color");

  // Get a handle for our "MVP" uniform
  MatrixID = glGetUniformLocation(programID, "MVP");

  // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  QMatrix4x4 projection;
  projection.perspective(45.0, 4.0/3.0, 0.1, 100.0);

  // Camera matrix
  QMatrix4x4 view;
  view.lookAt( QVector3D(4,3,3), // Camera is at (4,3,3), in World Space
               QVector3D(0,0,0), // and looks at the origin
               QVector3D(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
             );
  // Model matrix : an identity matrix (model will be at the origin)
  QMatrix4x4 model;
  model.setToIdentity();

  // Our ModelViewProjection : multiplication of our 3 matrices
  MVP =  projection * view * model; // Remember, matrix multiplication is the other way around

  glGenBuffers(1, &vertexbuffer);
  glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

  glGenBuffers(1, &colorbuffer);
  glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_colors), triangle_colors, GL_STATIC_DRAW);

  glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
}
Exemplo n.º 7
0
void OpenGLWidgetPrivate::render()
{
    const qreal retinaScale = q->devicePixelRatio();
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    matrix.translate(0, 0, -2);
    const qreal angle = 100.0f * m_frame / 30;
    matrix.rotate(angle, m_rotAxis);

    m_program->setUniformValue(m_matrixUniform, matrix);

    GLfloat vertices[] = {
        0.0f, 0.707f,
        -0.5f, -0.5f,
        0.5f, -0.5f
    };

    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };

    glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();

    ++m_frame;

    if (m_interval <= 0)
        q->update();
}
Exemplo n.º 8
0
void GLWidget::render_OffScreen(){
    if ( NULL == m_render_offscreen) {
        m_render_offscreen =  new QGLFramebufferObject(this->Width, this->Height,QGLFramebufferObject::Depth, GL_TEXTURE_2D);
    }
    if (!m_render_offscreen->isValid()) qDebug() << "fbo m_render_offscreen  isn't valid";
    if (!m_render_offscreen->bind()) qDebug() << "fbo m_render_offscreen is not bind";

    if (NULL!= scene &&  NULL!=this->scene->getBacgroundColor()){
        glClearColor(this->scene->getBacgroundColor()->x(), this->scene->getBacgroundColor()->y(), this->scene->getBacgroundColor()->z(), this->scene->getBacgroundColor()->w() );
    }
    else
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);

    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    QMatrix4x4 projection;
    projection.perspective(camera->getFOV(), 1.0 * width() / height(), camera->getNearPlane(), camera->getFarPlane());

    QMatrix4x4 model;
    model.rotate(camera->getXRot() , 1.0, 0.0, 0.0);
    model.rotate(camera->getYRot() , 0.0,-1.0, 0.0);

    QMatrix4x4 view;
    view.translate(0.0, 0.0, -camera->getDistance());

    if (NULL != this->scene){
         scene->draw(camera, projection   , view  , model);
     }



    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glFlush();

    m_render_offscreen->release();

}
Exemplo n.º 9
0
void OpenGLWindow::render()
{
    const qreal retinaScale = devicePixelRatio();

    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT);

    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    matrix.translate(0, 0, -2);
    matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);

    m_program->setUniformValue(m_matrixUniform, matrix);

    GLfloat vertices[] = {
        0.0f, 0.707f,
        -0.5f, -0.5f,
        0.5f, -0.5f
    };

    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };

    glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();

    ++m_frame;
}
Exemplo n.º 10
0
void DragonWidget::paintGL()
{
    makeCurrent();

    qglClearColor(Qt::white);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_ALPHA_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glShadeModel(GL_SMOOTH);
    
    // set model-view matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    QMatrix4x4 viewMatrix;
    viewMatrix.lookAt(QVector3D(0, 0, -1000), QVector3D(0, 0, 0), QVector3D(0, -1, 0));
    glMultMatrixf(viewMatrix.data());
    glMultMatrixf(_modelMatrix.data()); // now model-view matrix = lookAt(...) * _modelMatrix

    // set projection matrix 
    // (projection matrix is often set in resizeGL(width, height) functions, 
    //  since it only relies on the size of the window in most cases)
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    QMatrix4x4 projectionMatrix;
    projectionMatrix.perspective(30.0f, (float)width() / height(), 0.01f, 1e5f);
    glMultMatrixf(projectionMatrix.data());

    // draw mesh
    glBegin(GL_TRIANGLES);
    for(int vid : _triangleIndices)
    {
        glColor3fv(&(_vertices[vid].normal[0]));
        glVertex3fv(&(_vertices[vid].position[0]));
    }
    glEnd();    


    glDisable(GL_DEPTH_TEST);
    glDisable(GL_ALPHA_TEST);
    glDisable(GL_BLEND);
} 
Exemplo n.º 11
0
void TerrainGenerator::render()
{
    const qreal retinaScale = devicePixelRatio();
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT);

    if(_wireFrame)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 16.0f/9.0f, 0.1f, 100.0f);
    matrix.translate(_camX, _camY, _camZ);
    matrix.rotate(0, 0, 0, 0);
    matrix.rotate(100.0f * _rotX / screen()->refreshRate(), 0, 1, 0);

    m_program->setUniformValue(m_matrixUniform, matrix);

    //on remplie...
    //Num du tableau, nb coordonnée par vertex (2D = 2), type, normalized, ?, pointeur sur données
    glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, _map.constData());
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, _color.constData());

    //... puis active le tableau vertex attrib...
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //on affiche le triangle
    glDrawArrays(GL_TRIANGLES, 0, _width * _height * 6); //mode : forme final, indice du premier vertex, nb vertex

    //on désactive le tableau vertex attrib
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();

    ++m_frame;
}
Exemplo n.º 12
0
QMatrix4x4 Camera::generateMatrix() const
{
    //RTS

    QMatrix4x4 proj;

    proj.perspective( fov_, aspect_, zNear_, zFar_ );

    QMatrix4x4 invTranslate;
    invTranslate.setToIdentity();
    invTranslate.translate( -pos_ );

    QMatrix4x4 invRot( side_.x(),   side_.y(),  side_.z(),  0.0f,
                       up_.x(),     up_.y(),    up_.z(),    0.0f,
                       -dir_.x(),   -dir_.y(),  -dir_.z(),  0.0f,
                       0.0f,        0.0f,       0.0f,       1.0f );

    return proj * ( invRot * invTranslate );

}
Exemplo n.º 13
0
void Scene::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT );

    if ( !m_program.bind() )
        return;

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    QMatrix4x4 matrix;
    matrix.perspective( 60.0f, 4.0f / 3.0f, 0.1f, 400.0f );
    matrix.translate( 0.0f, 0.0f, -2.0f );
    matrix.rotate( m_angle, 0.0f, 1.0f, 0.0f );
    m_program.setUniformValue( m_matrixUniform, matrix );

    m_triangle->draw();

    m_program.release();
}
Exemplo n.º 14
0
void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    d->prog.bind();

    QMatrix4x4 projection;
    QMatrix4x4 mvp;

    //projection.ortho(-10,10,-10,10,1,1000);
    float aspect = (float)width()/(float)height();
    projection.perspective(10,aspect,1,1000);
    mvp = projection;

    mvp.translate(0,0,d->pz);
    mvp.rotate(d->ry,1,0,0);
    mvp.rotate(d->rx,0,1,0);
    d->prog.setUniformValue(d->mvpUniform,mvp);

    //glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, d->terrain);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    d->prog.setUniformValue(d->textureUniform,0);

    d->prog.enableAttributeArray(d->positionAttrib);
    d->prog.enableAttributeArray(d->colorAttrib);
    d->prog.enableAttributeArray(d->texcoordsAttrib);

    d->VBO->bind();

    d->prog.setAttributeBuffer(d->texcoordsAttrib, GL_FLOAT, offsetof(Vertex, texcoord), 2, sizeof(Vertex));
    d->prog.setAttributeBuffer(d->positionAttrib, GL_FLOAT, offsetof(Vertex, position), 3, sizeof(Vertex));
    d->prog.setAttributeBuffer(d->colorAttrib, GL_FLOAT, offsetof(Vertex, color), 3, sizeof(Vertex));

    d->EBO->bind();

    glDrawElements(GL_TRIANGLES, d->EBO->size(), GL_UNSIGNED_BYTE, NULL);

    d->prog.release();
}
Exemplo n.º 15
0
void OpenGLWidget::paintGL()
{
    if(!m_glFuncs)
    {
        return;
    }

    m_shaderProgram.bind();
    m_vertexArray.bind();
    m_texture.bind();

    static const QVector3D cameraUp(0.0f, 1.0f, 0.0f);
    QVector3D cameraRight = QVector3D::crossProduct(m_cameraFront, cameraUp).normalized();

    m_cameraPosition += m_forwardSpeed * 50 / 1000 * m_cameraFront;
    m_cameraPosition += m_strafeSpeed * 50 / 1000 * cameraRight;
    m_cameraPosition += m_upSpeed * 50 / 1000 * cameraUp;

    QMatrix4x4 viewMatrix;
    viewMatrix.lookAt(m_cameraPosition, m_cameraPosition + m_cameraFront, cameraUp);

    QMatrix4x4 projectionMatrix;
    projectionMatrix.perspective(45.0f, float(width()) / height(), 0.1f, 100.0f);

    m_shaderProgram.setUniformValue("viewMatrix", viewMatrix);
    m_shaderProgram.setUniformValue("projectionMatrix", projectionMatrix);

    m_glFuncs->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    if(!m_chunkMesh.isEmpty())
    {
        m_shaderProgram.setUniformValue("modelMatrix", QMatrix4x4());
        m_glFuncs->glDrawElements(GL_TRIANGLES, m_chunkMesh.getNumberOfElements(), GL_UNSIGNED_INT, 0);
    }

    m_texture.release();
    m_vertexArray.release();
    m_shaderProgram.release();
}
Exemplo n.º 16
0
void Painter::paint_3_3_shadowmap(float timef)
{
    glBindFramebuffer(GL_FRAMEBUFFER, m_depthFbo);
    glViewport(0, 0, ShadowMapSize, ShadowMapSize);

    glClearDepth(1.f);
    glClear(GL_DEPTH_BUFFER_BIT);

    QOpenGLShaderProgram * program(m_programs[ShadowMapProgram]);


    // Task_3_3 - ToDo Begin

    QMatrix4x4 L;
    QVector3D light = m_light.normalized();
    QVector3D center(0.0, 0.0, 0.0);
    QVector3D up = QVector3D::crossProduct(QVector3D::crossProduct(light - center, QVector3D(0.0, 1.0, 0.0)), light - center);

    L.perspective(camera()->fovy(), camera()->viewport().width()/static_cast<float>(camera()->viewport().height()), camera()->zNear(), camera()->zFar());
//    L.lookAt(light, center ,up);
    L.lookAt(m_light,QVector3D(0.f,0.f,0.f),QVector3D(0.f,0.1f,0.f));

    // Task_3_3 - ToDo End


    program->bind();
    program->setUniformValue("viewProjection", L);
    program->release();

    m_hpicgs->draw(*this, *program);
    m_portcc->draw(*this, *program);

    paint_3_2_label(L, timef);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, camera()->viewport().width(), camera()->viewport().height());
}
Exemplo n.º 17
0
void OpenGL2Common::paintGL()
{
    const bool frameIsEmpty = videoFrame.isEmpty();

    if (updateTimer.isActive())
        updateTimer.stop();

    if (frameIsEmpty && !hasImage)
        return;

    const QSize winSize = widget()->size();

    bool resetDone = false;

    if (!frameIsEmpty && hwAccellPossibleLock())
    {
        const GLsizei widths[3] = {
            videoFrame.size.width,
            videoFrame.size.chromaWidth(),
            videoFrame.size.chromaWidth(),
        };
        const GLsizei heights[3] = {
            videoFrame.size.height,
            videoFrame.size.chromaHeight(),
            videoFrame.size.chromaHeight()
        };

        if (doReset)
        {
            if (hwAccellnterface)
            {
                /* Release HWAccell resources */
                hwAccellnterface->clear(false);

                if (hwAccellnterface->canInitializeTextures())
                {
                    if (numPlanes == 2)
                    {
                        //NV12
                        for (int p = 0; p < 2; ++p)
                        {
                            glBindTexture(target, textures[p + 1]);
                            glTexImage2D(target, 0, !p ? GL_R8 : GL_RG8, widths[p], heights[p], 0, !p ? GL_RED : GL_RG, GL_UNSIGNED_BYTE, nullptr);
                        }
                    }
                    else if (numPlanes == 1)
                    {
                        //RGB32
                        glBindTexture(target, textures[1]);
                        glTexImage2D(target, 0, GL_RGBA, widths[0], heights[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
                    }
                }

                m_textureSize = QSize(widths[0], heights[0]);

                if (hqScaling)
                {
                    // Must be set before "HWAccelInterface::init()" and must have "m_textureSize"
                    maybeSetMipmaps(widget()->devicePixelRatioF());
                }

                /* Prepare textures, register GL textures */
                const bool hasHwAccelError = hwAccelError;
                hwAccelError = !hwAccellnterface->init(&textures[1]);
                if (hwAccelError && !hasHwAccelError)
                    QMPlay2Core.logError("OpenGL 2 :: " + tr("Can't init textures for") + " " + hwAccellnterface->name());


                /* Prepare texture coordinates */
                texCoordYCbCr[2] = texCoordYCbCr[6] = 1.0f;
            }
            else
            {
                /* Check linesize */
                const qint32 halfLinesize = (videoFrame.linesize[0] >> videoFrame.size.chromaShiftW);
                correctLinesize =
                (
                    (halfLinesize == videoFrame.linesize[1] && videoFrame.linesize[1] == videoFrame.linesize[2]) &&
                    (!sphericalView ? (videoFrame.linesize[1] == halfLinesize) : (videoFrame.linesize[0] == widths[0]))
                );

                /* Prepare textures */
                for (qint32 p = 0; p < 3; ++p)
                {
                    const GLsizei w = correctLinesize ? videoFrame.linesize[p] : widths[p];
                    const GLsizei h = heights[p];
                    if (p == 0)
                        m_textureSize = QSize(w, h);
                    if (hasPbo)
                    {
                        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[p + 1]);
                        glBufferData(GL_PIXEL_UNPACK_BUFFER, w * h, nullptr, GL_DYNAMIC_DRAW);
                    }
                    glBindTexture(GL_TEXTURE_2D, textures[p + 1]);
                    glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, nullptr);
                }

                /* Prepare texture coordinates */
                texCoordYCbCr[2] = texCoordYCbCr[6] = (videoFrame.linesize[0] == widths[0]) ? 1.0f : (widths[0] / (videoFrame.linesize[0] + 1.0f));

                if (hqScaling)
                    maybeSetMipmaps(widget()->devicePixelRatioF());
            }
            resetDone = true;
            hasImage = false;
        }

        if (hwAccellnterface)
        {
            const HWAccelInterface::Field field = (HWAccelInterface::Field)Functions::getField(videoFrame, Deinterlace, HWAccelInterface::FullFrame, HWAccelInterface::TopField, HWAccelInterface::BottomField);
            bool imageReady = false;
            if (!hwAccelError)
            {
                const HWAccelInterface::CopyResult res = hwAccellnterface->copyFrame(videoFrame, field);
                if (res == HWAccelInterface::CopyOk)
                    imageReady = true;
                else if (res == HWAccelInterface::CopyError)
                {
                    QMPlay2Core.logError("OpenGL 2 :: " + hwAccellnterface->name() + " " + tr("texture copy error"));
                    hwAccelError = true;
                }
            }
            hwAccellnterface->unlock();
            if (!imageReady && !hasImage)
                return;
            for (int p = 0; p < numPlanes; ++p)
            {
                glActiveTexture(GL_TEXTURE0 + p);
                glBindTexture(target, textures[p + 1]);
                if (m_useMipmaps)
                    glGenerateMipmap(target);
            }
        }
        else
        {
            /* Load textures */
            for (qint32 p = 0; p < 3; ++p)
            {
                const quint8 *data = videoFrame.buffer[p].constData();
                const GLsizei w = correctLinesize ? videoFrame.linesize[p] : widths[p];
                const GLsizei h = heights[p];
                if (hasPbo)
                {
                    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[p + 1]);
                    quint8 *dst;
                    if (glMapBufferRange)
                        dst = (quint8 *)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, w * h, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
                    else
                        dst = (quint8 *)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
                    if (!dst)
                        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
                    else
                    {
                        if (correctLinesize)
                            memcpy(dst, data, w * h);
                        else for (int y = 0; y < h; ++y)
                        {
                            memcpy(dst, data, w);
                            data += videoFrame.linesize[p];
                            dst  += w;
                        }
                        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
                        data = nullptr;
                    }
                }
                glActiveTexture(GL_TEXTURE0 + p);
                glBindTexture(GL_TEXTURE_2D, textures[p + 1]);
                if (hasPbo || correctLinesize)
                    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
                else for (int y = 0; y < h; ++y)
                {
                    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, y, w, 1, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
                    data += videoFrame.linesize[p];
                }
                if (m_useMipmaps)
                    glGenerateMipmap(GL_TEXTURE_2D);
            }
            if (hasPbo)
                glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
        }

        videoFrame.clear();
        hasImage = true;
    }

    if (!sphericalView)
    {
        deleteSphereVbo();
        shaderProgramVideo->setAttributeArray(positionYCbCrLoc, verticesYCbCr[verticesIdx], 2);
        shaderProgramVideo->setAttributeArray(texCoordYCbCrLoc, texCoordYCbCr, 2);
    }
    else
    {
        if (nIndices == 0)
            loadSphere();

        glBindBuffer(GL_ARRAY_BUFFER, sphereVbo[0]);
        shaderProgramVideo->setAttributeBuffer(positionYCbCrLoc, GL_FLOAT, 0, 3);

        glBindBuffer(GL_ARRAY_BUFFER, sphereVbo[1]);
        shaderProgramVideo->setAttributeBuffer(texCoordYCbCrLoc, GL_FLOAT, 0, 2);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
    shaderProgramVideo->enableAttributeArray(positionYCbCrLoc);
    shaderProgramVideo->enableAttributeArray(texCoordYCbCrLoc);

    shaderProgramVideo->bind();
    if (doReset)
    {
        const float brightness = videoAdjustment.brightness / 100.0f;
        const float contrast   = (videoAdjustment.contrast + 100) / 100.0f;
        const float sharpness  = videoAdjustment.sharpness / 50.0f;
        if (hwAccellnterface && numPlanes == 1)
        {
            hwAccellnterface->setVideAdjustment(videoAdjustment);
            const bool hasBrightness = videoAdjustmentKeys.contains("Brightness");
            const bool hasContrast   = videoAdjustmentKeys.contains("Contrast");
            const bool hasSharpness  = videoAdjustmentKeys.contains("Sharpness");
            shaderProgramVideo->setUniformValue
            (
                "uVideoAdj",
                hasBrightness ? 0.0f : brightness,
                hasContrast   ? 1.0f : contrast,
                hasSharpness  ? 0.0f : sharpness
            );
        }
        else
        {
            const float saturation = (videoAdjustment.saturation + 100) / 100.0f;
            const float hue = videoAdjustment.hue / -31.831f;
            shaderProgramVideo->setUniformValue("uVideoEq", brightness, contrast, saturation, hue);
            shaderProgramVideo->setUniformValue("uSharpness", sharpness);
        }
        if (hqScaling)
        {
            const qreal dpr = widget()->devicePixelRatioF();
            if (!resetDone)
                maybeSetMipmaps(dpr);
            const bool useBicubic = (W * dpr > m_textureSize.width() || H * dpr > m_textureSize.height());
            shaderProgramVideo->setUniformValue("uBicubic", useBicubic ? 1 : 0);
        }
        shaderProgramVideo->setUniformValue("uTextureSize", m_textureSize);

        doReset = !resetDone;
        setMatrix = true;
    }
    if (setMatrix)
    {
        QMatrix4x4 matrix;
        if (!sphericalView)
        {
            matrix.scale(W / (qreal)winSize.width(), H / (qreal)winSize.height());
            if (!videoOffset.isNull())
                matrix.translate(-videoOffset.x(), videoOffset.y());
        }
        else
        {
            const double z = qBound(-1.0, (zoom > 1.0 ? log10(zoom) : zoom - 1.0), 0.99);
            matrix.perspective(68.0, (qreal)winSize.width() / (qreal)winSize.height(), 0.001, 2.0);
            matrix.translate(0.0, 0.0, z);
            matrix.rotate(rot.x(), 1.0, 0.0, 0.0);
            matrix.rotate(rot.y(), 0.0, 0.0, 1.0);
        }
        shaderProgramVideo->setUniformValue("uMatrix", matrix);
        setMatrix = false;
    }
    if (!sphericalView)
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    else
    {
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphereVbo[2]);
        glDrawElements(GL_TRIANGLE_STRIP, nIndices, GL_UNSIGNED_SHORT, nullptr);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }
    shaderProgramVideo->release();

    shaderProgramVideo->disableAttributeArray(texCoordYCbCrLoc);
    shaderProgramVideo->disableAttributeArray(positionYCbCrLoc);

    glActiveTexture(GL_TEXTURE3);

    /* OSD */
    osdMutex.lock();
    if (!osdList.isEmpty())
    {
        glBindTexture(GL_TEXTURE_2D, textures[0]);

        QRect bounds;
        const qreal scaleW = (qreal)subsW / outW, scaleH = (qreal)subsH / outH;
        bool mustRepaint = Functions::mustRepaintOSD(osdList, osd_ids, &scaleW, &scaleH, &bounds);
        bool hasNewSize = false;
        if (!mustRepaint)
            mustRepaint = osdImg.size() != bounds.size();
        if (mustRepaint)
        {
            if (osdImg.size() != bounds.size())
            {
                osdImg = QImage(bounds.size(), QImage::Format_ARGB32);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.width(), bounds.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
                hasNewSize = true;
            }
            osdImg.fill(0);
            QPainter p(&osdImg);
            p.translate(-bounds.topLeft());
            Functions::paintOSD(false, osdList, scaleW, scaleH, p, &osd_ids);
            const quint8 *data = osdImg.constBits();
            if (hasPbo)
            {
                const GLsizeiptr dataSize = (osdImg.width() * osdImg.height()) << 2;
                glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[0]);
                if (hasNewSize)
                    glBufferData(GL_PIXEL_UNPACK_BUFFER, dataSize, nullptr, GL_DYNAMIC_DRAW);
                quint8 *dst;
                if (glMapBufferRange)
                    dst = (quint8 *)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, dataSize, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
                else
                    dst = (quint8 *)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
                if (!dst)
                    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
                else
                {
                    memcpy(dst, data, dataSize);
                    glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
                    data = nullptr;
                }
            }
            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.width(), bounds.height(), GL_RGBA, GL_UNSIGNED_BYTE, data);
            if (hasPbo && !data)
                glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
        }

        const QSizeF winSizeSubs = winSize * widget()->devicePixelRatioF();
        const float left   = (bounds.left() + subsX) * 2.0f / winSizeSubs.width() - osdOffset.x();
        const float right  = (bounds.right() + subsX + 1) * 2.0f / winSizeSubs.width() - osdOffset.x();
        const float top    = (bounds.top() + subsY) * 2.0f / winSizeSubs.height() - osdOffset.y();
        const float bottom = (bounds.bottom() + subsY + 1) * 2.0f / winSizeSubs.height() - osdOffset.y();
        const float verticesOSD[8] = {
            left  - 1.0f, -bottom + 1.0f,
            right - 1.0f, -bottom + 1.0f,
            left  - 1.0f, -top    + 1.0f,
            right - 1.0f, -top    + 1.0f,
        };

        shaderProgramOSD->setAttributeArray(positionOSDLoc, verticesOSD, 2);
        shaderProgramOSD->setAttributeArray(texCoordOSDLoc, texCoordOSD, 2);
        shaderProgramOSD->enableAttributeArray(positionOSDLoc);
        shaderProgramOSD->enableAttributeArray(texCoordOSDLoc);

        glEnable(GL_BLEND);
        shaderProgramOSD->bind();
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        shaderProgramOSD->release();
        glDisable(GL_BLEND);

        shaderProgramOSD->disableAttributeArray(texCoordOSDLoc);
        shaderProgramOSD->disableAttributeArray(positionOSDLoc);
    }
    osdMutex.unlock();

    glBindTexture(GL_TEXTURE_2D, 0);
}
Exemplo n.º 18
0
void TApplication::RenderToFBO() {
    Fbo->bind();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_MULTISAMPLE);

    Shader.bind();

    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


    QMatrix4x4 view;
    view.lookAt(QVector3D(0.0, 0.0, 6.0), QVector3D(0.0, 0.0, 0.0), QVector3D(0.0, 1.0, 0.0));

    QMatrix4x4 projection;
    projection.perspective(45.0f, float(WINDOW_WIDTH) / float(WINDOW_HEIGHT), 0.1f, 10.0f);

    Shader.setUniformValue("projection", projection);
    Shader.setUniformValue("view", view);

    Shader.setUniformValue("lightIntensities", QVector3D(1.0, 1.0, 1.0));
    Shader.setUniformValue("lightPosition", QVector3D(-30.0, 30.0, 30.0));

    QMatrix4x4 model;
    model.translate(0, 0, 0.0);
    model.rotate(AngleX, 1.0, 0.0, 0.0);
    model.rotate(AngleY, 0.0, 1.0, 0.0);
    model.rotate(AngleZ, 0.0, 0.0, 1.0);
    model.scale(0.42);

    QMatrix3x3 normalMatrix = model.normalMatrix();

    Shader.setUniformValue("model", model);
    Shader.setUniformValue("normalMatrix", normalMatrix);


    VertexBuff->bind();

    GLint attribute;
    GLint offset = 0;

    attribute = Shader.attributeLocation("gl_Vertex");
    Shader.enableAttributeArray(attribute);
    Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex));
    offset += sizeof(QVector3D);

    attribute = Shader.attributeLocation("gl_Normal");
    Shader.enableAttributeArray(attribute);
    Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex));
    offset += sizeof(QVector3D);

    attribute = Shader.attributeLocation("vertTexCoord");
    Shader.enableAttributeArray(attribute);
    Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 2, sizeof(TVertex));
    offset += sizeof(QVector2D);

    attribute = Shader.attributeLocation("tangent");
    Shader.enableAttributeArray(attribute);
    Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex));
    offset += sizeof(QVector3D);

    attribute = Shader.attributeLocation("bitangent");
    Shader.enableAttributeArray(attribute);
    Shader.setAttributeBuffer(attribute, GL_FLOAT, offset, 3, sizeof(TVertex));
    offset += sizeof(QVector3D);

    VertexBuff->release();

    Texture->bind(0, QOpenGLTexture::ResetTextureUnit);
    Shader.setUniformValue("texture", 0);
    NormalMap->bind(1, QOpenGLTexture::ResetTextureUnit);
    Shader.setUniformValue("normalMap", 1);

    glDrawArrays(GL_TRIANGLES, 0, 3 * ObjectSize);

    Shader.disableAttributeArray("gl_Vertex");
    Shader.disableAttributeArray("gl_Normal");
    Shader.disableAttributeArray("vertTexCoord");
    Shader.disableAttributeArray("tangent");
    Shader.disableAttributeArray("bitangent");

    Texture->release(0, QOpenGLTexture::ResetTextureUnit);
    NormalMap->release(1, QOpenGLTexture::ResetTextureUnit);


    Shader.disableAttributeArray("coord2d");
    Shader.disableAttributeArray("v_color");

    Shader.release();

/*
    /// DEBUG INFO

    QMatrix4x4 modelView = view * model;

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(projection.data());
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(modelView.data());

//    glDisable(GL_DEPTH_TEST);

    glColor3f(1, 1, 1);
    glBegin(GL_LINES);
    for (size_t i = 0; i < Obj.size(); i += 3) {
        const TVertex& v1 = Obj[i];
        const TVertex& v2 = Obj[i + 1];
        const TVertex& v3 = Obj[i + 2];
        glVertex3f(v1.Position.x(), v1.Position.y(), v1.Position.z());
        glVertex3f(v2.Position.x(), v2.Position.y(), v2.Position.z());

        glVertex3f(v1.Position.x(), v1.Position.y(), v1.Position.z());
        glVertex3f(v3.Position.x(), v3.Position.y(), v3.Position.z());

        glVertex3f(v2.Position.x(), v2.Position.y(), v2.Position.z());
        glVertex3f(v3.Position.x(), v3.Position.y(), v3.Position.z());
    }
    glEnd();

    // normals
    glColor3f(0,0,1);
    glBegin(GL_LINES);
    for (size_t i = 0; i < Obj.size(); ++i) {
        const TVertex& v = Obj[i];
        QVector3D p = v.Position;
        p *= 1.02;
        glVertex3f(p.x(), p.y(), p.z());
        QVector3D n = v.Normal.normalized();
        p += n * 0.8;
        glVertex3f(p.x(), p.y(), p.z());
    }
    glEnd();

    // tangent
    glColor3f(1,0,0);
    glBegin(GL_LINES);
    for (size_t i = 0; i < Obj.size(); ++i) {
        const TVertex& v = Obj[i];
        QVector3D p = v.Position;
        p *= 1.02;
        glVertex3f(p.x(), p.y(), p.z());
        QVector3D n = v.Tangent.normalized();
        p += n * 0.8;
        glVertex3f(p.x(), p.y(), p.z());
    }
    glEnd();

    // bitangent
    glColor3f(0,1,0);
    glBegin(GL_LINES);
    for (size_t i = 0; i < Obj.size(); ++i) {
        const TVertex& v = Obj[i];
        QVector3D p = v.Position;
        p *= 1.02;
        glVertex3f(p.x(), p.y(), p.z());
        QVector3D n = v.Bitangent.normalized();
        p += n * 0.3;
        glVertex3f(p.x(), p.y(), p.z());
    }
    glEnd();
*/
    Fbo->release();
}
Exemplo n.º 19
0
void GameWindow::render()
{
    const qreal retinaScale = devicePixelRatio();
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT);

    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(80.0f, 4.0f/3.0f, 0.1f, 100.0f);
    matrix.translate(0, 0, avancerZ);
    matrix.rotate(rotX, 1, 0, 0);
    matrix.rotate(rotY, 0, 1, 0);
    matrix.rotate(rotZ, 0, 0, 1);



    m_program->setUniformValue(m_matrixUniform, matrix);

    int i=0;
    int j=0;

    for(int j=0; j<m_image.height()-1; j++)
    {
        for(i = 0; i < m_image.width()*6; i+=3)
        {
            if((i/2)%3==0)
            {
                vertices[i]=points[(i/3/2)*3+j*largeur*3];
                vertices[i+1]=points[(i/3/2)*3+j*largeur*3+1];
                vertices[i+2]=points[(i/3/2)*3+j*largeur*3+2];
            }
            else
            {
                vertices[i]=points[(i/3/2)*3+j*largeur*3+largeur*3];
                vertices[i+1]=points[(i/3/2)*3+j*largeur*3+largeur*3+1];
                vertices[i+2]=points[(i/3/2)*3+j*largeur*3+largeur*3+2];
            }
            if(vertices[i+2] >=0.3)
            {
                couleurs[i]=1.0f;
                couleurs[i+1]=1.0f;
                couleurs[i+2]=1.0f;
            }
            else if(vertices[i+2] >=0.08)
            {
                couleurs[i]=0.5f;
                couleurs[i+1]=0.2f;
                couleurs[i+2]=0.2f;
            }
            else
            {
                couleurs[i]=0.5f;
                couleurs[i+1]=0.5f;
                couleurs[i+2]=0.0f;
            }

        }
        glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, couleurs);

        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, largeur*2);

        glDisableVertexAttribArray(1);
        glDisableVertexAttribArray(0);
    }

    //Affichage des points





    ++m_frame;
}
Exemplo n.º 20
0
void TriangleWindow::render()
{
    const qreal retinaScale = devicePixelRatio();
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);

    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 1.0 * _width/_height, 0.1f, 300.0f);
    matrix.translate(_camX, _camY, _camZ);
    matrix.rotate(20, 1, 0, 0);
    matrix.rotate(_angle, 0, 1, 0);
    matrix.translate(-70, 0, -60);

    m_program->setUniformValue(m_matrixUniform, matrix);

    vao.bind();

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture->textureId());

    glDrawArrays(GL_TRIANGLES, 0, _map.size());

    vao.release();
    m_program->release();

    if(allSeasons[currentSeason] == "WINTER")
    {
        _fall->setPointSize(4.0);
        _fall->setFaster(0.0);
        _fall->setPointColor(QVector4D(0.9, 0.9, 0.9, 0.0));
    }
    else if(allSeasons[currentSeason] == "AUTUMN")
    {
        _fall->setPointSize(1.0);
        _fall->setFaster(1.0);
        _fall->setPointColor(QVector4D(0.0, 0.9, 0.9, 0.5));
    }
    else
        return;

    particuleShader->bind();
    particuleShader->setUniformValue(particuleMatrixUniform, matrix);
    particuleShader->setUniformValue(particulePointSize, _fall->getPointSize());
    particuleShader->setUniformValue(particulePointColor, _fall->getPointColor());

    fallVao.bind();
    fallVbo.bind();
    size_t fallSize = _fall->getParticules().size()*sizeof(QVector3D);
    fallVbo.write(0, _fall->getParticules().constData(), fallSize);
    glDrawArrays(GL_POINTS, 0, _fall->getParticules().size());
    fallVao.release();

    particuleShader->release();

    ++m_frame;
}
Exemplo n.º 21
0
void Tutorial05::initializeGL()
{
  // initialize OpenGL
  initializeOpenGLFunctions();

  // Dark blue background
  glClearColor(0.0f, 0.0f, 0.3f, 0.0f);

  // Enable depth test
  glEnable(GL_DEPTH_TEST);

  // Accept fragment if it closer to the camera than the former one
  glDepthFunc(GL_LESS);

  bool success;
  // load and compile vertex shader
  success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,
                                  ":/TransformVertexShader.vert");

  // load and compile fragment shader
  success = shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,
                                                 ":/TextureFragmentShader.frag");

  programID = shaderProgram.programId();

  // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  QMatrix4x4 projection;
  projection.perspective(45.0, 4.0/3.0, 0.1, 100.0);
  // Camera matrix
  QMatrix4x4 view;
  view.lookAt( QVector3D(4,3,3), // Camera is at (4,3,3), in World Space
               QVector3D(0,0,0), // and looks at the origin
               QVector3D(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
             );
  // Model matrix : an identity matrix (model will be at the origin)
  QMatrix4x4 model;
  model.setToIdentity();

  // Our ModelViewProjection : multiplication of our 3 matrices
  MVP =  projection * view * model; // Remember, matrix multiplication is the other way around

  shaderProgram.link();

  // Get a handle for our "MVP" uniform
  MatrixID = glGetUniformLocation(programID, "MVP");

  // Get a handle for our buffers
  vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
  vertexUVID = glGetAttribLocation(programID, "vertexUV");

  // Load the texture Qt methods
  QImage image = QImage(":/uvtemplate.bmp").mirrored();
  mTexture = new QOpenGLTexture(image);
  mTexture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
  mTexture->setMagnificationFilter(QOpenGLTexture::Linear);
  shaderProgram.setUniformValue("myTextureSampler", 0);

  //enable texturing
  glEnable(GL_TEXTURE_2D);
  //generate textures

  //bind the texture
  mTexture->bind();

  // ... nice trilinear filtering.
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  //glGenerateMipmap(GL_TEXTURE_2D);

  glGenBuffers(1, &vertexbuffer);
  glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

  glGenBuffers(1, &uvbuffer);
  glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);

}
Exemplo n.º 22
0
void TriangleWindow::render()
{
    const qreal retinaScale = devicePixelRatio();
    glViewport(0, 0, width() * retinaScale, height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //Bind shading program
    m_program->bind();

    QMatrix4x4 matrix;
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 1000.0f);

    matrix.translate(0, 0, -2);


    //
    for(int i=0;i<4;++i) {
        if(i==0) {
            matrix.rotate(100.0f * m_frame / screen()->refreshRate(), 0, 1, 0);
            //Set uniform matrix
        }
        else {
            matrix.rotate(90.0f,0,1,0);
        }
        m_textures[i]->bind();
        m_program->setUniformValue(m_matrixUniform, matrix);
        oldWay();
        m_textures[i]->release();
    }
    m_program->release();
    matrix = QMatrix4x4();
    matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
    matrix.translate(0, 1, -5);
    matrix.rotate(45.0f,0,1,0);

//    m_color_program->bind();
//    m_program->setUniformValue(m_matrixUniform,matrix);

//    m_textures[4]->bind();

    glPushMatrix();
        glScalef(.30f,.30f,.30f);
        glTranslatef(-2.0f,0.,-2.0f);
    glRotatef(45* m_frame/screen()->refreshRate(),0,1,0);
//    glTranslatef(0.0f,0.,-2.0f);
    static GLUquadricObj *quadric = gluNewQuadric();

    gluQuadricDrawStyle(quadric, GLU_FILL );
//    m_textures[0]->bind();
    gluSphere( quadric , 1 , 36 , 18 );

    glPopMatrix();

    //gluDeleteQuadric(quadric);
    //m_textures[4]->release();


    //Relâche le programme
    //m_program->release();

    ++m_frame;
}
Exemplo n.º 23
0
void MainWindow::render()
{
    glViewport(0, 0, width(), height());

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_program->bind();


    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };

    time_t now;
//    time(&now);
    difftime(time, now);
    double move = 1;
    if(speed != 0)
    {
        move = speed * m_frame / screen()->refreshRate();
    }



    QMatrix4x4 scale;
    QMatrix4x4 orbitRotMatrix;
    QMatrix4x4 orbitMatrix;
    QMatrix4x4 rotMatrix;
    scale.scale(4.0f);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet sonne("sonne", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(5.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet erde("erde", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet mond("mond", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.5f);
    orbitRotMatrix.rotate(0.5 * rotateY * move + 15, 0, 1, 0);
    orbitMatrix.translate(10.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet mars("mars", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.3f);
    orbitRotMatrix.rotate(rotateY * move, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet deimos("deimos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    scale.setToIdentity();
    orbitRotMatrix.setToIdentity();
    orbitMatrix.setToIdentity();
    rotMatrix.setToIdentity();
    scale.scale(0.15f);
    orbitRotMatrix.rotate(rotateY * move *15, 0, 1, 0);
    orbitMatrix.translate(1.0f, 0, 0);
    rotMatrix.rotate(rotateY * move, 0, 1, 0);
    cg::Planet phobos("phobos", &myMesh->v[0], scale, rotMatrix, orbitMatrix, orbitRotMatrix);

    std::vector<cg::Planet*> vecPlanet {&sonne, &erde, &mond, &mars, &deimos, &phobos};
    cg::Sunsystem sys;
    sys.addChild(&sonne)->addChild(&erde)->addChild(&mond);
    sys.addChild(&mars)->addChild(&deimos);
    sys.getChild(1)->addChild(&phobos);
    sys.run();


    QMatrix4x4 camMatrix;
    camMatrix.perspective(60.f, 4.0f/3.0f, 0.1f, 100.0f);
    camMatrix.translate(0, 0, -15);
    camMatrix.rotate(45, 1, 0, 0);



    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//    glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, &myMesh->v[0]);
//    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

    glEnableVertexAttribArray(0);
//    glEnableVertexAttribArray(1);


    for(cg::Planet *n : vecPlanet)
    {
        m_program->setUniformValue(m_matrixUniform, camMatrix * n->resultmatrix);
        //glDrawArrays(GL_TRIANGLES, 0, myMesh->v.size()/3);
        glDrawElements(GL_TRIANGLES, myMesh->v_f.size(), GL_UNSIGNED_INT, 0);
//        glDrawElements(GL_TRIANGLES, myMesh->v_f.size(), GL_UNSIGNED_SHORT, &myMesh->v_f[0]);
    }


    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    m_program->release();

    ++m_frame;
//    time(&time);
}
Exemplo n.º 24
0
void Renderer::paintGL()
{
	if (!_shaderProgram)
	{
		glClearColor(0.5, 0.25, 0.4, _background.alphaF());
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		return;
	}

	emit reportSignal(MInfo, "Rendering frame");
	glClearColor(_background.redF(), _background.greenF(), _background.blueF(), _background.alphaF());
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// for each object that was split or loaded separately
	QMatrix4x4 modelMatrix = CreateMatrix(_desc[PositionModel]);
	QMatrix4x4 cameraMatrix = CreateMatrix(_desc[PositionCamera]);
	QMatrix4x4 perspectiveMatrix;
	float ratio = size().width() / (float)size().height();
	perspectiveMatrix.setToIdentity();
	perspectiveMatrix.perspective(90, ratio, .5f, 10000.0f);
	QMatrix4x4 mvp = perspectiveMatrix * cameraMatrix * modelMatrix;

	auto test1 = mvp*QVector3D(0, 0, 0);
	auto test2 = mvp*QVector3D(0, 0, 0.5);
	///////////////////////////////////////
	/////////// setting uniform values
	///////////////////////////////////////
	_shaderProgram->setUniformValue("modelToCamera", mvp);
	_shaderProgram->setUniformValue("viewMatrix", cameraMatrix);
	_shaderProgram->setUniformValue("modelMatrix", modelMatrix);
	QVector3D cameraPosition(_desc[PositionCamera]._xPos, _desc[PositionCamera]._yPos, _desc[PositionCamera]._zPos);
	_shaderProgram->setUniformValue("cameraPosition", cameraPosition);
	//const float * df = _mesh.Diffuse();
	//_shaderProgram->setUniformValue("MaterialDiffuseColor", QVector3D(df[0],df[1],df[2]) );
	// set 
	QVector3D lightPos(_desc[PositionLight]._xPos, _desc[PositionLight]._yPos, _desc[PositionLight]._zPos);
	_shaderProgram->setUniformValue("LightPosition_worldspace", lightPos);
	
	///////////////////////////////////////
	/////////// setting arrays
	///////////////////////////////////////

	// take vertex array from opengl context
	_shaderProgram->enableAttributeArray(VERTEX_LOCATION);
	_shaderProgram->enableAttributeArray(NORMAL_LOCATION);
	_shaderProgram->enableAttributeArray(BARYCENTRIC_LOCATION);

	_shaderProgram->setAttributeBuffer(VERTEX_LOCATION, GL_FLOAT, 3*VERTEX_LOCATION * sizeof(GLfloat), 3, 3 * NEntries * sizeof(GLfloat));

	_shaderProgram->setAttributeBuffer(NORMAL_LOCATION, GL_FLOAT, 3*NORMAL_LOCATION * sizeof(GLfloat), 3, 3 * NEntries * sizeof(GLfloat));

	_shaderProgram->setAttributeBuffer(BARYCENTRIC_LOCATION, GL_FLOAT, 3*BARYCENTRIC_LOCATION * sizeof(GLfloat), 3, 3 * NEntries * sizeof(GLfloat));

	_shaderProgram->setUniformValue("wireframe", 0);
	///////////////////////////////////////
	/////////// Actual drawing
	///////////////////////////////////////
	switch (_renderStyle)
	{
	case RenderPoints:
	{
		int size = 0;
		for (int i = 0; i < _renderData.size(); i++)
		{
			size += _renderData[i]._mesh.NVertices();
		}
		glDrawArrays(GL_POINTS, 0, size );
		break;
	}
	case RenderWireframe:
	{
		_shaderProgram->setUniformValue("wireframe", 1);
		//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glDrawElements(GL_TRIANGLES, _indices, GL_UNSIGNED_INT, 0);
		break;
	}
	default:
	{
		auto test = glGetString(GL_VERSION);
		glDrawElements(GL_TRIANGLES, _indices, GL_UNSIGNED_INT, 0);
		break;
	}
	}

	//for (int i = 0; i < total; ++i) {
	//	//textures[i]->bind();
	//	glDrawArrays(GL_TRIANGLE_STRIP, i * 3, 3);
	//}
}
Exemplo n.º 25
0
void SelectionPointer::renderSelectionPointer(GLuint defaultFboHandle, bool useOrtho)
{
    Q_UNUSED(defaultFboHandle)

    glViewport(m_mainViewPort.x(), m_mainViewPort.y(),
               m_mainViewPort.width(), m_mainViewPort.height());

    Q3DCamera *camera = m_cachedScene->activeCamera();

    QMatrix4x4 itModelMatrix;

    // Get view matrix
    QMatrix4x4 viewMatrix;
    QMatrix4x4 projectionMatrix;
    GLfloat viewPortRatio = (GLfloat)m_mainViewPort.width() / (GLfloat)m_mainViewPort.height();
    if (m_cachedIsSlicingActivated) {
        GLfloat sliceUnitsScaled = sliceUnits / m_autoScaleAdjustment;
        viewMatrix.lookAt(QVector3D(0.0f, 0.0f, 1.0f), zeroVector, upVector);
        projectionMatrix.ortho(-sliceUnitsScaled * viewPortRatio, sliceUnitsScaled * viewPortRatio,
                               -sliceUnitsScaled, sliceUnitsScaled,
                               -1.0f, 4.0f);
    } else if (useOrtho) {
        viewMatrix = camera->d_ptr->viewMatrix();
        GLfloat orthoRatio = 2.0f;
        projectionMatrix.ortho(-viewPortRatio * orthoRatio, viewPortRatio * orthoRatio,
                               -orthoRatio, orthoRatio,
                               0.0f, 100.0f);
    } else {
        viewMatrix = camera->d_ptr->viewMatrix();
        projectionMatrix.perspective(45.0f, viewPortRatio, 0.1f, 100.0f);
    }

    QMatrix4x4 modelMatrix;
    QMatrix4x4 MVPMatrix;

    // Position the pointer ball
    modelMatrix.translate(m_position);

    if (!m_rotation.isIdentity()) {
        modelMatrix.rotate(m_rotation);
        itModelMatrix.rotate(m_rotation);
    }

    // Scale the point with fixed values (at this point)
    QVector3D scaleVector(0.05f, 0.05f, 0.05f);
    modelMatrix.scale(scaleVector);
    itModelMatrix.scale(scaleVector);

    MVPMatrix = projectionMatrix * viewMatrix * modelMatrix;

    QVector3D lightPos =  m_cachedScene->activeLight()->position();

    //
    // Draw the point
    //
    m_pointShader->bind();
    m_pointShader->setUniformValue(m_pointShader->lightP(), lightPos);
    m_pointShader->setUniformValue(m_pointShader->view(), viewMatrix);
    m_pointShader->setUniformValue(m_pointShader->model(), modelMatrix);
    m_pointShader->setUniformValue(m_pointShader->nModel(), itModelMatrix.inverted().transposed());
    m_pointShader->setUniformValue(m_pointShader->color(), m_highlightColor);
    m_pointShader->setUniformValue(m_pointShader->MVP(), MVPMatrix);
    m_pointShader->setUniformValue(m_pointShader->ambientS(),
                                   m_cachedTheme->ambientLightStrength());
    m_pointShader->setUniformValue(m_pointShader->lightS(), m_cachedTheme->lightStrength() * 2.0f);
    m_pointShader->setUniformValue(m_pointShader->lightColor(),
                                   Utils::vectorFromColor(m_cachedTheme->lightColor()));

    m_drawer->drawObject(m_pointShader, m_pointObj);
}
Exemplo n.º 26
0
void SelectionPointer::renderSelectionLabel(GLuint defaultFboHandle, bool useOrtho)
{
    Q_UNUSED(defaultFboHandle)

    glViewport(m_mainViewPort.x(), m_mainViewPort.y(),
               m_mainViewPort.width(), m_mainViewPort.height());

    Q3DCamera *camera = m_cachedScene->activeCamera();

    // Get view matrix
    QMatrix4x4 viewMatrix;
    QMatrix4x4 modelMatrixLabel;
    QMatrix4x4 projectionMatrix;
    GLfloat viewPortRatio = (GLfloat)m_mainViewPort.width() / (GLfloat)m_mainViewPort.height();
    if (m_cachedIsSlicingActivated) {
        GLfloat sliceUnitsScaled = sliceUnits / m_autoScaleAdjustment;
        viewMatrix.lookAt(QVector3D(0.0f, 0.0f, 1.0f), zeroVector, upVector);
        projectionMatrix.ortho(-sliceUnitsScaled * viewPortRatio, sliceUnitsScaled * viewPortRatio,
                               -sliceUnitsScaled, sliceUnitsScaled,
                               -1.0f, 4.0f);
    } else if (useOrtho) {
        viewMatrix = camera->d_ptr->viewMatrix();
        GLfloat orthoRatio = 2.0f;
        projectionMatrix.ortho(-viewPortRatio * orthoRatio, viewPortRatio * orthoRatio,
                               -orthoRatio, orthoRatio,
                               0.0f, 100.0f);
    } else {
        viewMatrix = camera->d_ptr->viewMatrix();
        projectionMatrix.perspective(45.0f, viewPortRatio, 0.1f, 100.0f);
    }

    QSize textureSize = m_labelItem.size();

    // Calculate scale factor to get uniform font size
    GLfloat scaledFontSize = 0.05f + m_drawer->font().pointSizeF() / 500.0f;
    GLfloat scaleFactor = scaledFontSize / (GLfloat)textureSize.height();

    // Position label
    QVector3D labelAlign(0.0f, 1.0f * scaledFontSize + 0.05f, 0.0f);
    modelMatrixLabel.translate(m_position + labelAlign);

    // Position the label towards the camera
    float camRotationsX = camera->xRotation();
    float camRotationsY = camera->yRotation();
    if (!m_cachedIsSlicingActivated) {
        modelMatrixLabel.rotate(-camRotationsX, 0.0f, 1.0f, 0.0f);
        modelMatrixLabel.rotate(-camRotationsY, 1.0f, 0.0f, 0.0f);
    }

    // Scale label based on text size
    modelMatrixLabel.scale(QVector3D((GLfloat)textureSize.width() * scaleFactor,
                                     scaledFontSize,
                                     0.0f));

    // Make label to be always on top
    glDisable(GL_DEPTH_TEST);

    // Make label transparent
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    m_labelShader->bind();

    QMatrix4x4 MVPMatrix;

    // Set shader bindings
    MVPMatrix = projectionMatrix * viewMatrix * modelMatrixLabel;
    m_labelShader->setUniformValue(m_labelShader->MVP(), MVPMatrix);

    // Draw the object
    m_drawer->drawObject(m_labelShader, m_labelObj, m_labelItem.textureId());

    // Release shader
    glUseProgram(0);

    // Disable transparency
    glDisable(GL_BLEND);

    // Depth test back to normal
    glEnable(GL_DEPTH_TEST);
}
Exemplo n.º 27
0
QMatrix4x4 FreeLookCamera::projectionMatrix() const {
    QMatrix4x4 mat;
    mat.perspective(45, m_screen_size.width() / m_screen_size.height(), 1e-1, 1e3);
    return mat;
}
Exemplo n.º 28
0
void Scene::paintGL(){
    setStates();                    // включаем буфер глубины, свет и прочее (возможно можно вынести в initGL)

    //glClear(GL_COLOR_BUFFER_BIT); // если включен буфер глубины, то без его очистки мы ничего не увидим
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glMatrixMode(GL_PROJECTION);
    //qgluPerspective(60.0, width / height, 0.01, 15.0);

    //glMatrixMode(GL_MODELVIEW);

    //пишем матрицы преобразований
    QMatrix4x4 LMM; // Local Model matrix (делает преобразования в локальных координатах объекта, для одного объекта их может быть несколько для разных частей объекта)
    QMatrix4x4 MM; // Model matrix (выносит координаты объекта в координаты пространства сцены,
                  //выполняется в следующем порядке - масштабирование, поворот, перенос)
                  // TranslationMatrix * RotationMatrix * ScaleMatrix * OriginalVector; (в коде это выглядит в обратном порядке)
    QMatrix4x4 MVM; // ModelView matrix (View matrix)("масштабирует крутит и перемещает весь мир")
    QMatrix4x4 CameraView; // тоже самое что и MVM, но для использования функции LookAt
    QMatrix4x4 PM; // Projection matrix // проекционная матрица
    QMatrix4x4 MVPM; // ModelViewProjection matrix (projection * view * model)
    if (perspective) {
        // устанавливаем трёхмерную канву (в перспективной проекции) для рисования (плоскости отсечения)
        // угол перспективы, отношение сторон, расстояние до ближней отсекающей плоскости и дальней
        PM.perspective(cameraFocusAngle,ratio,0.1f,100.0f);  // glFrustum( xmin, xmax, ymin, ymax, near, far)  // gluPerspective(fovy, aspect, near, far)
    }
    else {
        // устанавливаем трёхмерную канву (в ортогональной проекции) для рисования (плоскости отсечения)
        PM.ortho(-2.0f,2.0f,-2.0f,2.0f,-8.0f,8.0f); // glOrtho(left,right,bottom,top,near,far) // увеличение значений уменьшает фигуры на сцене (по Z задаём больше, чтобы не видеть отсечение фигур)
        // переносим по z дальше, обязательное условие для перспективной проекции // по оси z 0 это "глаз", - движение камеры назад, + вперёд.
    }
    ///MVM.translate(0.0f,0.0f,-6.0f); // переносим по z от "глаз", сдвигаем камеру на минус, т.е. в сторону затылка.
    // не работает в ортогональной проекции если перенести слишком далеко, за пределы куба отсечения
    // оппа, мы видим передние границы пирамиды отсечения, где всё отсекается (тут-то шейдерным сферам и конец)
    // изменяем масштаб фигуры (увеличиваем)
    ///MVM.scale(10.0f);  // отрицательное число переворачивает проекцию // влияет только на ортогональную проекцию // убивает Шферы
    // указываем угол поворота и ось поворота смотрящую из центра координат к точке x,y,z,
    MVM.rotate(m_angle,0.0f,1.0f,0.0f);  // поворот вокруг оси центра координат
    CameraView.lookAt(cameraEye,cameraCenter,cameraUp); // установка камеры (матрицы трансфрмации)
    MVM=CameraView*MVM;  // получаем матрицу трансформации итогового вида

    // находим проекционную инверсную мтрицу
    bool inverted;
    QMatrix4x4 PMi=PM.inverted(&inverted);
    if (!inverted)
        qDebug() << "PMi не конвертится";
    MVPM=PM*MVM;
    QMatrix4x4 MVPMi=MVPM.inverted(&inverted);
    if (!inverted)
        qDebug() << "MVPMi не конвертится";

    // РИСУЕМ ТРЕУГОЛЬНИК
    // инициализируем данные программы матрицы и атрибуты
    m_triangle->init();
    // зaпихиваем в его программу матрицу ориентации
    m_triangle->m_program.setUniformValue(m_triangle->m_matrixUniform, MVPM);
    // вызываем функцию рисования объекта (или объектов в for)
    m_triangle->draw();
    // проводим сброс инициализации параметров
    m_triangle->drop();//*/

    //РИСУЕМ СФЕРЫ
    m_shphere->init();
    m_shphere->m_program->setUniformValue(m_shphere->m_matrixUniform, MVPM);
    m_shphere->m_program->setUniformValue("PMi", PMi);
    m_shphere->m_program->setUniformValue("MVM", MVM);
    m_shphere->m_program->setUniformValue("MVPMi", MVPMi);
    m_shphere->m_program->setUniformValue("viewport",viewport);
    m_shphere->draw();
    m_shphere->drop();//*/

}
Exemplo n.º 29
0
void SGGLWidget::paintGL() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glColor3f(1, 0, 0);

	QMatrix4x4 camInv;
	camInv.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
	camInv.translate(0, 0, -10);

	double points[] =
	{
		0, 1,   -1, -1,   1,-1
	};


	double color[] =
	{
		0, 1, 0,   
		1, 0, 0,
		0, 0, 1
	};

	float matrix[] = {
		1.516395, -1.135207, -0.624707, -0.624695,
		0.000000, 3.027220, -0.468531, -0.468522,
		-1.516395, -1.135207, -0.624707, -0.624695,
		0.000000, -0.000034, 44.622768, 44.821877
	};

	//vertex attribute start//
	GLuint vtxBufferId, uniformId;

	GLuint posLoc = glGetAttribLocation(prid, "position");
	GLuint colorLoc = glGetAttribLocation(prid, "color");
	glEnableVertexAttribArray(posLoc);
	glVertexAttribPointer(posLoc,   2, GL_DOUBLE, GL_FALSE, 0,  points);
	glEnableVertexAttribArray(colorLoc);
	glVertexAttribPointer(colorLoc, 3, GL_DOUBLE, GL_FALSE, 0,  color);

	uniformId = glGetUniformLocation(prid, "matrix");
	glUniformMatrix4fv(uniformId, 1, GL_FALSE, matrix );
	glDrawArrays(GL_TRIANGLES, 0, 3);
	
	
	/*
	glPushMatrix();
	glMultMatrixd(camInv.data());

	glDrawArrays(GL_TRIANGLES, 0, 3);
	glPopMatrix();
	/*
	glPushMatrix();
	glMultMatrixf(dMatrix);
	glBegin(GL_POLYGON);
	glVertex3f(-10.0f, -10.0f, 1.0f);
	glVertex3f(10.0f, -10.0f, 1.0f);
	glVertex3f(10.0f, 10.0f, 1.0f);
	glVertex3f(-10.0f, 10.0f, 1.0f);
	glEnd();

	glBegin(GL_POLYGON);
	glVertex3f(-10.0f, -10.0f, -1.0f);
	glVertex3f(-10.0f, 10.0f, -1.0f);
	glVertex3f(10.0f, 10.0f, -1.0f);
	glVertex3f(10.0f, -10.0f, -1.0f);
	glEnd();
	
	glPopMatrix();/**/
	
	sgPrintf("paint");
}
void HeightMap3DRenderer::render(){
    if(!m_initialized)initialize();

    QMatrix4x4 modelMatrix;
    modelMatrix.rotate(-90, {1,0,0});
    const float rootSize = 50;
    modelMatrix.scale(rootSize);

    //select terrain patches of different lod levels here
    //TODO maybe this is not the right place for this kind of code...
    std::vector<float> ranges;
    {
        ranges.push_back(0.125);
        float lowestLodRes = rootSize;
        while(lowestLodRes > 0.25){
            ranges.push_back(ranges.back()*2);
            lowestLodRes /= 2;
        }
    }
    std::vector<TerrainPatchSelection> selections;
    const int lodLevelCount = ranges.size()+1;
    const float rootScale = rootSize;
    TerrainPatchSelection rootQuad{lodLevelCount-1,{0,0,0},rootScale};
    QVector3D observerPosition = modelMatrix.inverted() * m_state.camera.position();
    observerPosition.setZ(0);//TODO don't ignore height
    rootQuad.lodSelect(ranges,observerPosition,selections);


    glClearColor(0.6, 0.85, 1, 1);
    glDepthMask(GL_TRUE);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    if(!m_program || m_sourceDirty){
        recompileProgram();
    }
    m_program->bind();

    m_program->enableAttributeArray(0);

    //set gl states for terrain rendering
    //TODO maybe some of these are remembered by vao?
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_CULL_FACE);
    glDisable(GL_BLEND);
    glCullFace(GL_BACK);


    QMatrix4x4 viewMatrix = m_state.camera.worldToLocalMatrix();
    QMatrix4x4 projectionMatrix;
    projectionMatrix.perspective(65, m_aspectRatio, 1.0, 10024);
//    projectionMatrix.ortho(-10,10,-10,10,0.10,10);
    projectionMatrix.scale({1,-1,1}); //flip Y coordinates because otherwise Qt will render it upside down

    QMatrix4x4 modelViewMatrix = viewMatrix * modelMatrix;
    QMatrix4x4 mvp = projectionMatrix * modelViewMatrix;

    //uniforms shared between patches
    m_program->setUniformValue("modelViewMatrix", modelViewMatrix);
    m_program->setUniformValue("normalMatrix", modelViewMatrix.normalMatrix());
    m_program->setUniformValue("projectionMatrix", projectionMatrix);
    m_program->setUniformValue("mvp", mvp);
    m_program->setUniformValue("sampleOffset", QVector2D(m_state.center.x(), m_state.center.y()));

    m_program->setUniformValue("scaling", QVector3D(
                                   1.0/m_state.widthScale * 0.1, //TODO get rid of these magic constants
                                   1.0/m_state.widthScale * 0.1,
                                   m_state.heightScale //height scaling
                                   )
                               );

    //this is where the magic happens
    {
        QOpenGLVertexArrayObject::Binder binder(&m_vao);
        //loop through all selected terrain patches
        for (TerrainPatchSelection selection : selections) {
            QVector2D patchOffset{selection.position.x(),
                                  selection.position.y()};
            m_program->setUniformValue("patchOffset", patchOffset);
            m_program->setUniformValue("patchSize", selection.size);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, m_vertexCount);
        }
    }
    m_program->release();
}