auxdemo.c
Go to the documentation of this file.
00001 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/oglport_0b71.asp */
00002 
00003 /* 
00004  * Example of an X Window System OpenGL program.   
00005  * OpenGL code is taken from auxdemo.c in the Platform SDK 
00006  */ 
00007 #include <GL/glx.h> 
00008 #include <GL/gl.h> 
00009 #include <GL/glu.h> 
00010 #include <X11/keysym.h> 
00011 #include <X11/Xlib.h> 
00012 #include <X11/Xutil.h> 
00013 #include <stdio.h> 
00014 
00015 /* X globals, defines, and prototypes */ 
00016 Display *dpy; 
00017 Window glwin; 
00018 //static int attributes[] = {GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None}; 
00019 //static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, 0};
00020 static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 1, 0};
00021  
00022 #define SWAPBUFFERS glXSwapBuffers(dpy, glwin) 
00023 #define BLACK_INDEX     0 
00024 #define RED_INDEX       1 
00025 #define GREEN_INDEX     2 
00026 #define BLUE_INDEX      4 
00027 #define WIDTH           300 
00028 #define HEIGHT          200 
00029  
00030  
00031 /* OpenGL globals, defines, and prototypes */ 
00032 GLfloat latitude, longitude, latinc, longinc; 
00033 GLdouble radius; 
00034  
00035 #define GLOBE    1 
00036 #define CYLINDER 2 
00037 #define CONE     3 
00038  
00039 GLvoid resize(GLsizei, GLsizei); 
00040 GLvoid initializeGL(GLsizei, GLsizei); 
00041 GLvoid drawScene(GLvoid); 
00042 void polarView( GLdouble, GLdouble, GLdouble, GLdouble); 
00043  
00044 static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg) 
00045 { 
00046     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) { 
00047     return GL_TRUE; 
00048     } 
00049     return GL_FALSE; 
00050 } 
00051  
00052 void 
00053 main(int argc, char **argv) 
00054 { 
00055     XVisualInfo    *vi; 
00056     Colormap        cmap; 
00057     XSetWindowAttributes swa; 
00058     GLXContext      cx; 
00059     XEvent          event; 
00060     GLboolean       needRedraw = GL_FALSE, recalcModelView = GL_TRUE; 
00061     int             dummy; 
00062 
00063     dpy = XOpenDisplay(NULL); 
00064     if (dpy == NULL){ 
00065         fprintf(stderr, "could not open display\n"); 
00066         exit(1); 
00067     } 
00068  
00069     if(!glXQueryExtension(dpy, &dummy, &dummy)){ 
00070         fprintf(stderr, "could not open display"); 
00071         exit(1); 
00072     } 
00073  
00074     /* find an OpenGL-capable Color Index visual with depth buffer */ 
00075     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes); 
00076     if (vi == NULL) { 
00077         fprintf(stderr, "could not get visual\n"); 
00078         exit(1); 
00079     } 
00080  
00081     /* create an OpenGL rendering context */ 
00082     cx = glXCreateContext(dpy, vi,  None, GL_TRUE);
00083     if (cx == NULL) { 
00084         fprintf(stderr, "could not create rendering context\n"); 
00085         exit(1); 
00086     } 
00087  
00088     /* create an X colormap since probably not using default visual */ 
00089 #ifndef OFFLINE_RENDERING
00090     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),  
00091                                 vi->visual, AllocNone); 
00092     swa.colormap = cmap; 
00093     swa.border_pixel = 0; 
00094     swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask; 
00095     glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH, 
00096                         HEIGHT, 0, vi->depth, InputOutput, vi->visual, 
00097                         CWBorderPixel | CWColormap | CWEventMask, &swa); 
00098     XSetStandardProperties(dpy, glwin, "xogl", "xogl", None, argv,  
00099                                 argc, NULL); 
00100 #else
00101     glwin = glXCreateGLXPixmap(dpy, vi, XCreatePixmap(dpy, RootWindow(dpy, vi->screen), WIDTH, HEIGHT, vi->depth));
00102 #endif
00103     glXMakeCurrent(dpy, glwin, cx); 
00104  
00105 #ifndef OFFLINE_RENDERING
00106     XMapWindow(dpy, glwin); 
00107     XIfEvent(dpy,  &event,  WaitForMapNotify,  (char *)glwin); 
00108 #endif
00109      
00110     initializeGL(WIDTH, HEIGHT); 
00111     resize(WIDTH, HEIGHT); 
00112     
00113     /* Animation loop */ 
00114     while (1) { 
00115     KeySym key; 
00116  
00117     while (XPending(dpy)) { 
00118         XNextEvent(dpy, &event); 
00119         switch (event.type) { 
00120         case KeyPress: 
00121                 XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL); 
00122         switch (key) { 
00123         case XK_Left: 
00124             longinc += 0.5; 
00125             break; 
00126         case XK_Right: 
00127             longinc -= 0.5; 
00128             break; 
00129         case XK_Up: 
00130             latinc += 0.5; 
00131             break; 
00132         case XK_Down: 
00133             latinc -= 0.5; 
00134             break; 
00135         } 
00136         break; 
00137         case ConfigureNotify: 
00138         resize(event.xconfigure.width, event.xconfigure.height); 
00139         break; 
00140         } 
00141     } 
00142     drawScene(); 
00143     } 
00144 } 
00145  
00146 /* OpenGL code */ 
00147  
00148 GLvoid resize( GLsizei width, GLsizei height ) 
00149 { 
00150     GLfloat aspect; 
00151  
00152     glViewport( 0, 0, width, height ); 
00153  
00154     aspect = (GLfloat) width / height; 
00155  
00156     glMatrixMode( GL_PROJECTION ); 
00157     glLoadIdentity(); 
00158     gluPerspective( 45.0, aspect, 3.0, 7.0 ); 
00159     glMatrixMode( GL_MODELVIEW ); 
00160 }     
00161  
00162 GLvoid createObjects() 
00163 { 
00164     GLUquadricObj *quadObj; 
00165  
00166     glNewList(GLOBE, GL_COMPILE); 
00167         quadObj = gluNewQuadric (); 
00168         gluQuadricDrawStyle (quadObj, GLU_LINE); 
00169         gluSphere (quadObj, 1.5, 16, 16); 
00170     glEndList(); 
00171  
00172     glNewList(CONE, GL_COMPILE); 
00173         quadObj = gluNewQuadric (); 
00174         gluQuadricDrawStyle (quadObj, GLU_FILL); 
00175         gluQuadricNormals (quadObj, GLU_SMOOTH); 
00176         gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10); 
00177     glEndList(); 
00178  
00179     glNewList(CYLINDER, GL_COMPILE); 
00180         glPushMatrix (); 
00181         glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0); 
00182         glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0); 
00183         quadObj = gluNewQuadric (); 
00184         gluQuadricDrawStyle (quadObj, GLU_FILL); 
00185         gluQuadricNormals (quadObj, GLU_SMOOTH); 
00186         gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2); 
00187         glPopMatrix (); 
00188     glEndList(); 
00189 } 
00190  
00191 GLvoid initializeGL(GLsizei width, GLsizei height) 
00192 { 
00193     GLfloat    maxObjectSize, aspect; 
00194     GLdouble    near_plane, far_plane; 
00195  
00196     glClearIndex( (GLfloat)BLACK_INDEX); 
00197     glClearDepth( 1.0 ); 
00198  
00199     glEnable(GL_DEPTH_TEST); 
00200  
00201     glMatrixMode( GL_PROJECTION ); 
00202     aspect = (GLfloat) width / height; 
00203     gluPerspective( 45.0, aspect, 3.0, 7.0 ); 
00204     glMatrixMode( GL_MODELVIEW ); 
00205  
00206     near_plane = 3.0; 
00207     far_plane = 7.0; 
00208     maxObjectSize = 3.0F; 
00209     radius = near_plane + maxObjectSize/2.0; 
00210  
00211     latitude = 0.0F; 
00212     longitude = 0.0F; 
00213     latinc = 6.0F; 
00214     longinc = 2.5F; 
00215  
00216     createObjects(); 
00217 } 
00218  
00219 void polarView(GLdouble radius, GLdouble twist, GLdouble latitude, 
00220            GLdouble longitude) 
00221 { 
00222     glTranslated(0.0, 0.0, -radius); 
00223     glRotated(-twist, 0.0, 0.0, 1.0); 
00224     glRotated(-latitude, 1.0, 0.0, 0.0); 
00225     glRotated(longitude, 0.0, 0.0, 1.0);      
00226  
00227 } 
00228  
00229 GLvoid drawScene(GLvoid) 
00230 { 
00231     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 
00232  
00233     glPushMatrix(); 
00234  
00235         latitude += latinc; 
00236         longitude += longinc; 
00237  
00238         // polarView( radius, 0, latitude, longitude ); 
00239         polarView( radius, 0, latitude/50.0f, longitude/50.0f ); 
00240  
00241         glIndexi(RED_INDEX); 
00242         glCallList(CONE); 
00243  
00244         glIndexi(BLUE_INDEX); 
00245         glCallList(GLOBE); 
00246  
00247         glIndexi(GREEN_INDEX); 
00248         glPushMatrix(); 
00249             glTranslatef(0.8F, -0.65F, 0.0F); 
00250             glRotatef(30.0F, 1.0F, 0.5F, 1.0F); 
00251             glCallList(CYLINDER); 
00252         glPopMatrix(); 
00253  
00254     glPopMatrix(); 
00255  
00256 #ifdef OFFLINE_RENDERING
00257     {
00258       static int i = 0;
00259       char imgbuf[WIDTH*HEIGHT*3];
00260       glReadBuffer(GL_FRONT);
00261       glPixelStorei(GL_PACK_ALIGNMENT, 1);
00262       glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, imgbuf);
00263 
00264       char filename[256];
00265       sprintf(filename, "test%08d.ppm", i++);
00266       fprintf(stderr, "writing to %s (%7.3f %7.3f)\n", filename, latitude, longitude);
00267 
00268       FILE *fp = fopen(filename, "w+");
00269       fprintf(fp, "P6\n");
00270       fprintf(fp, "#\n");
00271       fprintf(fp, "%d %d 255\n", WIDTH, HEIGHT);
00272       fwrite(imgbuf, 1, WIDTH*HEIGHT*3, fp);
00273       fclose(fp);
00274     }
00275 #endif
00276     SWAPBUFFERS; 
00277 } 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:19