opengl_x11

Playing with OpenGL
git clone git://bsandro.tech/opengl_x11
Log | Files | Refs | README | LICENSE

main.c (2992B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <X11/Xlib.h>
      4 #include <GL/glx.h>
      5 #include <GL/gl.h>
      6 
      7 Display *dpy;
      8 Window win;
      9 Bool doubleBuffer = True;
     10 
     11 GLfloat xAngle = 42.0;
     12 GLfloat yAngle = 82.0;
     13 GLfloat zAngle = 112.0;
     14 
     15 static int	sngBuf[] = {
     16                 GLX_RGBA,
     17                 GLX_RED_SIZE, 1,
     18                 GLX_GREEN_SIZE, 1,
     19                 GLX_BLUE_SIZE, 1,
     20                 GLX_DEPTH_SIZE, 12,
     21                 None };
     22 
     23 static int	dblBuf[] = {
     24                 GLX_RGBA,
     25                 GLX_RED_SIZE, 1,
     26                 GLX_GREEN_SIZE, 1,
     27                 GLX_BLUE_SIZE, 1,
     28                 GLX_DEPTH_SIZE, 12,
     29                 GLX_DOUBLEBUFFER,
     30                 None };
     31 
     32 static void fatalError(char *str) {
     33     fprintf(stderr, "%s\n", str);
     34     exit(1);
     35 }
     36 
     37 int main(int argc, char **argv) {
     38     XVisualInfo *vinfo = NULL;
     39     Colormap cmap;
     40     XSetWindowAttributes winattr;
     41     GLXContext ctx;
     42     XEvent event;
     43     int dummy;
     44 
     45     if (!(dpy = XOpenDisplay(NULL))) {
     46         fatalError("cannot open display");
     47     }
     48 
     49     if (!glXQueryExtension(dpy, &dummy, &dummy)) {
     50         fatalError("x server has no opengl glx extension");
     51     }
     52 
     53     if (!(vinfo = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf))) {
     54         if (!(vinfo = glXChooseVisual(dpy, DefaultScreen(dpy), sngBuf))) {
     55             fatalError("no rgb visual with depth buffer");
     56         }
     57         doubleBuffer = False;
     58     }
     59     if (vinfo->class != TrueColor) {
     60         fatalError("TrueColor required");
     61     }
     62 
     63     if (!(ctx = glXCreateContext(dpy, vinfo, None, True))) {
     64         fatalError("could not create rendering context");
     65     }
     66 
     67     cmap = XCreateColormap(dpy, RootWindow(dpy, vinfo->screen), vinfo->visual, AllocNone);
     68     winattr.colormap = cmap;
     69     winattr.border_pixel = 0;
     70     winattr.event_mask = ExposureMask|ButtonPressMask|StructureNotifyMask;
     71     win = XCreateWindow(dpy, RootWindow(dpy, vinfo->screen),0,0,300,300,0,
     72         vinfo->depth, InputOutput, vinfo->visual,
     73         CWBorderPixel|CWColormap|CWEventMask, &winattr);
     74     XSetStandardProperties(dpy, win, "glxsimple", "glxsimple", None, argv, argc, NULL);
     75 
     76     glXMakeCurrent(dpy, win, ctx);
     77     XMapWindow(dpy, win);
     78 
     79     glEnable(GL_DEPTH_TEST);
     80     glMatrixMode(GL_PROJECTION);
     81     glLoadIdentity();
     82     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
     83 
     84     int keepRunning = 1;
     85     while (keepRunning) {
     86         while (XPending(dpy)) {
     87             XNextEvent(dpy, &event);
     88             switch (event.type) {
     89             case ButtonPress:
     90                 switch (event.xbutton.button) {
     91                     case 1: xAngle += 10.0; break;
     92                     case 2: yAngle += 10.0; break;
     93                     case 3: zAngle += 10.0; break;
     94                 }
     95             break;
     96             case ConfigureNotify:
     97                 glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
     98             case Expose:
     99                 // needRedraw = True;
    100             break;
    101             }
    102         }
    103     }
    104 
    105     printf("ok\n");
    106     return 0;
    107 }