auxdemo.c
Go to the documentation of this file.
1 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/oglport_0b71.asp */
2 
3 /*
4  * Example of an X Window System OpenGL program.
5  * OpenGL code is taken from auxdemo.c in the Platform SDK
6  */
7 #include <GL/glx.h>
8 #include <GL/gl.h>
9 #include <GL/glu.h>
10 #include <X11/keysym.h>
11 #include <X11/Xlib.h>
12 #include <X11/Xutil.h>
13 #include <stdio.h>
14 
15 /* X globals, defines, and prototypes */
16 Display *dpy;
17 Window glwin;
18 //static int attributes[] = {GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
19 //static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 1, 0};
20 static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 1, 0};
21 
22 #define SWAPBUFFERS glXSwapBuffers(dpy, glwin)
23 #define BLACK_INDEX 0
24 #define RED_INDEX 1
25 #define GREEN_INDEX 2
26 #define BLUE_INDEX 4
27 #define WIDTH 300
28 #define HEIGHT 200
29 
30 
31 /* OpenGL globals, defines, and prototypes */
33 GLdouble radius;
34 
35 #define GLOBE 1
36 #define CYLINDER 2
37 #define CONE 3
38 
39 GLvoid resize(GLsizei, GLsizei);
40 GLvoid initializeGL(GLsizei, GLsizei);
41 GLvoid drawScene(GLvoid);
42 void polarView( GLdouble, GLdouble, GLdouble, GLdouble);
43 
44 static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
45 {
46  if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
47  return GL_TRUE;
48  }
49  return GL_FALSE;
50 }
51 
52 void
53 main(int argc, char **argv)
54 {
55  XVisualInfo *vi;
56  Colormap cmap;
57  XSetWindowAttributes swa;
58  GLXContext cx;
59  XEvent event;
60  GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
61  int dummy;
62 
63  dpy = XOpenDisplay(NULL);
64  if (dpy == NULL){
65  fprintf(stderr, "could not open display\n");
66  exit(1);
67  }
68 
69  if(!glXQueryExtension(dpy, &dummy, &dummy)){
70  fprintf(stderr, "could not open display");
71  exit(1);
72  }
73 
74  /* find an OpenGL-capable Color Index visual with depth buffer */
75  vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
76  if (vi == NULL) {
77  fprintf(stderr, "could not get visual\n");
78  exit(1);
79  }
80 
81  /* create an OpenGL rendering context */
82  cx = glXCreateContext(dpy, vi, None, GL_TRUE);
83  if (cx == NULL) {
84  fprintf(stderr, "could not create rendering context\n");
85  exit(1);
86  }
87 
88  /* create an X colormap since probably not using default visual */
89 #ifndef OFFLINE_RENDERING
90  cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
91  vi->visual, AllocNone);
92  swa.colormap = cmap;
93  swa.border_pixel = 0;
94  swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
95  glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH,
96  HEIGHT, 0, vi->depth, InputOutput, vi->visual,
97  CWBorderPixel | CWColormap | CWEventMask, &swa);
98  XSetStandardProperties(dpy, glwin, "xogl", "xogl", None, argv,
99  argc, NULL);
100 #else
101  glwin = glXCreateGLXPixmap(dpy, vi, XCreatePixmap(dpy, RootWindow(dpy, vi->screen), WIDTH, HEIGHT, vi->depth));
102 #endif
103  glXMakeCurrent(dpy, glwin, cx);
104 
105 #ifndef OFFLINE_RENDERING
106  XMapWindow(dpy, glwin);
107  XIfEvent(dpy, &event, WaitForMapNotify, (char *)glwin);
108 #endif
109 
111  resize(WIDTH, HEIGHT);
112 
113  /* Animation loop */
114  while (1) {
115  KeySym key;
116 
117  while (XPending(dpy)) {
118  XNextEvent(dpy, &event);
119  switch (event.type) {
120  case KeyPress:
121  XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL);
122  switch (key) {
123  case XK_Left:
124  longinc += 0.5;
125  break;
126  case XK_Right:
127  longinc -= 0.5;
128  break;
129  case XK_Up:
130  latinc += 0.5;
131  break;
132  case XK_Down:
133  latinc -= 0.5;
134  break;
135  }
136  break;
137  case ConfigureNotify:
138  resize(event.xconfigure.width, event.xconfigure.height);
139  break;
140  }
141  }
142  drawScene();
143  }
144 }
145 
146 /* OpenGL code */
147 
148 GLvoid resize( GLsizei width, GLsizei height )
149 {
150  GLfloat aspect;
151 
152  glViewport( 0, 0, width, height );
153 
154  aspect = (GLfloat) width / height;
155 
156  glMatrixMode( GL_PROJECTION );
157  glLoadIdentity();
158  gluPerspective( 45.0, aspect, 3.0, 7.0 );
159  glMatrixMode( GL_MODELVIEW );
160 }
161 
162 GLvoid createObjects()
163 {
164  GLUquadricObj *quadObj;
165 
166  glNewList(GLOBE, GL_COMPILE);
167  quadObj = gluNewQuadric ();
168  gluQuadricDrawStyle (quadObj, GLU_LINE);
169  gluSphere (quadObj, 1.5, 16, 16);
170  glEndList();
171 
172  glNewList(CONE, GL_COMPILE);
173  quadObj = gluNewQuadric ();
174  gluQuadricDrawStyle (quadObj, GLU_FILL);
175  gluQuadricNormals (quadObj, GLU_SMOOTH);
176  gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
177  glEndList();
178 
179  glNewList(CYLINDER, GL_COMPILE);
180  glPushMatrix ();
181  glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
182  glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
183  quadObj = gluNewQuadric ();
184  gluQuadricDrawStyle (quadObj, GLU_FILL);
185  gluQuadricNormals (quadObj, GLU_SMOOTH);
186  gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
187  glPopMatrix ();
188  glEndList();
189 }
190 
191 GLvoid initializeGL(GLsizei width, GLsizei height)
192 {
193  GLfloat maxObjectSize, aspect;
194  GLdouble near_plane, far_plane;
195 
196  glClearIndex( (GLfloat)BLACK_INDEX);
197  glClearDepth( 1.0 );
198 
199  glEnable(GL_DEPTH_TEST);
200 
201  glMatrixMode( GL_PROJECTION );
202  aspect = (GLfloat) width / height;
203  gluPerspective( 45.0, aspect, 3.0, 7.0 );
204  glMatrixMode( GL_MODELVIEW );
205 
206  near_plane = 3.0;
207  far_plane = 7.0;
208  maxObjectSize = 3.0F;
209  radius = near_plane + maxObjectSize/2.0;
210 
211  latitude = 0.0F;
212  longitude = 0.0F;
213  latinc = 6.0F;
214  longinc = 2.5F;
215 
216  createObjects();
217 }
218 
219 void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
220  GLdouble longitude)
221 {
222  glTranslated(0.0, 0.0, -radius);
223  glRotated(-twist, 0.0, 0.0, 1.0);
224  glRotated(-latitude, 1.0, 0.0, 0.0);
225  glRotated(longitude, 0.0, 0.0, 1.0);
226 
227 }
228 
229 GLvoid drawScene(GLvoid)
230 {
231  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
232 
233  glPushMatrix();
234 
235  latitude += latinc;
236  longitude += longinc;
237 
238  // polarView( radius, 0, latitude, longitude );
239  polarView( radius, 0, latitude/50.0f, longitude/50.0f );
240 
241  glIndexi(RED_INDEX);
242  glCallList(CONE);
243 
244  glIndexi(BLUE_INDEX);
245  glCallList(GLOBE);
246 
247  glIndexi(GREEN_INDEX);
248  glPushMatrix();
249  glTranslatef(0.8F, -0.65F, 0.0F);
250  glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
251  glCallList(CYLINDER);
252  glPopMatrix();
253 
254  glPopMatrix();
255 
256 #ifdef OFFLINE_RENDERING
257  {
258  static int i = 0;
259  char imgbuf[WIDTH*HEIGHT*3];
260  glReadBuffer(GL_FRONT);
261  glPixelStorei(GL_PACK_ALIGNMENT, 1);
262  glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, imgbuf);
263 
264  char filename[256];
265  sprintf(filename, "test%08d.ppm", i++);
266  fprintf(stderr, "writing to %s (%7.3f %7.3f)\n", filename, latitude, longitude);
267 
268  FILE *fp = fopen(filename, "w+");
269  fprintf(fp, "P6\n");
270  fprintf(fp, "#\n");
271  fprintf(fp, "%d %d 255\n", WIDTH, HEIGHT);
272  fwrite(imgbuf, 1, WIDTH*HEIGHT*3, fp);
273  fclose(fp);
274  }
275 #endif
276  SWAPBUFFERS;
277 }
attributes
static int attributes[]
Definition: auxdemo.c:20
resize
GLvoid resize(GLsizei, GLsizei)
Definition: auxdemo.c:148
XSetStandardProperties
int XSetStandardProperties
latitude
GLfloat latitude
Definition: auxdemo.c:32
XOpenDisplay
int XOpenDisplay
XPending
int XPending
dpy
Display * dpy
Definition: auxdemo.c:16
GLOBE
#define GLOBE
Definition: auxdemo.c:35
SWAPBUFFERS
#define SWAPBUFFERS
Definition: auxdemo.c:22
XIfEvent
int XIfEvent
longinc
GLfloat longinc
Definition: auxdemo.c:32
CONE
#define CONE
Definition: auxdemo.c:37
WIDTH
#define WIDTH
Definition: auxdemo.c:27
key
static void key(unsigned char c, int x, int y)
Definition: dinoshade.c:753
WaitForMapNotify
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
Definition: auxdemo.c:44
polarView
void polarView(GLdouble, GLdouble, GLdouble, GLdouble)
Definition: auxdemo.c:219
XLookupString
int XLookupString
longitude
GLfloat longitude
Definition: auxdemo.c:32
XCreateWindow
int XCreateWindow
CYLINDER
#define CYLINDER
Definition: auxdemo.c:36
NULL
#define NULL
Definition: transargv.c:8
XCreateColormap
int XCreateColormap
XCreatePixmap
int XCreatePixmap
RED_INDEX
#define RED_INDEX
Definition: auxdemo.c:24
latinc
GLfloat latinc
Definition: auxdemo.c:32
BLUE_INDEX
#define BLUE_INDEX
Definition: auxdemo.c:26
main
void main(int argc, char **argv)
Definition: auxdemo.c:53
f
f
argc
static int argc
Definition: transargv.c:56
initializeGL
GLvoid initializeGL(GLsizei, GLsizei)
Definition: auxdemo.c:191
XNextEvent
int XNextEvent
drawScene
GLvoid drawScene(GLvoid)
Definition: auxdemo.c:229
createObjects
GLvoid createObjects()
Definition: auxdemo.c:162
glwin
Window glwin
Definition: auxdemo.c:17
radius
GLdouble radius
Definition: auxdemo.c:33
BLACK_INDEX
#define BLACK_INDEX
Definition: auxdemo.c:23
HEIGHT
#define HEIGHT
Definition: auxdemo.c:28
XMapWindow
int XMapWindow
GREEN_INDEX
#define GREEN_INDEX
Definition: auxdemo.c:25


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:42