/* way cool cube */ /* cc wc_cube.c -o wc_cube -L/usr/lib -L/usr/X11R6/lib -lX11 -lGL -lGLU -lm */ #include #include #include #include #include int vattrib [] = { GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 1, /* at least 1 means get largest */ None }; Display *display; Window rootw, win; main (int argc, char **argv) { XSizeHints whisper; XEvent report; unsigned long eventmask; int screen, width, height; XVisualInfo *pgob; XSetWindowAttributes attrib; GLXContext ctx; XColor white, black; int bsz; if ((display = XOpenDisplay (NULL)) == NULL) { fprintf(stderr,"can't open\n"); exit(1); } screen = DefaultScreen (display); width = 512; height = 512; rootw = RootWindow (display, screen); pgob = glXChooseVisual (display, screen, vattrib); attrib.colormap = XCreateColormap (display, rootw, pgob->visual, AllocNone); white.red = 0xffff; white.green = 0xffff; white.blue = 0xffff; black.red = 0; black.green = 0; black.blue = 0; XAllocColor(display,attrib.colormap,&white); XAllocColor(display,attrib.colormap,&black); attrib.background_pixel = white.pixel; attrib.border_pixel = black.pixel; /* create output window with black border and white background; upper left corner will be at (0,0); border size at this level is overriden by window manager, so just make it 0 */ bsz=0; win = XCreateWindow (display, rootw, 0, 0, width, height, bsz, pgob->depth, InputOutput, pgob->visual, CWBorderPixel | CWColormap | CWBackPixel, &attrib); /* attach OpenGL output to selected window */ ctx = glXCreateContext (display, pgob, NULL, True); glXMakeCurrent (display, win, ctx); /* whisper hints */ whisper.flags = PSize | PPosition; whisper.width = width; whisper.height = height; XSetStandardProperties (display, win, "win", "win_icon", None, argv, argc, &whisper); eventmask = ExposureMask | KeyPressMask; XSelectInput (display, win, eventmask); XMapWindow (display, win); /* wait on expose */ XNextEvent (display, &report); while (report.type != Expose) { printf ("do wah\n"); XNextEvent (display,&report); } while (1) { switch (report.type) { case Expose: do_cube (); break; case KeyPress: glXDestroyContext (display, ctx); XCloseDisplay (display); exit (0); default: sleep (1); } XNextEvent(display,&report); } } struct point { float x; float y; float z; }; do_cube () { struct point eyept, viewpt; /* dark gray background */ glClearColor (0.3, 0.3, 0.3, 0.0); glClear (GL_COLOR_BUFFER_BIT); /* call for wireframe */ glPolygonMode (GL_FRONT,GL_LINE); glPolygonMode (GL_BACK,GL_LINE); /* fat yellow lines */ glColor4f (1.0, 1.0, 0.0, 1.0); glLineWidth (2.0); /* carve out gob */ glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (45.0, 1.0, 0.1, 20.0); /* draw pix at origin */ glMatrixMode (GL_MODELVIEW); glLoadIdentity (); eyept.x = 2.5; eyept.y = 1.8; eyept.z = 2.0; viewpt.x = 0.0; viewpt.y = 0.0; viewpt.z = 0.0; gluLookAt(eyept.x, eyept.y, eyept.z, viewpt.x, viewpt.y, viewpt.z, 0.0, 1.0, 0.0); picasso (); } float vertex [8] [3] = { {0.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 1.0}, {1.0, 1.0, 1.0}, {1.0, 0.0, 1.0} }; int faces [6] [4] = { /* there are 6 faces; 4 verticies specify a face; looking from positive z axis we would have: */ /* front face */ {4, 5, 6, 7}, /* back face */ {3, 2, 1, 0}, /* left side */ {0, 1, 5, 4}, /* right side */ {7, 6, 2, 3}, /* top */ {5, 1, 2, 6}, /* bottom */ {0, 4, 7, 3} }; picasso() { int i; for (i = 0; i < 6; i++){ glBegin (GL_QUADS); glVertex3fv (vertex [faces [i] [0]]); glVertex3fv (vertex [faces [i] [1]]); glVertex3fv (vertex [faces [i] [2]]); glVertex3fv (vertex [faces [i] [3]]); glEnd (); } glFlush (); }