Ejemplo n.º 1
0
//-----------------------------------------------------------------------------
//	Shader - Shader From a Buffer
//-----------------------------------------------------------------------------
bool shader::loadBuffer( const char* buffer, int length, int shaderType ) {
    GLint shaderStatus(0);
    GLuint shaderId( 0 );
    
    if ( !programId ) {
        programId = glCreateProgram();
        
        if ( !programId ) {
            std::cerr << "ERROR: Unable to create a shader program." << std::endl;
            return false;
        }
    }
    

    shaderId = glCreateShader( shaderType ); // Fragment shader or Vertex Shader
    glShaderSource( shaderId, 1, (const GLchar**)&buffer, (const GLint*)&length );

    glCompileShader( shaderId );
    glGetShaderiv( shaderId, GL_COMPILE_STATUS, &shaderStatus );

    if ( shaderStatus != GL_TRUE ) {
        printShaderError( 0, shaderId );
        return false;
    }
    
    glAttachShader( programId, shaderId );
    shaderIds.push_back( shaderId );

    printGlError("General shader loading error");
    return true;
}
Ejemplo n.º 2
0
/**
 * Creates a shader program. The two shader programs (vertex and
 * fragment) were already compiled.
 *
 * @param  vertShaderid A handle to the vertex shader
 * @param  fragShaderid A handle to the fragment shader
 * @param  shaderProgId A handle to the shader program.
 * @return              0 if successful, -1 if error
 */
GLint Shader::createShaderProgram(GLint vertShaderid, GLint fragShaderid, GLuint *shaderProgId) {
  int rc = 0;
  // get a handle to the shader program
  shaderProgramid = glCreateProgram();
  // connect the shaders subprogram to the "main" program
  glAttachShader(shaderProgramid, vertShaderid);
  rc = glGetError();
  if (rc != GL_NO_ERROR) {
    fprintf(stderr,"error in attach shaders \n");
    rc = -1;
    goto err;
  }

  glAttachShader(shaderProgramid, fragShaderid);
  rc = glGetError();
  if (rc != GL_NO_ERROR) {
    fprintf(stderr,"error in attach shaders \n");
    rc = -1;
    goto err;
  }

  // bind a varialbe before linking
  //glBindAttribLocation(shaderProgramid, 7, "testNormal");
  glLinkProgram(shaderProgramid);

  // check for errors

  glGetProgramiv(shaderProgramid, GL_LINK_STATUS, &rc);

  if (rc != GL_TRUE) {
    rc = -1;
    GLsizei length;
    GLsizei bufSize = 0;
    char *error = NULL;
    fprintf(stderr,"Error when creating a shader program \n");
    //fputs("Error when creating a shader \n",stderr);
    glGetProgramiv(shaderProgramid, GL_INFO_LOG_LENGTH, &bufSize);
    error = (char *) malloc(bufSize*sizeof(char)+1);
    if (error != NULL) {
      glGetProgramInfoLog(shaderProgramid, bufSize, &length, error);
      fprintf(stderr,"%s \n", error);
    }
    if (error != NULL) free(error);
  } else rc = 0;

  shaderStatus();  // may want to remove it.

  if (shaderProgId != NULL) *shaderProgId = shaderProgramid;
err:
  return (rc);
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------
//	Shader - Compilation
//-----------------------------------------------------------------------------
bool shader::compile() {
    if (shaderIds.empty()) {
        std::cerr << "WARNING: Attempted to compile a shader program without input" << std::endl;
        return false;
    }
    GLint shaderStatus(0);
    
    glLinkProgram(programId);
    glGetProgramiv(programId, GL_LINK_STATUS, &shaderStatus);
    if (shaderStatus != GL_TRUE) {
        std::cerr << "\nWARNING: A GLSL Shader Error has occurred\n";
        for ( int i = 0; i < shaderIds.size(); ++i )
            printShaderError(programId, shaderIds[ i ]);
        return false;
    }

    printGlError("Error compiling shader");
    return true;
}