コード例 #1
0
ファイル: GLUtils.hpp プロジェクト: akooos/BScSDLHomework
/* 

Az http://www.opengl-tutorial.org/ oldal alapján.

*/
GLuint loadShader(GLenum _shaderType, const char* _fileName)
{
	// shader azonosito letrehozasa
	GLuint loadedShader = glCreateShader( _shaderType );

	// ha nem sikerult hibauzenet es -1 visszaadasa
	if ( loadedShader == 0 )
	{
		fprintf(stderr, "Hiba a shader inicializálásakor (glCreateShader) %s!", _fileName);
		return 0;
	}
	
	// shaderkod betoltese _fileName fajlbol
	std::string shaderCode = "";

	// _fileName megnyitasa
	std::ifstream shaderStream(_fileName);

	if ( !shaderStream.is_open() )
	{
		fprintf(stderr, "Hiba a %s shader fájl betöltésekor!", _fileName);
		return 0;
	}

	// file tartalmanak betoltese a shaderCode string-be
	std::string line = "";
	while ( std::getline(shaderStream, line) )
	{
		shaderCode += line + " ";
	}

	shaderStream.close();

	// fajlbol betoltott kod hozzarendelese a shader-hez
	const char* sourcePointer = shaderCode.c_str();
	glShaderSource( loadedShader, 1, &sourcePointer, NULL );

	// shader leforditasa
	glCompileShader( loadedShader );

	// ellenorizzuk, h minden rendben van-e
	GLint result = GL_FALSE;
    int infoLogLength;

	// forditas statuszanak lekerdezese
	glGetShaderiv(loadedShader, GL_COMPILE_STATUS, &result);
	glGetShaderiv(loadedShader, GL_INFO_LOG_LENGTH, &infoLogLength);

	if ( GL_FALSE == result )
	{
		// hibauzenet elkerese es kiirasa
		std::vector<char> VertexShaderErrorMessage(infoLogLength);
		glGetShaderInfoLog(loadedShader, infoLogLength, NULL, &VertexShaderErrorMessage[0]);

		fprintf(stdout, "%s/n", &VertexShaderErrorMessage[0]);
	}

	return loadedShader;
}
コード例 #2
0
ファイル: renderer.cpp プロジェクト: Roboy/omgl
GLint Renderer::createRenderProgram(GLuint &vertex_shader, GLuint &fragment_shader, GLuint &program) {
    // Link the program
    program = glCreateProgram();
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);

    int params = -1;
    glGetProgramiv(program, GL_LINK_STATUS, &params);
    printf("GL_LINK_STATUS = %i\n", params);

    glGetProgramiv(program, GL_ATTACHED_SHADERS, &params);
    printf("GL_ATTACHED_SHADERS = %i\n", params);

    glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &params);
    printf("GL_ACTIVE_ATTRIBUTES = %i\n", params);

    GLint Result = GL_FALSE;
    int InfoLogLength;
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if (InfoLogLength > 0) {
        vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(vertex_shader, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
        printf("%s\n", &VertexShaderErrorMessage[0]);
        return Result;
    }

    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if (InfoLogLength > 0) {
        vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(fragment_shader, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
        printf("%s\n", &VertexShaderErrorMessage[0]);
        return Result;
    }

    glDetachShader(program, vertex_shader);
    glDetachShader(program, fragment_shader);

    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    return Result;
}
コード例 #3
0
ファイル: utils.cpp プロジェクト: silidragos/openGL-stuff
GLuint compileShaders(char* pixelShaders, char* vertexShaders){

	string vertexSourceStr = LoadFileToString(vertexShaders);
	string fragmentSourceStr = LoadFileToString(pixelShaders);
	const GLchar* vertexSource = vertexSourceStr.c_str();
	const GLchar* fragmentSource = fragmentSourceStr.c_str();

	GLint Result = GL_FALSE;
	int InfoLogLength;

	printf("Compiling shader : %s\n", vertexShaders);

	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexSource, NULL);
	glCompileShader(vertexShader);

	// Check Vertex Shader
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> VertexShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(vertexShader, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

	printf("Compiling shader : %s\n", pixelShaders);
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
	glCompileShader(fragmentShader);

	// Check Fragment Shader
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(fragmentShader, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

	printf("Linking program\n");
	GLuint shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glBindFragDataLocation(shaderProgram, 0, "outColor");
	glLinkProgram(shaderProgram);
	glUseProgram(shaderProgram);

	// Check the program
	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &Result);
	glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
	glGetProgramInfoLog(shaderProgram, InfoLogLength, NULL, &ProgramErrorMessage[0]);
	fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);

	return shaderProgram;

}
コード例 #4
0
ファイル: Shader.cpp プロジェクト: rafalcieslak/PGK
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if(VertexShaderStream.is_open()){
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}else{
		std::cout << "Failed to open " << vertex_file_path << ". Are you in the right directory?" << std::endl;
		return 0;
	}

	// Read the Fragment Shader code from the file
	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
	if(FragmentShaderStream.is_open()){
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}

	GLint Result = GL_FALSE;
	int InfoLogLength;

	// Compile Vertex Shader
	// std::cout << "Compiling shader " << vertex_file_path <<  std::endl;
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	glCompileShader(VertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 1 ){
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		std::cout << &VertexShaderErrorMessage[0] << std::endl;
	}



	// Compile Fragment Shader
	// std::cout << "Compiling shader " << fragment_file_path <<  std::endl;
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 1 ){
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		std::cout << &FragmentShaderErrorMessage[0] << std::endl;
	}



	// Link the program
	// std::cout << "Linking program" << std::endl;
	GLuint ProgramID = glCreateProgram();
	if(ProgramID == 0){
		std::cout << "glCreateProgram() failed! (?\?\?)" << std::endl;
	}
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 1 ){
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		std::cout << &ProgramErrorMessage[0] << std::endl;
	}

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #5
0
	void MaterialAsset::__loadShaderToGFXCard( void )
	{
		if ( !__isShaderLoaded )
		{
			GLuint VertexShaderID = glCreateShader( GL_VERTEX_SHADER );
			GLuint FragmentShaderID = glCreateShader( GL_FRAGMENT_SHADER );

			Resources* r = DWIngine::singleton()->resources();

			string fragSource = r->getFragmentShader( __fragmentShaderUniqueName )->sourceCode( );

			string vertexSource = r->getVertexShader( __vertexShaderUniqueName )->sourceCode();

			int Result = GL_FALSE;
			int InfoLogLength;

			char const* VertexSourcePointer = vertexSource.c_str();

			//Compile the Vertex Shader
			glShaderSource( VertexShaderID, 1, &VertexSourcePointer, NULL );
			glCompileShader( VertexShaderID );

			glGetShaderiv( VertexShaderID, GL_COMPILE_STATUS, &Result );
			glGetShaderiv( VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength );

			if ( InfoLogLength > 0 )
			{
				std::vector<char> VertexShaderErrorMessage( InfoLogLength + 1 );
				glGetShaderInfoLog( VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[ 0 ] );
				DWIngine::singleton()->logError( &VertexShaderErrorMessage[0] );
			}

			//Compile the Fragment Shader
			char const * FragmentSourcePointer = fragSource.c_str();
			glShaderSource( FragmentShaderID, 1, &FragmentSourcePointer, NULL );
			glCompileShader( FragmentShaderID );

			glGetShaderiv( FragmentShaderID, GL_COMPILE_STATUS, &Result );
			glGetShaderiv( FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength );

			if ( InfoLogLength > 0 )
			{
				std::vector<char> FragmentShaderErrorMessage( InfoLogLength + 1 );
				glGetShaderInfoLog( FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[ 0 ] );
				DWIngine::singleton( )->logError( &FragmentShaderErrorMessage[ 0 ] );
			}

			GLuint ProgramID = glCreateProgram();
			glAttachShader( ProgramID, VertexShaderID );
			glAttachShader( ProgramID, FragmentShaderID );
			glLinkProgram( ProgramID );

			glGetProgramiv( ProgramID, GL_LINK_STATUS, &Result );
			glGetProgramiv( ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength );
			if ( InfoLogLength > 0 )
			{
				std::vector<char> ProgramErrorMessage( InfoLogLength + 1 );
				glGetProgramInfoLog( ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[ 0 ] );
				DWIngine::singleton()->logError( &ProgramErrorMessage[ 0 ] );
			}

			glDeleteShader( VertexShaderID );
			glDeleteShader( FragmentShaderID );

			__shaderProgramID = ProgramID;

			__shaderMatrixID = glGetUniformLocation( ProgramID, "MVP" );
			__shaderViewID = glGetUniformLocation( ProgramID, "V" );
			__shaderModelID = glGetUniformLocation( ProgramID, "M" );
			__textureShaderID = glGetUniformLocation( ProgramID, "TextureSampler" );
			__shaderLightPos = glGetUniformLocation( ProgramID, "LightPosition_worldspace" );


			__isShaderLoaded = !__isShaderLoaded;
		}
	}
コード例 #6
0
	int MaterialManagerClass::loadShaders(std::string shader_name) {
		int ret = 0;
		GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
		//GLuint GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
		GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

		std::string VertexShaderCode;
		std::ifstream VertexShaderStream(shader_name+".vxshader", std::ios::in);
		if(VertexShaderStream.is_open()) {
			std::string Line = "";
			while(getline(VertexShaderStream, Line))
				VertexShaderCode += "\n" + Line;
			VertexShaderStream.close();
		} else ret |= SW_NOVXSHADER;

		/*std::string GeometryShaderCode;
		std::ifstream GeometryShaderStream(shader_name+".gmshader", std::ios::in);
		if(GeometryShaderStream.is_open()) {
			std::string Line = "";
			while(getline(GeometryShaderStream, Line))
				GeometryShaderCode += "\n" + Line;
			GeometryShaderStream.close();
		} else ret |= SW_NOGMSHADER;*/

		std::string FragmentShaderCode;
		std::ifstream FragmentShaderStream(shader_name+".pxshader", std::ios::in);
		printf("Opening shader: %s\n", (shader_name+".pxshader").c_str());
		if(FragmentShaderStream.is_open()) {
			std::string Line = "";
			while(getline(FragmentShaderStream, Line))
				FragmentShaderCode += "\n" + Line;
			FragmentShaderStream.close();
		} else ret |= SW_NOPXSHADER;

		GLint Result = GL_FALSE;
		int InfoLogLength;

		printf("Compiling shader: %s\n", (shader_name+".vxshader").c_str());
		const char * VertexSourcePointer = VertexShaderCode.c_str();
		glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
		glCompileShader(VertexShaderID);

		glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
		glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> VertexShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

		/*printf("Compiling shader: %s\n", (shader_name+".gmshader").c_str());
		const char * GeometrySourcePointer = GeometryShaderCode.c_str();
		glShaderSource(GeometryShaderID, 1, &GeometrySourcePointer, NULL);
		glCompileShader(GeometryShaderID);

		glGetShaderiv(GeometryShaderID, GL_COMPILE_STATUS, &Result);
		glGetShaderiv(GeometryShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> GeometryShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(GeometryShaderID, InfoLogLength, NULL, &GeometryShaderErrorMessage[0]);
		fprintf(stdout, "%s\n", &GeometryShaderErrorMessage[0]);*/

		printf("Compiling shader: %s\n", (shader_name+".pxshader").c_str());
		const char * FragmentSourcePointer = FragmentShaderCode.c_str();
		glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
		glCompileShader(FragmentShaderID);

		glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
		glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

		fprintf(stdout, "Linking program\n");
		GLuint ProgramID = glCreateProgram();
		glAttachShader(ProgramID, VertexShaderID);
		//glAttachShader(ProgramID, GeometryShaderID);
		glAttachShader(ProgramID, FragmentShaderID);
		glLinkProgram(ProgramID);

		glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
		glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)));
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

		glDeleteShader(VertexShaderID);
		//glDeleteShader(GeometryShaderID);
		glDeleteShader(FragmentShaderID);

		fprintf(stdout, "shader ID: %d\n", ProgramID);
		programs.insert(std::pair<std::string, GLuint>(shader_name, ProgramID));

		return ret;
	}
コード例 #7
0
GLuint LoadShaders(const std::string &vertex_file_path,const std::string &fragment_file_path){

  //std::cout << "load shaders" << std::endl;

  // Create the shaders
  GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  
  // Read the Vertex Shader code from the file
  std::string VertexShaderCode;
  std::ifstream VertexShaderStream(vertex_file_path.c_str(), std::ios::in);
  if (VertexShaderStream.is_open()){
    std::string Line = "";
    while(getline(VertexShaderStream, Line))
      VertexShaderCode += "\n" + Line;
    VertexShaderStream.close();
  } else {
    std::cerr << "ERROR: cannot open " << vertex_file_path << std::endl;
    exit(0);
  }
  // Read the Fragment Shader code from the file
  std::string FragmentShaderCode;
  std::ifstream FragmentShaderStream(fragment_file_path.c_str(), std::ios::in);
  if(FragmentShaderStream.is_open()){
    std::string Line = "";
    while(getline(FragmentShaderStream, Line))
      FragmentShaderCode += "\n" + Line;
    FragmentShaderStream.close();
  } else {
    std::cerr << "ERROR: cannot open " << vertex_file_path << std::endl;
    exit(0);
  }
  
  GLint Result = GL_FALSE;
  int InfoLogLength;
  
  // Compile Vertex Shader
  //std::cout << "Compiling shader : " << vertex_file_path << std::endl;
  char const * VertexSourcePointer = VertexShaderCode.c_str();
  glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
  glCompileShader(VertexShaderID);
  // Check Vertex Shader
  glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  if ( InfoLogLength > 0 ){
    std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    std::cerr << "VERTEX SHADER ERROR: " << std::string(VertexShaderErrorMessage.begin(),
                                                        VertexShaderErrorMessage.end()) << std::endl;
  }
  
  // Compile Fragment Shader
  //std::cout << "Compiling shader : " << fragment_file_path << std::endl;
  char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  glCompileShader(FragmentShaderID);
  // Check Fragment Shader
  glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  if ( InfoLogLength > 0 ){
    std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
    glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
    std::cerr << "FRAGMENT SHADER ERROR: " << std::string(FragmentShaderErrorMessage.begin(),
                                                          FragmentShaderErrorMessage.end()) << std::endl;
  }
  
  // Link the program
  //std::cout << "Linking program" << std::endl;
  GLuint ProgramID = glCreateProgram();
  glAttachShader(ProgramID, VertexShaderID);
  glAttachShader(ProgramID, FragmentShaderID);
  glLinkProgram(ProgramID);
  // Check the program
  glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  if ( InfoLogLength > 0 ){
    std::vector<char> ProgramErrorMessage(InfoLogLength+1);
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    std::cerr << "SHADER PROGRAM ERROR: " << std::string(ProgramErrorMessage.begin(),
                                                         ProgramErrorMessage.end()) << std::endl;
  }
  
  glDeleteShader(VertexShaderID);
  glDeleteShader(FragmentShaderID);
  
  return ProgramID;
}
コード例 #8
0
bool Renderer::LoadShaders(std::string vertex, std::string fragment)
{
    const char* vertex_file_path = vertex.c_str();
    const char* fragment_file_path = fragment.c_str();

    // Create the shaders
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    // Read the Vertex Shader code from the file
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if (VertexShaderStream.is_open())
    {
        std::string Line = "";
        while (getline(VertexShaderStream, Line))
            VertexShaderCode += "\n" + Line;
        VertexShaderStream.close();
    } else
    {
        fprintf(stderr, "RENDERER: Impossible to open shader file: \"%s\".\n",
                vertex_file_path);
        return false;
    }

    // Read the Fragment Shader code from the file
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if (FragmentShaderStream.is_open())
    {
        std::string Line = "";
        while (getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    } else
    {
        fprintf(stderr, "RENDERER: Impossible to open shader file: \"%s\".\n",
                vertex_file_path);
        return false;
    }


    GLint Result = GL_FALSE;
    int InfoLogLength;


    // Compile Vertex Shader
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
    glCompileShader(VertexShaderID);

    // Check Vertex Shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    
    // When empty, Info Log contains only \0 char, making the length = 1
    if (InfoLogLength > 1)
    {
        std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL,
                           &VertexShaderErrorMessage[0]);
        if (!VertexShaderErrorMessage.empty())
            fprintf(stderr, "RENDERER: Vertex shaders compilation errors:\n%s\n",
                    &VertexShaderErrorMessage[0]);
    }


    // Compile Fragment Shader
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
    glCompileShader(FragmentShaderID);

    // Check Fragment Shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    
    // When empty, Info Log contains only \0 char, making the length = 1
    if (InfoLogLength > 1)
    {
        std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL,
                           &FragmentShaderErrorMessage[0]);
        if (!FragmentShaderErrorMessage.empty())
            fprintf(stderr, "RENDERER: Vertex shaders compilation errors:\n%s\n",
                    &FragmentShaderErrorMessage[0]);
    }



    // Link the program
    mProgram = glCreateProgram();
    glAttachShader(mProgram, VertexShaderID);
    glAttachShader(mProgram, FragmentShaderID);
    glLinkProgram(mProgram);

    // Check the program
    glGetProgramiv(mProgram, GL_LINK_STATUS, &Result);
    glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &InfoLogLength);

    // When empty, Info Log contains only \0 char, making the length = 1
    if (InfoLogLength > 1)
    {
        std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
        glGetProgramInfoLog(mProgram, InfoLogLength, NULL, &ProgramErrorMessage[0]);
        if (!ProgramErrorMessage.empty())
            fprintf(stderr, "RENDERER: Shaders linking errors:\n%s\n",
                    &ProgramErrorMessage[0]);
    }

    // Clean up
    glDetachShader(mProgram, VertexShaderID);
    glDetachShader(mProgram, FragmentShaderID);

    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);

    return true;
}
コード例 #9
0
ファイル: common.cpp プロジェクト: kaczla/Simple_OpenGL_Slim
GLuint CreateShader( string vertex, string fragment ){
   /*
      Tworzenie shadera wierzchołków i fragmentu
      Zmienne w których przypisane będą ww shadery
   */
   GLuint VertexShaderID = glCreateShader( GL_VERTEX_SHADER );
   GLuint FragmentShaderID = glCreateShader( GL_FRAGMENT_SHADER );

   /*
      Zmienne dla pobierania tekstu z plików tekstowych z shaderami
   */
   string Line;
   //vertex_shader:
   /*
      Zmienna dla całego kodu shadera wierzchołków.
   */
   string VertexShaderCode;
   /*
      Otwarcie pliku tekstowego do odczytu
   */
   ifstream VertexShaderStream( vertex.c_str() );
   /*
      Sprawdzenie czy plik został poprawnie otwarty.
   */
   if( VertexShaderStream.good() ){
      Line = "";
      /*
         Dopuki coś jest w pliku to pobierz całą linię tekstu do zmiennej Line.
      */
      while( getline( VertexShaderStream, Line ) ){
         /*
            Dodaj to do zmiennej, w której będzie cały kod shadera.
         */
         VertexShaderCode += Line + "\n";
      }
      /*
         Zamknięcie pliku.
      */
      VertexShaderStream.close();
   }
   /*
      W przeciwnym wypadku wypisz błąd otwarcia pliku.
   */
   else{
      cout<<"File problem: "<<vertex<<"\n";
      return 0;
   }
   /*
      Podobnie dla shadera fragmentu.
   */
   //Load code fragment_shader:
   string FragmentShaderCode;
   ifstream FragmentShaderStream( fragment.c_str() );
   if( FragmentShaderStream.good() ){
      Line = "";
      while( getline( FragmentShaderStream, Line ) ){
         FragmentShaderCode += Line + "\n";
      }
      FragmentShaderStream.close();
   }
   else{
      cout<<"File problem: "<<fragment<<"\n";
      return 0;
   }

   /*
      Zmienna dla sprawdzenia błędu w OpenGL.
   */
   GLint Result;
   /*
      Zmienna informaująca ile znaków potrzebnych jest do wyświetlenia pełnego komunikatu o błędzie.
   */
   GLint InfoLogLength;
   string Error;

   /*
      Zamiana ze string na const char * (aby nie było problemów z dopasowaniem typów).
   */
   const char *VertexShaderCodePointer = VertexShaderCode.c_str();
   /*
      Dodanie źródła dla kompilacji shadera wierchołków.
   */
   glShaderSource( VertexShaderID, 1, &VertexShaderCodePointer, 0 );
   /*
      Kompilowanie shadera wierchołków.
   */
   glCompileShader( VertexShaderID );
   //Check vertex_shader:
   Result = GL_FALSE;
   /*
      Sprawdzenie kodu błędu.
   */
   glGetShaderiv( VertexShaderID, GL_COMPILE_STATUS, &Result );
   /*
      Gdy jest błąd to wyświetl go.
   */
   if( Result == GL_FALSE ){
      InfoLogLength = 0;
      /*
         Pobranie do zmiennej InfoLogLength ilości znaków dla tekstu błedu.
      */
      glGetShaderiv( VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength );
      /*
         Wektor znaków dla OpenGL.
      */
      vector <GLchar> VertexShaderErrorMessage(InfoLogLength);
      /*
         Pobranie tekstu błędu do zmiennej VertexShaderErrorMessage o ilości znaków InfoLogLength.
      */
      glGetShaderInfoLog( VertexShaderID, InfoLogLength, &InfoLogLength, &VertexShaderErrorMessage[0] );
      /*
         Przekopiowanie z wektora VertexShaderErrorMessage zmiennej string Error
      */
      Error = string( VertexShaderErrorMessage.begin(), VertexShaderErrorMessage.end() );
      /*
         Wyświetlenie błedu.
      */
      cout<<"Vertex shader: "<<Error<<"\n";
      /*
         Usunięcie shadera wierzchołków z pamięci.
      */
      glDeleteShader( VertexShaderID );
      return 0;
   }

   //fragment_shader:
   /*
      Tak samo dla shadera fragmentu.
   */
   const char *FragmentShaderCodePointer = FragmentShaderCode.c_str();
   glShaderSource( FragmentShaderID, 1, &FragmentShaderCodePointer, 0 );
   glCompileShader( FragmentShaderID );
   //Check fragment_shader:
   Result = GL_FALSE;
   glGetShaderiv( FragmentShaderID, GL_COMPILE_STATUS, &Result );
   if( Result == GL_FALSE ){
      InfoLogLength = 0;
      glGetShaderiv( FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength );
      vector <GLchar> FragmentShaderErrorMessage(InfoLogLength);
      glGetShaderInfoLog( FragmentShaderID, InfoLogLength, &InfoLogLength, &FragmentShaderErrorMessage[0] );
      Error.clear();
      Error = string( FragmentShaderErrorMessage.begin(), FragmentShaderErrorMessage.end() );
      cout<<"Fragment shader: "<<Error<<"\n";
      glDeleteShader( VertexShaderID );
      glDeleteShader( FragmentShaderID );
      return 0;
   }

   Result = GL_FALSE;
   InfoLogLength = 0;

   //Create program shader:
   /*
      Stworzenie programy z wszystkimi shaderami.
   */
   GLuint ProgramID = glCreateProgram();
   /*
      Dołączenie shadera wierzchołków do programu.
   */
   glAttachShader( ProgramID, VertexShaderID );
   /*
      Dołączenie shadera fragmentu do programu.
   */
   glAttachShader( ProgramID, FragmentShaderID );
   /*
      Linkuje program, aby mógł być wykorzystany później.
   */
   glLinkProgram( ProgramID );

   //Check program:
   /*
      Sprawdzenie poprawności stworzonego programu.
   */
   glGetProgramiv( ProgramID, GL_LINK_STATUS, &Result );
   /*
      Tak samo jak wcześniej, dla sprawdzania błędów w shaderach.
   */
   if( Result == GL_FALSE ){
      glGetProgramiv( ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength );
      vector <GLchar> ProgramErrorMessage(InfoLogLength);
      glGetProgramInfoLog( ProgramID, InfoLogLength, &InfoLogLength, &ProgramErrorMessage[0] );
      Error.clear();
      Error = string( ProgramErrorMessage.begin(), ProgramErrorMessage.end() );
      cout<<"program shader: "<<Error<<"\n";
      glDeleteProgram( ProgramID );
      glDeleteShader( VertexShaderID );
      glDeleteShader( FragmentShaderID );
      return 0;
   }

   //delete shaders:
   /*
      Wyczyszczenie pamięci po stworzonych shaderach, które teraz są w ProgramID.
   */
   glDetachShader( ProgramID, VertexShaderID );
   glDetachShader( ProgramID, FragmentShaderID );
   glDeleteShader( VertexShaderID );
   glDeleteShader( FragmentShaderID );

   /*
      Zwróć identyfikator programu shaderów.
   */
   return ProgramID;
}
コード例 #10
0
ファイル: Shader.cpp プロジェクト: SharkDX/Lightonic
bool Shader::LoadFromFile(const char *pathVertex, const char *pathFragment)
{
	mVertexShaderObj = glCreateShader(GL_VERTEX_SHADER);
	mFragmentShaderObj = glCreateShader(GL_FRAGMENT_SHADER);

	mProgram = glCreateProgram();

	std::ifstream fileVer;
	std::ifstream fileFrag;

	fileVer.open(pathVertex);
	if (fileVer.is_open())
	{
		std::string buffer;

		while (fileVer.good())
		{
			std::getline(fileVer, buffer);
			mVertexSource.append(buffer + "\n");

		}

		fileVer.close();
	}
	else
	{
		std::cout << "Cannot open shader file: " << pathVertex << std::endl;
		return false;
	}

	fileFrag.open(pathFragment);
	if (fileFrag.is_open())
	{
		std::string buffer;

		while (fileFrag.good())
		{
			getline(fileFrag, buffer);
			mFragmentSource.append(buffer + "\n");
		}

		fileFrag.close();
	}
	else
	{
		std::cout << "Cannot open shader file: " << pathFragment << std::endl;
		return false;
	}

	const char *vP = mVertexSource.c_str();
	const char *vF = mFragmentSource.c_str();

	glShaderSource(mVertexShaderObj, 1, &vP, NULL);
	glShaderSource(mFragmentShaderObj, 1, &vF, NULL);

	//assert(mVertexShaderObj != 4);

	glCompileShader(mVertexShaderObj);

	GLint Result;
	GLint InfoLogLength;

	glGetShaderiv(mVertexShaderObj, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(mVertexShaderObj, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> VertexShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(mVertexShaderObj, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

	glCompileShader(mFragmentShaderObj);
	glGetShaderiv(mFragmentShaderObj, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(mFragmentShaderObj, GL_INFO_LOG_LENGTH, &InfoLogLength);
	VertexShaderErrorMessage = std::vector<char>(InfoLogLength);
	glGetShaderInfoLog(mFragmentShaderObj, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

	glAttachShader(mProgram, mVertexShaderObj);
	glAttachShader(mProgram, mFragmentShaderObj);

	glLinkProgram(mProgram);

	glDeleteShader(mVertexShaderObj);
	glDeleteShader(mFragmentShaderObj);

	GLint numActiveAttribs = 0;
	GLint numActiveUniforms = 0;
	glGetProgramiv(mProgram, GL_ACTIVE_ATTRIBUTES, &numActiveAttribs);
	glGetProgramiv(mProgram, GL_ACTIVE_UNIFORMS, &numActiveUniforms);

	std::vector<GLchar> nameData(256);
	for (int unif = 0; unif < numActiveUniforms; ++unif)
	{
		GLint arraySize = 0;
		GLenum type = 0;
		GLsizei actualLength = 0;
		glGetActiveUniform(mProgram, unif, nameData.size(), &actualLength, &arraySize, &type, &nameData[0]);
		std::string name((char*) &nameData[0], actualLength);
		uniforms.insert(std::pair<std::string, GLuint>(name, glGetUniformLocation(mProgram, name.c_str())));
	}

	return true;
}
コード例 #11
0
GLuint ShaderLoader::LoadShaders(string vertex_file, string fragment_file)
{
	// Create the shader IDs
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	std::string VertexShaderCode;

	std::ifstream VertexShaderStream(vertex_file, std::ios::in);
	if(VertexShaderStream.is_open())
	{
		//Read the vertex shader line by line, and add the \n line ending
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}
	else
	{
		printf("Can't open Vertex Shader code file: %s !\n", vertex_file);
		assert(false);
	}


	// Read the Fragment Shader code from the file
	std::string FragmentShaderCode;

	std::ifstream FragmentShaderStream(fragment_file, std::ios::in);
	if(FragmentShaderStream.is_open())
	{
		//Read the fragment shader line by line, and add the \n line ending
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}
	else
	{
		printf("Can't open Fragment Shader code file: %s !\n", fragment_file);
		assert(false);
	}

	//Now we compile them
	GLint Result = GL_FALSE;
	int InfoLogLength = 0;

//Compile Vertex Shader
	printf("Compiling Vertex Shader : %s\n", vertex_file);

	//get the shader text as a pointer
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	//and pass it to OpenGL
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	//then compile and save in the VertexShaderID
	glCompileShader(VertexShaderID);

	//check the compiled vertex shader
	//write the compile status in Result
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	//write the log length
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);

	//now display the shader compilation output if any
	if ( InfoLogLength > 0 )
	{
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		printf("Vertex Shader compilation output:\n");
		printf("%s\n", &VertexShaderErrorMessage[0]);
	}

//Compile Fragment Shader
	printf("Compiling Fragment Shader : %s\n", fragment_file);
	
	//get the shader text as a pointer
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	//and pass it to OpenGL
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	//then compile and save in the FragmentShaderID
	glCompileShader(FragmentShaderID);

	//check the compiled vertex shader
	//write the compile status in Result
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	//write the log length
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);

	//now display the shader compilation output if any
	if ( InfoLogLength > 0 )
	{
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		printf("Fragment Shader compilation output:\n");
		printf("%s\n", &FragmentShaderErrorMessage[0]);
	}

	//Both shaders are compiled, now link the program
	printf("Linking program\n");

	GLuint ProgramID = glCreateProgram();

	//attach the compiled Vertex and Fragment shaders to the ProgramID
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	//now check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);

	if ( InfoLogLength > 0 )
	{
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		printf("Program Linking output:\n");
		printf("%s\n", &ProgramErrorMessage[0]);
	}

	//we are done with the shaders, it is safe to delete them
	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #12
0
ファイル: GLShader.cpp プロジェクト: xcasadio/casaengine
GLuint LoadShaderFrombuffer(const char* pVertexBuffer, const char* pFragmentBuffer)
{
	std::string shaderInit;

#if CA_PLATFORM_ANDROID
	shaderInit = "#version 100\n#define GLES2\n#define texture texture2D\nprecision mediump float;\n";
#else
	shaderInit = "#version 330 core\n";
#endif

	//TODO : use shader compilation option
	shaderInit += "#pragma debug (on)\n#pragma optimize (off)\n";

	const char *pArrayString[2];

	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	GLint Result = GL_FALSE;
	int InfoLGLength;

	// Compile Vertex Shader
	pArrayString[0] = shaderInit.c_str();;
	pArrayString[1] = pVertexBuffer;
	CA_TRACE("Compiling vertex shader : %d\n", VertexShaderID);
	GLCheck(glShaderSource(VertexShaderID, 2, pArrayString, nullptr));
	GLCheck(glCompileShader(VertexShaderID));

	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLGLength);
	if (Result == GL_FALSE)
	{
		if (InfoLGLength > 1)
		{
			std::vector<char> VertexShaderErrorMessage(InfoLGLength+1);
			glGetShaderInfoLog(VertexShaderID, InfoLGLength, nullptr, &VertexShaderErrorMessage[0]);
			CA_ERROR("Compile vertex shader error %s\n", &VertexShaderErrorMessage[0]);
		}
	}

	// Compile Fragment Shader
	pArrayString[0] = shaderInit.c_str();;
	pArrayString[1] = pFragmentBuffer;
	CA_TRACE("Compiling fragment shader : %d\n", FragmentShaderID);
	GLCheck(glShaderSource(FragmentShaderID, 2, pArrayString , nullptr));
	GLCheck(glCompileShader(FragmentShaderID));

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLGLength);
	if (Result == GL_FALSE)
	{
		if (InfoLGLength > 1)
		{
			std::vector<char> FragmentShaderErrorMessage(InfoLGLength+1);
			glGetShaderInfoLog(FragmentShaderID, InfoLGLength, nullptr, &FragmentShaderErrorMessage[0]);
			CA_ERROR("Compile fragment shader error %s\n", &FragmentShaderErrorMessage[0]);
		}
	}

	// Link the program
	GLuint ProgramID = glCreateProgram();
	CA_TRACE("Linking program %d (vertex shader %d, fragment shader %d)\n", ProgramID, VertexShaderID, FragmentShaderID);		
	GLCheck(glAttachShader(ProgramID, VertexShaderID));
	GLCheck(glAttachShader(ProgramID, FragmentShaderID));
	GLCheck(glLinkProgram(ProgramID));

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLGLength);
	
	if (Result == GL_FALSE)
	{
		if (InfoLGLength > 1)
		{
			std::vector<char> ProgramErrorMessage(InfoLGLength+1);
			glGetProgramInfoLog(ProgramID, InfoLGLength, nullptr, &ProgramErrorMessage[0]);
			CA_ERROR("Link program error %s\n", &ProgramErrorMessage[0]);
		}
	}

	GLCheck(glDeleteShader(VertexShaderID));
	GLCheck(glDeleteShader(FragmentShaderID));

	return ProgramID;
}
コード例 #13
0
ファイル: GLShader.cpp プロジェクト: xcasadio/casaengine
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path)
{
	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if(VertexShaderStream.is_open() == true)
	{
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}
	else
	{
		CA_ERROR("GLShader::LoadShaders() : Can't open %s.\n", vertex_file_path);
		return 0;
	}

	// Read the Fragment Shader code from the file
	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);

	if(FragmentShaderStream.is_open() == true)
	{
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}
	else
	{
		CA_ERROR("Impossible to open %s.\n", fragment_file_path);
		return 0;
	}

	GLint Result = GL_FALSE;
	int InfoLGLength;

	// Compile Vertex Shader
	CA_TRACE("Compiling shader : %d (%s)\n", VertexShaderID, vertex_file_path);
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	// (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length);
	GLCheck(glShaderSource(VertexShaderID, 1, &VertexSourcePointer, nullptr));
	GLCheck(glCompileShader(VertexShaderID));

	// Check Vertex Shader
	GLCheck(glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result));
	GLCheck(glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLGLength));
	if (Result == GL_FALSE)
	{
		std::vector<char> VertexShaderErrorMessage(InfoLGLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLGLength, nullptr, &VertexShaderErrorMessage[0]);
		CA_ERROR("Compile vertex shader error %s\n", &VertexShaderErrorMessage[0]);
	}

	// Compile Fragment Shader
	CA_TRACE("Compiling shader : %d (%s)\n", FragmentShaderID, fragment_file_path);
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	GLCheck(glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , nullptr));
	GLCheck(glCompileShader(FragmentShaderID));

	// Check Fragment Shader
	GLCheck(glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result));
	GLCheck(glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLGLength));
	if (Result == GL_FALSE)
	{
		std::vector<char> FragmentShaderErrorMessage(InfoLGLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLGLength, nullptr, &FragmentShaderErrorMessage[0]);
		CA_ERROR("Compile fragment shader error %s\n", &FragmentShaderErrorMessage[0]);
	}

	// Link the program
	GLuint ProgramID = glCreateProgram();
	CA_TRACE("Linking program %d (vertex shader %d, fragment shader %d)\n", ProgramID, VertexShaderID, FragmentShaderID);		
	GLCheck(glAttachShader(ProgramID, VertexShaderID));
	GLCheck(glAttachShader(ProgramID, FragmentShaderID));
	GLCheck(glLinkProgram(ProgramID));

	// Check the program
	GLCheck(glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result));
	GLCheck(glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLGLength));
	if (Result == GL_FALSE)
	{
		if ( InfoLGLength > 1 )
		{
			std::vector<char> ProgramErrorMessage(InfoLGLength+1);
			glGetProgramInfoLog(ProgramID, InfoLGLength, nullptr, &ProgramErrorMessage[0]);
			CA_ERROR("Link program error %s\n", &ProgramErrorMessage[0]);
		}
	}

	GLCheck(glDeleteShader(VertexShaderID));
	GLCheck(glDeleteShader(FragmentShaderID));

	return ProgramID;
}
コード例 #14
0
int ShaderLoader::load(const char * vertex_file_path, const char * fragment_file_path, const char * geometry_file_path)
{
	// Start of user code load
    // Crée les shaders
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    
    GLuint GeometryShaderID;
    if(geometry_file_path != NULL)
        GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER_EXT);
    
    // Lit le code du vertex shader à partir du fichier
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if(VertexShaderStream.is_open())
    {
        std::string Line = "";
        while(getline(VertexShaderStream, Line))
            VertexShaderCode += "\n" + Line;
        VertexShaderStream.close();
    }
    
    // Lit le code du fragment shader à partir du fichier
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if(FragmentShaderStream.is_open()){
        std::string Line = "";
        while(getline(FragmentShaderStream, Line))
            FragmentShaderCode += "\n" + Line;
        FragmentShaderStream.close();
    }
    
    // Lit le code du geomtry shader à partir du fichier
    std::string GeometryShaderCode;
    if(geometry_file_path != NULL)
    {
        std::ifstream GeometryShaderStream(geometry_file_path, std::ios::in);
        if(GeometryShaderStream.is_open())
        {
            std::string Line = "";
            while(getline(GeometryShaderStream, Line))
                GeometryShaderCode += "\n" + Line;
            GeometryShaderStream.close();
        }
    }
    
    GLint Result = GL_FALSE;
    int InfoLogLength;
    
    // Compile le vertex shader
    printf("Compiling shader : %s\n", vertex_file_path);
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    glCompileShader(VertexShaderID);
    
    // Vérifie le vertex shader
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> VertexShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
    
    // Compile le fragment shader
    printf("Compiling shader : %s\n", fragment_file_path);
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    glCompileShader(FragmentShaderID);
    
    // Vérifie le fragment shader
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
    
    if(geometry_file_path != NULL)
    {
        // Compile le geometry shader
        printf("Compiling shader : %s\n", geometry_file_path);
        char const * GeometrySourcePointer = GeometryShaderCode.c_str();
        glShaderSource(GeometryShaderID, 1, &GeometrySourcePointer , NULL);
        glCompileShader(GeometryShaderID);
        
        // Vérifie le geometry shader
        glGetShaderiv(GeometryShaderID, GL_COMPILE_STATUS, &Result);
        glGetShaderiv(GeometryShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
        std::vector<char> GeometryShaderErrorMessage(InfoLogLength);
        glGetShaderInfoLog(GeometryShaderID, InfoLogLength, NULL, &GeometryShaderErrorMessage[0]);
        fprintf(stdout, "%s\n", &GeometryShaderErrorMessage[0]);
    }
    
    // Lit le programme
    fprintf(stdout, "Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    if(geometry_file_path != NULL)
        glAttachShader(ProgramID, GeometryShaderID);
    glLinkProgram(ProgramID);
    
    // Vérifie le programme
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) );
    glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
    
    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);
    if(geometry_file_path != NULL)
        glDeleteShader(GeometryShaderID);
    
    return ProgramID;
	// End of user code
}
コード例 #15
0
ファイル: Renderer.cpp プロジェクト: ACPLMaverick/MAVNET
void Renderer::LoadShaders(const string* vertexFilePath, const string* fragmentFilePath, const string* newName, ShaderID* n)
{
	GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);


	// Read vs code from file
	char *vertexShaderCode, *fragmentShaderCode;
	vertexShaderCode = LoadShaderFromAssets(vertexFilePath);
	fragmentShaderCode = LoadShaderFromAssets(fragmentFilePath);

	GLint result = 500;
	int infoLogLength = 10;

	// Compile Vertex Shader
	LOGI("Compiling shader : %s\n", vertexFilePath->c_str());
	char const * VertexSourcePointer = vertexShaderCode;
	glShaderSource(vertexShaderID, 1, &VertexSourcePointer, NULL);
	glCompileShader(vertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &result);
	glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> VertexShaderErrorMessage(infoLogLength);
	glGetShaderInfoLog(vertexShaderID, infoLogLength, NULL, &VertexShaderErrorMessage[0]);
	LOGW("%s : %s\n", vertexFilePath->c_str(), &VertexShaderErrorMessage[0]);

	// Compile Fragment Shader
	LOGI("Compiling shader : %s\n", fragmentFilePath->c_str());
	char const * FragmentSourcePointer = fragmentShaderCode;
	glShaderSource(fragmentShaderID, 1, &FragmentSourcePointer, NULL);
	glCompileShader(fragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &result);
	glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> FragmentShaderErrorMessage(infoLogLength);
	glGetShaderInfoLog(fragmentShaderID, infoLogLength, NULL, &FragmentShaderErrorMessage[0]);
	LOGW("%s : %s\n", fragmentFilePath->c_str(), &FragmentShaderErrorMessage[0]);

	// Link the program
	LOGI("Linking program\n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, vertexShaderID);
	glAttachShader(ProgramID, fragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> ProgramErrorMessage(glm::max(infoLogLength, int(1)));
	glGetProgramInfoLog(ProgramID, infoLogLength, NULL, &ProgramErrorMessage[0]);
	LOGW("%s\n", &ProgramErrorMessage[0]);

	glDeleteShader(vertexShaderID);
	glDeleteShader(fragmentShaderID);
	delete[] vertexShaderCode;
	delete[] fragmentShaderCode;

	n->id = ProgramID;
	n->name = (*newName);

	n->id_worldViewProj = glGetUniformLocation(ProgramID, "WorldViewProj");
	n->id_world = glGetUniformLocation(ProgramID, "World");
	n->id_worldInvTrans = glGetUniformLocation(ProgramID, "WorldInvTrans");
	n->id_eyeVector = glGetUniformLocation(ProgramID, "EyeVector");
	n->id_lightDir = glGetUniformLocation(ProgramID, "LightDir");
	n->id_lightDiff = glGetUniformLocation(ProgramID, "LightDiff");
	n->id_lightSpec = glGetUniformLocation(ProgramID, "LightSpec");
	n->id_lightAmb = glGetUniformLocation(ProgramID, "LightAmb");
	n->id_gloss = glGetUniformLocation(ProgramID, "Gloss");
	n->id_specular = glGetUniformLocation(ProgramID, "Specular");
	n->id_highlight = glGetUniformLocation(ProgramID, "Highlight");
}
コード例 #16
0
ファイル: Renderer.cpp プロジェクト: ACPLMaverick/MAVNET
void Renderer::LoadKernel(const string * filePath, const string * newName, KernelID * data)
{
	GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);


	// Read vs code from file
	char *vertexShaderCode;
	vertexShaderCode = LoadKernelFromAssets(filePath);

	string fragmentShaderCode =
#ifdef PLATFORM_WINDOWS
		"#version 330 core  \n"
#else
		"#version 300 es	\n"
#endif
		"out vec4 color;	\n"
		"void main() { color = vec4(1.0f, 1.0f, 1.0f, 1.0f);}\0";

	GLint result = 500;
	int infoLogLength = 10;

	// Compile Vertex Shader
	LOGI("Compiling shader : %s\n", filePath->c_str());
	char const * VertexSourcePointer = vertexShaderCode;
	glShaderSource(vertexShaderID, 1, &VertexSourcePointer, NULL);
	glCompileShader(vertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &result);
	glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> VertexShaderErrorMessage(infoLogLength);
	glGetShaderInfoLog(vertexShaderID, infoLogLength, NULL, &VertexShaderErrorMessage[0]);
	LOGW("%s : %s\n", filePath->c_str(), &VertexShaderErrorMessage[0]);

	// Compile Fragment Shader
	LOGI("Compiling dummy fragment shader.\n");
	char const * FragmentSourcePointer = fragmentShaderCode.c_str();
	glShaderSource(fragmentShaderID, 1, &FragmentSourcePointer, NULL);
	glCompileShader(fragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &result);
	glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> FragmentShaderErrorMessage(infoLogLength);
	glGetShaderInfoLog(fragmentShaderID, infoLogLength, NULL, &FragmentShaderErrorMessage[0]);
	LOGW("Dummy fragment shader: : %s\n", &FragmentShaderErrorMessage[0]);

	// Link the program
	LOGI("Linking program\n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, vertexShaderID);
	glAttachShader(ProgramID, fragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &infoLogLength);
	vector<char> ProgramErrorMessage(glm::max(infoLogLength, int(1)));
	glGetProgramInfoLog(ProgramID, infoLogLength, NULL, &ProgramErrorMessage[0]);
	LOGW("%s\n", &ProgramErrorMessage[0]);

	glDeleteShader(vertexShaderID);
	glDeleteShader(fragmentShaderID);
	delete[] vertexShaderCode;

	data->id = ProgramID;
	data->name = *newName;
}
コード例 #17
0
void Game::LoadAndCompileShaders()
{
	// In the future, this should return an GLuint ID for the shader program.
	// Also, in the future, this should be adding a shader program to a material component.
	// Essentially, the eventual purpose of this function will be to load all the shaders for all the mats.
	std::ifstream inf("vertexShader.vertex");
	std::string vertexShaderCode ( (std::istreambuf_iterator<char>(inf) ), (std::istreambuf_iterator<char>() ) );
	inf.close();
	inf.open("fragmentShader.frag");
	std::string fragmentShaderCode ( (std::istreambuf_iterator<char>(inf)), (std::istreambuf_iterator<char>() ) );
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	GLint Result = GL_FALSE;
	int InfoLogLength;

	std::ofstream log("log.txt");
	std::cout << " Compiling Vertex Shader" << std::endl;
	char const * VertexSourcePointer = vertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
	glCompileShader(VertexShaderID);

	// Check shader compilation
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if(InfoLogLength > 0)
	{
		std::vector<char> VertexShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		std::cout << &VertexShaderErrorMessage[0] << std::endl;
		log << &VertexShaderErrorMessage[0] << std::endl;
	}

	std::cout << "Compiling Fragment Shader" << std::endl;
	char const * FragmentSourcePointer = fragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
	glCompileShader(FragmentShaderID);

	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if(InfoLogLength > 0)
	{
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		std::cout << &FragmentShaderErrorMessage[0] << std::endl;
		log << &FragmentShaderErrorMessage[0] << std::endl;
	}

	std::cout << "Linking Program" << std::endl;

	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);
	
	glGetProgramiv(ProgramID, GL_COMPILE_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> ProgramErrorMessage(std::max(InfoLogLength, int(1)));
	std::cout << &ProgramErrorMessage[0] << std::endl;

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);
	glValidateProgram(ProgramID);

	ShaderProgramID = ProgramID;
}
コード例 #18
0
GLuint load(const char * vertex_file_path, const char * fragment_file_path){

	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if (VertexShaderStream.is_open())
	{
		std::string Line = "";
		while (getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}

	// Read the Fragment Shader code from the file
	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
	if (FragmentShaderStream.is_open()){
		std::string Line = "";
		while (getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}

	GLint Result = GL_FALSE;
	int InfoLogLength;

	// Compile Vertex Shader
	printf("Compiling shader : %s\n", vertex_file_path);
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
	glCompileShader(VertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> VertexShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

	// Compile Fragment Shader
	printf("Compiling shader : %s\n", fragment_file_path);
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

	// Link the program
	fprintf(stdout, "Linking program\n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> ProgramErrorMessage(max(InfoLogLength, int(1)));
	glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
	fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #19
0
ファイル: utils.cpp プロジェクト: rohandas36/FlowuUiN
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if(VertexShaderStream.is_open()){
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}else{
		printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
		getchar();
		return 0;
	}

	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
	if(FragmentShaderStream.is_open()){
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}



	GLint Result = GL_FALSE;
	int InfoLogLength;



	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	glCompileShader(VertexShaderID);

	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		printf("%s\n", &VertexShaderErrorMessage[0]);
	}



	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		printf("%s\n", &FragmentShaderErrorMessage[0]);
	}



	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		printf("%s\n", &ProgramErrorMessage[0]);
	}

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #20
0
ファイル: renderer.cpp プロジェクト: adasm/vxc
bool Shader::rebuild()
{
	errors = "";
	std::string VertexShaderCode;
	std::string FragmentShaderCode;

	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	VertexShaderCode = "";
	std::ifstream VertexShaderStream(vertFile.c_str(), std::ios::in);
	if (VertexShaderStream.is_open())
	{
		std::string Line = "";
		while (getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}
	else
	{
		errors = "Shader rebuild failed! Couldn't open file " + vertFile;
		prn("Shader rebuild failed! Couldn't open file %s", vertFile.c_str());
		return false;
	}

	// Read the Fragment Shader code from the file
	fragBuff = FragmentShaderCode = "";
	std::ifstream FragmentShaderStream(fragFile.c_str(), std::ios::in);
	if (FragmentShaderStream.is_open()){
		std::string Line = "";
		while (getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}
	else
	{
		errors = "Shader rebuild failed! Couldn't open file " + fragFile;
		prn("Shader rebuild failed! Couldn't open file %s", fragFile.c_str());
		return false;
	}

	GLint result = GL_FALSE;
	int InfoLogLength;

	// Compile Vertex Shader
	prn("Compiling shader : %s\n", vertFile.c_str());
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
	glCompileShader(VertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &result);
	if (!result) {
		glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> VertexShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		prn("%s\n", &VertexShaderErrorMessage[0]);
		errors = "Shader rebuild failed!\n" + std::string(&VertexShaderErrorMessage[0]);
		return false;
	}


	// Compile Fragment Shader
	prn("Compiling shader : %s\n", fragFile.c_str());
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &result);
	if (!result) {
		glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		prn("%s\n", &FragmentShaderErrorMessage[0]);
		errors = "Shader rebuild failed!\n" + std::string(&FragmentShaderErrorMessage[0]);
		return false;
	}

	// Link the program
	prn("Linking program\n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &result);
	if (!result) {
		glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> ProgramErrorMessage(std::max(InfoLogLength, int(1)));
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		prn("%s\n", &ProgramErrorMessage[0]);
		errors = "Shader rebuild failed!\n" + std::string(&ProgramErrorMessage[0]);
		return false;
	}

	release();

	vertBuff = VertexShaderCode;
	fragBuff = FragmentShaderCode;

	vertexShader = VertexShaderID;
	fragmentShader = FragmentShaderID;
	shaderProgram = ProgramID;
	return vertexShader && fragmentShader && shaderProgram;
}
コード例 #21
0
ファイル: Shader.cpp プロジェクト: blueskies9041/AIE
GLuint LoadShaders(const char * vertex_file_path, const char * geometry_file_path,const char * fragment_file_path)
{
		// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Read the Vertex Shader code from the file
	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if(VertexShaderStream.is_open()){
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}else{
		printf("Impossible to open %s. Are you in the right directory ?\n", vertex_file_path);
		getchar();
		return 0;


	}

	
	// Read the Geometry Shader code from the file
	std::string GeometryShaderCode;
	std::ifstream GeometryShaderStream(geometry_file_path, std::ios::in);
	if(GeometryShaderStream.is_open()){
		std::string Line = "";
		while(getline(GeometryShaderStream, Line))
			GeometryShaderCode += "\n" + Line;
		GeometryShaderStream.close();
	}

	// Read the Fragment Shader code from the file
	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
	if(FragmentShaderStream.is_open()){
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}

	GLint Result = GL_FALSE;
	int InfoLogLength;

	// Compile Vertex Shader
	printf("Compiling shader : %s\n", vertex_file_path);
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	glCompileShader(VertexShaderID);

	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		printf("%s\n", &VertexShaderErrorMessage[0]);
	}

	
	// Compile Geometry Shader
	printf("Compiling shader : %s\n", geometry_file_path);
	char const * GeometrySourcePointer = GeometryShaderCode.c_str();
	glShaderSource(GeometryShaderID, 1, &GeometrySourcePointer , NULL);
	glCompileShader(GeometryShaderID);

	// Check Geometry Shader
	glGetShaderiv(GeometryShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(GeometryShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> GeometryShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(GeometryShaderID, InfoLogLength, NULL, &GeometryShaderErrorMessage[0]);
		printf("%s\n", &GeometryShaderErrorMessage[0]);
	}
	

	// Compile Fragment Shader
	printf("Compiling shader : %s\n", fragment_file_path);
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		printf("%s\n", &FragmentShaderErrorMessage[0]);
	}

	// Link the program
	printf("Linking program\n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, GeometryShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	// Check the program
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		printf("%s\n", &ProgramErrorMessage[0]);
	}

	glDeleteShader(VertexShaderID);
	glDeleteShader(GeometryShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #22
0
ファイル: Shader.cpp プロジェクト: UnTraDe/RAFReader
bool Shader::LoadFromFile(const char *pathVertex, const char *pathFragment)
{
	m_VertexShaderObj = glCreateShader(GL_VERTEX_SHADER);
	m_FragmentShaderObj = glCreateShader(GL_FRAGMENT_SHADER);
	m_Program = glCreateProgram();
	std::ifstream fileVer;
	std::ifstream fileFrag;
	fileVer.open(pathVertex);

	if (fileVer.is_open())
	{
		std::string buffer;

		while (fileVer.good())
		{
			std::getline(fileVer, buffer);
			m_VertexSource.append(buffer + "\n");

		}

		fileVer.close();
	}
	else
	{
		std::cout << "Cannot open shader file: " << pathVertex << std::endl;
		return false;
	}

	fileFrag.open(pathFragment);

	if (fileFrag.is_open())
	{
		std::string buffer;

		while (fileFrag.good())
		{
			getline(fileFrag, buffer);
			m_FragmentSource.append(buffer + "\n");
		}

		fileFrag.close();
	}
	else
	{
		std::cout << "Cannot open shader file: " << pathFragment << std::endl;
		return false;
	}

	const char *vP = m_VertexSource.c_str();
	const char *vF = m_FragmentSource.c_str();

	glShaderSource(m_VertexShaderObj, 1, &vP, NULL);
	glCompileShader(m_VertexShaderObj);
	GLint Result;
	GLint InfoLogLength;
	glGetShaderiv(m_VertexShaderObj, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(m_VertexShaderObj, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> VertexShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(m_VertexShaderObj, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	printf("Shader (Vertex): %s\n", &VertexShaderErrorMessage[0]);

	glShaderSource(m_FragmentShaderObj, 1, &vF, NULL);
	glCompileShader(m_FragmentShaderObj);
	glGetShaderiv(m_FragmentShaderObj, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(m_FragmentShaderObj, GL_INFO_LOG_LENGTH, &InfoLogLength);
	VertexShaderErrorMessage = std::vector<char>(InfoLogLength);
	glGetShaderInfoLog(m_FragmentShaderObj, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	printf("Shader (Fragment): %s\n", &VertexShaderErrorMessage[0]);
	
	glAttachShader(m_Program, m_VertexShaderObj);
	glAttachShader(m_Program, m_FragmentShaderObj);
	glLinkProgram(m_Program);
	glDeleteShader(m_VertexShaderObj);
	glDeleteShader(m_FragmentShaderObj);
	return true;
}
コード例 #23
0
bool my_glObject::LoadShaders(std::string vs,std::string fs)
{
    bool fail=0;

    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    std::string vShaderCode;
    std::string fShaderCode;

    vShaderCode = LoadTextFromFile(vs);
    //can check for error later

    fShaderCode = LoadTextFromFile(fs);
    //can check for error later

    GLint Result = GL_FALSE;
    int InfoLogLength;

    //compiling vs
    const char *VertexSourcePointer = vShaderCode.c_str();
    glShaderSource(VertexShaderID,1,&VertexSourcePointer,NULL);
    glCompileShader(VertexShaderID);

    // Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		printf("%s\n", &VertexShaderErrorMessage[0]);
	}

    char const * FragmentSourcePointer = fShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		printf("%s\n", &FragmentShaderErrorMessage[0]);
	}

    programID = glCreateProgram();
    glAttachShader(programID, VertexShaderID);
    glAttachShader(programID, FragmentShaderID);
    glLinkProgram(programID);

    // Check the program
	glGetProgramiv(programID, GL_LINK_STATUS, &Result);
	glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(programID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		printf("%s\n", &ProgramErrorMessage[0]);
	}

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);
    return fail;
}
コード例 #24
0
ファイル: Helpers.cpp プロジェクト: mxklsn/BachelorWork
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

	// Создаем шейдеры
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// Загружаем код Вершинного Шейдера из файла
	std::string VertexShaderCode;
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
	if(VertexShaderStream.is_open())
	{
		std::string Line = "";
		while(getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	}

	// Загружаем код Фрагментного шейдера из файла
	std::string FragmentShaderCode;
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
	if(FragmentShaderStream.is_open()){
		std::string Line = "";
		while(getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	}

	GLint Result = GL_FALSE;
	int InfoLogLength;

	// Компилируем Вершинный шейдер
	printf("Compile Vertex shader: %s\n", vertex_file_path);
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	glCompileShader(VertexShaderID);

	// Выполняем проверку Вершинного шейдера
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> VertexShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

	// Компилируем Фрагментный шейдер
	printf("Compile Fragment Shader: %s\n", fragment_file_path);
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	// Проверяем Фрагментный шейдер
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
	glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
	fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

	// Создаем шейдерную программу и привязываем шейдеры к ней
	fprintf(stdout, "Create shader program \n");
	GLuint ProgramID = glCreateProgram();
	glAttachShader(ProgramID, VertexShaderID);
	glAttachShader(ProgramID, FragmentShaderID);
	glLinkProgram(ProgramID);

	// Проверяем шейдерную программу
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> ProgramErrorMessage(max(InfoLogLength, int(1)) );
	glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
	fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}
コード例 #25
0
ファイル: shader.cpp プロジェクト: keke2014/MyOpenGL
/* 载入着色器(shader)文件并编译;创建着色器程序并链接 */
void CShaderProgram::loadShaders(const char* vertexFilePath,const char* fragmentFilePath)
{
	// 创建着色器
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

	// 读取顶点(Vertex)着色器文件
	string VertexShaderCode;
	ifstream VertexShaderStream(vertexFilePath, ios::in);
	if (VertexShaderStream.is_open()) {
		string Line = "";
		while (getline(VertexShaderStream, Line))
			VertexShaderCode += "\n" + Line;
		VertexShaderStream.close();
	} else {
		fprintf(stderr, "Can not open vertex shader file: %s \n", vertexFilePath);
		exit(1);
	}

	// 读取片段(Fragment)着色器文件
	string FragmentShaderCode;
	ifstream FragmentShaderStream(fragmentFilePath, ios::in);
	if (FragmentShaderStream.is_open()) {
		string Line = "";
		while (getline(FragmentShaderStream, Line))
			FragmentShaderCode += "\n" + Line;
		FragmentShaderStream.close();
	} else {
		fprintf(stderr, "Can not open fragment shader file: %s \n", fragmentFilePath);
		exit(1);
	}

	// 编译顶点着色器
	printf("Compiling Vertex Shader: %s\n", vertexFilePath);
	char const * VertexSourcePointer = VertexShaderCode.c_str();
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
	glCompileShader(VertexShaderID);

	// 检查顶点着色器编译情况
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &iStatus);
	if (!iStatus) {
		glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		fprintf(stderr, "Vertex Shader compiling error: %s\n", &VertexShaderErrorMessage[0]);
		exit(1);
	}

	// 编译片段着色器
	printf("Compiling Fragment Shader: %s\n", fragmentFilePath);
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
	glCompileShader(FragmentShaderID);

	// 检查片段着色器编译情况
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &iStatus);
	if (!iStatus){
		glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		fprintf(stderr, "Fragment Shader compiling error: %s\n", &FragmentShaderErrorMessage[0]);
		exit(1);
	}

	// 创建并链接着色器程序
	printf("Linking shader program\n");
	m_uiProgramID = glCreateProgram();
	glAttachShader(m_uiProgramID, VertexShaderID);
	glAttachShader(m_uiProgramID, FragmentShaderID);
	glLinkProgram(m_uiProgramID);

	// 检查着色器程序链接状况
	glGetProgramiv(m_uiProgramID, GL_LINK_STATUS, &iStatus);
	if (!iStatus){
		glGetProgramiv(m_uiProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
		vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(m_uiProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		fprintf(stderr, "shader program linking error: %s\n", &ProgramErrorMessage[0]);
	}

	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);
}
コード例 #26
0
GLuint ShaderLoader::loadShaders(
  const char * vertex_file_path,
  const char * tesselation_control_file_path,
  const char * tesselation_eval_file_path,
  const char * geometry_file_path,
  const char * fragment_file_path)
{
  // Create the shaders
  // Vertex and fragment shader is a must
  GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  
  GLuint TesselationControlShaderID;
  GLuint TesselationEvaluationShaderID;
  GLuint GeometryShaderID;

  if (tesselation_control_file_path)
    TesselationControlShaderID = glCreateShader(GL_TESS_CONTROL_SHADER);
  if (tesselation_eval_file_path)
    TesselationEvaluationShaderID = glCreateShader(GL_TESS_EVALUATION_SHADER);
  if (geometry_file_path)
    GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER);

  // Read the Vertex Shader code from the file
  std::string VertexShaderCode;
  std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  if(VertexShaderStream.is_open())
  {
    std::string Line = "";
    while(getline(VertexShaderStream, Line))
      VertexShaderCode += "\n" + Line;
    VertexShaderStream.close();
  }
  
  std::string TesselationControlShaderCode;
  if (tesselation_control_file_path){
    // Read the Tesselation Control Shader code from the file
    std::ifstream TesselationControlShaderStream(tesselation_control_file_path, std::ios::in);
    if(TesselationControlShaderStream.is_open()){
      std::string Line = "";
      while(getline(TesselationControlShaderStream, Line))
        TesselationControlShaderCode += "\n" + Line;
      TesselationControlShaderStream.close();
    }
  }

  std::string TesselationEvaluationShaderCode;
  if (tesselation_eval_file_path){
    // Read the Tesselation Evaluation Shader code from the file
    std::ifstream TesselationEvalStream(tesselation_eval_file_path, std::ios::in);
    if(TesselationEvalStream.is_open()){
      std::string Line = "";
      while(getline(TesselationEvalStream, Line))
        TesselationEvaluationShaderCode += "\n" + Line;
      TesselationEvalStream.close();
    }
  }

  std::string GeometryShaderCode;
  if (geometry_file_path){
    // Read the Geometry Shader code from the file
    std::ifstream GeomatryShaderStream(geometry_file_path, std::ios::in);
    if(GeomatryShaderStream.is_open()){
      std::string Line = "";
      while(getline(GeomatryShaderStream, Line))
        GeometryShaderCode += "\n" + Line;
      GeomatryShaderStream.close();
    }
  }

  // Read the Fragment Shader code from the file
  std::string FragmentShaderCode;
  std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  if(FragmentShaderStream.is_open()){
    std::string Line = "";
    while(getline(FragmentShaderStream, Line))
      FragmentShaderCode += "\n" + Line;
    FragmentShaderStream.close();
  }
  
  GLint Result = GL_FALSE;
  int InfoLogLength;
  
  // Compile Vertex Shader
  printf("Compiling shader : %s\n", vertex_file_path);
  char const * VertexSourcePointer = VertexShaderCode.c_str();
  glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
  glCompileShader(VertexShaderID);
  
  // Check Vertex Shader
  glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  std::vector<char> VertexShaderErrorMessage(InfoLogLength);
  glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
  


  if (tesselation_control_file_path){
    // Compile Tesselation Control Shader
    printf("Compiling shader : %s\n", tesselation_control_file_path);
    char const * TesselationControlSourcePointer = TesselationControlShaderCode.c_str();
    glShaderSource(TesselationControlShaderID, 1, &TesselationControlSourcePointer , NULL);
    glCompileShader(TesselationControlShaderID);
    
    // Check Tesselation Control Shader
    glGetShaderiv(TesselationControlShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(TesselationControlShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> TesselationControlShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(TesselationControlShaderID, InfoLogLength, NULL, &TesselationControlShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &TesselationControlShaderErrorMessage[0]);
  }

  if (tesselation_eval_file_path){
    // Compile Fragment Shader
    printf("Compiling shader : %s\n", tesselation_eval_file_path);
    char const * TesselationEvaluationSourcePointer = TesselationEvaluationShaderCode.c_str();
    glShaderSource(TesselationEvaluationShaderID, 1, &TesselationEvaluationSourcePointer , NULL);
    glCompileShader(TesselationEvaluationShaderID);
    
    // Check TesselationEvaluation Shader
    glGetShaderiv(TesselationEvaluationShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(TesselationEvaluationShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> TesselationEvaluationShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(TesselationEvaluationShaderID, InfoLogLength, NULL, &TesselationEvaluationShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &TesselationEvaluationShaderErrorMessage[0]);
  }

  if (geometry_file_path){
    // Compile Fragment Shader
    printf("Compiling shader : %s\n", geometry_file_path);
    char const * GeometrySourcePointer = GeometryShaderCode.c_str();
    glShaderSource(GeometryShaderID, 1, &GeometrySourcePointer , NULL);
    glCompileShader(GeometryShaderID);
    
    // Check Geometry Shader
    glGetShaderiv(GeometryShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(GeometryShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    std::vector<char> GeometryShaderErrorMessage(InfoLogLength);
    glGetShaderInfoLog(GeometryShaderID, InfoLogLength, NULL, &GeometryShaderErrorMessage[0]);
    fprintf(stdout, "%s\n", &GeometryShaderErrorMessage[0]);
  }

  // Compile Fragment Shader
  printf("Compiling shader : %s\n", fragment_file_path);
  char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  glCompileShader(FragmentShaderID);
  
  // Check Fragment Shader
  glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
  glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
  
  // Link the program
  fprintf(stdout, "Linking program\n");
  GLuint ProgramID = glCreateProgram();
  glAttachShader(ProgramID, VertexShaderID);
  if (tesselation_control_file_path)
    glAttachShader(ProgramID, TesselationControlShaderID);
  if (tesselation_eval_file_path)
    glAttachShader(ProgramID, TesselationEvaluationShaderID);
  if (geometry_file_path)
    glAttachShader(ProgramID, GeometryShaderID);
  glAttachShader(ProgramID, FragmentShaderID);

  glLinkProgram(ProgramID);
  
  // Check the program
  glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) );
  glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
  
  glDeleteShader(VertexShaderID);
  if (tesselation_control_file_path)
    glDeleteShader(TesselationControlShaderID);
  if (tesselation_eval_file_path)
    glDeleteShader(TesselationEvaluationShaderID);
  if (geometry_file_path)
    glDeleteShader(GeometryShaderID);
  glDeleteShader(FragmentShaderID);

  return ProgramID;
}