home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Development Libraries / SGI IRIX 6.2 Development Libraries.iso / dist / gl_dev.idb / usr / share / src / OpenGL / teach / texture / texobj.c.z / texobj.c
Encoding:
C/C++ Source or Header  |  1996-03-15  |  4.1 KB  |  160 lines

  1. /*
  2.  * texobj - texture object program
  3.  *
  4.  * Simple example for how to create, define, and bind texture objects.
  5.  */
  6. /* compile: cc -o texobj texobj.c -lGL -lX11 */
  7.  
  8. #include <GL/glx.h>
  9. #include <GL/glu.h>
  10. #include <X11/keysym.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. static int attributeList[] = { GLX_RGBA, None };
  15. unsigned int redtex[64*64], greentex[64*64], bluetex[64*64];
  16.  
  17. GLuint texnames[3];
  18. GLclampf priorities[3] = { 0.0, 1.0, 1.0 };
  19.  
  20. static void
  21. init(void) {
  22.     int i;
  23.     
  24.     glMatrixMode(GL_PROJECTION);
  25.     gluPerspective(60.0, 1.0, 1.0, 100.0 );
  26.     glMatrixMode(GL_MODELVIEW);
  27.     glTranslatef(0.,0.,-8.0);
  28.  
  29.     glClearColor(0.5, 0.5, 0.5, 1.0);
  30.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  31.  
  32.     for (i=0; i < 64*64; i++) {
  33.     redtex[i] = 0xff000000;
  34.     greentex[i] = 0x00ff0000;
  35.     bluetex[i] = 0x0000ff00;
  36.     }
  37.  
  38.     glEnable(GL_TEXTURE_2D);
  39.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  40.  
  41.     /* get some unused texture names */
  42.     glGenTexturesEXT(3, texnames);
  43.  
  44.     /* bind, then define, each texture */
  45.     glBindTextureEXT(GL_TEXTURE_2D, texnames[0]);
  46.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  47.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  48.          redtex);
  49.     glBindTextureEXT(GL_TEXTURE_2D, texnames[1]);
  50.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  51.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  52.          greentex);
  53.     glBindTextureEXT(GL_TEXTURE_2D, texnames[2]);
  54.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  55.     glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  56.          bluetex);
  57.  
  58.     /* optional; define a working set by giving some textures a higher */
  59.     /* priority than others */
  60.     glPrioritizeTexturesEXT(3, texnames, priorities);
  61. }
  62.  
  63. static void
  64. draw_square(void)
  65. {
  66.     glBegin(GL_TRIANGLE_STRIP);
  67.         glTexCoord2f(0,0); glVertex2f(-1,-1); 
  68.         glTexCoord2f(0,1); glVertex2f(-1, 1); 
  69.         glTexCoord2f(1,0); glVertex2f( 1,-1); 
  70.         glTexCoord2f(1,1); glVertex2f( 1, 1); 
  71.     glEnd();
  72. }
  73.  
  74. static void
  75. draw_scene(void) {
  76.     glClear(GL_COLOR_BUFFER_BIT);
  77.     glPushMatrix();
  78.  
  79.     glTranslatef(-3, 0, 0);
  80.     glBindTextureEXT(GL_TEXTURE_2D, texnames[0]);
  81.     draw_square();
  82.     glTranslatef( 3, 0, 0);
  83.     glBindTextureEXT(GL_TEXTURE_2D, texnames[1]);
  84.     draw_square();
  85.     glTranslatef( 3, 0, 0);
  86.     glBindTextureEXT(GL_TEXTURE_2D, texnames[2]);
  87.     draw_square();
  88.  
  89.     glPopMatrix();
  90.     glFlush();
  91. }
  92.  
  93. static void
  94. process_input(Display *dpy) {
  95.     XEvent event;
  96.     Bool redraw = 0;
  97.  
  98.     do {
  99.     char buf[31];
  100.     KeySym keysym;
  101.  
  102.     XNextEvent(dpy, &event);
  103.     switch(event.type) {
  104.     case Expose:
  105.         redraw = 1;
  106.         break;
  107.     case ConfigureNotify:
  108.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  109.         redraw = 1;
  110.         break;
  111.     case KeyPress:
  112.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  113.         switch (keysym) {
  114.         case XK_Escape:
  115.         exit(EXIT_SUCCESS);
  116.         default:
  117.         break;
  118.         }
  119.     default:
  120.         break;
  121.     }
  122.     } while (XPending(dpy));
  123.     if (redraw) draw_scene();
  124. }
  125.  
  126. static void
  127. error(const char *prog, const char *msg) {
  128.     fprintf(stderr, "%s: %s\n", prog, msg);
  129.     exit(EXIT_FAILURE);
  130. }
  131.  
  132. int
  133. main(int argc, char **argv) {
  134.     Display *dpy;
  135.     XVisualInfo *vi;
  136.     XSetWindowAttributes swa;
  137.     Window win;
  138.     GLXContext cx;
  139.  
  140.     dpy = XOpenDisplay(0);
  141.     if (!dpy) error(argv[0], "can't open display");
  142.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  143.     if (!vi) error(argv[0], "no suitable visual");
  144.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  145.  
  146.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  147.                                    vi->visual, AllocNone);
  148.     swa.border_pixel = 0;
  149.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  150.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 400, 400,
  151.             0, vi->depth, InputOutput, vi->visual,
  152.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  153.     XStoreName(dpy, win, "texobj");
  154.     XMapWindow(dpy, win);
  155.     glXMakeCurrent(dpy, win, cx);
  156.  
  157.     init();
  158.     while (1) process_input(dpy);
  159. }
  160.