|
g++ 可以,gcc报错如下:
gcc -Wall -lGL lglx.c -o lglx
lglx.c: In function 'event_loop':
lglx.c:50: error: syntax error before 'sym'
lglx.c:51: error: 'sym' undeclared (first use in this function)
lglx.c:51: error: (Each undeclared identifier is reported only once
lglx.c:51: error: for each function it appears in.)
- //for learn opengl as glx
- #include<GL/gl.h>
- #include<GL/glx.h>
- #include<X11/Xlib.h>
- #include<X11/keysym.h>
- #include<stdio.h>
- #include<stdlib.h>
- unsigned int width,height;
- void redraw( Display *dpy, Window w )
- {
- //printf("Redraw event\n");
- glClear( GL_COLOR_BUFFER_BIT );
- glShadeModel(GL_SMOOTH);
- // Draw the triangle
- glBegin(GL_TRIANGLES);
- // Red Apex
- glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);
- glVertex3f(0.0f,200.0f,0.0f);
- // Green on the right bottom corner
- glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);
- glVertex3f(200.0f,-70.0f,0.0f);
- // Blue on the left bottom corner
- glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255);
- glVertex3f(-200.0f, -70.0f, 0.0f);
- glEnd();
- glXSwapBuffers( dpy, w );
- }
- void resize( unsigned int width, unsigned int height)
- {
- //printf("Resize event\n");
- glViewport( 0, 0, width, height );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( -200.0, 200.0, -200.0, 200.0, -1.0, 1.0 );
- }
- void event_loop(Display *dpy)
- {
- XEvent event;
- while(1){
- while(XPending(dpy)>0){
- XNextEvent(dpy,&event);
- switch(event.type){
- case KeyRelease:
- KeySym sym;
- sym=XLookupKeysym((XKeyEvent*)&event,0);
- if(sym==XK_Escape){
- return;
- }
- break;
- case Expose:
- redraw( dpy, event.xany.window );
- break;
- case ConfigureNotify:
- resize( event.xconfigure.width, event.xconfigure.height );
- break;
- default:
- break;
- }
- }
-
- }
- }
- int main(int argc, char * argv[])
- {
- Display *dpy;
- dpy=XOpenDisplay(NULL);
- width=800;
- height=600;
- int attrib[]={
- GLX_RGBA,
- GLX_DOUBLEBUFFER,
- GLX_RED_SIZE,4,
- GLX_BLUE_SIZE,4,
- GLX_GREEN_SIZE,4,
- None
- };
-
- int scrnum;
- XSetWindowAttributes attr;
- unsigned long mask;
- Window root,win;
- GLXContext ctx;
- XVisualInfo *visinfo;
- scrnum=DefaultScreen(dpy);
- root=RootWindow(dpy,scrnum);
- visinfo=glXChooseVisual(dpy,scrnum,attrib);
- if(visinfo==NULL){
- fprintf(stderr,"Error:Couldn't found a RGBA double buffer visual\n");
- exit(1);
- }
- attr.background_pixel = 0;
- attr.border_pixel = 0;
- attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
- attr.event_mask = StructureNotifyMask | ExposureMask | KeyReleaseMask;
- mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
- win = XCreateWindow( dpy, root, 0, 0, width, height,
- 0, visinfo->depth, InputOutput,
- visinfo->visual, mask, &attr );
-
- ctx = glXCreateContext( dpy, visinfo, NULL, True );
- if (!ctx) {
- printf("Error: glXCreateContext failed\n");
- exit(1);
- }
-
- glXMakeCurrent( dpy, win, ctx );
- XMapWindow(dpy,win);
- resize(width,height);
- event_loop(dpy);
-
- glXDestroyContext(dpy, ctx);
- XDestroyWindow(dpy, win);
- XCloseDisplay(dpy);
- return 0;
- }
复制代码 |
|