Exemplo n.º 1
0
void ShaderLibrary::loadPreferences()
{
    QString defaultShader(Preferences::DefaultShader());
    if (defaultShader.isEmpty()) defaultShader = NoShader;
    QVariantMap defaultShaderParameters(Preferences::DefaultShaderParameters());
    setUniformVariables(defaultShader, defaultShaderParameters);
    bindShader(defaultShader);
}
Exemplo n.º 2
0
void render(void) {
	glUseProgram(meshObj->shaderProg()->getID());
	setUniformVariables();
	
	meshObj->render(GL_TRIANGLES);
	
	glUseProgram(0);

	checkError("render");
}
Exemplo n.º 3
0
void ShaderLibrary::loadAllShaders()
{
   s_shaders.insert(NoShader, 0);
   QVariantMap defaultParameters;
   defaultParameters.insert("Shininess", QVariant(0.5));
   defaultParameters.insert("Highlights", QVariant(0.5));
   setUniformVariables(NoShader, defaultParameters);

   QDir dir(Preferences::ShaderDirectory());
   if (!dir.exists() || !dir.isReadable()) {
      QLOG_WARN() << "Could not access shader directory: " + dir.path();
      return;
   }

   QDir::Filters filters(QDir::Files | QDir::Readable);
   QStringList contents(dir.entryList(filters));

   unsigned program;
   QStringList::iterator iter;
   for (iter = contents.begin(); iter != contents.end(); ++iter) {
       QFileInfo vertex(dir, *iter);
       if (vertex.suffix().contains("vert", Qt::CaseInsensitive)) {
          QFileInfo fragment(dir, vertex.completeBaseName() + ".frag"); 
          if (fragment.exists()) {
             QString name(fragment.completeBaseName());
             name = name.replace("_", " ");
             program = createProgram(vertex.filePath(), fragment.filePath());          
             if (program > 0) {
                QLOG_DEBUG() << "Shader compilation successful:" << name;
                s_shaders.insert(name, program);
                QVariantMap uniforms(parseUniformVariables(vertex.filePath()));
                uniforms.unite(parseUniformVariables(fragment.filePath()));
                setUniformVariables(name, uniforms);
             }
          }
       }
   }
}
Exemplo n.º 4
0
void render(void) {
    glUseProgram(shader_map1["id"]);
    setUniformVariables();

    setAttribPointers();
    // glDrawArrays(mode, first, count)
    glDrawArrays(GL_TRIANGLES, 0, numElements);

    disableBufObjs();

    glUseProgram(0);

    checkError("render");
}
Exemplo n.º 5
0
void render(void) {
	glUseProgram(shader_map1["id"]);
	setUniformVariables();
	
	setAttribPointers();
    // render the vertices to screen as triangles, start index 0
    // glDrawArrays(mode, first, count)
    //glDrawArrays(GL_TRIANGLES, 0, numElements);
    // modified 26 Jan
    // render the vertices to screen as triangles, from 6 vertices
    // glDrawElements(mode, count, type, idxPtr)
    glDrawElements(GL_TRIANGLES, sizeof(idxArr) / sizeof(GLuint), 
    	GL_UNSIGNED_INT, 0);
	
	disableBufObjs();
	
	glUseProgram(0);

	checkError("render");
}
Exemplo n.º 6
0
void ShaderLibrary::setFilterVariables(QVariantMap const& map)
{
   if (!filtersAvailable()) return;

   bool aa(map.contains("Antialias") && map.value("Antialias").toBool());
   bool bd(map.contains("Border") && map.value("Border").toBool());
   bool ao(map.contains("AmbientOcclusion") && map.value("AmbientOcclusion").toBool());
   m_filtersActive = aa || bd || ao;

   // Let the shaders know whether or not the Mask texture is valid
   QStringList shaders(availableShaders());
   QStringList::const_iterator iter;
   for (iter = shaders.begin(); iter != shaders.end(); ++iter) {
       setUniformVariable(*iter, "FiltersAreValid", m_filtersActive);
   }

   // Ambient Occlusion parameters
   if (ao) {
      setUniformVariables("Filters", map);
   }
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
  // UI-related variables
  int running = 0;
  int mousebtn1, lastmousebtn1;
  int mousex, mousey, lastmousex, lastmousey;
  float posx, posy, zoom, rotx, rotz;

  // Shader-related variables
  GLuint textureID;
  int texw = 0;
  int texh = 0;
  GLhandleARB programObj;
  
  // Initialise GLFW
  glfwInit();
  
  // Open OpenGL window
  if( !glfwOpenWindow( 512, 512, 0,0,0,0, 0,0, GLFW_WINDOW ) )
    {
      glfwTerminate();
      return 0;
    }

  // Init user interface (mouse drag for pan/zoom/tilt/rotation)
  posx = 0.0f;
  posy = 0.0f;
  zoom = 1.0f;
  rotx = 0.0f;
  rotz = 0.0f;
  glfwGetMousePos(&mousex, &mousey); // Requires an open window
  lastmousex = mousex;
  lastmousey = mousey;
  mousebtn1 = lastmousebtn1 = GLFW_RELEASE;
  
  // Load OpenGL extensions (requires an open window)
  loadExtensions();
  
  // Load textures  
  glEnable(GL_TEXTURE_2D);
  glGenTextures( 1, &textureID );
  loadDistTexture(PATH "disttex.tga", textureID, &texw, &texh);
  
  // Create, load and compile the shader programs
  programObj = createShader(PATH "vertex.glsl", PATH "fragment1.glsl");
  
  // Disable vertical sync (on cards that support
  // it, and if current driver settings so permit)
  glfwSwapInterval( 1 );
  
  // Main loop
  running = GL_TRUE;
  while( running )
    {
      showFPS(texw, texh, zoom);
      
      // Set the uniform shader variables
      setUniformVariables(programObj, 0, (float)texw, (float)texh);

      renderScene(programObj, posx, posy, zoom, rotx, rotz);
      
      glfwSwapBuffers();
      
      // Handle mouse pan (button 1 drag), zoom (shift-btn 1 drag up/down),
      // tilt (ctrl-btn 1 drag up/down) and rotation (ctrl-btn 1 drag left/right)
      lastmousebtn1 = mousebtn1;
      mousebtn1 = glfwGetMouseButton(GLFW_MOUSE_BUTTON_1);
      lastmousex = mousex;
      lastmousey = mousey;
      glfwGetMousePos(&mousex, &mousey);
      
      if((mousebtn1 == GLFW_PRESS) && (lastmousebtn1 == GLFW_PRESS)) {
        if(glfwGetKey(GLFW_KEY_LSHIFT)) {
          zoom *= pow(1.01, (lastmousey - mousey));
	      if(zoom < 0.26f) zoom = 0.26f; // Do not go beyond 180 degrees FOV
        }
        else if (glfwGetKey(GLFW_KEY_LCTRL)) {
          rotz -= (lastmousex - mousex) * 0.5;
          rotx += (lastmousey - mousey) * 0.5;
      	  if(rotx > 89.5f) rotx = 89.5f;
          if(rotx < 0.0f) rotx = 0.0f;
        }
        else {
      	  posx += (lastmousex - mousex) / zoom;
	      posy += (lastmousey - mousey) / zoom;
        }
      }

      if(glfwGetKey('1')) {
        programObj = createShader(PATH "vertex.glsl", PATH "fragment1.glsl");
      }
      if(glfwGetKey('2')) {
        programObj = createShader(PATH "vertex.glsl", PATH "fragment2.glsl");
      }
      if(glfwGetKey('3')) {
        programObj = createShader(PATH "vertex.glsl", PATH "fragment3.glsl");
      }
      if(glfwGetKey('4')) {
        programObj = createShader(PATH "vertex.glsl", PATH "fragment4.glsl");
      }

      // Check if the ESC key is pressed or the window has been closed
      running = !glfwGetKey( GLFW_KEY_ESC ) &&
	glfwGetWindowParam( GLFW_OPENED );
    }
  
  // Close the window (if still open) and terminate GLFW
  glfwTerminate();
  
  return 0;
}