bool gShaderProgram::LinkProgram() { if ( m_id_program == 0 ) return false; glLinkProgram(m_id_program); // linkeles ellenorzese GLint infoLogLength = 0, result = 0; glGetProgramiv(m_id_program, GL_LINK_STATUS, &result); glGetProgramiv(m_id_program, GL_INFO_LOG_LENGTH, &infoLogLength); if ( GL_FALSE == result) { if ( m_verbose ) { std::vector<char> ProgramErrorMessage( infoLogLength ); glGetProgramInfoLog(m_id_program, infoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); } return false; } return true; }
uint GLShader::LinkShaders( const uint& VertexShaderID, const uint& GeometryShaderID, const uint& FragmentShaderID) { int Result = GL_FALSE; int InfoLogLength; //- Link the program ---------------------------------------------------------------- LogMessage("Linking program"); uint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); if(GeometryShaderID) glAttachShader(ProgramID, GeometryShaderID); 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]); LogMessage("%s", &ProgramErrorMessage[0]); } else LogMessage("succesfully compiled vertex shaders<br>"); glDeleteShader(GeometryShaderID); glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); return ProgramID; }
GLuint Painter::makeProgram(const std::vector<GLuint> &programs) { GLuint prog = glCreateProgram(); for (GLuint program : programs) { glAttachShader(prog, program); } glLinkProgram(prog); for (GLuint program : programs) { glDetachShader(prog, program); glDeleteShader(program); } GLint linked; glGetProgramiv(prog, GL_LINK_STATUS, &linked); if (!linked) { std::cerr << "Failed to link program!" << std::endl; GLint length; glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &length); if ( length > 0 ){ std::vector<char> ProgramErrorMessage(length+1); glGetProgramInfoLog(prog, length, NULL, &ProgramErrorMessage[0]); fprintf(stderr, "%s\n", &ProgramErrorMessage[0]); } } return prog; }
void Shader::Link() { GLint Result = GL_FALSE; int InfoLogLength; printf("Linking program\n"); glLinkProgram(this->program); glGetProgramiv(this->program, GL_LINK_STATUS, &Result); glGetProgramiv(this->program, GL_INFO_LOG_LENGTH, &InfoLogLength); if (InfoLogLength > 0) { std::vector<char> ProgramErrorMessage(InfoLogLength + 1); glGetProgramInfoLog(this->program, InfoLogLength, NULL, &ProgramErrorMessage[0]); printf("%s\n", &ProgramErrorMessage[0]); } }
GLuint ShaderProgram::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); // Compile both shaders. Exit if compile errors. if ( !compileShader(vertex_file_path, VertexShaderID) || !compileShader(fragment_file_path, FragmentShaderID) ) { return 0; } // Link the program GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); // Check the program GLint Result = GL_FALSE; int InfoLogLength; 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 << &ProgramErrorMessage[0] << std::endl; } glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); return ProgramID; }
GLuint ShaderProgram::compile() { this->program = -1; // Make sure both shaders are set if (this->fragmentShaderFile == NULL) { printf("Fragmentshader file not set!"); return 0; } if (this->vertexShaderFile == NULL) { printf("Vertexshader file not set!"); return 0; } // Read the Vertex Shader code from the file std::string VertexShaderCode = this->loadShaderContent(this->vertexShaderFile); std::string FragmentShaderCode = this->loadShaderContent(this->fragmentShaderFile); // Compile Vertex Shader printf("Compiling shader : %s\n", this->vertexShaderFile); GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); bool vertexShaderCompiled = compileShader(VertexShaderCode, vertexShaderID); // Compile Fragment Shader printf("Compiling shader : %s\n", this->fragmentShaderFile); GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); bool fragmentShaderCompiled = compileShader(FragmentShaderCode, fragmentShaderID); if(!vertexShaderCompiled || !fragmentShaderCompiled) { return 0; } // Link the program printf("Linking program\n"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, vertexShaderID); glAttachShader(ProgramID, fragmentShaderID); glLinkProgram(ProgramID); // Check the program int infoLogLength; GLint result = GL_FALSE; glGetProgramiv(ProgramID, GL_LINK_STATUS, &result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); if (!result) { std::vector<char> ProgramErrorMessage(infoLogLength + 1); glGetProgramInfoLog(ProgramID, infoLogLength, NULL, &ProgramErrorMessage[0]); printf("%s\n", &ProgramErrorMessage[0]); } glDeleteShader(vertexShaderID); glDeleteShader(fragmentShaderID); printf("Done\n"); this->program = ProgramID; return ProgramID; }
void Shader::check_program(GLuint ProgramID) { int InfoLogLength; GLint Result=GL_FALSE; glGetProgramiv(ProgramID,GL_LINK_STATUS,&Result); glGetProgramiv(ProgramID,GL_INFO_LOG_LENGTH,&InfoLogLength); std::vector<char> ProgramErrorMessage(InfoLogLength>1?InfoLogLength:1); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); if(strcmp(&ProgramErrorMessage[0],""))fprintf(stdout,"%s\n", &ProgramErrorMessage[0]); }
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; }
void CShaderProgram::useProgram() { glValidateProgram(m_uiProgramID); glGetProgramiv(m_uiProgramID, GL_VALIDATE_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, "Invalid shader program: '%s'\n", &ProgramErrorMessage[0]); exit(1); } glUseProgram(m_uiProgramID); }
void Shader::linkShader() { GLint Result = GL_FALSE; int infoLogLen; _programID = glCreateProgram(); glAttachShader(_programID, _vertexShaderID); glAttachShader(_programID, _fragmentShaderID); glLinkProgram(_programID); glGetProgramiv(_programID, GL_LINK_STATUS, &Result); glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &infoLogLen); std::vector<char> ProgramErrorMessage(infoLogLen > 1 ? infoLogLen : 1); glGetProgramInfoLog(_programID, infoLogLen, NULL, &ProgramErrorMessage[0]); }
GLuint loadProgramVSGSFS(const char* _fileNameVS, const char* _fileNameGS, const char* _fileNameFS) { // a vertex, geometry es fragment shaderek betoltese GLuint vs_ID = loadShader(GL_VERTEX_SHADER, _fileNameVS); GLuint gs_ID = loadShader(GL_GEOMETRY_SHADER, _fileNameGS); GLuint fs_ID = loadShader(GL_FRAGMENT_SHADER, _fileNameFS); // ha barmelyikkel gond volt programot sem tudunk csinalni, 0 vissza if ( vs_ID == 0 || gs_ID == 0 || fs_ID == 0 ) { return 0; } // linkeljuk ossze a dolgokat GLuint program_ID = glCreateProgram(); fprintf(stdout, "Linking program/n"); glAttachShader(program_ID, vs_ID); glAttachShader(program_ID, gs_ID); glAttachShader(program_ID, fs_ID); glLinkProgram(program_ID); // linkeles ellenorzese GLint infoLogLength = 0, result = 0; glGetProgramiv(program_ID, GL_LINK_STATUS, &result); glGetProgramiv(program_ID, GL_INFO_LOG_LENGTH, &infoLogLength); if ( GL_FALSE == result) { std::vector<char> ProgramErrorMessage( infoLogLength ); glGetProgramInfoLog(program_ID, infoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s/n", &ProgramErrorMessage[0]); } // mar nincs ezekre szukseg glDeleteShader( vs_ID ); glDeleteShader( gs_ID ); glDeleteShader( fs_ID ); // adjuk vissza a program azonositojat return program_ID; }
ShaderProgram::ShaderProgram(VertexShader vshader, FragmentShader fshader) { GLint Result = GL_FALSE; int InfoLogLength; // Link the program fprintf(stdout, "Linking program\n"); this->id = glCreateProgram(); ShaderProgram::refCount.insert(std::pair<GLint, int>(this->id, 1)); glAttachShader(this->id, vshader.id); glAttachShader(this->id, fshader.id); glLinkProgram(this->id); // Check the program glGetProgramiv(this->id, GL_LINK_STATUS, &Result); glGetProgramiv(this->id, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) ); glGetProgramInfoLog(this->id, InfoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); this->textures.reset(new std::vector<Texture>()); ShaderProgram::gtextures.insert(std::pair<GLuint, std::shared_ptr<std::vector<Texture>>>(this->id, this->textures)); }
void DGLProgram::Load() { DResource::Load(); this->ProgramID = glCreateProgram(); for (auto &Shader : this->Shaders) { glAttachShader(this->ProgramID, Shader->GetShader()); } glLinkProgram(this->ProgramID); GLint Result = GL_FALSE; int InfoLogLength; glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); if (Result == GL_FALSE){ std::vector<char> ProgramErrorMessage(std::max(InfoLogLength + 1, 1)); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); std::string __tmp(ProgramErrorMessage.begin(), ProgramErrorMessage.end()); DApp::DF->DebugManager->Error(this, "Program Status: " + __tmp); } }
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; }
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; }
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; }
void CMyOGLApp::Init() { // törlési szín legyen kékes glClearColor(0.125f, 0.25f, 0.5f, 1.0f); glEnable(GL_CULL_FACE); // kapcsoljuk be a hatrafele nezo lapok eldobasat glEnable(GL_DEPTH_TEST); // mélységi teszt bekapcsolása (takarás) glCullFace(GL_BACK); // GL_BACK: a kamerától "elfelé" nézõ lapok, GL_FRONT: a kamera felé nézõ lapok // // geometria letrehozasa // Vertex vert[] = { // x, y, z R, G, B {glm::vec3(-1, -1, 0), glm::vec3(1, 0, 0)}, {glm::vec3( 1, -1, 0), glm::vec3(0, 1, 0)}, {glm::vec3(-1, 1, 0), glm::vec3(0, 0, 1)}, {glm::vec3( 1, 1, 0), glm::vec3(1, 1, 1)}, }; // indexpuffer adatai GLushort indices[]= { // 1. háromszög 0,1,2, // 2. háromszög 2,1,3, }; // 1 db VAO foglalasa glGenVertexArrays(1, &m_vaoID); // a frissen generált VAO beallitasa aktívnak glBindVertexArray(m_vaoID); // hozzunk létre egy új VBO erõforrás nevet glGenBuffers(1, &m_vboID); glBindBuffer(GL_ARRAY_BUFFER, m_vboID); // tegyük "aktívvá" a létrehozott VBO-t // töltsük fel adatokkal az aktív VBO-t glBufferData( GL_ARRAY_BUFFER, // az aktív VBO-ba töltsünk adatokat sizeof(vert), // ennyi bájt nagyságban vert, // errõl a rendszermemóriabeli címrõl olvasva GL_STATIC_DRAW); // úgy, hogy a VBO-nkba nem tervezünk ezután írni és minden kirajzoláskor felhasnzáljuk a benne lévõ adatokat // VAO-ban jegyezzük fel, hogy a VBO-ban az elsõ 3 float sizeof(Vertex)-enként lesz az elsõ attribútum (pozíció) glEnableVertexAttribArray(0); // ez lesz majd a pozíció glVertexAttribPointer( 0, // a VB-ben található adatok közül a 0. "indexû" attribútumait állítjuk be 3, // komponens szam GL_FLOAT, // adatok tipusa GL_FALSE, // normalizalt legyen-e sizeof(Vertex), // stride (0=egymas utan) 0 // a 0. indexû attribútum hol kezdõdik a sizeof(Vertex)-nyi területen belül ); // a második attribútumhoz pedig a VBO-ban sizeof(Vertex) ugrás után sizeof(glm::vec3)-nyit menve újabb 3 float adatot találunk (szín) glEnableVertexAttribArray(1); // ez lesz majd a szín glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(glm::vec3)) ); // index puffer létrehozása glGenBuffers(1, &m_ibID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindVertexArray(0); // feltöltüttük a VAO-t, kapcsoljuk le glBindBuffer(GL_ARRAY_BUFFER, 0); // feltöltöttük a VBO-t is, ezt is vegyük le glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // feltöltöttük a VBO-t is, ezt is vegyük le // // shaderek betöltése // GLuint vs_ID = loadShader(GL_VERTEX_SHADER, "myVert.vert"); GLuint fs_ID = loadShader(GL_FRAGMENT_SHADER, "myFrag.frag"); // a shadereket tároló program létrehozása m_programID = glCreateProgram(); // adjuk hozzá a programhoz a shadereket glAttachShader(m_programID, vs_ID); glAttachShader(m_programID, fs_ID); // VAO-beli attribútumok hozzárendelése a shader változókhoz // FONTOS: linkelés elõtt kell ezt megtenni! glBindAttribLocation( m_programID, // shader azonosítója, amibõl egy változóhoz szeretnénk hozzárendelést csinálni 0, // a VAO-beli azonosító index "vs_in_pos"); // a shader-beli változónév glBindAttribLocation( m_programID, 1, "vs_in_col"); // illesszük össze a shadereket (kimenõ-bemenõ változók összerendelése stb.) glLinkProgram(m_programID); // linkeles ellenorzese GLint infoLogLength = 0, result = 0; glGetProgramiv(m_programID, GL_LINK_STATUS, &result); glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &infoLogLength); if ( GL_FALSE == result ) { std::vector<char> ProgramErrorMessage( infoLogLength ); glGetProgramInfoLog(m_programID, infoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); char* aSzoveg = new char[ProgramErrorMessage.size()]; memcpy( aSzoveg, &ProgramErrorMessage[0], ProgramErrorMessage.size()); MessageBoxA(0, aSzoveg, "Sáder Huba panasza", 0); delete aSzoveg; } // mar nincs ezekre szukseg glDeleteShader( vs_ID ); glDeleteShader( fs_ID ); // // egyéb inicializálás // // vetítési mátrix létrehozása m_matProj = glm::perspective( 45.0f, m_client_width/(float)m_client_height, 1.0f, 1000.0f ); // shader-beli transzformációs mátrixok címének lekérdezése m_loc_world = glGetUniformLocation( m_programID, "world"); m_loc_view = glGetUniformLocation( m_programID, "view" ); m_loc_proj = glGetUniformLocation( m_programID, "proj" ); }
/* 载入着色器(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); }
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; }
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; } }
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; }
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; }
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; }
void CMyOGLApp::Init() { // clear color set to blue-ish glClearColor(0.125f, 0.25f, 0.5f, 1.0f); glEnable(GL_CULL_FACE); // turn on back-face culling glEnable(GL_DEPTH_TEST); // enable depth-test // // define the geometry // Vertex vert[] = { // x, y, z nx,ny,nz s, t {glm::vec3(-10, 0, -10), glm::vec3(0, 1, 0), glm::vec2(0, 0)}, {glm::vec3( 10, 0, -10), glm::vec3(0, 1, 0), glm::vec2(1, 0)}, {glm::vec3(-10, 0, 10), glm::vec3(0, 1, 0), glm::vec2(0, 1)}, {glm::vec3( 10, 0, 10), glm::vec3(0, 1, 0), glm::vec2(1, 1)}, }; // create index buffer GLushort indices[]= { 1,0,2, 1,2,3, }; // create a VAO glGenVertexArrays(1, &m_vaoID); // activate the new VAO m_vaoID glBindVertexArray(m_vaoID); // create a VBO glGenBuffers(1, &m_vboID); glBindBuffer(GL_ARRAY_BUFFER, m_vboID); // activate the VBO m_vboID // load the data stored in array vert into the VBO (essentially: upload the data to the GPU) glBufferData( GL_ARRAY_BUFFER, // allocate memory for the active VBO and set its data sizeof(vert), // size of the VBO allocation, in bytes vert, // load data into the VBO from this location of the system memory GL_STATIC_DRAW); // we only want to store data into the VBO once (STATIC), and we want to use the VBO as a source for drawing our scene at each frame (DRAW) // for other usage flags see http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml // {STREAM, STATIC, DYNAMIC} and {DRAW, READ, COPY} // activate the first general attribute 'channel' in the VAO glEnableVertexAttribArray(0); glVertexAttribPointer( 0, // set the attributes of VAO channel 0 3, // this channel has 3 componenets GL_FLOAT, // each of those componenets are floats GL_FALSE, // do not normalize sizeof(Vertex), // stride 0 // channel 0`s data begins at the beginning of the VBO, no offset ); // activate 'channel' idx 1 glEnableVertexAttribArray(1); glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(glm::vec3)) ); // texture coordinates glEnableVertexAttribArray(2); glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(2*sizeof(glm::vec3)) ); // create index buffer glGenBuffers(1, &m_ibID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // // shader initialization // GLuint vs_ID = loadShader(GL_VERTEX_SHADER, "myVert.vert"); GLuint fs_ID = loadShader(GL_FRAGMENT_SHADER, "myFrag.frag"); // create the shader container (program) m_programID = glCreateProgram(); // attach the vertex and fragment (pixel) shaders to the program glAttachShader(m_programID, vs_ID); glAttachShader(m_programID, fs_ID); // make correspondances between the VAO channels and the shader 'in' variables // IMPORTANT: do this prior to linking the programs! glBindAttribLocation( m_programID, // ID of the shader program from which we want to map a variable to a channel 0, // the VAO channel number we want to bind the variable to "vs_in_pos"); // the name of the variable in the shader glBindAttribLocation( m_programID, 1, "vs_in_normal"); glBindAttribLocation( m_programID, 2, "vs_in_tex0"); // link the shaders glLinkProgram(m_programID); // check the linking GLint infoLogLength = 0, result = 0; glGetProgramiv(m_programID, GL_LINK_STATUS, &result); glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &infoLogLength); if ( GL_FALSE == result ) { std::vector<char> ProgramErrorMessage( infoLogLength ); glGetProgramInfoLog(m_programID, infoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); char* aSzoveg = new char[ProgramErrorMessage.size()]; memcpy( aSzoveg, &ProgramErrorMessage[0], ProgramErrorMessage.size()); MessageBoxA(0, aSzoveg, "Sáder Huba panasza", 0); delete aSzoveg; } // we can dispose of the vertex and fragment shaders glDeleteShader( vs_ID ); glDeleteShader( fs_ID ); // // other initializations // // set the projection matrix m_matProj = glm::perspective( 45.0f, m_client_width/(float)m_client_height, 1.0f, 1000.0f ); // query the IDs of the shader uniform variables m_loc_world = glGetUniformLocation( m_programID, "world"); m_loc_worldIT = glGetUniformLocation( m_programID, "worldIT"); m_loc_view = glGetUniformLocation( m_programID, "view" ); m_loc_proj = glGetUniformLocation( m_programID, "proj" ); m_loc_texture = glGetUniformLocation( m_programID, "texture" ); // generate the texture m_textureID = TextureFromFile("texture.jpg"); }
GLuint GLProgram::load(const char* vertexShaderFile, const char* fragmentShaderFile, const char* geometryShaderFile){ assert(vertexShaderFile != nullptr); assert(fragmentShaderFile != nullptr); // Create the shaders GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); assert(glIsShader(VertexShaderID) == GL_TRUE); assert(glIsShader(FragmentShaderID) == GL_TRUE); //Read the shaders files std::string vscode = readFile(vertexShaderFile); const char* VertexShaderCode = vscode.c_str(); std::string fscode = readFile(fragmentShaderFile); const char* FragmentShaderCode = fscode.c_str(); GLint Result = GL_FALSE; int InfoLogLength; // Compile Vertex Shader glShaderSource(VertexShaderID, 1, &VertexShaderCode , NULL); glCompileShader(VertexShaderID); // Check Vertex Shader glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); if(Result == GL_FALSE) { glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); char* log = new char[InfoLogLength]; glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL,log); std::cerr << "Vexter Shader error:" << log << std::endl; std::cerr << VertexShaderCode << std::endl; delete [] log; return 0; } // Compile Fragment Shader glShaderSource(FragmentShaderID, 1, &FragmentShaderCode , NULL); glCompileShader(FragmentShaderID); // Check Fragment Shader glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); if(Result == GL_FALSE) { glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); char* log = new char[InfoLogLength]; glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, log); std::cerr << "Fragment Shader error:" << log << std::endl; std::cerr << FragmentShaderCode << std::endl; delete [] log; return 0; } m_programID = glCreateProgram(); glAttachShader(m_programID, VertexShaderID); glAttachShader(m_programID, FragmentShaderID); GLuint GeometryShaderID; if(geometryShaderFile) { GeometryShaderID = glCreateShader(GL_GEOMETRY_SHADER); assert(glIsShader(GeometryShaderID) == GL_TRUE); std::string gscode = readFile(geometryShaderFile); const char* GeometryShaderCode = gscode.c_str(); // Compile Geometry Shader glShaderSource(GeometryShaderID, 1, &GeometryShaderCode , NULL); glCompileShader(GeometryShaderID); // Check Geometry Shader glGetShaderiv(GeometryShaderID, GL_COMPILE_STATUS, &Result); if(Result == GL_FALSE) { glGetShaderiv(GeometryShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); char* log = new char[InfoLogLength]; glGetShaderInfoLog(GeometryShaderID, InfoLogLength, NULL,log); std::cerr << "Geometry Shader error:" << log << std::endl; std::cerr << GeometryShaderCode << std::endl; delete [] log; return 0; } glAttachShader(m_programID, GeometryShaderID); } // Link the program glLinkProgram(m_programID); // Check the program glGetProgramiv(m_programID, GL_LINK_STATUS, &Result); if(Result == GL_FALSE) { glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> ProgramErrorMessage( std::max(InfoLogLength, int(1)) ); glGetProgramInfoLog(m_programID, InfoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); return 0; } glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); if(geometryShaderFile) { glDeleteShader(GeometryShaderID); } return m_programID; }
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; }
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; }
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; }
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; }
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; }