Debian Codegraph @

debian/external_deps/fetk/sg/tools/tests/glu/main.c

raw
1 /*
2 * ***************************************************************************
3 * SG = < Socket Graphics >
4 * Copyright (C) 1994-- Michael Holst
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * rcsid="$Id: main.c,v 1.6 2010/08/12 04:56:34 fetk Exp $"
21 * ***************************************************************************
22 */
23
24 /*
25 * ***************************************************************************
26 * File: main.c
27 *
28 * Purpose: Driver program for testing OpenGL.
29 *
30 * Author: Michael Holst (Hacked the program found in the glXIntro manpage)
31 * ***************************************************************************
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <math.h>
38
39 #if defined(HAVE_X11)
40 #include <GL/glx.h>
41 #elif defined(HAVE_WIN32)
42 #elif defined(HAVE_NEXT)
43 #include <AppKit/AppKit.h>
44 #include <GL/osmesa.h>
45 #endif
46
47 #include <GL/gl.h>
48 #include <GL/glu.h>
49
50 #if defined(HAVE_X11)
51 extern Bool WaitForNotify(Display *d, XEvent *e, char *arg);
52 #elif defined(HAVE_WIN32)
53 #elif defined(HAVE_NEXT)
54 #endif
55
56 extern void mySphere(double x[3], double radius);
57 extern void myinit(void);
58 extern void myReshape(int w, int h);
59 extern void myDisplay(void);
60
61 static int gl_width=300;
62 static int gl_height=300;
63
64 #if defined(HAVE_X11)
65 static int attributeList[] = {
66 GLX_RGBA,
67 GLX_DOUBLEBUFFER,
68 None
69 };
70 Bool WaitForNotify(Display *d, XEvent *e, char *arg) {
71 return (e->type == MapNotify) && (e->xmap.window == (Window)arg);
72 }
73 #elif defined(HAVE_WIN32)
74 #elif defined(HAVE_NEXT)
75 #endif
76
77 void mySphere(double x[3], double radius)
78 {
79 GLUquadricObj *quadObj;
80 glPushMatrix();
81 glTranslated(x[0], x[1], x[2]);
82 quadObj = gluNewQuadric ();
83 gluQuadricDrawStyle (quadObj, GLU_FILL);
84 gluQuadricNormals (quadObj, GLU_SMOOTH);
85 gluSphere (quadObj, radius, 16, 16);
86 glPopMatrix();
87 }
88
89 void myinit(void)
90 {
91 GLfloat mat_ambient[] = { (float)0.0, (float)0.0, (float)0.0, (float)0.15 };
92 GLfloat mat_specular[] = { (float)1.0, (float)1.0, (float)1.0, (float)0.15 };
93 GLfloat mat_shininess[] = { (float)15.0 };
94
95 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
96 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
97 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
98
99 glEnable (GL_LIGHTING);
100 glEnable (GL_LIGHT0);
101 glDepthFunc(GL_LESS);
102 glEnable(GL_DEPTH_TEST);
103 }
104
105 void myReshape(int w, int h)
106 {
107 glViewport(0, 0, w, h);
108 glMatrixMode(GL_PROJECTION);
109 glLoadIdentity();
110 gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
111 glMatrixMode(GL_MODELVIEW);
112 glLoadIdentity();
113 }
114
115 void myDisplay(void)
116 {
117 GLdouble x[3] = { 0.0, 0.0, 0.0 };
118 GLdouble radius = 1.0;
119 GLfloat position[] = { (float)0.0, (float)0.0, (float)1.0, (float)1.0 };
120 GLfloat mat_torus[] = { (float)0.75, (float)0.75, (float)0.0, (float)1.0 };
121
122 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123 glLightfv (GL_LIGHT0, GL_POSITION, position);
124 glPushMatrix ();
125 gluLookAt (0.0, 0.0, -9.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
126 glPushMatrix ();
127 glTranslated (0.0, 0.0, 1.0);
128 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_torus);
129 mySphere(x, radius);
130 glPopMatrix ();
131
132 glFlush ();
133 }
134
135 int main(int argc, char **argv) {
136 #if defined(HAVE_X11)
137 Display *dpy;
138 XVisualInfo *vi;
139 Colormap cmap;
140 XSetWindowAttributes swa;
141 Window win;
142 GLXContext ctx;
143 XEvent event;
144 #elif defined(HAVE_WIN32)
145 #elif defined(HAVE_NEXT)
146 OSMesaContext ctx;
147 unsigned char *buffer;
148 NSWindow *myWindow;
149 NSView *myView;
150 NSMenu *myMenu;
151 NSBitmapImageRep *bitmap;
152 char name[50];
153 NSRect GR;
154 #endif
155
156 #if defined(HAVE_X11)
157 /* get a connection */
158 dpy = XOpenDisplay(0);
159
160 /* get an appropriate visual */
161 vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
162
163 /* create a color map */
164 cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
165 AllocNone);
166
167 /* create a window */
168 swa.colormap = cmap;
169 swa.border_pixel = 0;
170 swa.event_mask = StructureNotifyMask;
171 win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0,
172 (unsigned int)gl_width, (unsigned int)gl_height,
173 0, vi->depth, InputOutput, vi->visual,
174 CWBorderPixel|CWColormap|CWEventMask, &swa);
175 XMapWindow(dpy, win);
176 XIfEvent(dpy, &event, WaitForNotify, (char*)win);
177
178 #elif defined(HAVE_WIN32)
179 #elif defined(HAVE_NEXT)
180
181 [[NSAutoreleasePool alloc] init];
182 NSApp=[NSApplication sharedApplication];
183
184 /* Allocate the image buffer */
185 buffer = malloc( gl_width * gl_height * 4 );
186
187 bitmap = [[ NSBitmapImageRep alloc] initWithBitmapDataPlanes:&buffer
188 pixelsWide:gl_width
189 pixelsHigh:gl_height
190 bitsPerSample:8 samplesPerPixel:4
191 hasAlpha:YES isPlanar:NO
192 colorSpaceName:NSDeviceRGBColorSpace
193 bytesPerRow:0 bitsPerPixel:0];
194 GR = NSMakeRect(100, 100, gl_width, gl_height);
195
196 myWindow = [[ NSWindow alloc] initWithContentRect:GR
197 styleMask:NSTitledWindowMask|
198 NSMiniaturizableWindowMask
199 backing:NSBackingStoreBuffered defer:NO];
200
201 sprintf(name, "VGL: `%s'", argv[0]);
202
203 myView = [[ NSView alloc] initWithFrame:GR];
204
205 myMenu = [[ NSMenu alloc] initWithTitle:@"VGL"];
206 [myMenu addItemWithTitle:@"Quit"
207 action:@selector(terminate:)
208 keyEquivalent:@"q"];
209 [myMenu sizeToFit];
210
211 [myWindow setTitle:[NSString stringWithCString:name]];
212 [myWindow display];
213 [myWindow setContentView:myView];
214 [myWindow makeKeyAndOrderFront:nil];
215
216 [NSApp setMainMenu:myMenu];
217
218 [myView lockFocus];
219 #endif
220
221 #if defined(HAVE_X11)
222 ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
223 glXMakeCurrent(dpy, win, ctx);
224 #elif defined(HAVE_WIN32)
225 #elif defined(HAVE_NEXT)
226 ctx = OSMesaCreateContext( GL_RGBA, NULL );
227 OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, gl_width, gl_height );
228 #endif
229
230 myinit();
231 myReshape(gl_width,gl_height);
232 myDisplay();
233
234 #if defined(HAVE_X11)
235 glXSwapBuffers(dpy,win);
236 while(1);
237 #elif defined(HAVE_WIN32)
238 #elif defined(HAVE_NEXT)
239 [bitmap draw];
240 [bitmap release];
241 [myWindow flushWindow];
242 [myView unlockFocus];
243 free( buffer );
244 OSMesaDestroyContext( ctx );
245 [NSApp run];
246 [NSApp release];
247 #endif
248
249 return 0;
250 }