/* Xlib.h is the default header that is included and has the core functionallity */ #include /* Xatom.h includes functionallity for creating new protocol messages */ #include /* keysym.h contains keysymbols which we use to resolv what keys that are being pressed */ #include /* printf */ #include #include #include /* the XF86 Video Mode extension allows us to change the displaymode of the server * this allows us to set the display to fullscreen and also read videomodes and * other information. */ #include /* gl.h we need OpenGL :-) */ #include /* this file is needed for X11 applications if we want to use hardware rendering */ #include #include #define WIDTH 640 #define HEIGHT 480 #define TITLE "OpenGL in X11" #define PI (3.1415926535897932384626) #define DEGREE_TO_RADIAN(d,r) (r=(d*PI)/180) #define DEGREETORADIAN(d) ((d*PI)/180) /* most important variable * it contains information about the X server which we communicate with */ Display * m_pDisplay; int m_screen; /* our window instance */ Window m_glwin; GLXContext m_glContext; XSetWindowAttributes winAttr; //Bool fullscreen = True; //Bool doubleBuffered; /* original desktop mode which we save so we can restore it later */ //XF86VidModeModeInfo desktopMode; int x, y; unsigned int width, height; unsigned int depth; //GLfloat rotQuad = 0.0f; GLuint TextureId = 0; unsigned char u8GLBuffer[640*480*4]; unsigned char u8TexBuffer[64*64*4]; /* attributes for a double buffered visual in RGBA format with at least * 4 bits per color and a 16 bit depth buffer */ static int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 4, GLX_GREEN_SIZE,4, GLX_BLUE_SIZE, 4, GLX_DEPTH_SIZE, 16, None }; /* prototypes */ void createWindow(); void destroyWindow(); void initX(); /* * create a window */ void createWindow() { XVisualInfo *vi; Colormap cmap; int i, dpyWidth, dpyHeight; /* get a connection */ m_pDisplay = XOpenDisplay(0); m_screen = DefaultScreen(m_pDisplay); /* get an appropriate visual */ vi = glXChooseVisual(m_pDisplay, m_screen, attrListDbl); /* create a GLX context */ m_glContext = glXCreateContext(m_pDisplay, vi, 0, GL_TRUE); /* create a color map */ cmap = XCreateColormap(m_pDisplay, RootWindow(m_pDisplay, vi->screen), vi->visual, AllocNone); winAttr.colormap = cmap; winAttr.border_pixel = 0; dpyWidth = 640; dpyHeight = 480; /* set window attributes */ winAttr.override_redirect = True; winAttr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask; m_glwin = XCreateWindow(m_pDisplay, RootWindow(m_pDisplay, vi->screen), 0, 0, dpyWidth, dpyHeight, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &winAttr); XMapRaised(m_pDisplay, m_glwin); glXMakeCurrent(m_pDisplay, m_glwin, m_glContext); glShadeModel(GL_SMOOTH); glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glEnable(GL_LINE_SMOOTH); glEnable(GL_POINT_SMOOTH); glEnable(GL_POLYGON_SMOOTH); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (0, width, height, 0, 0, 1); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glFlush(); } /* * destroy the window */ void destroyWindow() { if( m_glContext ) { if( !glXMakeCurrent(m_pDisplay, None, NULL)) { printf("Could not release drawing context.\n"); } /* destroy the context */ glXDestroyContext(m_pDisplay, m_glContext); m_glContext = NULL; } /* switch back to original desktop resolution if we were in fullscreen */ XCloseDisplay(m_pDisplay); } void renderGL() { float angle, cX= 200.0, cY=200.0, dStartRad, dEndRad, dStart, dEnd; int radius = 40; int outerRadius = 100; float x1,x2,y1,y2; int y; glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_3D); glDisable(GL_DEPTH_TEST); glDisable(GL_COLOR_TABLE); glDisable(GL_DITHER); glDisable(GL_LIGHT0); //======================== stippled line glColor4f(1.0f, 0.0f, 1.0f, 1.0f); // purple //glLineStipple(1,0x00ff); glLineStipple(2,0xAAAA); glEnable(GL_LINE_STIPPLE); glLineWidth(1.0); glShadeModel(GL_FLAT); glBegin(GL_LINES); glVertex2i(0,0); glVertex2i(639,479); glVertex2i(0,479); glVertex2i(639,0); glEnd(); glDisable(GL_LINE_STIPPLE); glXSwapBuffers(m_pDisplay, m_glwin); } int main(int argc, char ** argv) { XEvent event; Bool done = False; width = WIDTH; height = HEIGHT; createWindow(); /* wait for events and eat up cpu. ;-) */ while (!done) { /* handle the events in the queue */ while (XPending(m_pDisplay) > 0) { XNextEvent(m_pDisplay, &event); switch (event.type) { case Expose: if (event.xexpose.count != 0) break; renderGL(); break; /* exit in case of a mouse button press */ case ButtonPress: done = True; break; case KeyPress: if (XLookupKeysym(&event.xkey, 0) == XK_Escape) { done = True; } if (XLookupKeysym(&event.xkey,0) == XK_F1) { destroyWindow(); //fullscreen = !fullscreen; createWindow(); } break; case ClientMessage: if (*XGetAtomName(m_pDisplay, event.xclient.message_type) == *"WM_PROTOCOLS") { done = True; } break; default: break; } } renderGL(); } destroyWindow(); return 0; }