A
download glxdino.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 310
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


/* $Revision: 1.1.1.1 $ */
/* compile: cc -o glxdino glxdino.c -lGLU -lGL -lXmu -lX11 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>		/* for cos(), sin(), and sqrt() */
#include <GL/glx.h>		/* this includes X and gl.h headers */
#include <GL/glu.h>		/* gluPerspective(), gluLookAt(), GLU polygon
				 * tesselator */
#include <X11/Xatom.h>		/* for XA_RGB_DEFAULT_MAP atom */
#include <X11/Xmu/StdCmap.h>	/* for XmuLookupStandardColormap() */
#include <X11/keysym.h>		/* for XK_Escape keysym */

typedef enum {
    RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
    LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE, DINOSAUR
}               displayLists;

Display *dpy;
Window win;
GLfloat angle = -150;	/* in degrees */
GLboolean doubleBuffer = GL_TRUE, iconic = GL_FALSE, keepAspect = GL_FALSE;
int W = 300, H = 300;
XSizeHints sizeHints = {0};
GLdouble bodyWidth = 2.0;
int configuration[] = {GLX_DOUBLEBUFFER, GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
    {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
    {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
    {1, 2} };
GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
    {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
    {13, 9}, {11, 11}, {9, 11} };
GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
    {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
    {9.6, 15.25}, {9, 15.25} };
GLfloat lightZeroPosition[] = {10.0, 4.0, 10.0, 1.0};
GLfloat lightZeroColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */
GLfloat lightOnePosition[] = {-1.0, -2.0, 1.0, 0.0};
GLfloat lightOneColor[] = {0.6, 0.3, 0.2, 1.0};  /* red-tinted */
GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};

void
fatalError(char *message)
{
    fprintf(stderr, "glxdino: %s\n", message);
    exit(1);
}

Colormap
getColormap(XVisualInfo * vi)
{
    Status          status;
    XStandardColormap *standardCmaps;
    Colormap        cmap;
    int             i, numCmaps;

    /* be lazy; using DirectColor too involved for this example */
    if (vi->class != TrueColor)
        fatalError("no support for non-TrueColor visual");
    /* if no standard colormap but TrueColor, just make an unshared one */
    status = XmuLookupStandardColormap(dpy, vi->screen, vi->visualid,
        vi->depth, XA_RGB_DEFAULT_MAP, /* replace */ False, /* retain */ True);
    if (status == 1) {
	status = XGetRGBColormaps(dpy, RootWindow(dpy, vi->screen),
			     &standardCmaps, &numCmaps, XA_RGB_DEFAULT_MAP);
	if (status == 1)
	    for (i = 0; i < numCmaps; i++)
		if (standardCmaps[i].visualid == vi->visualid) {
		    cmap = standardCmaps[i].colormap;
		    XFree(standardCmaps);
		    return cmap;
		}
    }
    cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
        vi->visual, AllocNone);
    return cmap;
}

void
extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,
    GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
{
    static GLUtriangulatorObj *tobj = NULL;
    GLdouble        vertex[3], dx, dy, len;
    int             i;
    int             count = dataSize / (2 * sizeof(GLfloat));

    if (tobj == NULL) {
	tobj = gluNewTess();	/* create and initialize a GLU polygon
				 * tesselation object */
	gluTessCallback(tobj, GLU_BEGIN, glBegin);
	gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);	/* semi-tricky */
	gluTessCallback(tobj, GLU_END, glEnd);
    }
    glNewList(side, GL_COMPILE);
        glShadeModel(GL_SMOOTH); /* smooth minimizes seeing tessellation */
        gluBeginPolygon(tobj);
            for (i = 0; i < count; i++) {
	        vertex[0] = data[i][0];
	        vertex[1] = data[i][1];
	        vertex[2] = 0;
	        gluTessVertex(tobj, vertex, &data[i]);
            }
        gluEndPolygon(tobj);
    glEndList();
    glNewList(edge, GL_COMPILE);
        glShadeModel(GL_FLAT);	/* flat shade keeps angular hands from being
				 * "smoothed" */
        glBegin(GL_QUAD_STRIP);
        for (i = 0; i <= count; i++) {
	    /* mod function handles closing the edge */
	    glVertex3f(data[i % count][0], data[i % count][1], 0.0);
	    glVertex3f(data[i % count][0], data[i % count][1], thickness);
	    /* Calculate a unit normal by dividing by Euclidean distance. We
	     * could be lazy and use glEnable(GL_NORMALIZE) so we could pass in
	     * arbitrary normals for a very slight performance hit. */
	    dx = data[(i + 1) % count][1] - data[i % count][1];
	    dy = data[i % count][0] - data[(i + 1) % count][0];
	    len = sqrt(dx * dx + dy * dy);
	    glNormal3f(dx / len, dy / len, 0.0);
        }
        glEnd();
    glEndList();
    glNewList(whole, GL_COMPILE);
        glFrontFace(GL_CW);
        glCallList(edge);
        glNormal3f(0.0, 0.0, -1.0); /* constant normal for side */
        glCallList(side);
        glPushMatrix();
            glTranslatef(0.0, 0.0, thickness);
            glFrontFace(GL_CCW);
            glNormal3f(0.0, 0.0, 1.0); /* opposite normal for other side */
            glCallList(side);
        glPopMatrix();
    glEndList();
}

void
makeDinosaur(void)
{
    GLfloat         bodyWidth = 3.0;

    extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
        BODY_SIDE, BODY_EDGE, BODY_WHOLE);
    extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,
        ARM_SIDE, ARM_EDGE, ARM_WHOLE);
    extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,
        LEG_SIDE, LEG_EDGE, LEG_WHOLE);
    extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
        EYE_SIDE, EYE_EDGE, EYE_WHOLE);
    glNewList(DINOSAUR, GL_COMPILE);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
        glCallList(BODY_WHOLE);
        glPushMatrix();
            glTranslatef(0.0, 0.0, bodyWidth);
            glCallList(ARM_WHOLE);
            glCallList(LEG_WHOLE);
            glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);
            glCallList(ARM_WHOLE);
            glTranslatef(0.0, 0.0, -bodyWidth / 4);
            glCallList(LEG_WHOLE);
            glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
            glCallList(EYE_WHOLE);
        glPopMatrix();
    glEndList();
}

void
redraw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glCallList(DINOSAUR);
    if (doubleBuffer)
	glXSwapBuffers(dpy, win);	/* buffer swap does implicit glFlush */
    else glFlush();		/* explicit flush for single buffered case */
}

void
main(int argc, char **argv)
{
    XVisualInfo    *vi;
    Colormap        cmap;
    XSetWindowAttributes swa;
    XWMHints       *wmHints;
    Atom            wmDeleteWindow;
    GLXContext      cx;
    XEvent          event;
    KeySym          ks;
    GLboolean       needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
    char           *display = NULL, *geometry = NULL;
    int             flags, x, y, width, height, lastX, i;

    /*** (1) process normal X command line arguments ***/
    for (i = 1; i < argc; i++) {
	if (!strcmp(argv[i], "-geometry")) {
	    if (++i >= argc)
		fatalError("follow -geometry option with geometry parameter");
	    geometry = argv[i];
	} else if (!strcmp(argv[i], "-display")) {
	    if (++i >= argc)
		fatalError("follow -display option with display parameter");
	    display = argv[i];
	} else if (!strcmp(argv[i], "-iconic")) iconic = GL_TRUE;
	else if (!strcmp(argv[i], "-keepaspect")) keepAspect = GL_TRUE;
	else if (!strcmp(argv[i], "-single")) doubleBuffer = GL_FALSE;
	else fatalError("bad option");
    }

    /*** (2) open a connection to the X server ***/
    dpy = XOpenDisplay(display);
    if (dpy == NULL) fatalError("could not open display");

    /*** (3) make sure OpenGL's GLX extension supported ***/
    if (!glXQueryExtension(dpy, NULL, NULL))
	fatalError("X server has no OpenGL GLX extension");

    /*** (4) find an appropriate visual and a colormap for it ***/
    /* find an OpenGL-capable RGB visual with depth buffer */
    if (!doubleBuffer) goto SingleBufferOverride;
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), configuration);
    if (vi == NULL) {
      SingleBufferOverride:
	vi = glXChooseVisual(dpy, DefaultScreen(dpy), &configuration[1]);
	if (vi == NULL)
	    fatalError("no appropriate RGB visual with depth buffer");
	doubleBuffer = GL_FALSE;
    }
    cmap = getColormap(vi);

    /*** (5) create an OpenGL rendering context  ***/
    /* create an OpenGL rendering context */
    cx = glXCreateContext(dpy, vi, /* no sharing of display lists */ NULL,
			   /* direct rendering if possible */ GL_TRUE);
    if (cx == NULL) fatalError("could not create rendering context");

    /*** (6) create an X window with selected visual and right properties ***/
    flags = XParseGeometry(geometry, &x, &y,
	(unsigned int *) &width, (unsigned int *) &height);
    if (WidthValue & flags) {
	sizeHints.flags |= USSize;
	sizeHints.width = width;
	W = width;
    }
    if (HeightValue & flags) {
	sizeHints.flags |= USSize;
	sizeHints.height = height;
	H = height;
    }
    if (XValue & flags) {
	if (XNegative & flags)
	    x = DisplayWidth(dpy, DefaultScreen(dpy)) + x - sizeHints.width;
	sizeHints.flags |= USPosition;
	sizeHints.x = x;
    }
    if (YValue & flags) {
	if (YNegative & flags)
	    y = DisplayHeight(dpy, DefaultScreen(dpy)) + y - sizeHints.height;
	sizeHints.flags |= USPosition;
	sizeHints.y = y;
    }
    if (keepAspect) {
	sizeHints.flags |= PAspect;
	sizeHints.min_aspect.x = sizeHints.max_aspect.x = W;
	sizeHints.min_aspect.y = sizeHints.max_aspect.y = H;
    }
    swa.colormap = cmap;
    swa.border_pixel = 0;
    swa.event_mask = ExposureMask | StructureNotifyMask |
	ButtonPressMask | Button1MotionMask | KeyPressMask;
    win = XCreateWindow(dpy, RootWindow(dpy, vi->screen),
                        sizeHints.x, sizeHints.y, W, H,
			0, vi->depth, InputOutput, vi->visual,
			CWBorderPixel | CWColormap | CWEventMask, &swa);
    XSetStandardProperties(dpy, win, "OpenGLosaurus", "glxdino",
        None, argv, argc, &sizeHints);
    wmHints = XAllocWMHints();
    wmHints->initial_state = iconic ? IconicState : NormalState;
    wmHints->flags = StateHint;
    XSetWMHints(dpy, win, wmHints);
    wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(dpy, win, &wmDeleteWindow, 1);

    /*** (7) bind the rendering context to the window ***/
    glXMakeCurrent(dpy, win, cx);

    /*** (8) make the desired display lists ***/
    makeDinosaur();

    /*** (9) configure the OpenGL context for rendering ***/
    glEnable(GL_CULL_FACE);	/* ~50% better perfomance than no back-face
				 * culling on Entry Indigo */
    glEnable(GL_DEPTH_TEST);	/* enable depth buffering */
    glEnable(GL_LIGHTING);	/* enable lighting */
    glMatrixMode(GL_PROJECTION);/* set up projection transform */
    gluPerspective( /* field of view in degree */ 40.0, /* aspect ratio */ 1.0,
		    /* Z near */ 1.0, /* Z far */ 40.0);
    glMatrixMode(GL_MODELVIEW);	/* now change to modelview */
    gluLookAt(0.0, 0.0, 30.0,	/* eye is at (0,0,30) */
	      0.0, 0.0, 0.0,	/* center is at (0,0,0) */
	      0.0, 1.0, 0.);	/* up is in postivie Y direction */
    glPushMatrix();		/* dummy push so we can pop on model recalc */
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
    glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);	/* enable both lights */

    /*** (10) request the X window to be displayed on the screen ***/
    XMapWindow(dpy, win);

    /*** (11) dispatch X events ***/
    while (1) {
	do {
	    XNextEvent(dpy, &event);
	    switch (event.type) {
	    case ConfigureNotify:
		glViewport(0, 0,
		    event.xconfigure.width, event.xconfigure.height);
		/* fall through... */
	    case Expose:
		needRedraw = GL_TRUE;
		break;
	    case MotionNotify:
		recalcModelView = GL_TRUE;
		angle -= (lastX - event.xmotion.x);
	    case ButtonPress:
		lastX = event.xbutton.x;
		break;
	    case KeyPress:
		ks = XLookupKeysym((XKeyEvent *) & event, 0);
		if (ks == XK_Escape) exit(0);
		break;
	    case ClientMessage:
		if (event.xclient.data.l[0] == wmDeleteWindow) exit(0);
		break;
	    }
	} while (XPending(dpy));/* loop to compress events */
	if (recalcModelView) {
	    glPopMatrix();	/* pop old rotated matrix (or dummy matrix if
				 * first time) */
	    glPushMatrix();
	    glRotatef(angle, 0.0, 1.0, 0.0);
	    glTranslatef(-8, -8, -bodyWidth / 2);
	    recalcModelView = GL_FALSE;
	    needRedraw = GL_TRUE;
	}
	if (needRedraw) {
	    redraw();
	    needRedraw = GL_FALSE;
	}
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us