Calc texcoord为一些像素图

时间:2017-11-16 17:49:22

标签: c opengl x11 xlib

我在Xephyr中有一个应用程序的pixmap,我需要在矩形openGL上绘制这个像素图。我在代码中创建的pixmap中看到了像khronos的例子,但它并不适合我:

mPixmap = XCreatePixmap(dpy, client->getWindow(), static_cast<unsigned int>(attr.width),
                        static_cast<unsigned int>(attr.height), static_cast<unsigned int>(attr.depth));

我画的代码:

void startPluginPixmap() {

    XEvent xev;
    GLuint texture_id;

    GLint pix_att[5] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
    vi = glXChooseVisual(dpy, 0, pix_att);

    swa.event_mask = ExposureMask | KeyPressMask;
    swa.colormap = XCreateColormap(dpy, root, vi->visual, AllocNone);

    uint64_t window_width = 300;
    uint64_t window_height = 400;
    uint32_t window_depth = vi->depth;
    win = XCreateWindow(dpy, root, 0, 0, window_width, window_height, 0, window_depth, InputOutput, vi->visual, CWEventMask  | CWColormap, &swa);
    XMapWindow(dpy, win);
    XStoreName(dpy, win, "PIXMAP TEST");

    glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);

    if (!glc) {
        exit(0);
    }

    glXMakeCurrent(dpy, win, glc);
    glEnable(GL_DEPTH_TEST);

    uint64_t pixmap_width = 150;
    uint64_t pixmap_height = 100;
    GC gc = DefaultGC(dpy, 0);
    XFlush(dpy);
    XImage * xim = XGetImage(dpy, mPixmap, 0, 0, pixmap_width, pixmap_height, AllPlanes, ZPixmap);

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap_width, pixmap_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)(&(xim->data[0])));

    XDestroyImage(xim);

    while(true) {
        XNextEvent(dpy, &xev);

        if (xev.type == Expose) {
            redrawPixmapImage();
        }

        else if (xev.type == KeyPress && xev.xkeymap.type == 0x19) {
            glXMakeCurrent(dpy, None, NULL);
            glXDestroyContext(dpy, glc);
            XDestroyWindow(dpy, win);
            XCloseDisplay(dpy);
            exit(0);
        }
    }

}

void Cube::redrawPixmapImage() {

    XWindowAttributes      gwa;

    XGetWindowAttributes(dpy, win, &gwa);
    glViewport(0, 0, gwa.width, gwa.height);
    glClearColor(.0, 1.0, .0, .5);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.25, 1.25, -1.25, 1.25, 1., 20.);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

    glColor3f(1.f, 1.f, 1.f);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0);
    //glColor3f(1.f, 0.f, 0.f);
    glVertex3f(-1.0,  1.0, 0.0);

    glTexCoord2f(1.0, 0.0);
    glVertex3f( 1.0,  1.0, 0.0);

    glTexCoord2f(1.0, 1.0);
    glVertex3f( 1.0, -1.0, 0.0);

    glTexCoord2f(0.0, 1.0);
    glVertex3f(-1.0, -1.0, 0.0);

    glTexCoord2f(1.0, 1.0);
    glVertex3f(-1.0, 1.0, 0.0);
    glEnd();

    glXSwapBuffers(dpy, win);

}

在这段代码中,我输入宽度和高度手册,因为它在调试信息中获取。 对我来说,这个textcoord的计算并不起作用(当我在Xephyr xeyes中运行,然后是这段代码,我看到了文物和绿色区域)。 那么,我如何计算pixmap某些应用的texcoords?

0 个答案:

没有答案