gltest.cxx File Reference

#include <iostream>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <sys/time.h>

Go to the source code of this file.

Functions

void draw_scene ()
int main ()

Variables

Displaydis
Window win
float X_Rot = 0.9f
float Y_Rot = 0.0f
float X_Speed = 0.0f
float Y_Speed = 0.5f
float Z_Off = -5.0f
float Light_Ambient [] = { 0.1f, 0.1f, 0.1f, 1.0f }
float Light_Diffuse [] = { 1.2f, 1.2f, 1.2f, 1.0f }
float Light_Position [] = { 2.0f, 2.0f, 0.0f, 1.0f }

Function Documentation

void draw_scene (  ) 

Definition at line 48 of file gltest.cxx.

References X_Rot, Y_Rot, and Z_Off.

Referenced by main().

00049 {
00050   glEnable(GL_LIGHTING);
00051 
00052   glEnable(GL_DEPTH_TEST);
00053 
00054   // Need to manipulate the ModelView matrix to move our model around.
00055    glMatrixMode(GL_MODELVIEW);
00056 
00057    // Reset to 0,0,0; no rotation, no scaling.
00058    glLoadIdentity();
00059 
00060    // Move the object back from the screen.
00061    glTranslatef(0.0f,0.0f,Z_Off);
00062 
00063    // Rotate the calculated amount.
00064    glRotatef(X_Rot,1.0f,0.0f,0.0f);
00065    glRotatef(Y_Rot,0.0f,1.0f,0.0f);
00066 
00067 
00068    // Clear the color and depth buffers.
00069    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00070 
00071 
00072    // OK, let's start drawing our planer quads.
00073    glBegin(GL_QUADS);
00074 
00075    // Bottom Face.  Red, 75% opaque, magnified texture
00076 
00077    glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
00078    glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
00079 
00080    glTexCoord2f(0.800f, 0.800f); glVertex3f(-1.0f, -1.0f, -1.0f);
00081    glTexCoord2f(0.200f, 0.800f); glVertex3f( 1.0f, -1.0f, -1.0f);
00082    glTexCoord2f(0.200f, 0.200f); glVertex3f( 1.0f, -1.0f,  1.0f);
00083    glTexCoord2f(0.800f, 0.200f); glVertex3f(-1.0f, -1.0f,  1.0f);
00084   // Top face; offset.  White, 50% opaque.
00085 
00086    glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
00087 
00088    glTexCoord2f(0.005f, 1.995f); glVertex3f(-1.0f,  1.3f, -1.0f);
00089    glTexCoord2f(0.005f, 0.005f); glVertex3f(-1.0f,  1.3f,  1.0f);
00090    glTexCoord2f(1.995f, 0.005f); glVertex3f( 1.0f,  1.3f,  1.0f);
00091    glTexCoord2f(1.995f, 1.995f); glVertex3f( 1.0f,  1.3f, -1.0f);
00092 
00093 
00094    // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
00095 
00096    glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
00097 
00098    glTexCoord2f(0.995f, 0.005f); glVertex3f(-1.0f, -1.0f, -1.3f);
00099    glTexCoord2f(2.995f, 2.995f); glVertex3f(-1.0f,  1.0f, -1.3f);
00100    glTexCoord2f(0.005f, 0.995f); glVertex3f( 1.0f,  1.0f, -1.3f);
00101    glTexCoord2f(0.005f, 0.005f); glVertex3f( 1.0f, -1.0f, -1.3f);
00102 
00103 
00104    // Right face.  Blue; 25% opaque
00105 
00106    glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
00107 
00108    glTexCoord2f(0.995f, 0.005f); glVertex3f( 1.0f, -1.0f, -1.0f);
00109    glTexCoord2f(0.995f, 0.995f); glVertex3f( 1.0f,  1.0f, -1.0f);
00110    glTexCoord2f(0.005f, 0.995f); glVertex3f( 1.0f,  1.0f,  1.0f);
00111    glTexCoord2f(0.005f, 0.005f); glVertex3f( 1.0f, -1.0f,  1.0f);
00112 
00113   // Front face; offset.  Multi-colored, 50% opaque.
00114 
00115    glNormal3f( 0.0f, 0.0f, 1.0f);
00116 
00117    glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
00118    glTexCoord2f( 0.005f, 0.005f); glVertex3f(-1.0f, -1.0f,  1.3f);
00119    glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
00120    glTexCoord2f( 0.995f, 0.005f); glVertex3f( 1.0f, -1.0f,  1.3f);
00121    glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
00122    glTexCoord2f( 0.995f, 0.995f); glVertex3f( 1.0f,  1.0f,  1.3f);
00123    glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
00124    glTexCoord2f( 0.005f, 0.995f); glVertex3f(-1.0f,  1.0f,  1.3f);
00125 
00126 
00127    // Left Face; offset.  Yellow, varying levels of opaque.
00128 
00129    glNormal3f(-1.0f, 0.0f, 0.0f);
00130 
00131    glColor4f(0.9,0.9,0.2,0.0);
00132    glTexCoord2f(0.005f, 0.005f); glVertex3f(-1.3f, -1.0f, -1.0f);
00133    glColor4f(0.9,0.9,0.2,0.66);
00134    glTexCoord2f(0.995f, 0.005f); glVertex3f(-1.3f, -1.0f,  1.0f);
00135    glColor4f(0.9,0.9,0.2,1.0);
00136    glTexCoord2f(0.995f, 0.995f); glVertex3f(-1.3f,  1.0f,  1.0f);
00137    glColor4f(0.9,0.9,0.2,0.33);
00138    glTexCoord2f(0.005f, 0.995f); glVertex3f(-1.3f,  1.0f, -1.0f);
00139 
00140 
00141    // All polygons have been drawn.
00142    glEnd();
00143 }

int main ( void   ) 

Definition at line 150 of file gltest.cxx.

References draw_scene(), MuELoss::e, exit(), Light_Ambient, Light_Diffuse, Light_Position, N_STARS, N_TIMES, set_body(), topo_star(), win, and Y_Rot.

00150            {
00151   dis = XOpenDisplay(NULL);
00152   int width = 500;
00153   int height = 500;
00154   win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, width, height,       \
00155                             0, BlackPixel (dis, 0), BlackPixel(dis, 0));
00156   XMapWindow(dis, win);
00157 
00158   XFlush(dis);  
00159 
00160   static int dblBuf[] = {
00161     GLX_DOUBLEBUFFER,
00162     GLX_RGBA, GLX_DEPTH_SIZE, 16,
00163     GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
00164     None
00165   };
00166   static int *snglBuf = &dblBuf[1];
00167   
00168   XVisualInfo* visInfo = glXChooseVisual(dis, DefaultScreen(dis), dblBuf);
00169   if(visInfo ==0)
00170     visInfo = glXChooseVisual(dis, DefaultScreen(dis), snglBuf);
00171   if(visInfo ==0) 
00172     cout << "Can't find good visual!" << endl;
00173 
00174   GLXContext context = glXCreateContext(dis, visInfo, None, GL_TRUE);  
00175 
00176   glXMakeCurrent(dis, win, context); 
00177 
00178   glViewport(0,0,500,500);
00179   glMatrixMode(GL_PROJECTION);
00180   glLoadIdentity();
00181   gluPerspective(45.0,1.0,0.1,100.0);
00182   glMatrixMode(GL_MODELVIEW);
00183 
00184   // Color to clear color buffer to.
00185   glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
00186 
00187   // Depth to clear depth buffer to; type of test.
00188   glClearDepth(1.0);
00189   glDepthFunc(GL_LESS);
00190   glShadeModel(GL_SMOOTH);
00191   
00192   // Set up a light, turn it on.
00193    glLightfv(GL_LIGHT1, GL_POSITION, Light_Position);
00194    glLightfv(GL_LIGHT1, GL_AMBIENT,  Light_Ambient);
00195    glLightfv(GL_LIGHT1, GL_DIFFUSE,  Light_Diffuse);
00196    glEnable (GL_LIGHT1);
00197 
00198    // A handy trick -- have surface material mirror the color.
00199    glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
00200    glEnable(GL_COLOR_MATERIAL);
00201 
00202    struct timeval t1;
00203    struct timeval t2;
00204    gettimeofday(&t1,NULL);
00205    int reps = 0;
00206 
00207    while(1) {
00208      draw_scene();
00209      glXSwapBuffers(dis, win);
00210      if (!glXIsDirect(dis, context)) {
00211       glFinish();
00212      }
00213      
00214      Y_Rot+=1;
00215      //usleep(100);
00216 
00217      // On my computer, this makes about 1 rev every 10 seconds.
00218      gettimeofday(&t2,NULL);
00219      double dt = (t2.tv_sec - t1.tv_sec)*1.0
00220        + (t2.tv_usec - t1.tv_usec)*1e-6;
00221      if(reps==100) {
00222        printf("FPS: %f\n",(double)reps/dt);
00223        t1.tv_sec = t2.tv_sec;
00224        t1.tv_usec = t2.tv_usec;
00225        reps=0;
00226 
00227 
00228 #ifdef USE_ASIMAGE       
00229        {
00230          cout << "Writing... " << endl;
00231          
00232          XImage* ximage = XGetImage(dis,win,
00233                                     0,0,width,height,
00234                                     XAllPlanes(), ZPixmap);
00235          if(ximage==NULL) {
00236            cout << "Couldn't make the XImage! " << endl;
00237            exit(1);
00238          }
00239          
00240          int screen  =  DefaultScreen(dis);
00241          int depth   =  DefaultDepth(dis,screen);
00242          Visual *vis   = DefaultVisual(dis,screen);
00243          Colormap cmap = DefaultColormap(dis,screen);
00244          
00245          ASVisual* asv =  create_asvisual_for_id(dis,
00246                                                  screen, depth,
00247                                                  XVisualIDFromVisual(vis), 
00248                                                  cmap, 0);
00249          if(asv==0) {
00250            cout << "Couldn't make the ASVisual! " << endl;
00251            exit(1);
00252          }
00253          
00254          ASImage* asimage = ximage2asimage(asv, ximage, 10); // 10 is compression from 0 to 100
00255          if(asimage==0) {
00256            cout << "Couldn't make the ASImage! " << endl;
00257            exit(1);
00258          }                      
00259          
00260          const char* dir = ".";
00261          const char* file= "file.gif";
00262          ASImageFileTypes atype = ASIT_Gif;
00263          
00264          ASImageExportParams parms;
00265          switch (atype) {
00266          case ASIT_Xpm:
00267            parms.xpm.type = atype;
00268            parms.xpm.flags = EXPORT_ALPHA;
00269            parms.xpm.dither = 4;
00270            parms.xpm.opaque_threshold = 127;
00271            parms.xpm.max_colors = 512;
00272            break;
00273          case ASIT_Png:
00274            parms.png.type = atype;
00275            parms.png.flags = EXPORT_ALPHA;
00276            parms.png.compression = 50;
00277            break;
00278          case ASIT_Jpeg:
00279            parms.jpeg.type = atype;
00280            parms.jpeg.flags = 0;
00281            parms.jpeg.quality = ASIMAGE_QUALITY_GOOD; // also poor, fast, top
00282            break;
00283          case ASIT_Gif:
00284            parms.gif.type = atype;
00285            parms.gif.flags = EXPORT_ALPHA;
00286            parms.gif.dither = 0; 
00287            parms.gif.opaque_threshold = 0;
00288            break;
00289          case ASIT_Tiff:
00290            parms.tiff.type = atype;
00291            parms.tiff.flags = EXPORT_ALPHA;
00292            parms.tiff.rows_per_strip = 0;
00293            parms.tiff.compression_type = TIFF_COMPRESSION_JPEG;
00294            parms.tiff.jpeg_quality = 100;
00295            parms.tiff.opaque_threshold = 0;
00296            break;
00297          default:
00298            exit(0);
00299          }
00300 
00301          if(
00302             ASImage2file( asimage, dir, file,
00303                           atype, &parms )
00304             ) 
00305          cout << "Write successful!" << endl;
00306          else 
00307          cout << "Write failed!" << endl;
00308        }
00309        
00310 
00311 #endif
00312 
00313 
00314 
00315 
00316      }
00317      reps++;
00318    }
00319    
00320    
00321    
00322    return(0);
00323 }


Variable Documentation

Definition at line 35 of file gltest.cxx.

Referenced by neugen_inputs::compute_process_mask().

float Light_Ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f }

Definition at line 44 of file gltest.cxx.

Referenced by main().

float Light_Diffuse[] = { 1.2f, 1.2f, 1.2f, 1.0f }

Definition at line 45 of file gltest.cxx.

Referenced by main().

float Light_Position[] = { 2.0f, 2.0f, 0.0f, 1.0f }

Definition at line 46 of file gltest.cxx.

Referenced by main().

Window win
float X_Rot = 0.9f

Definition at line 38 of file gltest.cxx.

Referenced by draw_scene().

float X_Speed = 0.0f

Definition at line 40 of file gltest.cxx.

float Y_Rot = 0.0f

Definition at line 39 of file gltest.cxx.

Referenced by draw_scene(), and main().

float Y_Speed = 0.5f

Definition at line 41 of file gltest.cxx.

float Z_Off = -5.0f

Definition at line 42 of file gltest.cxx.

Referenced by draw_scene().


Generated on 24 Jul 2018 for loon by  doxygen 1.6.1