Beispiel #1
0
void MyGLWidget::carregaShaders() 
{
  // Creem els shaders per al fragment shader i el vertex shader
  QGLShader fs (QGLShader::Fragment, this);
  QGLShader vs (QGLShader::Vertex, this);
  // Carreguem el codi dels fitxers i els compilem
  fs.compileSourceFile("shaders/fragshad.frag");
  vs.compileSourceFile("shaders/vertshad.vert");
  // Creem el program
  program = new QGLShaderProgram(this);
  // Li afegim els shaders corresponents
  program->addShader(&fs);
  program->addShader(&vs);
  // Linkem el program
  program->link();
  // Indiquem que aquest és el program que volem usar
  program->bind();

  // Obtenim identificador per a l'atribut “vertex” del vertex shader
  vertexLoc = glGetAttribLocation (program->programId(), "vertex");
  // Obtenim identificador per a l'atribut “color” del vertex shader
  colorLoc = glGetAttribLocation (program->programId(), "color");
  // Uniform locations
  transLoc = glGetUniformLocation(program->programId(), "TG");
  projLoc = glGetUniformLocation(program->programId(), "proj");
}
Beispiel #2
0
void MyGLWidget::initializeGL ()
{
  // glew és necessari per cridar funcions de les darreres versions d'OpenGL
  glewExperimental = GL_TRUE;
  glewInit(); 
  glGetError();  // Reinicia la variable d'error d'OpenGL

  glClearColor (0.5, 0.7, 1.0, 1.0); // defineix color de fons (d'esborrat)
  
  //afegim shaders
  QGLShader fs (QGLShader::Fragment,this);
  fs.compileSourceFile ("./fragshad.frag");
  QGLShader vs (QGLShader::Vertex,this);
  vs.compileSourceFile ("./vertshad.vert");
	

  QGLShaderProgram program(this);
  program.addShader(&fs);
  program.addShader(&vs);
  program.link();
   
  program.bind();

  //creem els buffers
  createBuffers();
}
Beispiel #3
0
PlainShader::PlainShader()
{
	QGLShader* vert = new QGLShader(QGLShader::Vertex);
	QGLShader* frag = new QGLShader(QGLShader::Fragment);
	
	vert->compileSourceFile("./shaders/plain.vert");
	frag->compileSourceFile("./shaders/plain.frag");
	
	m_shaderProg = new QGLShaderProgram();
	m_shaderProg->addShader(vert);
	m_shaderProg->addShader(frag);
	m_shaderProg->link();

	m_shaderFogFlag = m_shaderProg->uniformLocation("fog_enabled");
	m_shaderProg->setUniformValue(m_shaderFogFlag, false);
}
Beispiel #4
0
void GLWidget::loadShaders(QGLShaderProgram* program, QString vShaderFileName, QString fShaderFileName)
{
    QGLShader* vertexShader = NULL;
    QGLShader* fragmentShader = NULL;

    QFileInfo vsh(vShaderFileName);
    if(vsh.exists()) {
        vertexShader = new QGLShader(QGLShader::Vertex);
        if(vertexShader->compileSourceFile(vShaderFileName)) {
            program->addShader(vertexShader);
            qDebug() << "Vertex shader compiled successfully.";
        }
        else {
            qWarning() << "Vertex Shader Error" << vertexShader->log();
        }
    }
    else {
        qWarning() << "Vertex Shader source file " << fShaderFileName << " not found.";
    }

    QFileInfo fsh(fShaderFileName);
    if(fsh.exists()) {
        fragmentShader = new QGLShader(QGLShader::Fragment);
        if(fragmentShader->compileSourceFile(fShaderFileName)) {
            program->addShader(fragmentShader);
            qDebug() << "Fragment shader compiled successfully.";
        }
        else {
            qWarning() << "Fragment Shader Error" << fragmentShader->log();
        }
    }
    else {
        qWarning() << "Fragment Shader source file " << fShaderFileName << " not found.";
    }

    if(!program->link()){
        qWarning() << "Shader Program Linker Error" << program->log();
    }

}
Beispiel #5
0
void GLWidget::initializeGL()
{
    initializeGLFunctions();
    makeObject();

    // opengl settings
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);

    // transform matrices
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-1.0, +1.0, +1.0, -1.0, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);

    // shaders
    QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
    vshader->compileSourceFile(":/glsl/passThru_vert.glsl");

    QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
    fshader->compileSourceFile(":/glsl/uvMap_frag.glsl");

    m_uvMapProgram = new QGLShaderProgram(this);
    m_uvMapProgram->addShader(vshader);
    m_uvMapProgram->addShader(fshader);
    m_uvMapProgram->link();

    fshader = new QGLShader(QGLShader::Fragment, this);
    fshader->compileSourceFile(":/glsl/transparencyGrid_frag.glsl");

    m_gridProgram = new QGLShaderProgram(this);
    m_gridProgram->addShader(vshader);
    m_gridProgram->addShader(fshader);
    m_gridProgram->link();

    emit initialized();
}
Beispiel #6
0
QGLShader *SceneParser::loadShader(KeyValues *data)
{
  const char *type = data->getString("type");
  if(!type) {
    fprintf(stderr, "Key 'type' not found on Shader\n");
    return NULL;
  }

  const char *file = data->getString("file");
  if(!file) {
    fprintf(stderr, "Key 'file' not found on Shader\n");
    return NULL;
  }
  char *filename = resolvePath(file);

  QGLShader::ShaderType shaderType;
  if(strcmp(type, "vertex") == 0) {
    shaderType = QGLShader::Vertex;
  }
  else if(strcmp(type, "fragment") == 0) {
    shaderType = QGLShader::Fragment;
  }
  else {
    fprintf(stderr, "Invalid shader type: %s\n", type);
    delete[] filename;
    return NULL;
  }

  QGLShader *shader = new QGLShader(shaderType);
  if(!shader->compileSourceFile(filename)) {
    fprintf(stderr, "Failed to load shader %s\n", filename);
    fprintf(stdout, shader->log().toStdString().c_str());
    delete shader;
    delete[] filename;
    return NULL;
  }
  delete[] filename;

  return shader;
}