Пример #1
0
unsigned int OpenGL::Helper::createProgramFromMemory(const char *vertex, const char *fragment) {
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

    int vs_len = strlen(vertex);
    int fs_len = strlen(fragment);

    glShaderSource(vs, 1, &vertex, &vs_len);
    glShaderSource(fs, 1, &fragment, &fs_len);

    glCompileShader(vs);
    if(!checkShaderCompileStatus(vs)) return -1;
    glCompileShader(fs);
    if(!checkShaderCompileStatus(fs)) return -1;

    GLuint program = glCreateProgram();
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    //glDetachShader(program, vs);
    //glDetachShader(program, fs);
    if(!checkProgramLinkStatus(program)) return -1;

    return program;
}
Пример #2
0
bool Shader::load(const std::string& vert, const std::string& frag)
{
    core::Buffer loader;
    loader.get(vert);

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);

    const GLchar* v_src = loader.getRawString().c_str();
    const GLchar* v_array[] = {v_src};

    glShaderSource(vs, 1, v_array, NULL);
    glCompileShader(vs);
    checkCompileStatus(vs);

    loader.get(frag);

    const GLchar* f_src = loader.getRawString().c_str();
    const GLchar* f_array[] = {f_src};

    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, f_array, NULL);
    glCompileShader(fs);

    bool c_status = (checkCompileStatus(fs))? true : false;

    _program = glCreateProgram();
    glAttachShader(_program, vs);
    glAttachShader(_program, fs);
    glLinkProgram(_program);

    bool p_status = (checkProgramLinkStatus(_program))? true : false;
    return (p_status && c_status)? true : false;
}
Пример #3
0
GLuint createShaderProgram(const char * _strVertex, const char * _strFragment)
{
    GLuint vertex_shader_object = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader_object, 1, &_strVertex, nullptr);
    glCompileShader(vertex_shader_object);
    assert(checkShaderCompileStatus(vertex_shader_object));

    if (!checkShaderCompileStatus(vertex_shader_object))
        logErrorShader(GL_VERTEX_SHADER, _strVertex);

    GLuint fragment_shader_object = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader_object, 1, &_strFragment, nullptr);
    glCompileShader(fragment_shader_object);
    assert(checkShaderCompileStatus(fragment_shader_object));

    if (!checkShaderCompileStatus(fragment_shader_object))
        logErrorShader(GL_VERTEX_SHADER, _strFragment);

    GLuint program = glCreateProgram();
    glBindAttribLocation(program, SC_POSITION, "aPosition");
    glBindAttribLocation(program, SC_TEXCOORD0, "aTexCoord0");
    glBindAttribLocation(program, SC_TEXCOORD1, "aTexCoord1");
    glAttachShader(program, vertex_shader_object);
    glAttachShader(program, fragment_shader_object);
    glLinkProgram(program);
    glDeleteShader(vertex_shader_object);
    glDeleteShader(fragment_shader_object);
    assert(checkProgramLinkStatus(program));
    return program;
}
Пример #4
0
bool kore::ShaderProgram::init() {
  if (_programHandle != KORE_GLUINT_HANDLE_INVALID) {
    destroyProgram();
  }

  _programHandle = glCreateProgram();
  
  if (_vertex_prog) {
    glAttachShader(_programHandle, _vertex_prog->getHandle());
  }

  if (_fragment_prog) {
    glAttachShader(_programHandle, _fragment_prog->getHandle());
  }

  if (_geometry_prog) {
    glAttachShader(_programHandle, _geometry_prog->getHandle());
  }

  if (_tess_ctrl) {
    glAttachShader(_programHandle, _tess_ctrl->getHandle());
  }

  if (_tess_eval) {
    glAttachShader(_programHandle, _tess_eval->getHandle());
  }

  glLinkProgram(_programHandle);
    
  bool success = checkProgramLinkStatus(_programHandle, _name);
  if (!success) {
    destroyProgram();
    return false;
  }

  constructShaderInputInfo(GL_ACTIVE_ATTRIBUTES, _attributes);
  //for (uint i = 0; i < _attributes.size(); i++) {
  //    kore::Log::getInstance()->write("\tAttribute '%s' at location %i\n",
  //        _attributes[i].name.c_str(),
  //        _attributes[i].location);
  //}
  constructShaderInputInfo(GL_ACTIVE_UNIFORMS, _uniforms);
  //for (uint j = 0; j < _uniforms.size(); j++) {
  //    kore::Log::getInstance()->write("\tUniform '%s' at location %i\n",
  //        _uniforms[j].name.c_str(),
  //        _uniforms[j].location);
  //}

  /*
  /* OpenGL 4.3 or arb_program_interface_query needed
  /*
  constructShaderOutputInfo(_outputs);
  for (uint j = 0; j < _outputs.size(); j++) {
      kore::Log::getInstance()->write("\tOutput '%s'\n",
          _outputs[j].name.c_str());
  }
  */
  return success == GL_TRUE;
}
Пример #5
0
//--------------------------------------------------------------
void ofShader::checkAndCreateProgram() {
#ifndef TARGET_OPENGLES
	if(GL_ARB_shader_objects) {
#else
	if(ofIsGLProgrammableRenderer()){
#endif
		if(program == 0) {
			ofLogVerbose("ofShader") << "checkAndCreateProgram(): creating GLSL program";
			program = glCreateProgram();
			retainProgram(program);
		}
	} else {
		ofLogError("ofShader") << "sorry, it looks like you can't run 'ARB_shader_objects'";
		ofLogError("ofShader") << "please check the capabilites of your graphics card: http://www.ozone3d.net/gpu_caps_viewer";
	}
}

//--------------------------------------------------------------
bool ofShader::linkProgram() {
	if(shaders.empty()) {
		ofLogError("ofShader") << "linkProgram(): trying to link GLSL program, but no shaders created yet";
	} else {
		checkAndCreateProgram();

		for(unordered_map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){
			GLuint shader = it->second;
			if(shader) {
				ofLogVerbose("ofShader") << "linkProgram(): attaching " << nameForType(it->first) << " shader to program " << program;
				glAttachShader(program, shader);
			}
		}

		glLinkProgram(program);

		checkProgramLinkStatus(program);

		// bLoaded means we have loaded shaders onto the graphics card;
		// it doesn't necessarily mean that these shaders have compiled and linked successfully.
		bLoaded = true;
	}
	return bLoaded;
}

void ofShader::bindAttribute(GLuint location, const string & name) const{
	glBindAttribLocation(program,location,name.c_str());
}
Пример #6
0
GLuint prepareProgram(const std::vector<GLuint>& shaders, bool *errorFlagPtr) {
    *errorFlagPtr = false;

    GLuint programId = glCreateProgram();
    for(auto it = shaders.cbegin(); it != shaders.cend(); ++it) {
        glAttachShader(programId, *it);
    }
    glLinkProgram(programId);

    *errorFlagPtr = checkProgramLinkStatus(programId);
    if(*errorFlagPtr) {
        glDeleteProgram(programId);
        return 0;
    }

    return programId;
}
Пример #7
0
void Shader::init() {
	printf("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));

	std::cout << _vertexShader << std::endl;

	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	compileShader(vertexShader, loadFile(_vertexShader));

	GLuint geometryShader = 0;
	if (_geometryShader != "")
	{
		geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
		compileShader(geometryShader, loadFile(_geometryShader));
	}

	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	compileShader(fragmentShader, loadFile(_fragmentShader));

	// create program
	_shaderProgram.reset(new GLuint(glCreateProgram()));
	// attach shaders
	glAttachShader(*_shaderProgram, vertexShader);
	if (_geometryShader != "")
	{
		glAttachShader(*_shaderProgram, geometryShader);
	}
	glAttachShader(*_shaderProgram, fragmentShader);
	// link the program and check for errors
	glLinkProgram(*_shaderProgram);
	if (!checkProgramLinkStatus(*_shaderProgram))
	{
		exit(0);
	}

	_isInitialized = true;

	//exit(0);
}
Пример #8
0
void createProgram() {
    const GLchar* vertexShaderSource = readTextFile("tutorial08.vert");
    int vertexShaderSourceLength = strlen(vertexShaderSource);
    GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderId, 1, &vertexShaderSource, &vertexShaderSourceLength);
    glCompileShader(vertexShaderId);
	checkShaderCompileStatus(vertexShaderId);

    const GLchar* fragmentShaderSource = readTextFile("tutorial08.frag");
    int fragmentShaderSourceLength = strlen(fragmentShaderSource);
    GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderId, 1, &fragmentShaderSource, &fragmentShaderSourceLength);
    glCompileShader(fragmentShaderId);
	checkShaderCompileStatus(fragmentShaderId);

    programId = glCreateProgram();
    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);
    glBindAttribLocation(programId, POSITION_ATTRIBUTE_INDEX, "vPosition");
    glBindAttribLocation(programId, NORMAL_ATTRIBUTE_INDEX, "vNormal");
    glLinkProgram(programId);
    checkProgramLinkStatus(programId);
}
Пример #9
0
//--------------------------------------------------------------
bool ofShader::linkProgram() {
		if(shaders.empty()) {
			ofLog(OF_LOG_ERROR, "Trying to link GLSL program, but no shaders created yet");
		} else {
			checkAndCreateProgram();
			
			for(map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){
				GLuint shader = it->second;
				if(shader) {
					ofLog(OF_LOG_VERBOSE, "Attaching shader of type " + nameForType(it->first));
					glAttachShader(program, shader);
				}
			}
			
			glLinkProgram(program);
            
            checkProgramLinkStatus(program);

            // bLoaded means we have loaded shaders onto the graphics card;
            // it doesn't necessarily mean that these shaders have compiled and linked successfully.
			bLoaded = true;
		}
		return bLoaded;
}