int GLSLProgram::CompileShader( GLuint shader ) { glCompileShader( shader ); GLint infoLogLen; GLint compileStatus; CheckGlErrors( "CompileShader:" ); glGetShaderiv( shader, GL_COMPILE_STATUS, &compileStatus ); if( compileStatus == 0 ) { fprintf( stderr, "Failed to compile shader.\n" ); glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &infoLogLen ); if( infoLogLen > 0) { GLchar *infoLog = new GLchar[infoLogLen+1]; glGetShaderInfoLog( shader, infoLogLen, NULL, infoLog); infoLog[infoLogLen] = '\0'; FILE * file = fopen( "glsllog.txt", "w"); fprintf( file, "\n%s\n", infoLog ); fprintf( stderr, "\n%s\n", infoLog ); fclose( file ); delete [] infoLog; } glDeleteShader( shader ); return 0; } CheckGlErrors("LoadShader:Compile 2"); return 1; };
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>> void renderFBO(void) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texId[0]); glTexSubImage2D(GL_TEXTURE_2D,0, 0, 0, img.cols, img.rows, GL_RGB, GL_UNSIGNED_BYTE, img.data); CheckGlErrors("glTexSubImage2D"); #ifdef FBO glDrawBuffer(GL_COLOR_ATTACHMENT0); #endif glClear(GL_COLOR_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texId[0]); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(-1, -1); glTexCoord2f(1, 0); glVertex2f(1, -1); glTexCoord2f(1, 1); glVertex2f(1, 1); glTexCoord2f(0, 1); glVertex2f(-1, 1); glEnd(); glDisable(GL_TEXTURE_2D); checkFramebufferStatus(); CheckGlErrors("render"); #ifndef FBO glutSwapBuffers(); #endif }
int GLSLProgram::LoadShader( const char* fname, GLuint shader ) { FILE * in = fopen( fname, "rb" ); if( in == NULL ) { fprintf( stderr, "Cannot open shader file '%s'\n", fname ); return 0; } fseek( in, 0, SEEK_END ); int length = ftell( in ); fseek( in, 0, SEEK_SET ); // rewind GLubyte * buf = new GLubyte[length+1]; fread( buf, sizeof(GLubyte), length, in ); buf[length] = '\0'; fclose( in ) ; // Now, tell GL about the source! glShaderSource( shader, 1, (const char**)&buf, NULL ); delete [] buf; CheckGlErrors( "LoadShader:ShaderSource" ); return 1; };
void Graphics::StartRendering() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Shader.begin(); Shader.setProjection(ProjectionMatrix); Shader.setView(ViewMatrix); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, SquareVBO); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, SquareUV); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0); CheckGlErrors(); LastTexture = 0; CheckGlErrors(); }
int GLSLProgram::LinkProgram() { if( Gshader != 0 ) { glProgramParameteriEXT( Program, GL_GEOMETRY_INPUT_TYPE_EXT, InputTopology ); glProgramParameteriEXT( Program, GL_GEOMETRY_OUTPUT_TYPE_EXT, OutputTopology ); glProgramParameteriEXT( Program, GL_GEOMETRY_VERTICES_OUT_EXT, 1024 ); } glLinkProgram( Program ); CheckGlErrors("LoadShader:Link 1"); GLchar* infoLog; GLint infoLogLen; GLint linkStatus; glGetProgramiv( this->Program, GL_LINK_STATUS, &linkStatus ); CheckGlErrors("LoadShader:Link 2"); if( linkStatus == 0 ) { glGetProgramiv( this->Program, GL_INFO_LOG_LENGTH, &infoLogLen ); fprintf( stderr, "Failed to link program -- Info Log Length = %d\n", infoLogLen ); if( infoLogLen > 0 ) { infoLog = new GLchar[infoLogLen+1]; glGetProgramInfoLog( this->Program, infoLogLen, NULL, infoLog ); infoLog[infoLogLen] = '\0'; fprintf( stderr, "Info Log:\n%s\n", infoLog ); delete [] infoLog; } glDeleteProgram( Program ); glDeleteShader( Vshader ); glDeleteShader( Gshader ); glDeleteShader( Fshader ); return 0; } return 1; };
void Graphics::Render(IRenderable &renderable) { Shader.setModel(renderable.getTransformation()); CheckGlErrors(); GLuint texture = renderable.getTexture(); CheckGlErrors(); if(LastTexture != texture) { glActiveTexture(GL_TEXTURE0); CheckGlErrors(); glBindTexture(GL_TEXTURE_2D, texture); CheckGlErrors(); Shader.setTexture(0); CheckGlErrors(); LastTexture = texture; } glDrawArrays(GL_TRIANGLES, 0, 6); CheckGlErrors(); }
void GLSLProgram::Create( char *vfile, char *gfile, char *ffile ) { CanDoFragmentShader = IsExtensionSupported( "GL_ARB_fragment_shader" ); CanDoGeometryShader = IsExtensionSupported( "GL_EXT_geometry_shader4" ); CanDoVertexShader = IsExtensionSupported( "GL_ARB_vertex_shader" ); InputTopology = GL_TRIANGLES; OutputTopology = GL_TRIANGLE_STRIP; Vshader = Gshader = Fshader = 0; Program = 0; AttributeLocs.clear(); UniformLocs.clear(); Verbose = false; this->Program = glCreateProgram(); CheckGlErrors( "glCreateProgram" ); if( vfile != NULL && vfile[0] != '\0' ) { if( ! CanDoVertexShader ) { fprintf( stderr, "Warning: this system cannot handle vertex shaders\n" ); } this->Vshader = LoadVertexShader( vfile ); int status = CompileShader( this->Vshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", vfile ); AttachShader( this->Vshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", vfile ); } } if( gfile != NULL && gfile[0] != '\0' ) { if( ! CanDoGeometryShader ) { fprintf( stderr, "Warning: this system cannot handle geometry shaders\n" ); } this->Gshader = LoadGeometryShader( gfile ); int status = CompileShader( this->Gshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", gfile ); AttachShader( Gshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", gfile ); } } if( ffile != NULL && ffile[0] != '\0' ) { if( ! CanDoFragmentShader ) { fprintf( stderr, "Warning: this system cannot handle fragment shaders\n" ); } this->Fshader = LoadFragmentShader( ffile ); int status = CompileShader( this->Fshader ); if( status != 0 ) { if( this->Verbose ) fprintf( stderr, "Shader '%s' compiled.\n", ffile ); AttachShader( Fshader ); } else { fprintf( stderr, "Shader '%s' did not compile.\n", ffile ); } } LinkProgram(); };
bool GLSLProgram::CreateHelper(char *file0, ...) { GLsizei n = 0; GLchar *buf; Valid = true; IncludeGstap = false; Cshader = Vshader = TCshader = TEshader = Gshader = Fshader = 0; Program = 0; AttributeLocs.clear(); UniformLocs.clear(); if (Program == 0) { Program = glCreateProgram(); CheckGlErrors("glCreateProgram"); } va_list args; va_start(args, file0); // This is a little dicey // There is no way, using var args, to know how many arguments were passed // I am depending on the caller passing in a NULL as the final argument. // If they don't, bad things will happen. char *file = file0; int type; while (file != NULL) { int maxBinaryTypes = sizeof(BinaryTypes) / sizeof(struct GLbinarytype); type = -1; char *extension = GetExtension(file); // fprintf( stderr, "File = '%s', extension = '%s'\n", file, extension ); for (int i = 0; i < maxBinaryTypes; i++) { if (strcmp(extension, BinaryTypes[i].extension) == 0) { // fprintf( stderr, "Legal extension = '%s'\n", extension ); LoadProgramBinary(file, BinaryTypes[i].format); break; } } int maxShaderTypes = sizeof(ShaderTypes) / sizeof(struct GLshadertype); for (int i = 0; i < maxShaderTypes; i++) { if (strcmp(extension, ShaderTypes[i].extension) == 0) { // fprintf( stderr, "Legal extension = '%s'\n", extension ); type = i; break; } } if (type < 0) { fprintf(stderr, "Unknown filename extension: '%s'\n", extension); fprintf(stderr, "Legal Extensions are: "); for (int i = 0; i < maxBinaryTypes; i++) { if (i != 0) fprintf(stderr, " , "); fprintf(stderr, "%s", BinaryTypes[i].extension); } fprintf(stderr, "\n"); for (int i = 0; i < maxShaderTypes; i++) { if (i != 0) fprintf(stderr, " , "); fprintf(stderr, "%s", ShaderTypes[i].extension); } fprintf(stderr, "\n"); Valid = false; goto cont; } GLuint shader; switch (ShaderTypes[type].name) { case GL_COMPUTE_SHADER: if (!CanDoComputeShaders) { fprintf(stderr, "Warning: this system cannot handle compute shaders\n"); Valid = false; goto cont; } shader = glCreateShader(GL_COMPUTE_SHADER); break; case GL_VERTEX_SHADER: if (!CanDoVertexShaders) { fprintf(stderr, "Warning: this system cannot handle vertex shaders\n"); Valid = false; goto cont; } shader = glCreateShader(GL_VERTEX_SHADER); break; case GL_TESS_CONTROL_SHADER: if (!CanDoTessControlShaders) { fprintf(stderr, "Warning: this system cannot handle tessellation control shaders\n"); Valid = false; goto cont; } shader = glCreateShader(GL_TESS_CONTROL_SHADER); break; case GL_TESS_EVALUATION_SHADER: if (!CanDoTessEvaluationShaders) { fprintf(stderr, "Warning: this system cannot handle tessellation evaluation shaders\n"); Valid = false; goto cont; } shader = glCreateShader(GL_TESS_EVALUATION_SHADER); break; case GL_GEOMETRY_SHADER: if (!CanDoGeometryShaders) { fprintf(stderr, "Warning: this system cannot handle geometry shaders\n"); Valid = false; goto cont; } //glProgramParameteriEXT( Program, GL_GEOMETRY_INPUT_TYPE_EXT, InputTopology ); //glProgramParameteriEXT( Program, GL_GEOMETRY_OUTPUT_TYPE_EXT, OutputTopology ); //glProgramParameteriEXT( Program, GL_GEOMETRY_VERTICES_OUT_EXT, 1024 ); shader = glCreateShader(GL_GEOMETRY_SHADER); break; case GL_FRAGMENT_SHADER: if (!CanDoFragmentShaders) { fprintf(stderr, "Warning: this system cannot handle fragment shaders\n"); Valid = false; goto cont; } shader = glCreateShader(GL_FRAGMENT_SHADER); break; } // read the shader source into a buffer: FILE * in; int length; FILE * logfile; in = fopen(file, "rb"); if (in == NULL) { fprintf(stderr, "Cannot open shader file '%s'\n", file); Valid = false; goto cont; } fseek(in, 0, SEEK_END); length = ftell(in); fseek(in, 0, SEEK_SET); // rewind buf = new GLchar[length + 1]; fread(buf, sizeof(GLchar), length, in); buf[length] = '\0'; fclose(in); GLchar *strings[2]; int n = 0; if (IncludeGstap) { strings[n] = Gstap; n++; } strings[n] = buf; n++; // Tell GL about the source: glShaderSource(shader, n, (const GLchar **)strings, NULL); delete[] buf; CheckGlErrors("Shader Source"); // compile: glCompileShader(shader); GLint infoLogLen; GLint compileStatus; CheckGlErrors("CompileShader:"); glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus); if (compileStatus == 0) { fprintf(stderr, "Shader '%s' did not compile.\n", file); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen); if (infoLogLen > 0) { GLchar *infoLog = new GLchar[infoLogLen + 1]; glGetShaderInfoLog(shader, infoLogLen, NULL, infoLog); infoLog[infoLogLen] = '\0'; logfile = fopen("glsllog.txt", "w"); if (logfile != NULL) { fprintf(logfile, "\n%s\n", infoLog); fclose(logfile); } fprintf(stderr, "\n%s\n", infoLog); delete[] infoLog; } glDeleteShader(shader); Valid = false; goto cont; } else { if (Verbose) fprintf(stderr, "Shader '%s' compiled.\n", file); glAttachShader(this->Program, shader); } cont: // go to the next file: file = va_arg(args, char *); } va_end(args); // link the entire shader program: glLinkProgram(Program); CheckGlErrors("Link Shader 1"); GLchar* infoLog; GLint infoLogLen; GLint linkStatus; glGetProgramiv(this->Program, GL_LINK_STATUS, &linkStatus); CheckGlErrors("Link Shader 2"); if (linkStatus == 0) { glGetProgramiv(this->Program, GL_INFO_LOG_LENGTH, &infoLogLen); fprintf(stderr, "Failed to link program -- Info Log Length = %d\n", infoLogLen); if (infoLogLen > 0) { infoLog = new GLchar[infoLogLen + 1]; glGetProgramInfoLog(this->Program, infoLogLen, NULL, infoLog); infoLog[infoLogLen] = '\0'; fprintf(stderr, "Info Log:\n%s\n", infoLog); delete[] infoLog; } glDeleteProgram(Program); Valid = false; } else { if (Verbose) fprintf(stderr, "Shader Program linked.\n"); // validate the program: GLint status; glValidateProgram(Program); glGetProgramiv(Program, GL_VALIDATE_STATUS, &status); if (status == GL_FALSE) { fprintf(stderr, "Program is invalid.\n"); Valid = false; } else { if (Verbose) fprintf(stderr, "Shader Program validated.\n"); } } return Valid; }
Graphics::Graphics(int w, int h) { if(!glfwInit()) { cerr << "Error initializing glfw" << endl; } glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL main_window = glfwCreateWindow(w, h, "PEW PEW", NULL, NULL); glfwMakeContextCurrent(main_window); glewExperimental = true; GLenum glew_error = glewInit(); glGetError(); if (GLEW_OK != glew_error) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_error)); } cout << "Vendor: " << glGetString(GL_VENDOR) << endl; CheckGlErrors(); cout << "Renderer: " << glGetString(GL_RENDERER) << endl; CheckGlErrors(); cout << "OpenGL version: " << glGetString(GL_VERSION) << endl; CheckGlErrors(); cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl; CheckGlErrors(); if( glew_error != GLEW_OK ) { cerr << "Error initializing GLEW: " << glewGetErrorString(glew_error) << endl; exit(1); } if( !GLEW_VERSION_3_3 ) { cerr << "Error: GL3.3 is required" << endl; exit(1); } CheckGlErrors(); glDisable(GL_DEPTH_TEST); CheckGlErrors(); glEnable(GL_MULTISAMPLE); CheckGlErrors(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); CheckGlErrors(); glEnable(GL_BLEND); CheckGlErrors(); ProjectionMatrix = glm::ortho((float) 0, (float) w, (float) 0, (float) h, (float) 0.01, (float) 100); ViewMatrix = glm::lookAt( glm::vec3(0, 0, 3), // Camera location glm::vec3(0, 0, 0), // Camera is directed to screen center glm::vec3(0, 1, 0) // Head is up ); CheckGlErrors(); Shader.init(); InitializeRectangle(); CheckGlErrors(); //StartRendering(); CheckGlErrors(); //glClearColor(0.8f, 0.8f, 0.8f, 1.0); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); CheckGlErrors(); spriteRenderer = new SpriteRenderer(); Flip(); CheckGlErrors(); }
void Graphics::Flip() { glDisableVertexAttribArray(0); CheckGlErrors(); glDisableVertexAttribArray(1); CheckGlErrors(); Shader.end(); spriteRenderer->Flip(ProjectionMatrix, ViewMatrix); glfwSwapBuffers(main_window); CheckGlErrors(); glfwPollEvents(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); CheckGlErrors(); Shader.begin(); Shader.setProjection(ProjectionMatrix); Shader.setView(ViewMatrix); CheckGlErrors(); glEnableVertexAttribArray(0); CheckGlErrors(); glBindBuffer(GL_ARRAY_BUFFER, SquareVBO); CheckGlErrors(); glVertexAttribPointer( 0, //index 3, //size GL_FLOAT, //type GL_FALSE, //normalized 0, //stride (void*) 0); // pointer CheckGlErrors(); glEnableVertexAttribArray(1); CheckGlErrors(); glBindBuffer(GL_ARRAY_BUFFER, SquareUV); CheckGlErrors(); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0); CheckGlErrors(); LastTexture = 0; }
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>> void myInit(void) { img = cv::imread("1.png"); if(img.empty()) { printf("Error imread.\n"); return ; } //cv::cvtColor(img, img, CV_BGR2RGB); glGenTextures(2, texId); CheckGlErrors("check glGenTextures"); glBindTexture(GL_TEXTURE_2D, texId[0]); CheckGlErrors("check glBindTextures"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.cols, img.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); CheckGlErrors("check glTexImage2D"); #ifdef FBO img1 = cv::Mat(screenHeight, screenWidth, CV_8UC3); if(img1.empty()) { printf("Error create img1.\n"); return ; } glBindTexture(GL_TEXTURE_2D, texId[1]); CheckGlErrors("check glBindTextures"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1.cols, img1.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &fb); glBindFramebuffer(GL_FRAMEBUFFER, fb); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId[1], 0); #endif GLint nModelViewMatrixCount = 0; GLint nProjectionMatrixCount = 0; glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &nModelViewMatrixCount);// 32 glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &nProjectionMatrixCount); // 4 printf("before, modelview matrix count %d, projection matrix count %d\n", nModelViewMatrixCount, nProjectionMatrixCount); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, screenWidth, screenHeight); //glRotatef(90, 0, 0, -1); glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &nModelViewMatrixCount);// 32 glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &nProjectionMatrixCount); // 4 printf("after, modelview matrix count %d, projection matrix count %d\n", nModelViewMatrixCount, nProjectionMatrixCount); float mv_matrix[16]; float projection_matrix[16]; glGetFloatv(GL_MODELVIEW_MATRIX, mv_matrix); glGetFloatv(GL_PROJECTION_MATRIX, projection_matrix); printf("model view matrix\n"); for(int i=0;i<16;++i) printf("%f ", mv_matrix[i]); printf("\n"); printf("projection matrix\n"); for(int i=0;i<16;++i) printf("%f ", projection_matrix[i]); printf("\n"); CheckGlErrors("myInit()"); }
void main(int argc, char ** argv) { int argc_ = 1; char *argv_[1] = {(char *)"something"}; //glutInit(&argc_, argv_); // initialize the toolkit //GLuint glutwindow = glutCreateWindow("something"); // open the screen window if(!glfwInit()) { printf("glfwinit fail"); return ; } CheckGlErrors("glfwInit"); GLFWwindow *glfwwindow = glfwCreateWindow(screenWidth, screenHeight, "example", NULL, NULL); CheckGlErrors("glfwCreateWindow"); if(glfwwindow == NULL) { printf("glfwCreateWindow fail"); glfwTerminate(); return ; } glfwHideWindow(glfwwindow); CheckGlErrors("glfwHideWindow"); glfwMakeContextCurrent(glfwwindow); CheckGlErrors("glfwMakeContextCurrent"); #ifndef FBO glutDisplayFunc(renderFBO); // register the redraw function #endif GLenum err = glewInit(); if(GLEW_OK != err) { printf("glewInit Error: %s\n", glewGetErrorString(err)); getchar(); return ; } myInit(); glslProcess(); #ifdef FBO for(int i=1;i<3;++i) { char filename[30]; sprintf(filename, "%d.png", i+1); img = cv::imread(filename); if(img.empty()) return ; //cv::cvtColor(img, img, CV_BGR2RGB); renderFBO(); readBack(); //cv::cvtColor(img1, img1, CV_BGR2RGB); printf("output img, width is %d, height is %d, %d\n", img1.cols, img1.rows, img1.channels()); sprintf(filename, "D:\\%d.bmp", 20+i); cv::imwrite(filename, img1); } //getchar(); #else glutMainLoop(); // go into a perpetual loop #endif glDeleteFramebuffers(1, &fb); glDeleteTextures(2, texId); //glutDestroyWindow(glutwindow); glfwTerminate(); getchar(); }