2
0

glxtest.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. GLX demo for FreePascal
  3. 2005 Bart Tierens, [email protected]
  4. This program is in the public domain
  5. Warning: This demo works only with FreePascal 2.1 and better, due to changes to the glx header
  6. }
  7. program glxTest;
  8. {$MODE delphi}
  9. uses glx,unix,x,xlib,xutil,gl,glu;
  10. var
  11. attr: Array[0..8] of integer = (GLX_RGBA,GLX_RED_SIZE,1,GLX_GREEN_SIZE,1,GLX_BLUE_SIZE,1,GLX_DOUBLEBUFFER,none);
  12. visinfo: PXVisualInfo;
  13. cm: TColormap;
  14. winAttr: TXSetWindowAttributes;
  15. glXCont: GLXContext;
  16. dpy: PDisplay;
  17. win: TWindow;
  18. procedure redraw();
  19. begin
  20. glClear(GL_COLOR_BUFFER_BIT);
  21. glTranslatef(-0.5,-0.5,-2);
  22. glBegin(GL_QUADS);
  23. glColor3f(1,0,0);
  24. glVertex3f(0,0,0);
  25. glColor3f(0,1,0);
  26. glVertex3f(1,0,0);
  27. glColor3f(0,0,1);
  28. glVertex3f(1,1,0);
  29. glColor3f(1,1,1);
  30. glVertex3f(0,1,0);
  31. glEnd();
  32. glXSwapBuffers(dpy, win); //Swap the buffers
  33. end;
  34. procedure resize(width,height: integer);
  35. begin
  36. glViewport(0,0,width,height);
  37. glMatrixMode(GL_PROJECTION);
  38. glLoadIdentity();
  39. gluPerspective(45,width/height,0.1,200);
  40. glMatrixMode(GL_MODELVIEW);
  41. end;
  42. procedure loop();
  43. var
  44. event: TXEvent;
  45. begin
  46. while true do
  47. begin
  48. XNextEvent(dpy,@event);
  49. case event._type of
  50. Expose: redraw();
  51. ConfigureNotify: resize(event.xconfigure.width,event.xconfigure.height);
  52. KeyPress: halt(1);
  53. end;
  54. end;
  55. end;
  56. var
  57. errorBase,eventBase: integer;
  58. window_title_property: TXTextProperty;
  59. title: String;
  60. begin
  61. initGlx();
  62. dpy := XOpenDisplay(nil);
  63. if(dpy = nil) then
  64. writeLn('Error: Could not connect to X server');
  65. if not (glXQueryExtension(dpy,errorBase,eventBase)) then
  66. writeLn('Error: GLX extension not supported');
  67. visinfo := glXChooseVisual(dpy,DefaultScreen(dpy), Attr);
  68. if(visinfo = nil) then
  69. writeLn('Error: Could not find visual');
  70. //Create a new colormap
  71. cm := XCreateColormap(dpy,RootWindow(dpy,visinfo.screen),visinfo.visual,AllocNone);
  72. winAttr.colormap := cm;
  73. winAttr.border_pixel := 0;
  74. winAttr.background_pixel := 0;
  75. winAttr.event_mask := ExposureMask or ButtonPressMask or StructureNotifyMask or KeyPressMask;
  76. //Create a window
  77. win := XCreateWindow(dpy,RootWindow(dpy,visinfo.screen),0,0,640,480,0,visinfo.depth,InputOutput,visinfo.visual,CWBorderPixel or CWColormap or CWEventMask,@winAttr);
  78. title := 'FreePascal GLX demo --------- Press any key to exit';
  79. XStringListToTextProperty(@title,1,@window_title_property);
  80. XSetWMName(dpy,win,@window_title_property);
  81. //Create an OpenGL rendering context
  82. glXCont := glXCreateContext(dpy,visinfo,none,true);
  83. if(glXCont = nil) then
  84. writeLn('Error: Could not create an OpenGL rendering context');
  85. //Make it current
  86. glXMakeCurrent(dpy,win,glXCont);
  87. //Map the window on the display
  88. XMapWindow(dpy,win);
  89. loop();
  90. end.