#include #include #include #include #include #include #include #include #include Display *dpy = NULL; Window root; GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; XVisualInfo *vi = NULL; Colormap cmap; XSetWindowAttributes swa; Window win, win1; GLXContext glc; XWindowAttributes gwa; XEvent xev; #define WIN_WIDTH 128 #define WIN_HEIGHT 128 bool extensionEnabled(const char* extList, const char* extName) { #if 0 const char* start = NULL; const char* where = NULL; const char* terminator = NULL; where = strchr(extName, ' '); if (where || *extName == '\0') return false; for (start=extList;;) { where = strstr(start, extName); if (!where) break; terminator = where + strlen(extName); if (where == start || *(where-1)==' ') if (*terminator == ' ' || *terminator == '\0') return true; start = terminator; } return false; #else return true; #endif } int main(int argc, char** argv) { dpy = XOpenDisplay(NULL); if (dpy==NULL) { printf("\n\tcannot connect to X server\n\n"); exit(0); } root = DefaultRootWindow(dpy); vi = glXChooseVisual(dpy, 0, att); if(vi == NULL) { printf("\n\tno appropriate visual found\n\n"); exit(0); } else { /* %p creates hexadecimal output like in glxinfo */ printf("\n\tvisual %p selected\n", (void *)vi->visualid); } cmap = XCreateColormap(dpy, root, vi->visual, AllocNone); swa.colormap = cmap; swa.event_mask = ExposureMask | KeyPressMask; win = XCreateWindow(dpy, root, 0, 0, WIN_WIDTH, WIN_HEIGHT, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa); XMapWindow(dpy, win); XStoreName(dpy, win, "VERY SIMPLE APPLICATION"); glc = glXCreateContext(dpy, vi, NULL, GL_TRUE); glXMakeCurrent(dpy, win, glc); GLenum glErrno = GL_NO_ERROR; GLuint fbo = 0; GLuint textures[2] = {0,0}; GLuint mProgram_1 = 0; //glEnable(GL_DEPTH_TEST); //set up shader programe const char* vs = "attribute vec4 position;\n" "varying vec2 texcoord;\n" "void main()\n" "{\n" " gl_Position = vec4(position.xy, 0.0, 1.0);\n" " texcoord = (position.xy * 0.5) + 0.5;\n" "}\n"; const char* fs = "uniform sampler2D tex;\n" "varying vec2 texcoord;\n" "void main()\n" "{\n" " gl_FragColor = texture2D(tex, texcoord);\n" "}\n"; mProgram_1 = glCreateProgram(); GLuint vsShader_1 = glCreateShader(GL_VERTEX_SHADER); GLuint fsShader_1 = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vsShader_1, 1, &vs, NULL); glCompileShader(vsShader_1); GLint vsCompileStatus_1; glGetShaderiv(vsShader_1, GL_COMPILE_STATUS, &vsCompileStatus_1); char* infoLog = NULL; GLint infoLen = 0; if (vsCompileStatus_1==0) { printf("Compile vertex shader error!\n"); glGetShaderiv(vsShader_1, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 1) { infoLog = malloc(infoLen); glGetShaderInfoLog(vsShader_1, infoLen, NULL, infoLog); printf("Compile vs error info:%s\n",infoLog); free(infoLog); } } glShaderSource(fsShader_1, 1, &fs, NULL); glCompileShader(fsShader_1); GLint fsCompileStatus_1; glGetShaderiv(fsShader_1, GL_COMPILE_STATUS, &fsCompileStatus_1); if (fsCompileStatus_1==0) { printf("Compile fragment shader error!\n"); glGetShaderiv(fsShader_1, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 1) { infoLog = malloc(infoLen); glGetShaderInfoLog(fsShader_1, infoLen, NULL, infoLog); printf("Compile fs error info:%s\n",infoLog); free(infoLog); } } glAttachShader(mProgram_1, vsShader_1); glAttachShader(mProgram_1, fsShader_1); glLinkProgram(mProgram_1); GLint linkStatus_1; glGetProgramiv(mProgram_1, GL_LINK_STATUS, &linkStatus_1); if (linkStatus_1==0){ printf("Link programe error\n"); } //vendor,renderer,version info printf("Vendor:%s\n Render:%s\n Version:%s\n\n", glGetString(GL_VENDOR), glGetString(GL_RENDERER), glGetString(GL_VERSION)); //Check extensions whethre support bool extensionEnable = false; const char* glExtensions = glGetString(GL_EXTENSIONS); extensionEnable = extensionEnabled(glExtensions, "GL_EXT_texture_storage"); if (!extensionEnable) { printf("%d:Can not support GL_EXT_texture_storage!\n",__LINE__); goto final_out; } extensionEnable = extensionEnabled(glExtensions, "GL_OES_texture_float"); if (!extensionEnable) { printf("%d:Can not support GL_OES_texture_float\n",__LINE__); goto final_out; } extensionEnable = extensionEnabled(glExtensions, "GL_EXT_texture_rg"); if (!extensionEnable) { printf("%d: Can not support GL_EXT_texture_rg\n",__LINE__); goto final_out; } #if 1 GLfloat sourceImageData[12] = { //RGB 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f }; #else GLubyte sourceImageData[12] = { 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255,0, }; #endif glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); #if 1 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB32F, 2, 2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGB, GL_FLOAT, sourceImageData); #else glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB8, 2, 2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGB, GL_UNSIGNED_BYTE, sourceImageData); #endif glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } //Set up fbo and copy texture from source image to dst image glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0); glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } glErrno = glCheckFramebufferStatus(GL_FRAMEBUFFER); //printf("glCheckFramebufferStatus result glErrno=0X%x\n",glErrno); if (glErrno != GL_FRAMEBUFFER_COMPLETE) { printf("%d glCheckFramebufferStatus errno no=0x%x\n",__LINE__,glErrno); goto final_out; } #if 0 glBindTexture(GL_TEXTURE_2D, textures[1]); glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB32F, 2, 2); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 2, 2); glErrno= glGetError(); if (glErrno != GL_NO_ERROR) { printf("%d:The glError no=0x%x\n",__LINE__,glErrno); goto final_out; } #endif final_out: glBindFramebuffer(GL_FRAMEBUFFER, 0); //draw quad on window surface ando compare with dst image if (fbo != 0) { glDeleteFramebuffers(1, &fbo); } if (textures[0] !=0 && textures[1]!=0) { glDeleteTextures(2, textures); } if (mProgram_1!=0) { glDeleteProgram(mProgram_1); } glXMakeCurrent(dpy, None, NULL); glXDestroyContext(dpy, glc); XDestroyWindow(dpy, win); XCloseDisplay(dpy); exit(0); }