/* gcc -o line_aa line_aa_freeze.c -Wall `pkg-config --cflags --libs x11 gl glu` */ #include #include #include #include #include Display *display; Window window; GLXContext context; int width, height; static int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_ALPHA_SIZE, 8, GLX_DEPTH_SIZE, 16, None }; void initGL(int, int); void createWindow(int width, int height) { display = XOpenDisplay(0); int screen = DefaultScreen(display); Window root = RootWindow(display, screen); XVisualInfo *vi = glXChooseVisual(display, screen, attrListDbl); XSetWindowAttributes winAttr; memset(&winAttr, 0, sizeof(XSetWindowAttributes)); winAttr.colormap = XCreateColormap (display, root, vi->visual, AllocNone); winAttr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask; window = XCreateWindow(display, root, 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask, &winAttr); XMapRaised(display, window); context = glXCreateContext(display, vi, 0, GL_TRUE); glXMakeCurrent(display, window, context); initGL(width, height); } void destroyWindow() { if (context) { glXMakeCurrent(display, None, NULL); glXDestroyContext(display, context); context = NULL; } XCloseDisplay(display); } void resizeGL(int _width, int _height) { width = _width; height = _height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); glViewport(0, 0, width, height); } void initGL(int width, int height) { glShadeModel(GL_SMOOTH); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glLineWidth(1.5); glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthMask(GL_FALSE); glPolygonMode(GL_FRONT, GL_LINE); resizeGL(width, height); } void renderGL(int width, int height) { glViewport(0, 0, width, height); glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -3.0); glRotatef(15.0, 0, 0, 1); glBegin(GL_TRIANGLES); glColor4f(1.0f,0.0f,0.0f,1.0); glVertex3f(-0.1f, -1.0f, 0.0f); glColor4f(0.0f,1.0f,0.0f,1.0); glVertex3f( 1.0f, -1.0f, 0.0f); glColor4f(0.0f,0.0f,1.0f,1.0); glVertex3f( 0.0f, 1.0f, 0.0f); glEnd(); glXSwapBuffers(display, window); } int main(int argc, char ** argv) { XEvent event; int done = 0; createWindow(640, 480); while (!done) { while (XPending(display) > 0) { XNextEvent(display, &event); switch (event.type) { case ConfigureNotify: resizeGL(event.xconfigure.width, event.xconfigure.height); break; case KeyPress: if (XLookupKeysym(&event.xkey, 0) == XK_Escape) done = 1; break; default: break; } } renderGL(width, height); } destroyWindow(); return 0; }