static void Key(unsigned char key, int x, int y) { const GLfloat step = 3.0; (void) x; (void) y; switch (key) { case 'a': case ' ': Anim = !Anim; if (Anim) glutIdleFunc(Idle); else glutIdleFunc(NULL); break; case 'b': Blend = !Blend; break; case 's': Scale /= 1.1; break; case 'S': Scale *= 1.1; break; case 'f': Filter = (Filter + 1) % NUM_FILTERS; SetTexParams(); break; case 'r': Randomize(); break; #if TEST_CLAMP case 'c': Clamp = !Clamp; SetTexParams(); break; #endif case 'z': Zrot -= step; break; case 'Z': Zrot += step; break; case 27: glutDestroyWindow(Win); exit(0); break; } PrintState(); glutPostRedisplay(); }
static void LoadTextures(GLuint n, const char *files[]) { GLuint i; NumTextures = n < MAX_TEXTURES ? n : MAX_TEXTURES; glGenTextures(n, Textures); SetTexParams(); for (i = 0; i < n; i++) { GLint w, h; glBindTexture(GL_TEXTURE_2D, Textures[i]); #if TEST_MIPMAPS { static const GLubyte color[9][4] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {0, 255, 255}, {255, 0, 255}, {255, 255, 0}, {255, 128, 255}, {128, 128, 128}, {64, 64, 64} }; GLubyte image[256*256*4]; int i, level; w = h = 256; for (level = 0; level <= 8; level++) { for (i = 0; i < w * h; i++) { image[i*4+0] = color[level][0]; image[i*4+1] = color[level][1]; image[i*4+2] = color[level][2]; image[i*4+3] = color[level][3]; } printf("Load level %d: %d x %d\n", level, w>>level, h>>level); glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w>>level, h>>level, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); } } #else if (!LoadRGBMipmaps2(files[i], GL_TEXTURE_2D, GL_RGB, &w, &h)) { printf("Error: couldn't load %s\n", files[i]); exit(1); } #endif TexAspect[i] = (float) w / (float) h; printf("Loaded %s\n", files[i]); } }
int main(int argc, char **argv) { if(!argv[1])return -1; if(argv[1][0]==0)return -1; if(!loadShaders(argv[1]))return -1; Display *dpy; int screen; Window win; GLXContext ctx; XSetWindowAttributes attr; XVisualInfo *vi; Colormap cmap; Atom wmDelete; dpy = XOpenDisplay(0); screen = DefaultScreen(dpy); #ifdef _DEBUG unsigned int width=512,height=320; Bool bOverride=False; #else unsigned int width,height; width=XDisplayWidth(dpy, screen); height=XDisplayHeight(dpy, screen); Bool bOverride=True; #endif vi = glXChooseVisual(dpy, screen, attrlist); if(!vi){printf("No doublebuffering.\n");return -1;} ctx = glXCreateContext(dpy, vi, 0, GL_TRUE); cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone); attr.colormap = cmap; attr.border_pixel = 0; attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; attr.override_redirect = bOverride; win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &attr); if(bOverride){ XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0); XMapRaised(dpy, win); XGrabKeyboard(dpy, win, True, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(dpy, win, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime); }else{ XMapRaised(dpy, win); XSetStandardProperties(dpy, win, "ShaderDemoX", "ShaderDemoX", None, NULL, 0, NULL); } glXMakeCurrent(dpy, win, ctx);// connect the glx-context to the window and get real size Window winDummy; unsigned int borderDummy,x,y,depth; XGetGeometry(dpy, win, &winDummy, &x, &y, &width, &height, &borderDummy, &depth); if (!glXIsDirect(dpy, ctx)){printf("No Direct Rendering\n");return -1;} //setup textures & buffers GLuint texture,fbo[2],rttex[2]; glGenFramebuffers(2, fbo); // Rendering to buffers glGenTextures(2, rttex); // Create named textures to render to glGenTextures(1, &texture); // For keyboard buffer glActiveTexture(GL_TEXTURE0); // Render to texture 0 int i; for(i=0;i<2;i++){ glBindTexture(GL_TEXTURE_2D, rttex[i]); // Bind to the render texture SetTexParams(width,height); // Create it and set params glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, fbo[i]); //Bind to the frame buffer glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rttex[i], 0); //Attach texture GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (status != GL_FRAMEBUFFER_COMPLETE) {glBindFramebuffer(GL_FRAMEBUFFER, 0);return -1;}//Bail } glViewport(0,0,width,height);// Set the Viewport glClear(GL_COLOR_BUFFER_BIT); //create the programs bufferA & image GLuint P_A, VS_A, FS_A, P_I, VS_I, FS_I; GLint zBufA, zTexA, zUniA, zBufI, zTexI, zUniI; char *FSscript=(char *)malloc(65536); //max text for scripts sprintf(FSscript,fsh,bufferA); if(!createprogram(VSscript, FSscript, &P_A, &VS_A, &FS_A)){free(FSscript);return -1;} glUseProgram(P_A); zUniA=glGetUniformLocation(P_A,"Zuni");//set uniforms zBufA=glGetUniformLocation(P_A,"Zbuf"); zTexA=glGetUniformLocation(P_A,"Ztex"); sprintf(FSscript,fsh,image); if(!createprogram(VSscript, FSscript, &P_I, &VS_I, &FS_I)){free(FSscript);return -1;} glUseProgram(P_I); zUniI=glGetUniformLocation(P_I,"Zuni"); zBufI=glGetUniformLocation(P_I,"Zbuf"); zTexI=glGetUniformLocation(P_I,"Ztex"); #ifdef ADD_SOUND pthread_t tid; if(sound[0]!=0){ char fssh[]="uniform float Zuni[2];\nvec2 mainSound(in float);\n\ void main(){float t=floor(gl_FragCoord.y)*Zuni[0]+floor(gl_FragCoord.x);\n\ vec2 v1=mainSound(t*2.0/Zuni[1]),v2=mainSound((t*2.0+1.0)/Zuni[1]);\n\ gl_FragColor=clamp(vec4(v1,v2),-1.0,1.0);}\n%s";
static void LoadTextures(GLuint n, const char *files[]) { GLuint i; NumTextures = n < MAX_TEXTURES ? n : MAX_TEXTURES; glGenTextures(n, Textures); SetTexParams(); for (i = 0; i < n; i++) { GLint w, h; glBindTexture(GL_TEXTURE_2D, Textures[i]); #if TEST_MIPMAPS { static const GLubyte color[9][4] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {0, 255, 255}, {255, 0, 255}, {255, 255, 0}, {255, 128, 255}, {128, 128, 128}, {64, 64, 64} }; GLubyte image[256*256*4]; int i, level; w = h = 256; for (level = 0; level <= 8; level++) { for (i = 0; i < w * h; i++) { image[i*4+0] = color[level][0]; image[i*4+1] = color[level][1]; image[i*4+2] = color[level][2]; image[i*4+3] = color[level][3]; } printf("Load level %d: %d x %d\n", level, w>>level, h>>level); glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w>>level, h>>level, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); } } #elif TEST_GEN_COMPRESSED_MIPMAPS { GLenum intFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; int f; GLenum format; GLubyte *img = LoadRGBImage(files[i], &w, &h, &format); GLboolean write_compressed = GL_FALSE; GLboolean read_compressed = GL_FALSE; glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_2D, 0, intFormat, w, h, 0, format, GL_UNSIGNED_BYTE, img); glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); free(img); glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_INTERNAL_FORMAT, &f); printf("actual internal format 0x%x\n", f); if (write_compressed) { GLint i, sz, w, h; int num_levels = 8; for (i = 0; i < num_levels; i++) { char name[20], *buf; FILE *f; glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &sz); printf("Writing level %d: %d x %d bytes: %d\n", i, w, h, sz); buf = malloc(sz); glGetCompressedTexImageARB(GL_TEXTURE_2D, i, buf); sprintf(name, "comp%d", i); f = fopen(name, "w"); fwrite(buf, 1, sz, f); fclose(f); free(buf); } } if (read_compressed) { GLint i, sz, w, h; int num_levels = 8; for (i = 01; i < num_levels; i++) { char name[20], *buf; FILE *f; glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &sz); buf = malloc(sz); sprintf(name, "comp%d", i); printf("Reading level %d: %d x %d bytes: %d from %s\n", i, w, h, sz, name); f = fopen(name, "r"); fread(buf, 1, sz, f); fclose(f); glCompressedTexImage2DARB(GL_TEXTURE_2D, i, intFormat, w, h, 0, sz, buf); free(buf); } } } #else if (!LoadRGBMipmaps2(files[i], GL_TEXTURE_2D, GL_RGB, &w, &h)) { printf("Error: couldn't load %s\n", files[i]); exit(1); } #endif TexAspect[i] = (float) w / (float) h; printf("Loaded %s\n", files[i]); } }