#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <GL/glx.h>
#include <GL/gl.h>
#include <X11/X.h>
#include <X11/keysym.h>
#include <time.h>
#include "c_timer.h"
using namespace std;
static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
Display *dpy;
Window win;
GLfloat xAngle = 42.0, yAngle = 82.0, zAngle = 112.0;
GLboolean doubleBuffer = GL_TRUE;
GLboolean needRedraw = GL_FALSE;
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
bool keyR = false;
int a = 0;
void (* redraw2)();
void timemake();
void timemake2()
{
cout << a << endl;
a = 0;
}
void fatalError(char *message)
{
fprintf(stderr, "main: %s\n", message);
exit(1);
}
void redraw(timespec*req)
{
a++;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
glRotatef(xAngle, 0.1, 0.0, 0.0);
glRotatef(yAngle, 0.0, 0.1, 0.0);
glRotatef(zAngle, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0, 0.7, 0.1);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.9, 1.0, 0.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.2, 0.2, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glColor3f(0.7, 0.0, 0.1);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
if (doubleBuffer)
glXSwapBuffers(dpy, win);/* buffer swap does implicit glFlush */
else
glFlush(); /* explicit flush for single buffered case */
nanosleep(req, (struct timespec *)NULL);
}
int main(int argc, char **argv)
{
//redraw2 = &redraw;
struct timespec req = {0};
req.tv_sec = 0;
req.tv_nsec =10 * 1000000L;
dpy = XOpenDisplay(NULL);
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
if (vi == NULL)
{
vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
doubleBuffer = GL_FALSE;
}
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = KeyPressMask | ExposureMask
| ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0,
300, 300, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, win, "main", "main", None,
argv, argc, NULL);
cx = glXCreateContext(dpy, vi, /* no shared dlists */ None,
/* direct rendering if possible */ GL_TRUE);
glXMakeCurrent(dpy, win, cx);
XMapWindow(dpy, win);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
glViewport(0, 0, 300, 300);
printf( "Press left mouse button to rotate around X axis\n" );
printf( "Press middle mouse button to rotate around Y axis\n" );
printf( "Press right mouse button to rotate around Z axis\n" );
printf( "Press ESC to quit the application\n" );
c_timer tim(10,timemake);
c_timer tim2(1000, timemake2);
while (1)
{
if (XPending(dpy) == 0) {redraw(&req);} else {
XNextEvent(dpy, &event);
switch (event.type)
{
case KeyRelease:
{
keyR = true;
break;
}
case KeyPress:
{
KeySym keysym;
char buffer[1];
XLookupString((XKeyEvent *)&event,buffer,1,&keysym,NULL);
cout << buffer[0];
if ( (XLookupString((XKeyEvent *)&event,buffer,1,&keysym,NULL) == 1)
&& (keysym == (KeySym)XK_Escape) )
exit(0);
if (keysym == (KeySym)'r') keyR = false;
break;
}
case ButtonPress:
switch (event.xbutton.button)
{
case 1: {xAngle += 10.0; }
break;
case 2: yAngle += 10.0;
break;
case 3: zAngle += 10.0;
break;
}
break;
case ConfigureNotify:
glViewport(0, 0, event.xconfigure.width,
event.xconfigure.height);
break;
}
}
}
return 0;
}
void timemake()
{
xAngle += 1.0;
if (keyR == true) yAngle += 1.0;
}