// On successful return, the returned program object ID can be // used to install the program as part of current state // using glUseProgram(). Example: // // GLuint prog = makeShaderProgramFromFiles( vertShaderFile, fragShaderFile, // myBindAttribLocFunc ); // if ( prog != 0 ) // { // glUseProgram( prog ); // // // Set values of uniform variables only // // after the prog has become current. // glUniform3f( getUniLoc(prog, "LightPosition"), 10.0, 10.0, 15.0 ); // printOpenGLError(); // glUniform1f( getUniLoc(prog, "Density"), 16.0 ); // printOpenGLError(); // } // // Use glUseProgram(0) to restore the original vertex processing // and fragement processing fixed functionalities. // ///////////////////////////////////////////////////////////////////////////// GLuint makeShaderProgramFromFiles( const char *vertShaderSrcFilename, const char *fragShaderSrcFilename, void (*bindAttribLocFunc)( GLuint progObj ) ) { // Read shaders' source files. GLchar *vertSrc = NULL, *fragSrc = NULL; if ( vertShaderSrcFilename != NULL ) if ( readShaderSource( vertShaderSrcFilename, &vertSrc ) == 0 ) { free( vertSrc ); free( fragSrc ); return 0; } if ( fragShaderSrcFilename != NULL ) if ( readShaderSource( fragShaderSrcFilename, &fragSrc ) == 0 ) { free( vertSrc ); free( fragSrc ); return 0; } // Create shader program object. GLuint shaderProg = makeShaderProgram( vertSrc, fragSrc, bindAttribLocFunc); free( vertSrc ); free( fragSrc ); return shaderProg; }
int main( int argc, char **argv ) { int success = 0; int gl_major, gl_minor; GLchar *VertexShaderSource, *FragmentShaderSource; glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(500, 500); window = glutCreateWindow( "3Dlabs Brick Shader"); glutIdleFunc(play); glutDisplayFunc(display); glutKeyboardFunc(key); glutReshapeFunc(reshape); glutMotionFunc(motion); glutMouseFunc(mouse); glutSpecialFunc(special); glutTimerFunc(TIMER_FREQUENCY_MILLIS , timer, 0); // Initialize the "OpenGL Extension Wrangler" library glewInit(); // Make sure that OpenGL 2.0 is supported by the driver getGlVersion(&gl_major, &gl_minor); printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); if (gl_major < 2) { printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); printf("Support for OpenGL 2.0 is required for this demo...exiting\n"); exit(1); } glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); NextClearColor(); key('?', 0, 0); // display help #if 0 readShaderSource("brick", &VertexShaderSource, &FragmentShaderSource); #else readShaderSource("cloud", &VertexShaderSource, &FragmentShaderSource); #endif success = installBrickShaders(VertexShaderSource, FragmentShaderSource); CreateNoise3D(); if (success) glutMainLoop(); return 0; }
void initShader(char* shaderName, int id ) { int success = 0; int gl_major, gl_minor; GLchar *VertexShaderSource, *FragmentShaderSource; glewInit(); // Make sure that OpenGL 2.0 is supported by the driver getGlVersion(&gl_major, &gl_minor); printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); if (gl_major < 2) { printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); printf("Support for OpenGL 2.0 is required for this demo...exiting\n"); exit(1); } readShaderSource(shaderName, &VertexShaderSource, &FragmentShaderSource); success = installShaders(VertexShaderSource, FragmentShaderSource, id); if (!success) { printf("Fail to load Shaders!!\n"); exit(0); } }
void kzsGLSL::initShaderProgram( const char* vertFile, const char* fragFile ) { vertShader = glCreateShader( GL_VERTEX_SHADER ); fragShader = glCreateShader( GL_FRAGMENT_SHADER ); if ( readShaderSource( vertShader, vertFile ) ) exit( 1 ); if ( readShaderSource( fragShader, fragFile ) ) exit( 1 ); CompileShader( vertShader, "Compile error in vertex shader.\n\n" ); CompileShader( fragShader, "Compile error in fragment shader.\n\n" ); CreateProgramObject( FRAG_EXIST ); LinkProgram(); }
static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType) { // create a shader object GLuint ShaderObj = glCreateShader(ShaderType); if (ShaderObj == 0) { fprintf(stderr, "Error creating shader type %d\n", ShaderType); exit(0); } std::string outShader = readShaderSource(pShaderText); const char* pShaderSource = outShader.c_str(); // Bind the source code to the shader, this happens before compilation glShaderSource(ShaderObj, 1, (const GLchar**)&pShaderSource, NULL); // compile the shader and check for errors glCompileShader(ShaderObj); GLint success; // check for shader related errors using glGetShaderiv glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success); if (!success) { GLchar InfoLog[1024]; glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog); fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog); system("PAUSE"); exit(1); } // Attach the compiled shader object to the program object glAttachShader(ShaderProgram, ShaderObj); }
int main( int argc, char **argv ) { int success = 0; GLcharARB *VertexShaderSource, *FragmentShaderSource; int error = 0; if (Args(argc, argv) == FALSE) exit(0); glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ); glutInitWindowSize(500, 500); window = glutCreateWindow( "Fire and Smoke" ); glutIdleFunc( play ); glutDisplayFunc( display ); glutKeyboardFunc( key ); glutReshapeFunc( reshape ); printOpenGLError(); if ( !isExtensionSupported( "GL_ARB_shader_objects" ) || !isExtensionSupported( "GL_ARB_fragment_shader" ) || !isExtensionSupported( "GL_ARB_vertex_shader" ) || !isExtensionSupported( "GL_ARB_shading_language_100" ) ) { printf("OpenGL Shading Language extensions not available\n" ); return 1; } if( error ) { printf( "ERROR from wglGetProcAddress\n" ); } createPoints( numParticles ); glDepthFunc( GL_LESS ); glEnable( GL_DEPTH_TEST ); glClearColor( 0.0f, 0.67f, 0.94f, 1.0f ); ParticleTime = 0.0f; readShaderSource( "smoke", &VertexShaderSource, &FragmentShaderSource ); success = installParticleShaders( VertexShaderSource, FragmentShaderSource ); if ( success ) { glutMainLoop(); } else { printf( "Error installing particle shaders" ); scanf( "%d", ParticleTime ); } return 0; }
void MyGlWindow::loadShader( GLhandleARB& program, const char* vertexFile, const char* fragmentFile ){ // initialize glew int glewError = glewInit(); if( glewError ) printf("Error: %s\n", glewGetErrorString(glewError)); // create shader program object and both a vertex and fragment object program = glCreateProgramObjectARB(); GLhandleARB vertex = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GLhandleARB fragment = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // read shaders into strings const char* vertexString = readShaderSource(vertexFile); const char* fragmentString = readShaderSource(fragmentFile); // give ogl the shader source string glShaderSourceARB(vertex, 1, &vertexString, NULL); glShaderSourceARB(fragment, 1, &fragmentString, NULL); // compile shaders glCompileShaderARB(vertex); printInfoLog(vertex); glCompileShaderARB(fragment); printInfoLog(fragment); // attach shaders to program object and link glAttachObjectARB(program, vertex); glAttachObjectARB(program, fragment); glLinkProgramARB(program); // display results GLint success; glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &success); if( !success ) { printf("%s and %s shader could not be linked!\n", vertexFile, fragmentFile ); exit(-1); // bail out, display isn't going to happen at this point. } else { printf("%s and %s loaded and linked successfully.\n", vertexFile, fragmentFile ); } }
// Create a GLSL program object from vertex and fragment shader files GLuint InitShader(const char* vShaderFile, const char* fShaderFile) { struct Shader { const char* filename; GLenum type; GLchar* source; } shaders[2] = { { vShaderFile, GL_VERTEX_SHADER, NULL }, { fShaderFile, GL_FRAGMENT_SHADER, NULL } }; GLuint program = glCreateProgram(); for (int i = 0; i < 2; ++i) { Shader& s = shaders[i]; s.source = readShaderSource(s.filename); if (shaders[i].source == NULL) { std::cerr << "Failed to read " << s.filename << std::endl; exit(EXIT_FAILURE); } GLuint shader = glCreateShader(s.type); glShaderSource(shader, 1, (const GLchar**) &s.source, NULL); glCompileShader(shader); GLint compiled; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); if (!compiled) { std::cerr << s.filename << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetShaderInfoLog(shader, logSize, NULL, logMsg); std::cerr << logMsg << std::endl; delete [] logMsg; exit(EXIT_FAILURE); } delete [] s.source; glAttachShader(program, shader); } /* link and error check */ glLinkProgram(program); GLint linked; glGetProgramiv(program, GL_LINK_STATUS, &linked); if (!linked) { std::cerr << "Shader program failed to link" << std::endl; GLint logSize; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetProgramInfoLog(program, logSize, NULL, logMsg); std::cerr << logMsg << std::endl; delete [] logMsg; exit(EXIT_FAILURE); } /* use program object */ glUseProgram(program); return program; }
void kzsGLSL::initShaderProgram( const char* vertFile ) { vertShader = glCreateShader( GL_VERTEX_SHADER ); if ( readShaderSource( vertShader, vertFile ) ) exit( 1 ); CompileShader( vertShader, "Compile error in vertex shader.\n\n" ); CreateProgramObject( FRAG_NONE ); LinkProgram(); }
bool Shader::Load(const char *filename) { strcpy(m_FileName, filename); // console->printfAndRender("Compiling Shader '%s'", filename); if( !readShaderSource() ) return false; if( !installShaders() ) return false; return true; }
GLuint createShader(GLuint stype, const std::string& file) { string str = readShaderSource(file); assert(!str.empty()); if(!str.empty()) { GLuint shader = createShaderFromSource(stype, str); if(!check(shader)) { cerr << file << endl; printShaderLog(shader); glDeleteShader(shader); return 0; } return shader; } return 0; }
// Use this method to add shaders to the program. When finished - call finalize() void ShadedObject::addShader(GLenum shaderType, const char* fileName) { GLchar* source = readShaderSource(fileName); if (source == NULL) { std::cerr << "Failed to read " << fileName << std::endl; exit(EXIT_FAILURE); } GLuint shaderObj = glCreateShader(shaderType); if (shaderObj == 0) { std::cerr << "Error creating shader type " << shaderType << std::endl; exit(EXIT_FAILURE); } // Save the shader object - will be deleted in the destructor m_shaderObjList.push_back(shaderObj); glShaderSource(shaderObj, 1, (const GLchar**)&source, NULL); glCompileShader(shaderObj); GLint compiled; glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &compiled); if (!compiled) { std::cerr << fileName << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv(shaderObj, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetShaderInfoLog(shaderObj, logSize, NULL, logMsg); std::cerr << logMsg << std::endl; delete[] logMsg; exit(EXIT_FAILURE); } delete[] source; glAttachShader(_programID, shaderObj); }
/* * Main * ******************************************************************************/ int main( int argc, char **argv ) { int success = 0; int gl_major, gl_minor; GLchar *VertexShaderSource, *FragmentShaderSource; glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(500, 500); window = glutCreateWindow( "Temperature Shader"); glutDisplayFunc(display); glutReshapeFunc(reshape); // Initialize the "OpenGL Extension Wrangler" library glewInit(); // Make sure that OpenGL 2.0 is supported by the driver getGlVersion(&gl_major, &gl_minor); printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); if (gl_major < 2) { printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); printf("Support for OpenGL 2.0 is required for this demo...exiting\n"); exit(1); } readShaderSource("temp", &VertexShaderSource, &FragmentShaderSource); success = installShaders(VertexShaderSource, FragmentShaderSource); if (success) glutMainLoop(); return 0; }
unsigned int Shader::genShader(unsigned int type, const char* fileName) { char* log; // create a shader object. GLuint shader = glCreateShader(type); // read the shader code. char* shaderSource = readShaderSource(fileName); if (!shaderSource) return 0; const char* ptrShaderSource = shaderSource; // bind the code to the shader object. glShaderSource(shader, 1, &ptrShaderSource, NULL); free(shaderSource); // compile the shader. glCompileShader(shader); GLint status = 0; // query the status of the compiling. glGetShaderiv(shader, GL_COMPILE_STATUS, &status); GLint length; // read the length of the log. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); log = (GLchar*)malloc(length); // get the log info. glGetShaderInfoLog(shader, length, &length, log); printf("%s\n", log); free(log); if (!status) { // delete shader. glDeleteShader(shader); return 0; } return shader; }
std::string readFragmentSource() { return readShaderSource("frag"); }
std::string readVertexSource() { return readShaderSource("vert"); }
GLuint InitShader(const char* vShader, const char* fShader) { Shader shaders[2]; shaders[0].filename = vShader; shaders[0].type = GL_VERTEX_SHADER; shaders[1].filename = fShader; shaders[1].type = GL_FRAGMENT_SHADER; // create the program in the GPU GLuint program = glCreateProgram(); for ( int i = 0; i < 2; ++i ) { Shader& s = shaders[i]; // read file contents onto ONE LINE, doesn't seem to work // if file passed to glShaderSource in multiple lines bool read = readShaderSource( s.filename, s.source ); if ( !read ) { std::cerr << "Failed to read " << s.filename << std::endl; exit( EXIT_FAILURE ); } // First build the shader GLuint shader = glCreateShader( s.type ); // Second load the source const GLchar* sourceCode = s.source.c_str(); glShaderSource( shader, 1, const_cast<const GLchar**>(&sourceCode), NULL ); // Third compile the shader glCompileShader( shader ); // Make sure it compiled, other display error message GLint compiled; glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { std::cerr << s.filename << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize ); char* logMsg = new char[logSize]; glGetShaderInfoLog( shader, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } glAttachShader( program, shader ); } // link and error check glLinkProgram(program); GLint linked; glGetProgramiv( program, GL_LINK_STATUS, &linked ); if ( !linked ) { std::cerr << "Shader program failed to link" << std::endl; GLint logSize; glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetProgramInfoLog( program, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } // use program object glUseProgram(program); return program; }
// Create a GLSL program object from vertex and fragment shader files GLuint InitShader(const char* vShaderFileName, const char* fShaderFileName) { GLuint vertex_shader, fragment_shader; GLchar *vs_text, *fs_text; GLuint program; // check GLSL version printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); // Create shader handlers vertex_shader = glCreateShader(GL_VERTEX_SHADER); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); // Read source code from file vs_text = readShaderSource(vShaderFileName); fs_text = readShaderSource(fShaderFileName); // error check if ( vs_text == NULL ) { printf("Failed to read from vertex shader file %s\n", vShaderFileName); exit( 1 ); } else if (DEBUG_ON) { printf("read shader code:\n%s\n", vs_text); } if ( fs_text == NULL ) { printf("Failed to read from fragent shader file %s\n", fShaderFileName); exit( 1 ); } else if (DEBUG_ON) { printf("read shader code:\n%s\n", fs_text); } // Set shader source const char *vv = vs_text; const char *ff = fs_text; glShaderSource(vertex_shader, 1, &vv, NULL); glShaderSource(fragment_shader, 1, &ff, NULL); // Compile shaders glCompileShader(vertex_shader); glCompileShader(fragment_shader); // Check for errors in compiling shaders GLint compiled; glGetShaderiv( vertex_shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { printf("vertex_shader failed to compile\n"); if (DEBUG_ON) { GLint logMaxSize, logLength; glGetShaderiv( vertex_shader, GL_INFO_LOG_LENGTH, &logMaxSize ); printf("printing error message of %d bytes\n", logMaxSize); char* logMsg = new char[logMaxSize]; glGetShaderInfoLog( vertex_shader, logMaxSize, &logLength, logMsg ); printf("%d bytes retrieved\n", logLength); printf("error message: %s\n", logMsg); delete[] logMsg; } exit(1); } glGetShaderiv( fragment_shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { printf("fragment_shader failed to compile\n"); if (DEBUG_ON) { GLint logMaxSize, logLength; glGetShaderiv( fragment_shader, GL_INFO_LOG_LENGTH, &logMaxSize ); printf("printing error message of %d bytes\n", logMaxSize); char* logMsg = new char[logMaxSize]; glGetShaderInfoLog( fragment_shader, logMaxSize, &logLength, logMsg ); printf("%d bytes retrieved\n", logLength); printf("error message: %s\n", logMsg); delete[] logMsg; } exit(1); } // Create the program program = glCreateProgram(); // Attach shaders to program glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); // Link and set program to use glLinkProgram(program); glUseProgram(program); return program; }
static void initShader(const GLchar* vShaderFile, const GLchar* fShaderFile) { GLint status; GLchar *vSource, *fSource; GLuint vShader, fShader; /* read shader files */ vSource = readShaderSource(vShaderFile); if(vSource==NULL) { printf( "Failed to read vertex shaderi\n"); exit(EXIT_FAILURE); } fSource = readShaderSource(fShaderFile); if(fSource==NULL) { printf("Failed to read fragment shader"); exit(EXIT_FAILURE); } /* create program and shader objects */ vShader = glCreateShader(GL_VERTEX_SHADER); fShader = glCreateShader(GL_FRAGMENT_SHADER); program = glCreateProgram(); /* attach shaders to the program object */ glAttachShader(program, vShader); glAttachShader(program, fShader); /* read shaders */ glShaderSource(vShader, 1, (const GLchar**) &vSource, NULL); glShaderSource(fShader, 1, (const GLchar**) &fSource, NULL); /* compile vertex shader shader */ glCompileShader(vShader); /* error check */ glGetShaderiv(vShader, GL_COMPILE_STATUS, &status); if(status==GL_FALSE) { printf("Failed to compile the vertex shader.\n"); glGetShaderiv(vShader, GL_INFO_LOG_LENGTH, &elength); ebuffer = malloc(elength*sizeof(char)); glGetShaderInfoLog(vShader, elength, NULL, ebuffer); printf("%s\n", ebuffer); exit(EXIT_FAILURE); } /* compile fragment shader shader */ glCompileShader(fShader); /* error check */ glGetShaderiv(fShader, GL_COMPILE_STATUS, &status); if(status==GL_FALSE) { printf("Failed to compile the fragment shader.\n"); glGetShaderiv(fShader, GL_INFO_LOG_LENGTH, &elength); ebuffer = malloc(elength*sizeof(char)); glGetShaderInfoLog(fShader, elength, NULL, ebuffer); printf("%s\n", ebuffer); exit(EXIT_FAILURE); } /* link and error check */ glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &status); if(status==GL_FALSE) { printf("Failed to link program object.\n"); glGetProgramiv(program, GL_INFO_LOG_LENGTH, &elength); ebuffer = malloc(elength*sizeof(char)); glGetProgramInfoLog(program, elength, &elength, ebuffer); printf("%s\n", ebuffer); exit(EXIT_FAILURE); } /* use program object */ glUseProgram(program); /* set up uniform parameter */ /* timeParam = glGetUniformLocation(program, "time"); */ }
// Create a GLSL program object from vertex and fragment shader files GLuint Painter::init_shader(const char* vShaderFile, const char* fShaderFile, const char* outputAttributeName, const char* gShaderFile) { struct Shader { const char* filename; GLenum type; GLchar* source; }; std::vector<Shader> shaders = { { vShaderFile, GL_VERTEX_SHADER, NULL }, { fShaderFile, GL_FRAGMENT_SHADER, NULL } }; if(gShaderFile) { shaders.push_back({ gShaderFile, GL_GEOMETRY_SHADER, NULL }); } GLuint program = glCreateProgram(); for ( int i = 0; i < shaders.size(); ++i ) { Shader& s = shaders[i]; s.source = readShaderSource( s.filename ); if ( shaders[i].source == NULL ) { std::cerr << "Failed to read " << s.filename << std::endl; exit( EXIT_FAILURE ); } GLuint shader = glCreateShader( s.type ); glShaderSource( shader, 1, (const GLchar**) &s.source, NULL ); glCompileShader( shader ); GLint compiled; glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { std::cerr << s.filename << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize ); char* logMsg = new char[logSize]; glGetShaderInfoLog( shader, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } delete [] s.source; glAttachShader( program, shader ); } /* Link output */ glBindFragDataLocation(program, 0, outputAttributeName); /* link and error check */ glLinkProgram(program); GLint linked; glGetProgramiv( program, GL_LINK_STATUS, &linked ); if ( !linked ) { std::cerr << "Shader program failed to link" << std::endl; GLint logSize; glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetProgramInfoLog( program, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } /* use program object */ glUseProgram(program); return program; }
// Create a GLSL program object from vertex and fragment shader files GLuint initShader(const char* vShaderFile, const char* fShaderFile) { // Function-local class for holding shader information struct Shader { const char* filename; GLenum type; GLchar* source; } shaders[2] = { { vShaderFile, GL_VERTEX_SHADER, NULL }, { fShaderFile, GL_FRAGMENT_SHADER, NULL } }; // create a program object and get a UID to it GLuint program = glCreateProgram(); // Read and then compile each shader for ( int i = 0; i < 2; ++i ) { // Read in and check for parse errors Shader& s = shaders[i]; s.source = readShaderSource( s.filename ); if ( shaders[i].source == NULL ) { std::cerr << "Failed to read " << s.filename << std::endl; ; exit( EXIT_FAILURE ); } // create a shader object and get a UID to it GLuint shader = glCreateShader( s.type ); // Attach the program string to the shader object (on the card) glShaderSource( shader, 1, (const GLchar**) &s.source, NULL ); // Attempt to compile the shader and check for errors glCompileShader( shader ); GLint compiled; glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { std::cerr << s.filename << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize ); char* logMsg = new char[logSize]; glGetShaderInfoLog( shader, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; ; exit( EXIT_FAILURE ); } // Clean up memory allocated in readShaderSource() delete [] s.source; // Plug the shader into the program object glAttachShader( program, shader ); } // link program and check for errors glLinkProgram(program); GLint linked; glGetProgramiv( program, GL_LINK_STATUS, &linked ); if ( !linked ) { std::cerr << "Shader program failed to link" << std::endl; GLint logSize; glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetProgramInfoLog( program, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; ; exit( EXIT_FAILURE ); } // activate the program object glUseProgram(program); return program; }
/////////////////////////////////////////////////////// // // This routine creates the shader program, including the vertex shader and fragment shader objects // GLuint SetupGLSL(char *fileName){ GLuint programObject; GLuint vertexShaderObject; GLuint fragmentShaderObject; check_graphics(); // check the capability of the graphics card in use programObject = glCreateProgram(); // create an overall shader program if (programObject == 0) { // error checking printf(" Error creating shader program object.\n"); exit(1); } else printf(" Succeeded creating shader program object.\n"); vertexShaderObject = glCreateShader(GL_VERTEX_SHADER); if (vertexShaderObject == 0) { // error checking printf(" Error creating vertex shader object.\n"); exit(1); } else printf(" Succeeded creating vertex shader object.\n"); fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER); if (fragmentShaderObject == 0) { // error checking printf(" Error creating fragment shader object.\n"); exit(1); } else printf(" Succeeded creating fragment shader object.\n"); // now input the vertex and fragment programs as ascii readShaderSource(fileName, &vertexShaderSource, &fragmentShaderSource); glShaderSource(vertexShaderObject,1,(const GLchar**)&vertexShaderSource,NULL); glShaderSource(fragmentShaderObject,1,(const GLchar**)&fragmentShaderSource,NULL); // now compile the shader code; vertex shader first, followed by fragment shader glCompileShader(vertexShaderObject); // error checking and printing out log if error GLint result; glGetShaderiv(vertexShaderObject, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { printf(" vertex shader compilation failed!\n"); GLint logLen; glGetShaderiv(vertexShaderObject, GL_INFO_LOG_LENGTH, &logLen); if (logLen > 0) { char *log = (char*) malloc(logLen); GLsizei written; glGetShaderInfoLog(vertexShaderObject, logLen, &written, log); printf("Sahder log: \n %s", log); free(log); } } glCompileShader(fragmentShaderObject); glGetShaderiv(fragmentShaderObject, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { printf(" fragment shader compilation failed!\n"); GLint logLen; glGetShaderiv(fragmentShaderObject, GL_INFO_LOG_LENGTH, &logLen); if (logLen > 0) { char *log = (char*) malloc(logLen); GLsizei written; glGetShaderInfoLog(fragmentShaderObject, logLen, &written, log); printf("Sahder log: \n %s", log); free(log); } } // insert compiler error checking code later glAttachShader(programObject, vertexShaderObject); glAttachShader(programObject, fragmentShaderObject); glLinkProgram(programObject); // insert linking error checking code later return(programObject); }
void begin() { // old-BATB didn't like to be initialized more than once :( // hence, ignore re-init for specified parts, and continue with previous state static bool empty = true; // clear exit, making possible to start over again //do_exit = false; // copied from old::main: //MainWindow::winPosX = 100; //MainWindow::winPosY = 100; //MainWindow::winWidth = 640; //MainWindow::winHeight = 480; // init glut //glutInitWindowPosition(MainWindow::winPosX, MainWindow::winPosY); //glutInitWindowSize (MainWindow::winWidth, MainWindow::winHeight); //glutInit (&argc, argv); //glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //MainWindow::main_window = glutCreateWindow("Beat About the Bush"); MainWindow::enableDisplayFunc(); // implemented as GLFW: GLFWwindow* win = glfwGetCurrentContext(); glfwSetKeyCallback( win, MainWindow::glfwKey ); //glutKeyboardFunc (MainWindow::keyDownFn); //glutSpecialFunc (MainWindow::specialDownFn); ////glutKeyboardUpFunc (MainWindow::keyUpFn); ////glutSpecialUpFunc (MainWindow::specialUpFn); glfwSetCursorPosCallback( win, MainWindow::glfwCursorPos ); //glutMouseFunc (MainWindow::mousefn); glfwSetMouseButtonCallback( win, MainWindow::glfwMouseButton); //glutMotionFunc (MainWindow::motionfn); glfwSetWindowSizeCallback( win, MainWindow::glfwWindowSize ); //glutReshapeFunc (MainWindow::reshapefn); glfwSetWindowFocusCallback( win, MainWindow::glfwWindowFocus ); //glutVisibilityFunc (MainWindow::visibility); if ( empty ) { // init plib, with no GLUT-binding! puInitOLD(); puSetDefaultStyle ( PUSTYLE_SMALL_SHADED ); puSetDefaultColourScheme ( 0.3f, 0.4f, 0.6f, 1.0f); // Initialize the "OpenGL Extension Wrangler" library //glewInit(); } mainWindow.initLights(); if ( empty ) { mainWindow.init(); printVersions(); // Make sure that OpenGL 2.0 is supported by the driver int gl_major, gl_minor; getGlVersion(&gl_major, &gl_minor); printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); if (gl_major < 2) { printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); printf("Support for OpenGL 2.0 is required for this demo...exiting\n"); //exit(1); old::exit(1); return; } // init shaders GLchar *dayVSSource, *dayFSSource; readShaderSource( old::file("shader/day").c_str(), &dayVSSource, &dayFSSource); dayShader = installShaders(dayVSSource, dayFSSource); float forestGreen[] = {34.0/255, 139.0/255, 34.0/255}; //float auburn[] = {113.0/255, 47.0/255, 38.0/255}; float grey[] = {0.5, 0.5, 0.5}; //float sepia[] = {112.0/255, 66.0/255, 20.0/255}; setUniform3f(dayShader, "GrassColor", forestGreen[0], forestGreen[1], forestGreen[2]); setUniform3f(dayShader, "RockColor", grey[0], grey[1], grey[2]); //setUniform3f(dayShader, "DirtColor", sepia[0], sepia[1], sepia[2]); //setUniform4f(dayShader, "LightPos", 0.0, 0.0, 100.0, 1.0); setUniform1f(dayShader, "Scale", 1.0); setUniform1f(dayShader, "TerrainHigh", 0.1); setUniform1f(dayShader, "TerrainLow", 0.1); setUniform1i(dayShader, "Trees", 0); // sampler setUniform1i(dayShader, "AltGrad", 1); // sampler setUniform1i(dayShader, "Noise", 2); // sampler GLchar *nightVSSource, *nightFSSource; readShaderSource( old::file("shader/night3").c_str(), &nightVSSource, &nightFSSource); nightShader = installShaders(nightVSSource, nightFSSource); setUniform3f(nightShader, "BrickColor", 1.0, 0.3, 0.2); setUniform3f(nightShader, "MortarColor", 0.85, 0.86, 0.84); setUniform2f(nightShader, "BrickSize", 0.30, 0.15); setUniform2f(nightShader, "BrickPct", 0.90, 0.85); setUniform1i(nightShader, "numEnabledLights", 2); setUniform3f(nightShader, "GrassColor", forestGreen[0], forestGreen[1], forestGreen[2]); setUniform3f(nightShader, "RockColor", grey[0], grey[1], grey[2]); setUniform1f(nightShader, "Scale", 1.0); setUniform1f(nightShader, "TerrainHigh", 0.1); setUniform1f(nightShader, "TerrainLow", 0.1); setUniform1i(nightShader, "Trees", 0); // sampler setUniform1i(nightShader, "AltGrad", 1); // sampler setUniform1i(nightShader, "Noise", 2); // sampler GLchar *nightTreeVS, *nightTreeFS; readShaderSource( old::file("shader/nightTree").c_str(), &nightTreeVS, &nightTreeFS); nightTrees = installShaders(nightTreeVS, nightTreeFS); setUniform1i(nightTrees, "Trees", 0); // sampler // BUGFIX: free( dayVSSource ); free( dayFSSource ); free( nightVSSource ); free( nightFSSource ); free( nightTreeVS ); free( nightTreeFS ); } // enter main loop //if (dayShader && nightShader) // glutMainLoop(); //return 0; empty = false; }
// Create a NULL-terminated string by reading the provided file static char* readShaderSource(const char* shaderFile) { //FILE* fp = fopen(shaderFile, "r"); // VS gives warning about being insecure :-) // one way to deal with warning: http://stackoverflow.com/questions/2575116/fopen-fopen-s-and-writing-to-files: FILE* fp; #ifdef WIN32 errno_t err; if( (err = fopen_s( &fp, shaderFile, "rb" )) !=0 ) { #else if ((fp_config = fopen(shaderFile, "r")) == NULL) { #endif fprintf(stderr, "Cannot open config file %s!\n", shaderFile); } if ( fp == NULL ) { return NULL; } fseek(fp, 0L, SEEK_END); long size = ftell(fp); fseek(fp, 0L, SEEK_SET); char* buf = new char[size + 1]; fread(buf, 1, size, fp); buf[size] = '\0'; fclose(fp); return buf; } // Create a GLSL program object from vertex and fragment shader files GLuint CompileShader(const char* vShaderFile, const char* fShaderFile) { struct Shader { const char* filename; GLenum type; GLchar* source; } shaders[2] = { { vShaderFile, GL_VERTEX_SHADER, NULL }, { fShaderFile, GL_FRAGMENT_SHADER, NULL } }; GLuint program = glCreateProgram(); for ( int i = 0; i < 2; ++i ) { Shader& s = shaders[i]; s.source = readShaderSource( s.filename ); if ( shaders[i].source == NULL ) { std::cerr << "Failed to read " << s.filename << std::endl; exit( EXIT_FAILURE ); } GLuint shader = glCreateShader( s.type ); glShaderSource( shader, 1, (const GLchar**) &s.source, NULL ); glCompileShader( shader ); GLint compiled; glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled ); if ( !compiled ) { std::cerr << s.filename << " failed to compile:" << std::endl; GLint logSize; glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize ); char* logMsg = new char[logSize]; glGetShaderInfoLog( shader, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } delete [] s.source; glAttachShader( program, shader ); } /* link and error check */ glLinkProgram(program); GLint linked; glGetProgramiv( program, GL_LINK_STATUS, &linked ); if ( !linked ) { std::cerr << "Shader program failed to link" << std::endl; GLint logSize; glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize); char* logMsg = new char[logSize]; glGetProgramInfoLog( program, logSize, NULL, logMsg ); std::cerr << logMsg << std::endl; delete [] logMsg; exit( EXIT_FAILURE ); } /* use program object */ glUseProgram(program); return program; }
int initResources( void ) { const char* vsSource; const char* fsSource; char* attribute_coord3d_name; char* attribute_corRGB_name; const char* uniform_matriz_global_name; GLint compileOK = GL_FALSE; GLint linkOK = GL_FALSE; /* String contendo o codigo (GLSL) do vertex-shader */ vsSource = readShaderSource( "vShader.glsl" ); if( vsSource == NULL ) { fprintf( stderr, "ERRO no LEITURA do VERTEX-SHADER\n" ); exit( EXIT_FAILURE ); } /* Para passar vertices 3D ao Vertex-Shader */ attribute_coord3d_name = "coord3d"; /* Para passar cores ao Vertex-Shader */ attribute_corRGB_name = "v_corRGB"; /* Para passar a matriz global ao Vertex-Shader */ uniform_matriz_global_name = "matriz"; /* String contendo o codigo (GLSL) do fragment-shader */ fsSource = readShaderSource( "fShader.glsl" ); if( fsSource == NULL ) { fprintf( stderr, "ERRO no LEITURA do FRAGMENT-SHADER\n" ); exit( EXIT_FAILURE ); } /* Criar e compilar o Vertex-Shader */ vs = glCreateShader( GL_VERTEX_SHADER ); glShaderSource( vs, 1, &vsSource, NULL ); glCompileShader( vs ); glGetShaderiv( vs, GL_COMPILE_STATUS, &compileOK ); if( compileOK == 0 ) { fprintf( stderr, "Error in vertex shader\n" ); return 0; } /* Criar e compilar o Fragment-Shader */ fs = glCreateShader( GL_FRAGMENT_SHADER ); glShaderSource( fs, 1, &fsSource, NULL ); glCompileShader( fs ); glGetShaderiv( fs, GL_COMPILE_STATUS, &compileOK ); if( compileOK == 0 ) { fprintf( stderr, "Error in fragment shader\n" ); return 0; } /* Criando o programa em GLSL */ programaGLSL = glCreateProgram(); glAttachShader( programaGLSL, vs ); glAttachShader( programaGLSL, fs ); glLinkProgram( programaGLSL ); glGetProgramiv( programaGLSL, GL_LINK_STATUS, &linkOK ); if( linkOK == 0 ) { fprintf( stderr, "Error while linking\n" ); return 0; } /* Para input ao Vertex-Shader */ attribute_coord3d = glGetAttribLocation( programaGLSL, attribute_coord3d_name ); if( attribute_coord3d == -1 ) { fprintf( stderr, "Could not bind attribute %s\n", attribute_coord3d_name ); return 0; } attribute_corRGB = glGetAttribLocation( programaGLSL, attribute_corRGB_name ); if( attribute_corRGB == -1 ) { fprintf( stderr, "Could not bind attribute %s\n", attribute_corRGB_name ); return 0; } uniform_location_matriz_global = glGetUniformLocation( programaGLSL, "matriz"); if( uniform_location_matriz_global == -1 ) { fprintf( stderr, "Could not bind uniform %s\n", uniform_matriz_global_name ); return 0; } return 1; }