Ran Yakir (rany++at++rtset.co.il)
Sun, 2 Feb 1997 00:54:01 -0800
Here it is :
/*************************************************************************
*
* texlines.c
* compile with :
* cc texlines.c -o texlines -lGLU -lGL -lX11 -lm
*
* When run, the program displays a rectangle.
* pressing 'l' turns linesmooth ON
* pressing 'L' turns linesmooth OFF
* pressing 'b' turns blending ON
* pressing 'B' turns blending OFF
* pressing 't' turns texture ON
* pressing 'T' turns texture OFF
* pressing 'f' turns flat shading ON
* pressing 'F' turns flat shading OFF
*
* When all the above are on, weird things happen.
*
*************************************************************************/
#include <stdio.h>
#include <X11/X.h>
#include <X11/keysym.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <malloc.h>
#include <sys/time.h>
#include <math.h>
#include <gl/image.h>
Display *xdisp;
Window win;
int done = 0;
float azimuth = 0, elevation = 0, distance = 10.0;
int is_texture = 0;
int is_polygon = 0;
int is_blend = 0;
int is_flat = 0;
int is_linesmooth = 0;
/*** WM hints for noborder GLX windows ***/
/*
** The following structure is actually defined in MOTIF's Xm/MwmUtils.h, but
** we don't want to require header file.
*/
typedef struct MWMHints {
long flags;
long functions;
long decorations;
long input_mode;
} MWMHints;
#define MWM_HINTS_FUNCTIONS 1
#define MWM_HINTS_DECORATIONS 2
#define MWM_HINTS_INPUT_MODE 4
static Atom mwm_hints_atom = 0;
static void
getMWMHints(Display *dsp, Window w, MWMHints *pHints)
{
MWMHints *hp;
int type, fmt;
unsigned long nitems, bytes_after;
/* get special atom for 4DWM motif window manager */
if (!mwm_hints_atom)
mwm_hints_atom = XInternAtom(dsp, "_MOTIF_WM_HINTS", 0);
XGetWindowProperty(dsp, w, mwm_hints_atom, 0, 4, False, mwm_hints_atom,
(unsigned long *)&type, &fmt, &nitems, &bytes_after, (unsigned
char**)&hp);
if (hp) {
*pHints = *hp;
XFree((void *)hp);
} else
bzero(pHints, sizeof *pHints);
}
static void noXWindowDecoration(Display *dsp, Window w)
{
MWMHints hints;
/* get special atom for 4DWM motif window manager */
if (!mwm_hints_atom)
mwm_hints_atom = XInternAtom(dsp, "_MOTIF_WM_HINTS", 0);
getMWMHints(dsp, w, &hints);
hints.decorations = 0x0;
hints.flags |= MWM_HINTS_DECORATIONS;
XChangeProperty(dsp, w, mwm_hints_atom, mwm_hints_atom, 32,
PropModeReplace, (unsigned char *)&hints, 4);
}
open_window (char *name, int visual_id)
{
int nred, ngreen, nblue, nalpha;
int vinfo_noms[] =
{
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_ALPHA_SIZE, 1,
None
};
XVisualInfo vtemplate;
XVisualInfo *v;
GLXContext c;
XSetWindowAttributes wa;
Colormap cmap;
XEvent event;
float j;
int fmt;
int n;
unsigned long nitems, byaf;
XSizeHints sh;
struct hints_struct
{
long flags,functions,decorations,input_mode;
} *hints;
Atom type, mwmhints;
int multibuffer;
xdisp = XOpenDisplay (0);
if (visual_id < 0)
{
v = glXChooseVisual(xdisp, DefaultScreen(xdisp), vinfo_noms);
if (v == 0)
{
fprintf(stderr, "glXChooseVisual failed\n");
exit(8);
}
}
else
{
vtemplate.visualid = visual_id;
vtemplate.screen = DefaultScreen(xdisp);
v = XGetVisualInfo (xdisp,
VisualIDMask |
VisualScreenMask,
&vtemplate, &n);
if (n == 0)
{
fprintf(stderr,
"XGetVisualInfo failed on VisualID
%d\n", visual_id);
exit (8);
}
}
if ((c = glXCreateContext(xdisp, v, 0, GL_TRUE)) == 0)
{
fprintf(stderr, "glXCreateContext failed\n");
exit(3);
}
wa.colormap = XCreateColormap (xdisp, RootWindow(xdisp,v->screen),
v->visual,
AllocNone);
wa.border_pixel = 0;
wa.event_mask = StructureNotifyMask
| KeyPressMask
| ButtonReleaseMask
| KeyReleaseMask
| ButtonPressMask;
win = XCreateWindow(xdisp, RootWindow(xdisp, v->screen),
0, 0, 1024, 1024, 0, v->depth,
InputOutput, v->visual,
CWBorderPixel|CWEventMask|CWColormap,
&wa);
noXWindowDecoration (xdisp, win);
sh.flags = USPosition | USSize;
XmbSetWMProperties(xdisp, win, &name[0],0,0,0,&sh, 0, 0);
XMapWindow(xdisp, win);
if(! glXMakeCurrent(xdisp, win, c))
{
printf ("error: gfxMakeCurrent failed\n");
exit(1);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
XFlush (xdisp);
glGetIntegerv (GL_RED_BITS, &nred);
glGetIntegerv (GL_GREEN_BITS, &ngreen);
glGetIntegerv (GL_BLUE_BITS, &nblue);
glGetIntegerv (GL_ALPHA_BITS, &nalpha);
printf ("rgba size : %d %d %d %d\n", nred, ngreen, nblue, nalpha);
return win;
}
main (int argc, char *argv[])
{
char *sw;
int visual_id = -1;
while (*++argv)
{
if (**argv == '-')
{
sw = *argv;
while (*++sw)
{
switch (*sw)
{
case 'v' :
argv ++;
if (strncmp (*argv, "0x", 2) ==
0)
sscanf (*argv, "%x",
&visual_id);
else
sscanf (*argv, "%d",
&visual_id);
}
}
}
}
open_window ("clip", visual_id);
make_texture();
print_status();
while (!done)
{
draw_geom();
read_user_interface();
glXSwapBuffers (xdisp, win);
glFlush();
}
}
make_texture()
{
static unsigned int image[4] =
{
0x00ff00ff,
0xff0000ff,
0xff0000ff,
0x00ff00ff,
};
glEnable (GL_TEXTURE_2D);
glBindTextureEXT (GL_TEXTURE_2D, 1);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
}
draw_geom()
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective (40.0, 1.0, 0.01, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -distance);
glRotatef (-(float) elevation, 1.0, 0.0, 0.0);
glRotatef ((float) azimuth, 0.0, 0.0, -1.0);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable (GL_DEPTH_TEST);
glDisable (GL_ALPHA_TEST);
glDisable (GL_LIGHTING);
if (is_texture)
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
glBindTextureEXT(GL_TEXTURE_2D, 1);
if (is_blend)
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
else
glDisable (GL_BLEND);
if (is_flat)
glShadeModel(GL_FLAT);
else
glShadeModel(GL_SMOOTH);
if (is_linesmooth)
glEnable (GL_LINE_SMOOTH);
else
glDisable (GL_LINE_SMOOTH);
glColor4f(1, 1, 1, 1);
if (is_polygon)
glBegin(GL_POLYGON);
else
glBegin(GL_LINE_LOOP);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
#if 0
glTexCoord2f(1, 0);
glVertex2f(1, -1);
#endif
glEnd();
}
read_user_interface()
{
long dev;
short val;
int mousex, mousey;
int foo;
XEvent event;
char buf[1000];
KeySym ks;
static int start_x, start_y;
static int left_on = 0, middle_on = 0, right_on = 0;
mousex = -1;
mousey = -1;
XQueryPointer (xdisp, win, (Window *) &foo, (Window *) &foo,
&foo, &foo, &mousex, &mousey, (unsigned
int *) &foo);
mousey = 1279 - mousey;
if (left_on)
{
azimuth -= (mousex - start_x) / 10;
azimuth = fmodf (azimuth, 360.0);
elevation += (mousey - start_y) / 10;
if (elevation > 90) elevation = 90;
if (elevation < 0) elevation = 0;
start_x = mousex;
start_y = mousey;
}
if (middle_on)
{
distance /= 1.01f;
if (distance < 1.0f) distance = 1.0f;
}
if (right_on)
{
distance *= 1.01f;
if (distance > 50.0f) distance = 50.0f;
}
while (XPending (xdisp))
{
XNextEvent (xdisp, &event);
switch (event.type)
{
case MappingNotify :
XRefreshKeyboardMapping ((XMappingEvent *)
&event);
break;
case Expose :
break;
case ConfigureNotify :
break;
case MotionNotify :
break;
case ButtonPress :
/*
* If left mouse
*/
if (event.xbutton.button == 1)
{
left_on = 1;
start_x = mousex;
start_y = mousey;
}
else
if (event.xbutton.button == 2)
{
middle_on = 1;
}
else
if (event.xbutton.button == 3)
{
right_on = 1;
}
break;
case ButtonRelease :
/*
* If left mouse
*/
if (event.xbutton.button == 1)
{
left_on = 0;
}
else
if (event.xbutton.button == 2)
{
middle_on = 0;
}
else
if (event.xbutton.button == 3)
{
right_on = 0;
}
break;
case KeyPress :
XLookupString (&event.xkey, buf, sizeof (buf),
&ks, 0);
switch (ks)
{
case XK_Escape :
done = 1;
break;
case XK_f :
is_flat = 1;
break;
case XK_F :
is_flat = 0;
break;
case XK_p :
is_polygon = 1;
break;
case XK_P :
is_polygon = 0;
break;
case XK_t :
is_texture = 1;
break;
case XK_T :
is_texture = 0;
break;
case XK_b :
is_blend = 1;
break;
case XK_B :
is_blend = 0;
break;
case XK_l :
is_linesmooth = 1;
break;
case XK_L :
is_linesmooth = 0;
break;
}
print_status();
}
}
}
print_status()
{
printf ("\n\n");
printf ("primitive : %s\n", is_polygon ? "polygon" : "line");
printf ("shademodel : %s\n", is_flat ? "flat" : "smooth");
printf ("texture : %s\n", is_texture ? "on" : "off");
printf ("blend : %s\n", is_blend ? "on" : "off");
printf ("linesmooth : %s\n", is_linesmooth ? "on" : "off");
}
--
__ | Ran Yakir
/_) _ __ \ / _ / o __ | RT-SET Ltd.
/ )_ (_(_) ) \/ (_(_/<_(_)( |
_/ |
-------------------------------------+--------------------------------
Phone : | E-mail : rany++at++rtset.co.il
Work : 972-9-9552236 | rany++at++netvision.net.il
Res. : 972-9-7489974 |
Fax : 972-9-9552239 |
=======================================================================
List Archives, FAQ, FTP: http://www.sgi.com/Technology/Performer/
Submissions: info-performer++at++sgi.com
Admin. requests: info-performer-request++at++sgi.com
This archive was generated by hypermail 2.0b2 on Mon Aug 10 1998 - 17:54:33 PDT