Example #1
0
void EarthScene::render()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    m_sphere->material()->bind();
    QOpenGLShaderProgramPtr shader = m_sphere->material()->shader();

    // Set the usual view/projection matrices
    QMatrix4x4 modelViewMatrix = m_camera->viewMatrix() * m_modelMatrix;
    QMatrix3x3 normalMatrix = modelViewMatrix.normalMatrix();
    QMatrix4x4 mvp = m_camera->viewProjectionMatrix() * m_modelMatrix;

    shader->setUniformValue( "modelViewMatrix", modelViewMatrix );
    shader->setUniformValue( "normalMatrix", normalMatrix );
    shader->setUniformValue( "projectionMatrix", m_camera->projectionMatrix() );
    shader->setUniformValue( "mvp", mvp );

    // Set the lighting parameters - specify the direction in world coordinates
    // and transform to eye space as the shader expects
    QVector4D worldLightDirection( 4.0f, 2.0f, 1.5f, 0.0f );
    QVector4D eyeLightDirection = m_camera->viewMatrix() * worldLightDirection;
    shader->setUniformValue( "light.position", eyeLightDirection );
    shader->setUniformValue( "light.intensity", QVector3D( 1.0f, 1.0f, 1.0f ) );

    // Set the material properties
    shader->setUniformValue( "material.ka", QVector3D( 0.01f, 0.01f, 0.01f ) );
    shader->setUniformValue( "material.kd", QVector3D( 1.0f, 1.0f, 1.0f ) );
    shader->setUniformValue( "material.ks", QVector3D( 0.6f, 0.6f, 0.6f ) );
    shader->setUniformValue( "material.shininess", 5.0f );

    // Let the mesh setup the remainder of its state and draw itself
    m_sphere->render();
}
Example #2
0
void SatHorizon::render(QMatrix4x4 projection, float distance, QQuaternion quat, QVector3D posnorm, float alt, QColor rendercolor)
{

    float radius = sqrt( alt * alt - 1 ) / alt;

    float theta = acos(QVector3D::dotProduct(QVector3D(0,0,1), posnorm));
    QVector3D vecnorm = QVector3D::crossProduct(QVector3D(0,0,1), posnorm);
    vecnorm.normalize();

    createCircleBuffer(radius, SEGMENTS);

    QMatrix4x4 modelview;
    modelview.translate(0.0, 0.0, distance);
    modelview.rotate(quat);

    modelview.translate(posnorm * (1/alt) * (alt > 1.5 ? 1.0015 : 1.0001));
    modelview.rotate(theta * 180.0f/ PI, vecnorm );

    posBuf.bind();
    posBuf.write(0, vertexData, SEGMENTS * sizeof(QVector3D));
    posBuf.release();

    program->bind();

    program->setUniformValue("MVP", projection * modelview);
    QMatrix3x3 norm = modelview.normalMatrix();
    program->setUniformValue("NormalMatrix", norm);

    program->setUniformValue("outcolor", QVector4D(rendercolor.redF(), rendercolor.greenF(), rendercolor.blueF(), 1.0f));
    QOpenGLVertexArrayObject::Binder vaoBinder(&vao);
    glDrawArrays(GL_LINE_LOOP, 0, SEGMENTS);

}
Example #3
0
void Object::display(int stage)
{
    if (material == NULL)
    {
        return;
    }

    if (stage == material->visibleStage)
    {
        QMatrix4x4 modelview = glContext->view * transform.GetModelMatrix();
        QMatrix3x3 norm =  modelview.normalMatrix();

        material->PreRender(modelview, norm);
        Render();
        material->PostRender();
    }
}
Example #4
0
void CObject::draw(const CMVP* p_camera, QList<CLight*>* p_lights, QVector2D p_biasVP){
	float mvp[16];
	float mv[16];
	float mvi[9];
	const QMatrix4x4 camView = p_camera->viewMatrix();
	const QMatrix4x4 camProj = p_camera->projMatrix();
	const QMatrix4x4 modelView = camView * modelMatrix();
	const QMatrix4x4 modelViewProj = camProj * modelView;
	const CLight* light;

	p_camera->setViewPort(p_biasVP);

    this->convertMatrix(modelView, mv);
    this->convertMatrix(modelView.normalMatrix(), mvi);
    this->convertMatrix(modelViewProj, mvp);

	glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, CVA::vertices() );
	glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, CVA::textures() );
	glVertexAttribPointer( 2, 3, GL_FLOAT, GL_FALSE, 0, CVA::normals() );

	CShaderInterface::sendUniformMatrix4fv("mvp", 1, GL_TRUE, mvp);
	CShaderInterface::sendUniformMatrix4fv("mv", 1, GL_TRUE, mv);
	CShaderInterface::sendUniformMatrix3fv("mvi", 1, GL_TRUE, mvi);

	order(modelViewProj);

	CVA::enable();

	if(m_mat != NULL)
		m_mat->sendToProgram();

	if(p_lights == NULL)
		drawSub();
	else{
		for(int a = 0; a < p_lights->size(); a++){
			light = p_lights->at(a);
			if(p_lights->at(a)->enabled()){
				light->sendToProgram(p_camera, m_modelMatrix);

				drawSub();
			}
		}
	}

	CVA::disable();
}
void TriangleWindow::paintGL()
{
    static unsigned cnt;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 model;
    model.rotate(cnt%360, 1,0,0);
    model.rotate(45, 0,0,1);
    QMatrix4x4 mv = m_view * model;
    m_pgm.bind();
    m_pgm.setUniformValue("modelViewMatrix", mv);
    m_pgm.setUniformValue("normalMatrix", mv.normalMatrix());
    m_pgm.setUniformValue("projectionMatrix", m_projection);
    m_vao.bind();
    glDrawElements(GL_TRIANGLES, m_cnt, GL_UNSIGNED_BYTE, 0);

    renderNow();
    ++cnt;
}
Example #6
0
    void paintGL()
    {
        static unsigned cnt;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_tex);

        QMatrix4x4 model;
        model.rotate(cnt%360, 1,0,0);
        model.rotate(45, 0,0,1);
        QMatrix4x4 mv = m_view * model;
        m_pgm.bind();
        m_pgm.setUniformValue("modelViewMatrix", mv);
        m_pgm.setUniformValue("normalMatrix", mv.normalMatrix());
        m_pgm.setUniformValue("projectionMatrix", m_projection);
        m_vao.bind();
        glDrawElements(GL_TRIANGLES, m_cnt, GL_UNSIGNED_BYTE, 0);

        update();
        ++cnt;
    }
Example #7
0
void QVRMinimalExample::render(QVRWindow* /* w */,
        const QVRRenderContext& context, int viewPass,
        unsigned int texture)
{
    // Set up framebuffer object to render into
    GLint width, height;
    glBindTexture(GL_TEXTURE_2D, texture);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
    glBindTexture(GL_TEXTURE_2D, _fboDepthTex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height,
            0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);

    // Set up view
    glViewport(0, 0, width, height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    QMatrix4x4 projectionMatrix = context.frustum(viewPass).toMatrix4x4();
    QMatrix4x4 viewMatrix = context.viewMatrix(viewPass);

    // Set up shader program
    glUseProgram(_prg.programId());
    _prg.setUniformValue("projection_matrix", projectionMatrix);
    glEnable(GL_DEPTH_TEST);

    // Render
    QMatrix4x4 modelMatrix;
    modelMatrix.translate(0.0f, 0.0f, -15.0f);
    modelMatrix.rotate(_rotationAngle, 1.0f, 0.5f, 0.0f);
    QMatrix4x4 modelViewMatrix = viewMatrix * modelMatrix;
    _prg.setUniformValue("modelview_matrix", modelViewMatrix);
    _prg.setUniformValue("normal_matrix", modelViewMatrix.normalMatrix());
    glBindVertexArray(_vao);
    glDrawElements(GL_TRIANGLES, _vaoIndices, GL_UNSIGNED_INT, 0);
}
Example #8
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();
}
void GLWidget::paintMesh()
{

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;

    QMatrix4x4 cameraTransformation;
    cameraTransformation.rotate(alpha, 0, 1, 0);
    cameraTransformation.rotate(beta, 1, 0, 0);

    QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);

    vMatrix.lookAt(cameraPosition, lastCenterPosition, cameraUpDirection);

    mMatrix.setToIdentity();

    QMatrix4x4 mvMatrix;
    mvMatrix = vMatrix * mMatrix;

    QMatrix3x3 normalMatrix;
    normalMatrix = mvMatrix.normalMatrix();

    QMatrix4x4 lightTransformation;
    lightTransformation.rotate(lightAngle, 0, 1, 0);

    QVector3D lightPosition = lightTransformation * QVector3D(0, 10, 10);

    shaderProgram.bind();

    shaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix);
    shaderProgram.setUniformValue("mvMatrix", mvMatrix);
    shaderProgram.setUniformValue("normalMatrix", normalMatrix);
    shaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition);

    shaderProgram.setUniformValue("ambientColor", QColor(32, 32, 32));
    shaderProgram.setUniformValue("diffuseColor", QColor(128, 128, 128));
    shaderProgram.setUniformValue("specularColor", QColor(255, 255, 255));
    shaderProgram.setUniformValue("ambientReflection", (GLfloat) 1.0);
    shaderProgram.setUniformValue("diffuseReflection", (GLfloat) 1.0);
    shaderProgram.setUniformValue("specularReflection", (GLfloat) 1.0);
    shaderProgram.setUniformValue("shininess", (GLfloat) 100.0);

    glPointSize(INITIAL_POINT_SIZE);

    renderGrid();
    renderMesh(mode);

    shaderProgram.release();

    mMatrix.setToIdentity();
    mMatrix.translate(lightPosition);
    mMatrix.rotate(lightAngle, 0, 1, 0);
    mMatrix.rotate(45, 1, 0, 0);
    //    mMatrix.scale(0.1);

    coloringShaderProgram.bind();

    coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

    coloringShaderProgram.setAttributeArray("vertex", lightSource.getSpotlightVertices().constData());
    coloringShaderProgram.enableAttributeArray("vertex");

    coloringShaderProgram.setAttributeArray("color", lightSource.getSpotlightColors().constData());
    coloringShaderProgram.enableAttributeArray("color");

    glDrawArrays(GL_TRIANGLES, 0, lightSource.getSpotlightVertices().size());

    coloringShaderProgram.disableAttributeArray("vertex");
    coloringShaderProgram.disableAttributeArray("color");

    coloringShaderProgram.release();
}
Example #10
0
void NormalMapScene::render()
{
    //dumpTextureUnitConfiguration();

    // Enable depth buffer writes (QQ2 disables them)
    glDepthMask( true );

    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    m_mesh->material()->bind();
    QOpenGLShaderProgramPtr shader = m_mesh->material()->shader();

    {
        QMutexLocker locker( &m_mutex );

        // Set the usual view/projection matrices
        QMatrix4x4 modelViewMatrix = m_camera->viewMatrix() * m_modelMatrix;
        QMatrix3x3 normalMatrix = modelViewMatrix.normalMatrix();
        QMatrix4x4 mvp = m_camera->viewProjectionMatrix() * m_modelMatrix;

        shader->setUniformValue( "modelViewMatrix", modelViewMatrix );
        shader->setUniformValue( "normalMatrix", normalMatrix );
        shader->setUniformValue( "projectionMatrix", m_camera->projectionMatrix() );
        shader->setUniformValue( "mvp", mvp );
    }

    // Set the lighting parameters
    shader->setUniformValue( "light.position", QVector4D( 0.0f, 0.0f, 0.0f, 1.0f ) );
    shader->setUniformValue( "light.intensity", QVector3D( 1.0f, 1.0f, 1.0f ) );

    // Set the material properties
    shader->setUniformValue( "material.Ka", QVector3D( 0.1f, 0.1f, 0.1f ) );
    shader->setUniformValue( "material.Kd", QVector3D( 0.9f, 0.9f, 0.9f ) );
    shader->setUniformValue( "material.Ks", QVector3D( 0.2f, 0.2f, 0.2f ) );
    shader->setUniformValue( "material.shininess", 20.0f );

    // Let the mesh setup the remainder of its state and draw itself
#if !defined(Q_OS_MAC)
    m_mesh->render();
#else
    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    m_mesh->bindBuffers();
    m_mesh->render();
    glPopClientAttrib();
#endif

    // Unbind shader
    shader->release();

    // Restore texture unit state
    //  - remove sampler objects
    //  - set active texture unit back to the first unit
#if !defined(Q_OS_MAC)
    m_funcs->glBindSampler( 0, 0 );
    m_funcs->glBindSampler( 1, 0 );
#endif
    m_funcs->glActiveTexture( GL_TEXTURE0 );

    //dumpTextureUnitConfiguration();
}
Example #11
0
//! [3]
void GlWidget::paintGL()
{
    //! [3]
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;

    QMatrix4x4 cameraTransformation;
    cameraTransformation.rotate(alpha, 0, 1, 0);
    cameraTransformation.rotate(beta, 1, 0, 0);

    QVector3D cameraPosition = cameraTransformation * QVector3D(0, 0, distance);
    QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);

    vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);

    mMatrix.setToIdentity();

    QMatrix4x4 mvMatrix;
    mvMatrix = vMatrix * mMatrix;

    QMatrix3x3 normalMatrix;
    normalMatrix = mvMatrix.normalMatrix();

    QMatrix4x4 lightTransformation;
    lightTransformation.rotate(lightAngle, 0, 1, 0);

    QVector3D lightPosition = lightTransformation * QVector3D(0, 1, 1);

    lightingShaderProgram.bind();

    lightingShaderProgram.setUniformValue("mvpMatrix", pMatrix * mvMatrix);
    lightingShaderProgram.setUniformValue("mvMatrix", mvMatrix);
    lightingShaderProgram.setUniformValue("normalMatrix", normalMatrix);
    lightingShaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition);

    lightingShaderProgram.setUniformValue("ambientColor", QColor(32, 32, 32));
    lightingShaderProgram.setUniformValue("diffuseColor", QColor(128, 128, 128));
    lightingShaderProgram.setUniformValue("specularColor", QColor(255, 255, 255));
    lightingShaderProgram.setUniformValue("ambientReflection", (GLfloat) 1.0);
    lightingShaderProgram.setUniformValue("diffuseReflection", (GLfloat) 1.0);
    lightingShaderProgram.setUniformValue("specularReflection", (GLfloat) 1.0);
    lightingShaderProgram.setUniformValue("shininess", (GLfloat) 100.0);
    lightingShaderProgram.setUniformValue("texture", 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, cubeTexture);
    glActiveTexture(0);

    //! [4]
    cubeBuffer.bind();
    int offset = 0;
    lightingShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);
    lightingShaderProgram.enableAttributeArray("vertex");
    offset += numCubeVertices * 3 * sizeof(GLfloat);
    lightingShaderProgram.setAttributeBuffer("normal", GL_FLOAT, offset, 3, 0);
    lightingShaderProgram.enableAttributeArray("normal");
    offset += numCubeVertices * 3 * sizeof(GLfloat);
    lightingShaderProgram.setAttributeBuffer("textureCoordinate", GL_FLOAT, offset, 2, 0);
    lightingShaderProgram.enableAttributeArray("textureCoordinate");
    cubeBuffer.release();

    glDrawArrays(GL_TRIANGLES, 0, numCubeVertices);
    //! [4]

    lightingShaderProgram.disableAttributeArray("vertex");
    lightingShaderProgram.disableAttributeArray("normal");
    lightingShaderProgram.disableAttributeArray("textureCoordinate");

    lightingShaderProgram.release();

    mMatrix.setToIdentity();
    mMatrix.translate(lightPosition);
    mMatrix.rotate(lightAngle, 0, 1, 0);
    mMatrix.rotate(45, 1, 0, 0);
    mMatrix.scale(0.1);

    coloringShaderProgram.bind();

    coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

    //! [5]
    spotlightBuffer.bind();
    offset = 0;
    coloringShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);
    coloringShaderProgram.enableAttributeArray("vertex");
    offset += numSpotlightVertices * 3 * sizeof(GLfloat);
    coloringShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0);
    coloringShaderProgram.enableAttributeArray("color");
    spotlightBuffer.release();

    glDrawArrays(GL_TRIANGLES, 0, numSpotlightVertices);
    //! [5]

    coloringShaderProgram.disableAttributeArray("vertex");
    coloringShaderProgram.disableAttributeArray("color");

    coloringShaderProgram.release();
    //! [6]
}
void InstancedHistogramScene::render()
{
    glDepthMask( true );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    {
        QMutexLocker locker( &m_mutex );
        glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
        m_dataBuffer.bind();
        m_dataBuffer.allocate( m_data.data(), m_data.size() * sizeof( float ) );
        glPopClientAttrib();

        // Bind the shader program
        m_cube->material()->bind();
        QOpenGLShaderProgramPtr shader = m_cube->material()->shader();

        // Calculate needed matrices
        m_modelMatrix.setToIdentity();
        m_modelMatrix.rotate( m_theta, 0.0f, 1.0f, 0.0f );

        QMatrix4x4 modelViewMatrix = m_camera->viewMatrix() * m_modelMatrix;
        QMatrix3x3 normalMatrix = modelViewMatrix.normalMatrix();
        shader->setUniformValue( "modelViewMatrix", modelViewMatrix );
        shader->setUniformValue( "normalMatrix", normalMatrix );
        shader->setUniformValue( "projectionMatrix", m_camera->projectionMatrix() );
    }

    // Scale the x-z dimensions of the cuboids
    QOpenGLShaderProgramPtr shader = m_cube->material()->shader();
    shader->setUniformValue( "cubeScale", QVector2D( 0.25f, 0.25f ) );

    // Set the lighting parameters
    shader->setUniformValue( "light.position", QVector4D( 0.0f, 0.0f, 0.0f, 1.0f ) );
    shader->setUniformValue( "light.intensity", QVector3D( 1.0f, 1.0f, 1.0f ) );
    shader->setUniformValue( "material.ks", QVector3D( 0.95f, 0.95f, 0.95f ) );
    shader->setUniformValue( "material.ka", QVector3D( 0.1f, 0.1f, 0.1f ) );
    shader->setUniformValue( "material.shininess", 100.0f );

    // Draw the cuboids
#if !defined(Q_OS_MAC)
    m_cube->vertexArrayObject()->bind();
    m_funcs->glDrawElementsInstanced( GL_TRIANGLES, m_cube->indexCount(), GL_UNSIGNED_INT, 0, pointCount );
#else
    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    m_cube->bindBuffers();

    m_dataBuffer.bind();
    shader->enableAttributeArray( "point" );
    shader->setAttributeBuffer( "point", GL_FLOAT, 0, 3 );
    GLuint pointLocation = shader->attributeLocation( "point" );
    m_instanceFuncs->glVertexAttribDivisorARB( pointLocation, 1 );

    m_drawInstanced->glDrawElementsInstancedARB( GL_TRIANGLES, m_cube->indexCount(), GL_UNSIGNED_INT, 0, pointCount );
    glPopClientAttrib();
#endif

    // Draw the background sphere
    m_sphere->material()->bind();
    shader = m_sphere->material()->shader();

    {
        QMutexLocker locker( &m_mutex );

        // Set the mvp matrix
        QMatrix4x4 mvp = m_camera->viewProjectionMatrix() * m_sphereModelMatrix;
        shader->setUniformValue( "mvp", mvp );
        shader->setUniformValue( "time", m_time );
        shader->setUniformValue( "texCoordScale", m_scale );
        shader->setUniformValue( "texCoordTimeScale", m_timeScale );
        QVector3D colorBias( m_redBias, m_greenBias, m_blueBias );
        shader->setUniformValue( "colorBias", colorBias );
    }

    // Let the mesh setup the remainder of its state and draw itself
#if !defined(Q_OS_MAC)
    m_sphere->render();
#else
    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    m_sphere->bindBuffers();
    m_sphere->render();
    glPopClientAttrib();
#endif

    shader->release();
}
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();
}
void OpenGLWidget :: paintGL () {
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;

//    qDebug() << "paintGL 1";

    if( objects.size() == 0 ) return;

    std::list<Entity*>::iterator object = objects.begin();

//    qDebug() << "paintGL 2";
    Entity * obj;


    modelView.setToIdentity () ;
    modelView.lookAt ( camera.eye , camera.at , camera.up ) ;

//    qDebug() << "paintGL 3";

    QMatrix4x4 modelViewMatrix;

    while( object != objects.end() ){
        obj = (*object);

//        qDebug() << "paintGL 4";

        if( !obj->live ){
            object++;
            continue;
        }

        obj->getShaderProgram()->bind();
//        qDebug() << "paintGL 5";

        ambientProduct = light.ambient * obj->getMaterial()->ambient ;
        diffuseProduct = light.diffuse * obj->getMaterial()->diffuse ;
        specularProduct = light.specular * obj->getMaterial()->specular ;

//        qDebug() << "paintGL 6";

        modelViewMatrix = modelView * obj->getTransformation();

        obj->getShaderProgram()->setUniformValue("lightPosition", light.position ) ;
        obj->getShaderProgram()->setUniformValue("ambientProduct", ambientProduct ) ;
        obj->getShaderProgram()->setUniformValue("diffuseProduct", diffuseProduct ) ;
        obj->getShaderProgram()->setUniformValue("specularProduct", specularProduct ) ;
        obj->getShaderProgram()->setUniformValue("shininess", static_cast < GLfloat >( material.shininess ) ) ;

        obj->getShaderProgram()->setUniformValue("modelView", modelViewMatrix ) ;
        obj->getShaderProgram()->setUniformValue("normalMatrix", modelViewMatrix.normalMatrix() ) ;
        obj->getShaderProgram()->setUniformValue("projectionMatrix", projectionMatrix );

//        qDebug() << "paintGL 7";

        obj->getVboVertices()-> bind () ;
        obj->getShaderProgram() -> enableAttributeArray ("vPosition") ;
        obj->getShaderProgram() -> setAttributeBuffer ("vPosition", GL_FLOAT , 0 , 4 , 0) ;

//        qDebug() << "paintGL 8";

        obj->getVboNormals()->bind();
        obj->getShaderProgram() -> enableAttributeArray ("vNormal") ;
        obj->getShaderProgram() -> setAttributeBuffer ("vNormal", GL_FLOAT , 0 , 3 , 0) ;

//        qDebug() << "paintGL 9";

        obj->getVboTangents()->bind();
        obj->getShaderProgram()->enableAttributeArray ("vTangent") ;
        obj->getShaderProgram()->setAttributeBuffer ("vTangent", GL_FLOAT , 0 , 4 , 0) ;

//        qDebug() << "paintGL 10";

        obj->getVboIndices()->bind() ;

//        qDebug() << "paintGL 11";

        obj->getVbocoordText() -> bind ();
        obj->getShaderProgram() -> enableAttributeArray ("vcoordText") ;
        obj->getShaderProgram() -> setAttributeBuffer ("vcoordText", GL_FLOAT , 0 , 2 , 0) ;

//        qDebug() << "paintGL 12";

        obj->getMaterial()->texture -> bind (0) ;
        obj->getShaderProgram() -> setUniformValue ("colorTexture", 0) ;

        if( obj->getMaterial()->map1 != NULL ){
            obj->getMaterial()->map1 -> bind (1) ;
            obj->getShaderProgram() -> setUniformValue ("colorMap", 1);
        }

//        qDebug() << "paintGL 13";

        glDrawElements ( GL_TRIANGLES , obj->getFacesCount() * 3 , GL_UNSIGNED_INT , 0) ;
//       qDebug() << "paintGL 14";

        obj->getShaderProgram()->release();
        obj->getVboVertices()->release();
        obj->getVboNormals()->release();
        obj->getVboIndices()->release();
        obj->getVbocoordText()->release();
        obj->getMaterial()->texture->release(0);

        if( obj->getMaterial()->map1 != NULL ){
            obj->getMaterial()->map1->release(1);
        }

//        qDebug() << "paintGL 15";

        object++;
    }
//    qDebug() << "paintGL 16";

    float deltaTime = (float) elapsedTime.elapsed() / 1000;
    gameLogic( deltaTime );
    elapsedTime.restart();
}
Example #15
0
//
//  Draw the window
//
void Ex07opengl::paintGL()
{
    //  Wall time (seconds)
    float t = 0.001*time.elapsed();
    if (move) zh = fmod(90*t,360);

    //  Clear screen and Z-buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    //  Translate intensity to color vectors
    float Ambient[]  = {0.3,0.3,0.3,1.0};
    float Diffuse[]  = {0.8,0.8,0.8,1.0};
    float Specular[] = {1.0,1.0,1.0,1.0};
    float Position[] = {(float)(3*Cos(zh)),z0,(float)(3*Sin(zh)),1.0};

    //  Draw light position (no lighting yet)
    glColor3f(1,1,1);
    ball(Position[0],Position[1],Position[2] , 0.1);

    //  Set view
    glLoadIdentity();
    if (fov) glTranslated(0,0,-2*dim);
    glRotated(ph,1,0,0);
    glRotated(th,0,1,0);

    //  Fixed pipeline
    if (mode==0)
    {
        //  OpenGL should normalize normal vectors
        glEnable(GL_NORMALIZE);
        //  Enable lighting
        glEnable(GL_LIGHTING);
        //  glColor sets ambient and diffuse color materials
        glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
        glEnable(GL_COLOR_MATERIAL);
        //  Enable light 0
        glEnable(GL_LIGHT0);
        //  Set ambient, diffuse, specular components and position of light 0
        glLightfv(GL_LIGHT0,GL_AMBIENT ,Ambient);
        glLightfv(GL_LIGHT0,GL_DIFFUSE ,Diffuse);
        glLightfv(GL_LIGHT0,GL_SPECULAR,Specular);
        glLightfv(GL_LIGHT0,GL_POSITION,Position);

        //  Enable textures
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,tex);

        //  Enabe arrays
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        //  Set pointers

        glVertexPointer  (4,GL_FLOAT,12*sizeof(GLfloat),cube_data);
        glNormalPointer  (  GL_FLOAT,12*sizeof(GLfloat),cube_data+4);
        glColorPointer   (3,GL_FLOAT,12*sizeof(GLfloat),cube_data+7);
        glTexCoordPointer(2,GL_FLOAT,12*sizeof(GLfloat),cube_data+10);

        //  Draw the cube
        glDrawArrays(GL_TRIANGLES,0,cube_size);

        //  Disable arrays
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //  Disable textures and lighting
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_LIGHTING);
    }
    //  OpenGL 4 style shaders
    else
    {
        //  Create View matrix
        QMatrix4x4 view;
        if (fov) view.translate(0,0,-2*dim);
        view.rotate(ph,1,0,0);
        view.rotate(th,0,1,0);
        //  Create ModelView matrix
        QMatrix4x4 mv = view;

        // Enable shader
        shader.bind();
        //  Set Modelview, Normal and Projection Matrix
        shader.setUniformValue("ProjectionMatrix",proj);
        shader.setUniformValue("ViewMatrix",view);
        shader.setUniformValue("ModelViewMatrix",mv);
        shader.setUniformValue("NormalMatrix",mv.normalMatrix());

        //  Light Properties
        shader.setUniformValue("Ambient" ,QVector3D(Ambient[0],Ambient[1],Ambient[2]));
        shader.setUniformValue("Diffuse" ,QVector3D(Diffuse[0],Diffuse[1],Diffuse[2]));
        shader.setUniformValue("Specular",QVector3D(Specular[0],Specular[1],Specular[2]));
        shader.setUniformValue("Position",QVector4D(Position[0],Position[1],Position[2],Position[3]));

        //  Select cube buffer
        cube_buffer.bind();
        //   Attribute 0: vertex coordinate (vec4) at offset 0
        shader.enableAttributeArray(0);
        shader.setAttributeBuffer(0,GL_FLOAT,0,4,12*sizeof(float));
        //   Attribute 1:  vertex color (vec3) offset 7 floats
        shader.enableAttributeArray(1);
        shader.setAttributeBuffer(1,GL_FLOAT,7*sizeof(float),3,12*sizeof(float));
        //   Attribute 2:  vertex normal (vec3) offset 4 floats
        shader.enableAttributeArray(2);
        shader.setAttributeBuffer(2,GL_FLOAT,4*sizeof(float),3,12*sizeof(float));
        //   Attribute 3:  texture coordinates (vec32 offset 10 floats
        shader.enableAttributeArray(3);
        shader.setAttributeBuffer(3,GL_FLOAT,10*sizeof(float),2,12*sizeof(float));

        // Draw the cube
        glDrawArrays(GL_TRIANGLES,0,cube_size);

        //  Disable vertex arrays
        shader.disableAttributeArray(0);
        shader.disableAttributeArray(1);
        shader.disableAttributeArray(2);
        shader.disableAttributeArray(3);

        //  Unbind this buffer
        cube_buffer.release();

        // Back to fixed pipeline
        shader.release();
    }

    //  Axes for reference
    glColor3f(1,1,1);
    glBegin(GL_LINES);
    glVertex3f(0,0,0);
    glVertex3f(2,0,0);
    glVertex3f(0,0,0);
    glVertex3f(0,2,0);
    glVertex3f(0,0,0);
    glVertex3f(0,0,2);
    glEnd();
    glDisable(GL_DEPTH_TEST);
    renderText(2,0,0,"X");
    renderText(0,2,0,"Y");
    renderText(0,0,2,"Z");

    //  Emit angles to display
    emit angles(QString::number(th)+","+QString::number(ph));
    //  Emit light angle
    emit light((int)zh);
}
Example #16
0
void SegmentGL::RenderContour(Segment *seg, QMatrix4x4 projection, QMatrix4x4 modelview, int width, int height)
{

    QVector3D vec;
    QVector3D pos;
    QVector<GLfloat> positions;
    QEci qeci;


    CalculateSegmentContour(&positions, seg->cornerpointfirst1.latitude, seg->cornerpointfirst1.longitude, seg->cornerpointlast1.latitude, seg->cornerpointlast1.longitude);
    CalculateSegmentContour(&positions,seg->cornerpointlast1.latitude, seg->cornerpointlast1.longitude, seg->cornerpointlast2.latitude, seg->cornerpointlast2.longitude);
    CalculateSegmentContour(&positions, seg->cornerpointlast2.latitude, seg->cornerpointlast2.longitude, seg->cornerpointfirst2.latitude, seg->cornerpointfirst2.longitude);
    CalculateSegmentContour(&positions,seg->cornerpointfirst2.latitude, seg->cornerpointfirst2.longitude, seg->cornerpointfirst1.latitude, seg->cornerpointfirst1.longitude);

    seg->qsgp4->getPosition(seg->minutes_since_state_vector, qeci);
    QGeodetic qgeo = qeci.ToGeo();
    double lat1 = qgeo.latitude;
    double lon1 = qgeo.longitude;

    seg->qsgp4->getPosition(seg->minutes_since_state_vector + seg->minutes_sensing, qeci);
    qgeo = qeci.ToGeo();
    double lat2 = qgeo.latitude;
    double lon2 = qgeo.longitude;

    CalculateSegmentContour(&positions, lat1, lon1, lat2, lon2);

    positionsBuf.bind();
    positionsBuf.write(0, positions.data(), positions.size() * sizeof(GLfloat));
    positionsBuf.release();

    QOpenGLVertexArrayObject::Binder vaoBinder(&vao);

    program->bind();
    program->setUniformValue("MVP", projection * modelview);
    QColor rendercolor(opts.satsegmentcolor);
    QColor rendercolorsel(opts.satsegmentcolorsel);

    if((*seg).segmentselected)
        program->setUniformValue("outcolor", QVector4D(rendercolorsel.redF(), rendercolorsel.greenF(), rendercolorsel.blueF(), 1.0f));
    else
        program->setUniformValue("outcolor", QVector4D(rendercolor.redF(), rendercolor.greenF(), rendercolor.blueF(), 1.0f));

    QMatrix3x3 norm = modelview.normalMatrix();
    program->setUniformValue("NormalMatrix", norm);

    glDrawArrays(GL_LINE_LOOP, 0, nbrOfVertices - 10);
    glDrawArrays(GL_LINE_STRIP, nbrOfVertices - 10, 10);


    float mvmatrix[16], projmatrix[16];
    QMatrix4x4 MVP;
    MVP = projection * modelview;

    float *ptr = modelview.data();
    for(int i = 0; i < 16; i++)
        mvmatrix[i] = *(ptr + i);

    ptr = projection.data();
    for(int i = 0; i < 16; i++)
        projmatrix[i] = *(ptr + i);

    QVector2D win;

    LonLat2PointRad((float)seg->cornerpointfirst1.latitude, (float)seg->cornerpointfirst1.longitude, &vec, 1.0);
    win = glhProjectf (vec, mvmatrix, projmatrix, width, height);
    seg->winvecend1 = win;

    LonLat2PointRad((float)seg->cornerpointfirst2.latitude, (float)seg->cornerpointfirst2.longitude, &vec, 1.0);
    win = glhProjectf (vec, mvmatrix, projmatrix, width, height);
    seg->winvecend2 = win;

    LonLat2PointRad((float)seg->cornerpointlast1.latitude, (float)seg->cornerpointlast1.longitude, &vec, 1.0);
    win = glhProjectf (vec, mvmatrix, projmatrix, width, height);
    seg->winvecend3 = win;

    LonLat2PointRad((float)seg->cornerpointlast2.latitude, (float)seg->cornerpointlast2.longitude, &vec, 1.0);
    win = glhProjectf (vec, mvmatrix, projmatrix, width, height);
    seg->winvecend4 = win;

    win = glhProjectf (seg->vec1, mvmatrix, projmatrix, width, height);
    seg->winvec1 = win;

    win = glhProjectf (seg->vec2, mvmatrix, projmatrix, width, height);
    seg->winvec2 = win;

}
Example #17
0
void SatGL::RenderTrail(Satellite *sat, QMatrix4x4 projection, float distance, QQuaternion quat, bool trackon) // QMatrix4x4 modelview, bool trackon)
{
    QVector<GLfloat> postrail;

    double
            tsince,            // Time since epoch (in minutes)
            jul_epoch,         // Julian date of epoch
            jul_utc;           // Julian UTC date


    QSgp4Date nowutc = QSgp4Date::NowUTC();
    jul_utc = nowutc.Julian();
    jul_epoch = Julian_Date_of_Epoch(sat->GetEpoch());

    QMatrix4x4 model;
    model.translate(0.0, 0.0, distance);
    model.rotate(quat);

    QMatrix4x4 modelview;
    modelview = model;
    QMatrix4x4 modelocta;
    QColor col(0,255,255);

    QEci qeci;
    QVector3D pos;
    double id;
    int nbrVertices = 0;

    tsince = (jul_utc - jul_epoch) * MIN_PER_DAY; // in minutes

    for( id = tsince - 5; id < tsince; id++ )
    {
        (*sat).qsgp4->getPosition(id, qeci);
        QGeodetic qgeo = qeci.ToGeo();
        LonLat2PointRad(qgeo.latitude, qgeo.longitude, &pos, 1.001f);
        if(id < tsince && id >= tsince - 5 )
        {
            modelocta = model;
            modelocta.translate(pos.x(), pos.y(), pos.z());
            modelocta.scale(0.004f);
            octa->render(projection, modelocta, col);
        }
    }


    if(trackon)
    {
        for( id = tsince - opts.realminutesshown + 1; id <= tsince + opts.realminutesshown; id++ )  // nbr of id's = 2 * opts.realminutesshown
        {
            (*sat).qsgp4->getPosition(id, qeci);
            QGeodetic qgeo = qeci.ToGeo();
            LonLat2PointRad(qgeo.latitude, qgeo.longitude, &pos, 1.001f);
            postrail.append(pos.x());
            postrail.append(pos.y());
            postrail.append(pos.z());
        }

        positionsTrail.bind();

        if(tdiff != opts.realminutesshown)
        {
            tdiff = opts.realminutesshown;
            positionsTrail.allocate(postrail.data(), postrail.size() * sizeof(GLfloat));

        }
        else
        {
            positionsTrail.write(0, postrail.data(), postrail.size() * sizeof(GLfloat));
        }

        nbrVertices = positionsTrail.size() / (3 * sizeof(GLfloat));

        positionsTrail.release();

        QOpenGLVertexArrayObject::Binder vaoBinder(&vaotrail);

        program->bind();

        program->setUniformValue("MVP", projection * modelview);
        QMatrix3x3 norm = modelview.normalMatrix();
        program->setUniformValue("NormalMatrix", norm);

        QColor rendercolor(opts.sattrackcolor);
        program->setUniformValue("outcolor", QVector4D(rendercolor.redF(), rendercolor.greenF(), rendercolor.blueF(), 1.0f));

        glDrawArrays(GL_LINE_STRIP, 0, nbrVertices);

    }
}
void CubeMapRefractionScene::render()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Set up the view matrix
    QVector3D eye( 0.0f, 0.0f, 0.0f );
    QVector3D center( 0.0f, 0.0f, 1.0f );
    QVector3D up( 0.0f, 1.0f, 0.0f );
    m_viewMatrix.setToIdentity();
    m_viewMatrix.lookAt( eye, center, up );
    m_viewMatrix.rotate( m_phi, 1.0f, 0.0f, 0.0f );
    m_viewMatrix.rotate( m_theta, 0.0f, 1.0f, 0.0f );

    m_modelMatrix.setToIdentity();
    QMatrix4x4 modelViewMatrix = m_viewMatrix * m_modelMatrix;
    QMatrix4x4 mvp = m_projectionMatrix * modelViewMatrix;

    // We need a pointer to the material's shader to set uniform variables
    QOpenGLShaderProgramPtr shader = m_skyBox->material()->shader();

    // Draw the skybox
    shader->setUniformValue( "drawSkyBox", true );
    glDepthFunc(GL_LEQUAL);
    m_skyBox->render( mvp );
    glDepthFunc(GL_LESS);

    eye = QVector3D( 0.0f, 0.0f, -4.0f );
    m_viewMatrix.setToIdentity();
    m_viewMatrix.lookAt( eye, center, up );
    m_viewMatrix.rotate( m_phi, 1.0f, 0.0f, 0.0f );
    m_viewMatrix.rotate( m_theta, 0.0f, 1.0f, 0.0f );

    // Calculate the position of the camera in world coordinates
    QMatrix4x4 rot;
    rot.rotate( -m_theta, 0.0f, 1.0f, 0.0f );
    rot.rotate( -m_phi, 1.0f, 0.0f, 0.0f );
    QVector4D worldEye = rot * eye;
    QVector3D worldCamera = worldEye.toVector3D();

    // Rotate (and scale the model)
    m_modelMatrix.setToIdentity();
    static float theta = 0.0f;
    theta += 0.3f;
    m_modelMatrix.rotate( theta, 0.0f, 1.0f, 0.0f );
    m_modelMatrix.scale( 0.05f ); // Scale the toyplane mesh down to a reasonable size

    modelViewMatrix = m_viewMatrix * m_modelMatrix;
    mvp = m_projectionMatrix * modelViewMatrix;
    QMatrix3x3 normalMatrix = modelViewMatrix.normalMatrix();

    shader->setUniformValue( "worldCameraPosition", worldCamera );
    shader->setUniformValue( "modelViewMatrix", modelViewMatrix );
    shader->setUniformValue( "modelMatrix", m_modelMatrix );
    shader->setUniformValue( "normalMatrix", normalMatrix );
    shader->setUniformValue( "projectionMatrix", m_projectionMatrix );
    shader->setUniformValue( "mvp", mvp );

    // Draw the reflective mesh
    shader->setUniformValue( "drawSkyBox", false );
    shader->setUniformValue( "material.eta", 0.96f );
    shader->setUniformValue( "material.reflectionFactor", 0.0f );

    m_mesh->render();
}
Example #19
0
void Visualizer::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 mMatrix;
    QMatrix4x4 vMatrix;

    QMatrix4x4 cameraTransformation;
    cameraTransformation.rotate(alpha,0,1,0);
    cameraTransformation.rotate(beta,1,0,0);

    QVector3D cameraPosition = cameraTransformation * QVector3D(0,0,distance);
    QVector3D cameraUpDirection = cameraTransformation * QVector3D(0,1,0);

    vMatrix.lookAt(cameraPosition,QVector3D(0,0,0),cameraUpDirection);

    mMatrix.setToIdentity();

    QMatrix4x4 mvMatrix;
    mvMatrix = vMatrix * mMatrix;

    QMatrix3x3 normalMatrix;
    normalMatrix = mvMatrix.normalMatrix();

    QMatrix4x4 lightTransformation;
    lightTransformation.rotate(lightAngle,0 ,1, 0);

    //QVector3D lightPosition = lightTransformation * QVector3D(0,1,1);
    QVector3D lightPosition = QVector3D(0,1,1);
    QVector3D leftlightPosition = QVector3D(-1,0,0);

    int offset;
//    for(int k=0;k<myBlocks.size();k++)
    for(int k=0;k<27;k++)
    {

        mMatrix.setToIdentity();

//        mMatrix *= stones[k].mMatrix;
//        mMatrix.translate(stones[k].position);
        if(isRotating)
        {
            mMatrix = myBlocks.value(k)->getModelMatrix() * mMatrix;
            mMatrix.translate(myBlocks.value(k)->getPosition());
        }
        if(!isRotating)
        {

            mMatrix.translate(myBlocks.value(k)->getPosition());
            mMatrix = myBlocks.value(k)->getOrientation() * mMatrix ;
        }
        //mMatrix = myBlocks.value(k)->getOrientation() * mMatrix;

        mMatrix.scale(1);
        mvMatrix = vMatrix * mMatrix;
        normalMatrix = mvMatrix.normalMatrix();

        m_pBufferList[k]->shaderProgram->bind();
        m_pBufferList[k]->shaderProgram->setUniformValue("mvpMatrix", pMatrix * mvMatrix);
        m_pBufferList[k]->shaderProgram->setUniformValue("mvMatrix", mvMatrix);
        m_pBufferList[k]->shaderProgram->setUniformValue("normalMatrix", normalMatrix);
        m_pBufferList[k]->shaderProgram->setUniformValue("lightPosition", vMatrix * lightPosition);
        m_pBufferList[k]->shaderProgram->setUniformValue("leftlightPosition", vMatrix * leftlightPosition);

        m_pBufferList[k]->shaderProgram->setUniformValue("ambientColor", QColor(162,162,162));
        m_pBufferList[k]->shaderProgram->setUniformValue("diffuseColor", QColor(128,128,128));
        m_pBufferList[k]->shaderProgram->setUniformValue("specularColor", QColor(255,255,255));
        m_pBufferList[k]->shaderProgram->setUniformValue("ambientReflection", (GLfloat) 1.0);
        m_pBufferList[k]->shaderProgram->setUniformValue("diffuseReflection", (GLfloat) 1.0);
        m_pBufferList[k]->shaderProgram->setUniformValue("specularReflection", (GLfloat) 1.0);
        m_pBufferList[k]->shaderProgram->setUniformValue("shininess", (GLfloat) 100.0);
        //m_pBufferList[0]->shaderProgram->setUniformValue("texture",0);

        //glBindTexture(GL_TEXTURE_2D,cubeTexture);



        WriteBufferToGPU(m_pBufferList[k],0);
        glDrawArrays(GL_TRIANGLES, 0, m_pBufferList[k]->numVertices);
        releaseShaderProgram(m_pBufferList[k],0);

    }


    mMatrix.setToIdentity();
    mMatrix.translate(lightPosition);
    mMatrix.rotate(lightAngle,0,1,0);
    mMatrix.rotate(45,1,0,0);
    mMatrix.scale(0.1);

    coloringShaderProgram.bind();

    coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix*vMatrix*mMatrix);

    offset = 0;
    spotlightBuffer.bind();
    coloringShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);
    coloringShaderProgram.enableAttributeArray("vertex");
    offset += numSpotlightVertices * 3 * sizeof (GLfloat);
    coloringShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0);
    coloringShaderProgram.enableAttributeArray("color");

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

    coloringShaderProgram.disableAttributeArray("vertex");
    coloringShaderProgram.disableAttributeArray("color");

    coloringShaderProgram.release();

    mMatrix.setToIdentity();
    mMatrix.translate(-2,0,0);
    mMatrix.scale(0.1);

    leftlightShaderProgram.bind();
    leftlightShaderProgram.setUniformValue("mvpMatrix", pMatrix*vMatrix*mMatrix);

    blenderObjectFileBuffer.bind();
    offset = 0;
    leftlightShaderProgram.setAttributeBuffer("vertex", GL_FLOAT, offset, 3, 0);
    leftlightShaderProgram.enableAttributeArray("vertex");
    offset += numSpotlightVertices * 3 * sizeof (GLfloat);
    leftlightShaderProgram.setAttributeBuffer("color", GL_FLOAT, offset, 3, 0);
    leftlightShaderProgram.enableAttributeArray("color");

    glDrawArrays(GL_TRIANGLES, 0, 12);

    leftlightShaderProgram.disableAttributeArray("vertex");
    leftlightShaderProgram.disableAttributeArray("color");

    leftlightShaderProgram.release();

}
Example #20
0
void GLWidget::renderAxis3D()
{
    QMatrix4x4 axisPos[] = {modelView, modelView, modelView};

    axisPos[0].rotate(-90.0f, 0.0f, 0.0f, 1.0f);
    axisPos[2].rotate(-90.0f, 1.0f, 0.0f);

    float color[] =
    {
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    };

    program->setUniformValue("color", 1.0f, 1.0f, 1.0f, 1.0f);
    program->setUniformValue("modelViewProj", proj * modelView);
    program->setUniformValue("modelView", modelView);
    program->setUniformValue("normalMatr", modelView.normalMatrix());
    program->setUniformValue("normMethod", 0);
    axisSphereBuffer.bind();
    program->setAttributeArray("vertexis", GL_FLOAT, NULL, 3);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, axisSphereSize / 3);
    axisBottomSphereBuffer.bind();
    program->setAttributeArray("vertexis", GL_FLOAT, NULL, 3);
    glDrawArrays(GL_TRIANGLE_FAN, 0, axisBottomSphereSize / 3);

    for (int i = 0; i < 3; i++)
    {
        program->setUniformValue("color", color[i * 4], color[i * 4 + 1], color[i * 4 + 2], color[i * 4 + 3]);

        program->setUniformValue("modelViewProj", proj * axisPos[i]);
        program->setUniformValue("modelView", axisPos[i]);
        program->setUniformValue("normalMatr", axisPos[i].normalMatrix());
        program->setUniformValue("normMethod", 2);
        axisCylynderBuffer.bind();
        program->setAttributeArray("vertexis", GL_FLOAT, NULL, 3);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, axisCylynderSize / 3);

        QMatrix4x4 tmp = axisPos[i];
        tmp.translate(0.0f, 0.5f, 0.0f);
        tmp.rotate(180.0f, 1.0f, 0.0f);
        program->setUniformValue("modelViewProj", proj * tmp);
        program->setUniformValue("modelView", tmp);
        program->setUniformValue("normalMatr", tmp.normalMatrix());
        program->setUniformValue("normMethod", 1);

        axisDiskBuffer.bind();
        program->setAttributeArray("vertexis", GL_FLOAT, NULL, 3);
        glDrawArrays(GL_TRIANGLE_FAN, 0, axisDiskSize / 3);

        tmp.rotate(180.0f, 1.0f, 0.0f);
        program->setUniformValue("modelViewProj", proj * tmp);
        program->setUniformValue("modelView", tmp);
        program->setUniformValue("normalMatr", tmp.normalMatrix());
        program->setUniformValue("normMethod", 1);

        axisConeBuffer.bind();
        program->setAttributeArray("vertexis", GL_FLOAT, NULL, 3);
        glDrawArrays(GL_TRIANGLE_FAN, 0, axisConeSize / 3);

    }

}
Example #21
0
void SatGL::render(QMatrix4x4 projection, float distance, QQuaternion quat )
{
    QMatrix4x4 modelview;
    modelview.translate(0.0, 0.0, distance);
    modelview.rotate(quat);


    QVector<GLfloat> positions;

    QList<Satellite>::iterator sat = sats->GetSatlist()->begin();

    int nbrSats = 0;
    int nbrVertices;

    QMatrix4x4 modelocta;
    QColor col(255, 0, 0);
    float alt;
    while ( sat != sats->GetSatlist()->end() )
    {
        if( (*sat).active  == true)
        {
            QVector3D pos, posnorm;
            (*sat).GetSatellitePosition(pos, posnorm, alt);
            positions.append(0.0f);
            positions.append(0.0f);
            positions.append(0.0f);
            positions.append(pos.x());
            positions.append(pos.y());
            positions.append(pos.z());
            modelocta = modelview;
            modelocta.translate(posnorm.x(), posnorm.y(), posnorm.z());
            modelocta.scale(0.005f);

            octa->render(projection, modelocta, col);
            QColor horizoncolour(opts.sathorizoncolor);
            horizon->render(projection, distance, quat, posnorm, alt, horizoncolour);

            nbrSats++;
        }
        ++sat;
    }

    positionsBuf.bind();
    if(nbrSats != nbrActiveSats)
    {
        nbrActiveSats = nbrSats;

        positionsBuf.allocate(positions.data(), positions.size() * sizeof(GLfloat));

        qDebug() << "positionsBuf.size() != nbrActiveSats * 2 * 3 * sizeof(GLfloat)";
        qDebug() << "nbrActiveSats = " << nbrActiveSats << " positionsBuf.size() = " << positionsBuf.size();
    }
    else
    {
        positionsBuf.write(0, positions.data(), positions.size() * sizeof(GLfloat));
    }

    nbrVertices = positionsBuf.size() / (3 * sizeof(GLfloat));

    positionsBuf.release();

    QOpenGLVertexArrayObject::Binder vaoBinder(&vao);

    program->bind();

    program->setUniformValue("MVP", projection * modelview);
    QMatrix3x3 norm = modelview.normalMatrix();
    program->setUniformValue("NormalMatrix", norm);

    QColor rendercolor(255, 0, 0);
    program->setUniformValue("outcolor", QVector4D(rendercolor.redF(), rendercolor.greenF(), rendercolor.blueF(), 1.0f));

    glDrawArrays(GL_LINES, 0, nbrVertices);

    sat = sats->GetSatlist()->begin();
    while ( sat != sats->GetSatlist()->end() )
    {
        if( (*sat).active  == true)
        {
            if( (*sat).GetCatalogueNbr() == sats->GetSelectedSat() )
                RenderTrail(&(*sat), projection, distance, quat, true);
            else
                RenderTrail(&(*sat), projection, distance, quat, false);
        }
        ++sat;
    }

    //    QMatrix4x4 mod;
    //    mod.setToIdentity();
    //    mod.translate(0.0, 0.0, distance);
    //    mod.rotate(quat);

    //    modelocta = mod;
    //    modelocta.translate(0.0, 1.0, 0.0);
    //    modelocta.scale(0.009f);
    //    octa->render(projection, modelocta, col);

    //    modelocta = mod;
    //    modelocta.translate(0.0, 0.0, 1.0);
    //    modelocta.scale(0.009f);
    //    octa->render(projection, modelocta, col);

    //    modelocta = mod;
    //    modelocta.translate(1.0, 0.0, 0.0);
    //    modelocta.scale(0.009f);
    //    octa->render(projection, modelocta, col);


}
void Traingle::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;

mMatrix.setToIdentity();

QMatrix4x4 mvMatrix;
mvMatrix = vMatrix * mMatrix;
//vMatrix.setToIdentity();

QMatrix3x3 normalMatrix;
normalMatrix = mvMatrix.normalMatrix();

QMatrix4x4 lightTransformation;
lightTransformation.rotate(lightAngle,0,1,0);

QVector3D lightPosition = lightTransformation*QVector3D(0,1,1);

lightingShaderProgram.bind();
lightingShaderProgram.setUniformValue("mvpMatrix",pMatrix * mvMatrix);
lightingShaderProgram.setUniformValue("mvMatrix",mvMatrix);
lightingShaderProgram.setUniformValue("normalMatrix",normalMatrix);
lightingShaderProgram.setUniformValue("lightPosition", vMatrix * lightPosition);

lightingShaderProgram.setUniformValue("ambientColor",QColor(32,32,32));
lightingShaderProgram.setUniformValue("diffuesColor",QColor(128,128,128));
lightingShaderProgram.setUniformValue("ambientReflection", (GLfloat) 1.0);
lightingShaderProgram.setUniformValue("diffuseReflection", (GLfloat) 1.0);
lightingShaderProgram.setUniformValue("specularReflection", (GLfloat) 1.0);
lightingShaderProgram.setUniformValue("shininess", (GLfloat) 100.0);
lightingShaderProgram.setUniformValue("texture", 0);

glBindTexture(GL_TEXTURE_2D,cubeTexture);
//glActiveTe

lightingShaderProgram.setAttributeArray("vertex", cubeVertices.constData());
lightingShaderProgram.enableAttributeArray("vertex");
lightingShaderProgram.setAttributeArray("normal", cubeNormals.constData());
lightingShaderProgram.enableAttributeArray("normal");
lightingShaderProgram.setAttributeArray("textureCoordinate", cubeTextureCoordinates.constData());
lightingShaderProgram.enableAttributeArray("textureCoordinate");

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

lightingShaderProgram.disableAttributeArray("vertex");
lightingShaderProgram.disableAttributeArray("normal");
lightingShaderProgram.disableAttributeArray("textureCoordinate");
lightingShaderProgram.release();

QMatrix4x4 cameraTransformation;
// qDebug()<<QString("alpha %1 beta %2 distance %3").arg(alpha).arg(beta).arg(distance);
cameraTransformation.rotate(alpha,0,1,0);
cameraTransformation.rotate(beta,1,0,0);

QVector3D cameraPosition = cameraTransformation*QVector3D(0,0,distance);
QVector3D cameraUpDirection = cameraTransformation*QVector3D(0,1,0);

vMatrix.lookAt(cameraPosition, QVector3D(0,0,0),cameraUpDirection);

mMatrix.setToIdentity();
mMatrix.translate(lightPosition);
mMatrix.rotate(lightAngle, 0, 1, 0);
mMatrix.rotate(45, 1, 0, 0);
mMatrix.scale(0.1);

coloringShaderProgram.bind();
coloringShaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);

coloringShaderProgram.setAttributeArray("vertex", spotlightVertices.constData());
coloringShaderProgram.enableAttributeArray("vertex");

coloringShaderProgram.setAttributeArray("color", spotlightColors.constData());
coloringShaderProgram.enableAttributeArray("color");

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

coloringShaderProgram.disableAttributeArray("vertex");
coloringShaderProgram.disableAttributeArray("color");
coloringShaderProgram.release();


/*
//shaderProgram.setUniformValue("color", QColor(Qt::white));
shaderProgram.setAttributeArray("color",colors.constData());
shaderProgram.enableAttributeArray("color");
*/

/*coloringShaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
coloringShaderProgram.enableAttributeArray("textureCoordinate");

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

    coloringShaderProgram.disableAttributeArray("vertex");
//    shaderProgram.disableAttributeArray("color");
    coloringShaderProgram.disableAttributeArray("textureCoordinate");

    coloringShaderProgram.release();*/
}