Example #1
0
Shader::Shader(const std::string &vertex_shader_code, const std::string &fragment_shader_code) {
  // Create shaders
  auto vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
  auto fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
  auto result = GL_FALSE;
  auto info_length = 0;

  // Compile vertex shader
  auto vertex_shader_code_ptr = vertex_shader_code.c_str();
  glShaderSource(vertex_shader_id, 1, &vertex_shader_code_ptr, NULL);
  glCompileShader(vertex_shader_id);

  // Check vertex shader log
  glGetShaderiv(vertex_shader_id, GL_COMPILE_STATUS, &result);
  if (result == GL_FALSE) {
    glGetShaderiv(vertex_shader_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string vertex_shader_log((unsigned int) info_length, ' ');
    glGetShaderInfoLog(vertex_shader_id, info_length, NULL,
                       &vertex_shader_log[0]);
    std::cout << "Error Compiling Vertex Shader ..." << std::endl;
    std::cout << vertex_shader_log << std::endl;
  }

  // Compile fragment shader
  auto fragment_shader_code_ptr = fragment_shader_code.c_str();
  glShaderSource(fragment_shader_id, 1, &fragment_shader_code_ptr, NULL);
  glCompileShader(fragment_shader_id);

  // Check fragment shader log
  glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS, &result);
  if (result == GL_FALSE) {
    glGetShaderiv(fragment_shader_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string fragment_shader_log((unsigned long) info_length, ' ');
    glGetShaderInfoLog(fragment_shader_id, info_length, NULL,
                       &fragment_shader_log[0]);
    std::cout << "Error Compiling Fragment Shader ..." << std::endl;
    std::cout << fragment_shader_log << std::endl;
  }

  // Create and link the program
  auto program_id = glCreateProgram();
  glAttachShader(program_id, vertex_shader_id);
  glAttachShader(program_id, fragment_shader_id);
  glBindFragDataLocation(program_id, 0, "FragmentColor");
  glLinkProgram(program_id);

  // Check program log
  glGetProgramiv(program_id, GL_LINK_STATUS, &result);
  if (result == GL_FALSE) {
    glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &info_length);
    std::string program_log((unsigned long) info_length, ' ');
    glGetProgramInfoLog(program_id, info_length, NULL, &program_log[0]);
    std::cout << "Error Linking Shader Program ..." << std::endl;
    std::cout << program_log << std::endl;
  }
  glDeleteShader(vertex_shader_id);
  glDeleteShader(fragment_shader_id);

  program = program_id;
}
Example #2
0
std::string Program::getInfoLog() const {
   int length = 0;
   int written_length = 0;
   getProgram(Program::InfoLogLength, &length);
   if(length <= 0) {
      return std::string();
   }

   std::vector<char> program_log(length);
   glGetProgramInfoLog(program_id_, program_log.size(), &length, &program_log[0]);
   return std::string(&program_log[0], length);
}
	// creaza un program de tip shader
	unsigned int _createProgram(const std::vector<unsigned int> &shader_objects){
		int info_log_length=0,link_result=0;

		//build OpenGL program object and link all the OpenGL shader objects
		unsigned int gl_program_object = glCreateProgram();
		for(std::vector<unsigned int>::const_iterator it=shader_objects.begin(); it!=shader_objects.end(); it++) glAttachShader(gl_program_object,(*it));
		glLinkProgram(gl_program_object);												
		glGetProgramiv(gl_program_object, GL_LINK_STATUS, &link_result);

		//if we get link errors log them
		if(link_result == GL_FALSE){														
			glGetProgramiv(gl_program_object, GL_INFO_LOG_LENGTH, &info_log_length);		
			std::vector<char> program_log(info_log_length);
			glGetProgramInfoLog(gl_program_object, info_log_length, NULL, &program_log[0]);
			std::cout<<"Shader Loader : EROARE DE LINKARE"<<std::endl<<&program_log[0]<<std::endl;
			return 0;
		}
			
		//delete the shader objects because we do not need them any more (this should be re-written if usage of separate shader objects is intended!)
		for(std::vector<unsigned int>::const_iterator it=shader_objects.begin(); it!=shader_objects.end(); it++) glDeleteShader((*it));
			
		return gl_program_object;
	}
Example #4
0
static int GenerateShaders(std::vector<AutoGLProgram> *blend_programs) {
  // Limits: GL_MAX_VARYING_COMPONENTS, GL_MAX_TEXTURE_IMAGE_UNITS,
  // GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
  // clang-format off
  const GLchar *shader_preamble = "#version 300 es\n#define LAYER_COUNT ";

  const GLchar *vertex_shader_source =
"\n"
"precision mediump int;                                                     \n"
"uniform vec4 uViewport;                                                    \n"
"uniform sampler2D uLayerTextures[LAYER_COUNT];                             \n"
"uniform vec4 uLayerCrop[LAYER_COUNT];                                      \n"
"in vec2 vPosition;                                                         \n"
"in vec2 vTexCoords;                                                        \n"
"out vec2 fTexCoords[LAYER_COUNT];                                          \n"
"void main() {                                                              \n"
"  for (int i = 0; i < LAYER_COUNT; i++) {                                  \n"
"    fTexCoords[i] = (uLayerCrop[i].xy + vTexCoords * uLayerCrop[i].zw) /   \n"
"                     vec2(textureSize(uLayerTextures[i], 0));              \n"
"  }                                                                        \n"
"  vec2 scaledPosition = uViewport.xy + vPosition * uViewport.zw;           \n"
"  gl_Position = vec4(scaledPosition * vec2(2.0) - vec2(1.0), 0.0, 1.0);    \n"
"}                                                                          \n";

  const GLchar *fragment_shader_source =
"\n"
"precision mediump float;                                                   \n"
"uniform sampler2D uLayerTextures[LAYER_COUNT];                             \n"
"uniform float uLayerAlpha[LAYER_COUNT];                                    \n"
"in vec2 fTexCoords[LAYER_COUNT];                                           \n"
"out vec4 oFragColor;                                                       \n"
"void main() {                                                              \n"
"  vec3 color = vec3(0.0, 0.0, 0.0);                                        \n"
"  float alphaCover = 1.0;                                                  \n"
"  for (int i = 0; i < LAYER_COUNT; i++) {                                  \n"
"    vec4 texSample = texture(uLayerTextures[i], fTexCoords[i]);            \n"
"    float a = texSample.a * uLayerAlpha[i];                                \n"
"    color += a * alphaCover * texSample.rgb;                               \n"
"    alphaCover *= 1.0 - a;                                                 \n"
"    if (alphaCover <= 0.5/255.0)                                           \n"
"      break;                                                               \n"
"  }                                                                        \n"
"  oFragColor = vec4(color, 1.0 - alphaCover);                              \n"
"}                                                                          \n";
  // clang-format on

  int i, ret = 1;
  GLint max_texture_images, status;
  AutoGLShader vertex_shader, fragment_shader;
  AutoGLProgram program;
  std::string shader_log;

  glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_images);

  for (i = 1; i <= max_texture_images; i++) {
    std::ostringstream layer_count_formatter;
    layer_count_formatter << i;
    std::string layer_count(layer_count_formatter.str());
    const GLchar *shader_sources[3] = {shader_preamble, layer_count.c_str(),
                                       NULL};

    shader_sources[2] = vertex_shader_source;
    vertex_shader = CompileAndCheckShader(GL_VERTEX_SHADER, 3, shader_sources,
                                          ret ? &shader_log : NULL);
    if (!vertex_shader.get()) {
      if (ret)
        ALOGE("Failed to make vertex shader:\n%s", shader_log.c_str());
      break;
    }

    shader_sources[2] = fragment_shader_source;
    fragment_shader = CompileAndCheckShader(
        GL_FRAGMENT_SHADER, 3, shader_sources, ret ? &shader_log : NULL);
    if (!fragment_shader.get()) {
      if (ret)
        ALOGE("Failed to make fragment shader:\n%s", shader_log.c_str());
      break;
    }

    program = AutoGLProgram(glCreateProgram());
    if (!program.get()) {
      if (ret)
        ALOGE("Failed to create program %s", GetGLError());
      break;
    }

    glAttachShader(program.get(), vertex_shader.get());
    glAttachShader(program.get(), fragment_shader.get());
    glBindAttribLocation(program.get(), 0, "vPosition");
    glBindAttribLocation(program.get(), 1, "vTexCoords");
    glLinkProgram(program.get());
    glDetachShader(program.get(), vertex_shader.get());
    glDetachShader(program.get(), fragment_shader.get());

    glGetProgramiv(program.get(), GL_LINK_STATUS, &status);
    if (!status) {
      if (ret) {
        GLint log_length;
        glGetProgramiv(program.get(), GL_INFO_LOG_LENGTH, &log_length);
        std::string program_log(log_length, ' ');
        glGetProgramInfoLog(program.get(), log_length, NULL, &program_log[0]);
        ALOGE("Failed to link program: \n%s", program_log.c_str());
      }
      break;
    }

    ret = 0;
    blend_programs->emplace_back(std::move(program));
  }

  return ret;
}