To compile on Linux use this.
gcc GLwindow.c -o GLwindow -lGL -lglut
This is just some code I decided to do as a lark and it is usable code for a Linux X windows system. I am certain it will not function in another operating system, as it uses X keys. It implements an IFS Sierpinski using randoms. If you want to see how to do basic OpenGL in Linux, this is a good template. It includes key handling and window initialization and the initial GL context so that the rest is just filling in what you need to display. I was planning to do an implementation of the einstein game using a set of six rotating dice that contain the possible symbols for a particular location and uses a technique I developed for AI. When I get the code done , I will post it. The AI method is unique , I think, but I am not Google. I started to change the code for einstein to make it re-sizable, but it was just easier to just redo the whole thing in OpenGL using scaled images and relative positions. I also wanted to test a solving scheme that used an artificial imagination with randomness.
/** @author Paul Mohr @name GLwindow.c @brief Simplest window implementation using openGL I threw an IFS (Iterated Function System ) Serpinski in here for fun so something was on the screen. I don't think this is really elegant math, but it looks interesting. */ #include <stdlib.h> #include <stdio.h> #include <X11/Xlib.h> #include <X11/keysym.h> #include <GL/gl.h> #include <GL/glut.h> #include <GL/glx.h> /* This is just to make a truly pseudo ramdom. */ #include <sys/time.h> /*Functions */ void seedRandom(void); void DrawGLScene(GLvoid); void draw(void); static void init(void); static void reshape(int width, int height, Display *dpy,Window win, GLXContext ctx); static void make_window( Display *dpy, const char *name, int x, int y, int width, int height, Window *winRet, GLXContext *ctxRet); static void event_loop(Display *dpy,Window win, GLXContext ctx); /* Now the code */ int main(int argc, char *argv[]){ /* unVariables */ static int windowWidth=640; static int windowHeight=480; /* Variables within the scope of main */ int displays; char *dpyName = ":0.0"; Display *dpy; GLXContext ctx; Window win; char AppVersion[] ="Simple OpenGL window only. "; seedRandom(); dpy = XOpenDisplay(dpyName); if (!dpy) {printf("Error: couldn't open display %s\n", dpyName);return -1;} make_window(dpy, AppVersion, 0, 0, windowWidth, windowHeight, &win, &ctx); XMapWindow(dpy, win); glXMakeCurrent(dpy, win, ctx); init(); event_loop(dpy, win, ctx); glXMakeCurrent(dpy, win, ctx); glXDestroyContext(dpy, ctx); XDestroyWindow(dpy, win); XCloseDisplay(dpy); return 0; } void seedRandom(void){ struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_sec * tv.tv_usec);} void DrawGLScene(GLvoid) { int i;float x=-3, y=2;float tx, ty; glLoadIdentity(); glTranslatef(0,0,-6); glBegin(GL_POINTS); for (i=0; i<1000; i++) { /* Pick 1 out of 3 end points at random */ switch(rand()%3) { case 0: tx = -3; ty = 2; break; case 1: tx = 3; ty = 2; break; case 2: tx = 0; ty = -2; break; } x = (x + tx) / 2; y = (y + ty) / 2; glVertex2f(x,y); } glEnd(); glFlush(); } void draw(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawGLScene(); } static void init(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } static void reshape(int width, int height, Display *dpy,Window win, GLXContext ctx){ glXMakeCurrent(dpy, win, ctx); GLdouble NearScale = 90.0; GLdouble FarScale = 450.0; GLdouble FrontScreen = 0.001; GLdouble BackScreen = 1.0; glXMakeCurrent(dpy, win, ctx); glViewport(0, 0, (GLint) width, (GLint) height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(NearScale,BackScreen,FrontScreen,FarScale); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Create an RGB, double-buffered window. * Return the window and context handles. */ static void make_window( Display *dpy, const char *name, int x, int y, int width, int height,Window *winRet, GLXContext *ctxRet) { int attrib[] = { GLX_RGBA,GLX_RED_SIZE,1, GLX_GREEN_SIZE,1,GLX_BLUE_SIZE,1,GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,1,None }; int scrnum; XSetWindowAttributes attr; unsigned long mask; Window root; Window win; GLXContext ctx; XVisualInfo *visinfo; scrnum = DefaultScreen( dpy ); root = RootWindow( dpy, scrnum ); visinfo = glXChooseVisual( dpy, scrnum, attrib ); if (!visinfo) {printf("Error: couldn't get an RGB, Double-buffered visual\n");exit(1);} /* window attributes */ attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone); attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; win = XCreateWindow( dpy, root, x, y, // dpy, root, 0, 0, width, height,0, visinfo->depth, InputOutput,visinfo->visual, mask, &attr ); /* set hints and properties */ { XSizeHints sizehints; sizehints.x = x; sizehints.y = y; sizehints.width = width; sizehints.height = height; sizehints.flags = USSize | USPosition; XSetNormalHints(dpy, win, &sizehints); XSetStandardProperties(dpy, win, name, name,None, (char **)NULL, 0, &sizehints); } ctx = glXCreateContext( dpy, visinfo, NULL, True ); if (!ctx) {printf("Error: glXCreateContext failed\n");exit(1);} XFree(visinfo); *winRet = win; *ctxRet = ctx; } static void event_loop(Display *dpy,Window win, GLXContext ctx) { char buffer[10]; int r, code; while (1) { glXMakeCurrent(dpy, win, ctx); while (XPending(dpy) > 0) { XEvent event;XNextEvent(dpy, &event);switch (event.type) { case Expose: break; case ConfigureNotify: reshape(event.xconfigure.width, event.xconfigure.height,dpy, win, ctx);break; case KeyPress: { code = XLookupKeysym(&event.xkey, 0); if (code == XK_Left ) {} if (code == XK_Right) {} if (code == XK_Up ) {} else { r=XLookupString(&event.xkey, buffer, sizeof(buffer),NULL, NULL); if (buffer[0] == 27) {return;}} } } } draw();glXSwapBuffers(dpy, win); } }
Quantum certainty is the title and it was just something I was considering and how it relates to the way the universe operates. The future is certainly going to bring some strange things and it seems that much I see as science fiction is really out of date. Even the oddest of it is tame compared to what is likely to happen in the not so distant infinite future.
ADDED: I changed the way create_window works as I goofed up the positioning :) substituted "dpy, root, x, y," for "dpy, root, 0, 0,"
0 comments:
Post a Comment