00001
00002
00003
00004
00005
00006
00007
00008 #include "OpenGLApp.h"
00009
00010 #include "OpenGLWindow.h"
00011 #include "OpenGL.h"
00012
00013 #include <iostream>
00014
00015 OpenGLApp::OpenGLApp()
00016 :m_display(0)
00017 ,m_colormap(0)
00018 ,m_vinfo(0)
00019 ,m_ctx(0)
00020 ,m_privateColormap(true)
00021 {
00022 }
00023
00024 OpenGLApp::~OpenGLApp ()
00025 {
00026 }
00027
00028 bool OpenGLApp::initialize ( int , char** )
00029 {
00030 static int attributeList[] = { GLX_RGBA,
00031 GLX_RED_SIZE, 2,
00032 GLX_GREEN_SIZE, 2,
00033 GLX_BLUE_SIZE, 2,
00034 GLX_DOUBLEBUFFER,
00035 GLX_DEPTH_SIZE, 1,
00036 None };
00037 m_display = XOpenDisplay(NULL);
00038 if(!m_display) {
00039 std::cout << "Can't open display." << std::endl;
00040 return false;
00041 }
00042
00043 m_vinfo = glXChooseVisual(m_display,XDefaultScreen(m_display),attributeList);
00044 if(!m_vinfo) {
00045 std::cout << "Can't choose a visual." << std::endl;
00046 return false;
00047 }
00048
00049 m_ctx = glXCreateContext(m_display,m_vinfo,NULL,GL_FALSE);
00050 if(!m_ctx) {
00051 std::cout << "Can't create a GLX context." << std::endl;
00052 return false;
00053 }
00054
00055 if(m_privateColormap) {
00056
00057 m_colormap = XCreateColormap(m_display,XDefaultRootWindow(m_display),
00058 m_vinfo->visual, AllocNone);
00059 } else {
00060
00061 m_colormap = XDefaultColormap(m_display,XDefaultScreen(m_display));
00062 }
00063 if(!m_colormap) {
00064 std::cout << "Can't create X colormap." << std::endl;
00065 return false;
00066 }
00067
00068 return true;
00069 }
00070
00071 int OpenGLApp::exec()
00072 {
00073 if(!m_display || !m_ctx) return 0;
00074 glXWaitX ();
00075 while(1) {
00076 XEvent xevent;
00077 if(XPending(m_display)) {
00078 XNextEvent (m_display,&xevent);
00079 if(xevent.type==ConfigureNotify) {
00080 OpenGLWindow* view = findView(xevent.xconfigure.window);
00081 view->resize((int)xevent.xconfigure.width,
00082 (int)xevent.xconfigure.height);
00083 }
00084 }
00085 }
00086 return 0;
00087 }
00088
00089 Display* OpenGLApp::getXDisplay() { return m_display; }
00090 Colormap OpenGLApp::getXColormap() { return m_colormap; }
00091 XVisualInfo* OpenGLApp::getXVisualInfo() { return m_vinfo; }
00092 GLXContext OpenGLApp::getGLXContext() { return m_ctx; }
00093
00094 OpenGLWindow* OpenGLApp::findView(Window aWindow) const {
00095 for(unsigned int index=0;index<fViews.size();index++){
00096 OpenGLWindow* view = fViews[index];
00097 if(aWindow==view->window()) return view;
00098 }
00099 return 0;
00100 }