Exemple #1
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;
}
/* Shader Program Helper
 This function will attempt to create a shader program that has
 the Vertex and Fragement shader code that you pass in. If successfully
 compiled and linked to the program, the unique program ID given by
 OpenGL will be returned. This ID will be >1 if successful, or 0 (an
 invalid ID) if any of the above fails.

 Most of the Code below is for checking if the shaders compiled and
 linked correctly.
 */
GLuint CreateShaderProgram(const std::string &vsSource,
                           const std::string &fsSource) {
  GLuint programID = glCreateProgram();
  GLuint vsID = glCreateShader(GL_VERTEX_SHADER);
  GLuint fsID = glCreateShader(GL_FRAGMENT_SHADER);

  if (programID == 0 || vsID == 0 || fsID == 0) {
    // Clean up others that were created
    glDeleteProgram(programID);
    glDeleteShader(vsID);
    glDeleteShader(fsID);

    std::cerr << "Cannot create Shaders or Program" << std::endl;
    return 0; // invalid ID
  }

  // glShaderSource() expects char**, so these are helper variables
  const char *vsSourceArray = vsSource.c_str();
  const char *fsSourceArray = fsSource.c_str();

  // https://www.opengl.org/sdk/docs/man4/xhtml/glShaderSource.xml
  glShaderSource(vsID, 1, &vsSourceArray, NULL);

  glShaderSource(fsID, 1, &fsSourceArray, NULL);

  // Compile the Shader Sources, check for errors
  glCompileShader(vsID);
  glCompileShader(fsID);

  if (!checkCompileStatus(vsID) || !checkCompileStatus(fsID)) {
    // Clean up others that were created
    glDeleteProgram(programID);
    glDeleteShader(vsID);
    glDeleteShader(fsID);

    std::cerr << "Cannot create Shaders or Program" << std::endl;
    return 0; // invalid ID
  }

  glAttachShader(programID, vsID);
  glAttachShader(programID, fsID);

  glLinkProgram(programID);

  if (!checkLinkStatus(programID)) {
    // Clean up others that were created
    glDeleteProgram(programID);
    glDeleteShader(vsID);
    glDeleteShader(fsID);

    std::cerr << "Cannot create Shaders or Program" << std::endl;
    return 0; // invalid ID
  }

  return programID;
}
		bool Shader::compile()
		{
			if (isCompiled)
				return false;

			glCompileShader(m_shader);
			isCompiled = checkCompileStatus();

			return isCompiled;
		}
Exemple #4
0
void Shader::createVertexShader(const std::string& source)
{
    auto cStrSource = source.c_str();

    m_VertexShader = glCreateShader(GL_VERTEX_SHADER);

    glShaderSource(m_VertexShader, 1, &cStrSource, NULL);
    glCompileShader(m_VertexShader);

    checkCompileStatus(m_VertexShader);
}
Exemple #5
0
void Shader::createFragmentShader(const std::string& source)
{
    auto cStrSource = source.c_str();

    m_FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(m_FragmentShader, 1, &cStrSource, NULL);
    glCompileShader(m_FragmentShader);

    checkCompileStatus(m_FragmentShader);
}
Exemple #6
0
GLuint Shader::ComputeByGPU(const char* computeShaderName)
{
	const char *computeShaderSource = loadComputeFile(computeShaderName);
	GLuint computeShaderID = glCreateShader(GL_COMPUTE_SHADER);
	glShaderSource(computeShaderID, 1, &computeShaderSource, NULL);
	glCompileShader(computeShaderID);
	checkCompileStatus(computeShaderID, computeShaderName);

	GLuint program = glCreateProgram();
	glAttachShader(program, computeShaderID);
	glLinkProgram(program);
	checkLinkStatus(program, computeShaderName);
	return program;
}
Exemple #7
0
bool Shader::compile() const
{
    if (m_compilationFailed)
    {
        return false;
    }

    shadingLanguageIncludeImplementation().compile(this);

    m_compiled = checkCompileStatus();

    m_compilationFailed = !m_compiled;

    changed();

    return m_compiled;
}
Exemple #8
0
GLuint initShader(const char * srcPath, GLenum shaderType)
{
	GLuint shader = loadShader(srcPath,shaderType);
	checkCompileStatus(shader,srcPath);
	return shader;
}
GLuint GLSLProgram::compileProgram(const char *vsource, const char *gsource, const char *fsource,
GLenum gsInput, GLenum gsOutput)
{
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

	GLint compiled = 0;

	glShaderSource(vertexShader, 1, &vsource, 0);
	glShaderSource(fragmentShader, 1, &fsource, 0);

	glCompileShader(vertexShader);

	if (checkCompileStatus(vertexShader, &compiled) == 0)
	{
		printf("<compileProgram compilation error with vertexShader>:\n");
		printf("%s\n", vsource);
		return 0;
	}

	glCompileShader(fragmentShader);

	if (checkCompileStatus(fragmentShader, &compiled) == 0)
	{
		printf("<compileProgram compilation error with fragmentShader>:\n");
		printf("%s\n", fsource);
		return 0;
	}

	GLuint program = glCreateProgram();

	glAttachShader(program, vertexShader);
	glAttachShader(program, fragmentShader);

	if (gsource)
	{
		GLuint geomShader = glCreateShader(GL_GEOMETRY_SHADER_EXT);
		glShaderSource(geomShader, 1, &gsource, 0);
		glCompileShader(geomShader);
		glGetShaderiv(geomShader, GL_COMPILE_STATUS, (GLint *)&compiled);

		if (checkCompileStatus(geomShader, &compiled) == 0)
		{
			printf("<compileProgram compilation error with geomShader>:\n");
			printf("%s\n", gsource);
			return 0;
		}

		glAttachShader(program, geomShader);

		glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, gsInput);
		glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, gsOutput);
		glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, 4);
	}

	glLinkProgram(program);

	// check if program linked
	GLint success = 0;
	glGetProgramiv(program, GL_LINK_STATUS, &success);

	if (!success)
	{
		char temp[1024];
		glGetProgramInfoLog(program, 1024, 0, temp);
		fprintf(stderr, "Failed to link program:\n%s\n", temp);
		glDeleteProgram(program);
		program = 0;
		exit(EXIT_FAILURE);
	}

	return program;
}