Menu Home

xlib创建一个openGL简单窗口

void CreateWindow(int width, int height) {
	XInitThreads();
	m_scrWidth = width;
	m_scrHeight = height;
	int attr[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_DOUBLEBUFFER, True,
			GLX_DEPTH_SIZE, 16, None, };
	m_dpy = XOpenDisplay(NULL);
	if (m_dpy == NULL) {
		cerr << "XOpenDisplay error" << endl;
		return;
	}
	int nelements;
	GLXFBConfig *fc = glXChooseFBConfig(m_dpy, 0, attr, &nelements);
	if (fc == NULL) {
		cerr << "glXChooseFBConfig error" << endl;
		return;
	}
	XVisualInfo *vi = glXGetVisualFromFBConfig(m_dpy, *fc);
	if (vi == NULL) {
		cerr << "glXGetVisualFromFBConfig error" << endl;
		return;
	}
	Colormap cmap = XCreateColormap(m_dpy, DefaultRootWindow(m_dpy), vi->visual,
			AllocNone);
	XSetWindowAttributes swa;
	swa.colormap = cmap;
	swa.event_mask = ExposureMask | KeyPressMask;
	m_win = XCreateWindow(m_dpy, DefaultRootWindow(m_dpy), 0, 0, m_scrWidth,
			m_scrHeight, 0, vi->depth, InputOutput, vi->visual,
			CWColormap | CWEventMask, &swa);
	XFree(vi);
	/* 显示窗口 */
	XMapWindow(m_dpy, m_win);
	/* 设置窗口的OpenGl属性 */
	m_glc = glXCreateNewContext(m_dpy, *fc, GLX_RGBA_TYPE, NULL, GL_TRUE);
	/* 绑定当前窗口到OpenGl */
	glXMakeContextCurrent(m_dpy, m_win, m_win, m_glc);
	/* 初始化OpenGl */
	initializeGl();
	glXSwapBuffers(m_dpy, m_win);
}

Categories: linux opengl xlib 图形学

Tagged as:

lnmcc

Leave a Reply

Your email address will not be published.