Ejemplo n.º 1
0
/**
    Creates a shader program from both vert and frag shaders
  **/
QGLShaderProgram * ResourceLoader::newShaderProgram(const QGLContext *context, QString vertShader, QString fragShader)
{
    QGLShaderProgram *program = new QGLShaderProgram(context);
    program->addShaderFromSourceFile(QGLShader::Vertex, vertShader);
    program->addShaderFromSourceFile(QGLShader::Fragment, fragShader);
    program->link();
    return program;
}
Ejemplo n.º 2
0
//
//  Load shader
//
void Ex07opengl::Shader(QGLShaderProgram& shader,QString vert,QString frag)
{
    //  Vertex shader
    if (vert.length() && !shader.addShaderFromSourceFile(QGLShader::Vertex,vert))
        Fatal("Error compiling "+vert+"\n"+shader.log());
    //  Fragment shader
    if (frag.length() && !shader.addShaderFromSourceFile(QGLShader::Fragment,frag))
        Fatal("Error compiling "+frag+"\n"+shader.log());
    //  Link
    if (!shader.link())
        Fatal("Error linking shader\n"+shader.log());
}
Ejemplo n.º 3
0
bool prepareShaderProgram( QGLShaderProgram& program,
                           const QString& vertexShaderPath,
                           const QString& fragmentShaderPath )
{
    // First we load and compile the vertex shader...
    bool result = program.addShaderFromSourceFile( QGLShader::Vertex, vertexShaderPath );
    if ( !result )
        qWarning() << program.log();

    // ...now the fragment shader...
    result = program.addShaderFromSourceFile( QGLShader::Fragment, fragmentShaderPath );
    if ( !result )
        qWarning() << program.log();

    // ...and finally we link them to resolve any references.
    result = program.link();
    if ( !result )
        qWarning() << "Could not link shader program:" << program.log();

    return result;
}
Ejemplo n.º 4
0
//
//  Initialize
//
void Hw1opengl::initializeGL()
{
   if (init) return;
   init = true;

   //  Enable Z-buffer depth testing
   glEnable(GL_DEPTH_TEST);

   //  Build shaders
   QGLShaderProgram* shader = new QGLShaderProgram();
   if (!shader->addShaderFromSourceFile(QGLShader::Vertex,":/ex01.vert"))
      Fatal("Error compiling ex01.vert\n"+shader->log());
   if (!shader->addShaderFromSourceFile(QGLShader::Fragment,":/ex01.frag"))
      Fatal("Error compiling ex01.frag\n"+shader->log());
   if (!shader->link())
      Fatal("Error linking shader\n"+shader->log());
   shaders.push_back(shader);

   shader = new QGLShaderProgram();
   if (!shader->addShaderFromSourceFile(QGLShader::Vertex,":/hw1.vert"))
      Fatal("Error compiling hw1.vert\n"+shader->log());
   if (!shader->addShaderFromSourceFile(QGLShader::Fragment,":/hw1.frag"))
      Fatal("Error compiling hw1.frag\n"+shader->log());
   if (!shader->link())
      Fatal("Error linking shader\n"+shader->log());
   shaders.push_back(shader);

   // Cube
   objects.push_back(new Cube());

   // Teapot
   Teapot* pot = new Teapot(8);
   pot->scale(0.5);
   pot->color(0,1,1);
   objects.push_back(pot);

   // Tyra
   WaveOBJ* tyra=0;
   try
   {
      tyra = new WaveOBJ(":/tyra.obj");
   }
   catch (QString err)
   {
      Fatal("Error loading object\n"+err);
   }
   if (tyra)
   {
      tyra->color(1,1,0);
      objects.push_back(tyra);
   }

   //  Set initial object
   obj = objects[0];

   //  Start 100 fps timer connected to updateGL
   timer.setInterval(10);
   connect(&timer,SIGNAL(timeout()),this,SLOT(updateGL()));
   timer.start();
   time.start();
}
Ejemplo n.º 5
0
void Visualizer::initializeGL()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    qglClearColor(QColor(Qt::black));

    QFile file("../../../../RubicCubeSolver/objects/box3.obj");
    int x;
    for(int i=0;i<27;i++)
        x = ObjectLoader(&file);

    //Bind Fragment and Vertex Shader for the Cube
    QGLShaderProgram* tmpShaderProg = new QGLShaderProgram;
    tmpShaderProg->addShaderFromSourceFile(QGLShader::Vertex, "../../../../RubicCubeSolver/lightingVertexShader2.vs");
    tmpShaderProg->addShaderFromSourceFile(QGLShader::Fragment, "../../../../RubicCubeSolver/lightingFragmentShader2.fs");
    tmpShaderProg->link();

    lightingShaderProgram.addShaderFromSourceFile(QGLShader::Vertex, "../../../../RubicCubeSolver/lightingVertexShader.vs");
    lightingShaderProgram.addShaderFromSourceFile(QGLShader::Fragment, "../../../../RubicCubeSolver/lightingFragmentShader.fs");
    lightingShaderProgram.link();

    cubeVertices << QVector3D(-0.5, -0.5,  0.5) << QVector3D( 0.5, -0.5,  0.5) << QVector3D( 0.5,  0.5,  0.5) << QVector3D(-0.5,  0.5,  0.5) // Front
                 << QVector3D( 0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5,  0.5, -0.5) << QVector3D( 0.5,  0.5, -0.5) // Back
                 << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5,  0.5) << QVector3D(-0.5,  0.5,  0.5) << QVector3D(-0.5,  0.5, -0.5) // Left
                 << QVector3D( 0.5, -0.5,  0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5,  0.5, -0.5) << QVector3D( 0.5,  0.5,  0.5) // Right
                 << QVector3D(-0.5,  0.5,  0.5) << QVector3D( 0.5,  0.5,  0.5) << QVector3D( 0.5,  0.5, -0.5) << QVector3D(-0.5,  0.5, -0.5) // Top
                 << QVector3D(-0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5,  0.5) << QVector3D(-0.5, -0.5,  0.5);// Bottom

    cubeTextureCoordinates << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
                           << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
                           << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
                           << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
                           << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1) // Front
                           << QVector2D(0, 0) << QVector2D(1, 0) << QVector2D(1, 1) << QVector2D(0, 1);// Front

    cubeNormals << QVector3D( 0,  0,  1) << QVector3D( 0,  0,  1) << QVector3D( 0,  0,  1) << QVector3D( 0,  0,  1) // Front
                << QVector3D( 0,  0, -1) << QVector3D( 0,  0, -1) << QVector3D( 0,  0, -1) << QVector3D( 0,  0, -1) // Back
                << QVector3D(-1,  0,  0) << QVector3D(-1,  0,  0) << QVector3D(-1,  0,  0) << QVector3D(-1,  0,  0) // Left
                << QVector3D( 1,  0,  0) << QVector3D( 1,  0,  0) << QVector3D( 1,  0,  0) << QVector3D( 1,  0,  0) // Right
                << QVector3D( 0,  1,  0) << QVector3D( 0,  1,  0) << QVector3D( 0,  1,  0) << QVector3D( 0,  1,  0) // Top
                << QVector3D( 0, -1,  0) << QVector3D( 0, -1,  0) << QVector3D( 0, -1,  0) << QVector3D( 0, -1,  0); // Bottom

    cubeTexture = bindTexture(QPixmap("../../../../RubicCubeSolver/texture.png"));

//    QGLBuffer* tmpBuffer = WriteBufferData(cubeVertices,cubeNormals,cubeTextureCoordinates);
//    m_pBufferList.append(new m_pBufferShaderList(2, cubeVertices.size(), tmpBuffer, tmpShaderProg));
    for(int i=0;i<27;i++)
    {
    m_pBufferList[i]->shaderProgram = tmpShaderProg;
    m_pBufferList[i]->variable1 = "vertex";
    m_pBufferList[i]->variable2 = "normal";
    m_pBufferList[i]->variable3 = "color";
    m_pBufferList[i]->numVaiables = 3;
    }

    //Bind Fragment and Vertex shadet for the Rotating Spotlight

    coloringShaderProgram.addShaderFromSourceFile(QGLShader::Vertex, "../../../../RubicCubeSolver/coloringVertexShader.vs");
    coloringShaderProgram.addShaderFromSourceFile(QGLShader::Fragment, "../../../../RubicCubeSolver/coloringFragmentShader.fs");
    coloringShaderProgram.link();

    spotlightVertices << QVector3D(   0,    1,    0) << QVector3D(-0.5,    0,  0.5) << QVector3D( 0.5,    0,  0.5) // Front
                      << QVector3D(   0,    1,    0) << QVector3D( 0.5,    0, -0.5) << QVector3D(-0.5,    0, -0.5) // Back
                      << QVector3D(   0,    1,    0) << QVector3D(-0.5,    0, -0.5) << QVector3D(-0.5,    0,  0.5) // Left
                      << QVector3D(   0,    1,    0) << QVector3D( 0.5,    0,  0.5) << QVector3D( 0.5,    0, -0.5) // Right
                      << QVector3D(-0.5,    0, -0.5) << QVector3D( 0.5,    0, -0.5) << QVector3D( 0.5,    0,  0.5) // Bottom
                      << QVector3D( 0.5,    0,  0.5) << QVector3D(-0.5,    0,  0.5) << QVector3D(-0.5,    0, -0.5);
    spotlightColors << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) // Front
                    << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) // Back
                    << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) // Left
                    << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) << QVector3D(0.2, 0.2, 0.2) // Right
                    << QVector3D(  1,   1,   1) << QVector3D(  1,   1,   1) << QVector3D(  1,   1,   1) // Bottom
                    << QVector3D(  1,   1,   1) << QVector3D(  1,   1,   1) << QVector3D(  1,   1,   1);

    //Bind Fragment and Vertex shadet for the non-Rotating left-Spotlight

    leftlightShaderProgram.addShaderFromSourceFile(QGLShader::Vertex, "../../../../RubicCubeSolver/leftlightVertexShader.vs");
    leftlightShaderProgram.addShaderFromSourceFile(QGLShader::Fragment, "../../../../RubicCubeSolver/leftlightFragmentShader.fs");
    leftlightShaderProgram.link();


    //Writing Buffer Vertices, Normals and textures for the Cube

    numCubeVertices = 24;
    cubeBuffer.create();
    cubeBuffer.bind();
    cubeBuffer.allocate(numCubeVertices * (3+3+2) * sizeof (GLfloat));
    int offset = 0;
    cubeBuffer.write(offset, cubeVertices.constData(), numCubeVertices * 3 * sizeof (GLfloat));
    offset += numCubeVertices * 3 * sizeof (GLfloat);
    cubeBuffer.write(offset, cubeNormals.constData(), numCubeVertices * 3 * sizeof (GLfloat));
    offset += numCubeVertices * 3 * sizeof (GLfloat);
    cubeBuffer.write(offset, cubeTextureCoordinates.constData(), numCubeVertices * 2 *sizeof (GLfloat));
    cubeBuffer.release();

    //Writting Buffer Vertices and Colors for the Rotating spotlight

    numSpotlightVertices = 18;
    spotlightBuffer.create();
    spotlightBuffer.bind();
    spotlightBuffer.allocate(numSpotlightVertices * (3+3) * sizeof (GLfloat));
    offset = 0;
    spotlightBuffer.write(offset, spotlightVertices.constData(), numSpotlightVertices * 3 * sizeof (GLfloat));
    offset += numSpotlightVertices * 3 * sizeof (GLfloat);
    spotlightBuffer.write(offset, spotlightColors.constData(), numSpotlightVertices * 3 * sizeof (GLfloat));
    offset += numSpotlightVertices * 3 * sizeof (GLfloat);
    spotlightBuffer.release();
}
Ejemplo n.º 6
0
    /// @overload QGLWidget
    void initializeGL(){     
        printf("OpenGL %d.%d\n",this->format().majorVersion(),this->format().minorVersion());
 
        ///--- Background
        glClearColor(1.0, 1.0, 1.0, 1.0);
        
        ///--- Viewport (simple, for unresizeable window)
        glViewport(0, 0, this->width(), this->height());
        
        ///--- Setup opengl flags
        glEnable(GL_DEPTH_TEST);
        
        ///--- Create the triangle index buffer
        {
            assert(mesh.is_triangle_mesh());
            triangles.clear();
            for(auto f: mesh.faces())
                for(auto v: mesh.vertices(f))
                    triangles.push_back(v.idx());
        }
        
        ///--- Create an array object to store properties
        {
            bool success = vao.create();
            assert(success);
            vao.bind();
        }
        
        ///--- Load/compile shaders
        {
            bool vok = program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
            bool fok = program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
            bool lok = program.link ();
            assert(lok && vok && fok);
            bool success = program.bind();
            assert(success);
        }
        
        ///--- Create vertex buffer/attributes "position"
        {
            auto vpoints = mesh.get_vertex_property<Vec3>("v:point");
            bool success = vertexbuffer.create();
            assert(success);
            vertexbuffer.setUsagePattern( QGLBuffer::StaticDraw ); 
            success = vertexbuffer.bind();
            assert(success);
            vertexbuffer.allocate( vpoints.data(), sizeof(Vec3) * mesh.n_vertices() );
            program.setAttributeBuffer("vpoint", GL_FLOAT, 0, 3 );
            program.enableAttributeArray("vpoint");
        }
        
        ///--- Create vertex buffer/attributes "normal"
        {
            auto vnormal = mesh.get_vertex_property<Vec3>("v:normal");
            bool success = normalbuffer.create();
            assert(success);
            normalbuffer.setUsagePattern( QGLBuffer::StaticDraw ); 
            success = normalbuffer.bind();
            assert(success);
            normalbuffer.allocate( vnormal.data(), sizeof(Vec3) * mesh.n_vertices() );
            program.setAttributeBuffer("vnormal", GL_FLOAT, 0, 3 );
            program.enableAttributeArray("vnormal");
        }
        
        ///--- Create the index "triangle" buffer
        {
            bool success = indexbuffer.create();
            assert(success);
            indexbuffer.setUsagePattern( QGLBuffer::StaticDraw ); 
            success = indexbuffer.bind();
            assert(success);
            indexbuffer.allocate(&triangles[0], triangles.size()*sizeof(unsigned int));
        }
        
#ifdef WITH_QGLVIEWER
        ///--- Setup camera
        {        
            Box3 bbox = OpenGP::bounding_box(mesh);
            camera()->setType(qglviewer::Camera::ORTHOGRAPHIC);
            camera()->setSceneCenter(qglviewer::tr(bbox.center()));
            camera()->setSceneRadius(bbox.diagonal().norm()/2.0);
            camera()->showEntireScene();
        }
#endif
        
        ///--- Unbind to avoid pollution
        vao.release();
        program.release();
    }