OpenGL:渲染到多个上下文代码?

时间:2018-02-17 00:09:20

标签: opengl

下面我有以下代码,我尝试启动两个线程,这样我就可以一次渲染我模型的多个转换。

const int renderWidth = 640;
const int renderHeight = 480;

class Renderer{
public:
    Renderer(){

    }

    void startOnThread(){
        // OpenGL - Initialization
        std::cout << "Initializing.." << std::endl;
        char *myargv [1];
        int myargc=1;
        myargv[0]=strdup ("Window");
        glutInit(&myargc, myargv);
        glutInitDisplayMode(GLUT_DOUBLE); // glEndList();Enable double buffered mode
        glutInitWindowSize(renderWidth, renderHeight);   // Set the window's initial width & height
        glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
        glutCreateWindow("Window");          // Create window with the given title
        glutHideWindow();
        glewInit();

        glEnable(GL_LIGHTING);
        glEnable( GL_DEPTH_TEST );
        glShadeModel( GL_SMOOTH );
        glEnable( GL_CULL_FACE );
        glClearColor( 1, 1, 1, 1 );
    }

    cv::Mat draw(Eigen::MatrixXf mV, Eigen::MatrixXf mF){
        struct Vertex{
          GLfloat position[3];
          GLfloat normal[3];
          GLfloat texcoord[2];
        };
        std::vector<Vertex> vertexdata;

        // Iterate v
        for(int i=0; i<mV.rows(); i++){
            Vertex v;
            for(int j=0; j<mV.cols(); j++){
                v.position[j] = mV(i,j);
            }
            vertexdata.push_back(v);
        }

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glEnable(GL_LIGHTING);
        glShadeModel( GL_SMOOTH );
        glEnable( GL_TEXTURE_2D );

        glViewport( 0, 0, (float)renderWidth/1, (float)renderHeight/1. );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 60, (float)renderWidth/(float)renderHeight, 0.1, 10000. );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();

        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
        glPushMatrix();
        glTranslatef(0,-0.5,-0.5);
        for(int i=0; i<mF.rows(); i++){
            glBegin(GL_POLYGON);
            glVertex3fv((const GLfloat*)&vertexdata[mF(i,0)].position);
            glVertex3fv((const GLfloat*)&vertexdata[mF(i,1)].position);
            glVertex3fv((const GLfloat*)&vertexdata[mF(i,2)].position);
            glEnd();
        }
        glPopMatrix();

        // Read buffer test
        std::vector<std::uint8_t> data(renderWidth*renderHeight*4);
        glReadBuffer(GL_BACK);
        glReadPixels(0,0,renderWidth,renderHeight,GL_BGRA,GL_UNSIGNED_BYTE,&data[0]);
        cv::Mat m(cv::Size(renderWidth, renderHeight),CV_8UC4, &data[0]);
        cv::flip(m, m, -1);

        glutSwapBuffers();
        glutPostRedisplay();
        glutMainLoopEvent();

        return m.clone();
    }

    /*
     * Launch 100 threads externally, where each thread starts a renderer, and runs in loop waiting
     *
     */

};

void thread_worker(const SMPL& smpl){
    Renderer r;
    r.startOnThread();

    while(1){
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }


}

int test(){

    SMPL smpl;
    smpl.loadModelFromJSONFile(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/model.json");
    std::cout.setstate(std::ios_base::failbit);
    smpl.updateModel();
    std::cout.clear();

    const int num_threads = 2;
    std::thread t[num_threads];

    //Launch a group of threads
    for (int i = 0; i < num_threads; ++i) {
        t[i] = std::thread(thread_worker, smpl);
    }

    std::cout << "Launched from the main\n";

    //Join the threads with the main thread
    for (int i = 0; i < num_threads; ++i) {
        t[i].join();
    }
}

不幸的是,运行此代码会导致崩溃,

Initializing..
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
test: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)

我明白我需要以某种方式渲染屏幕。但是,设置帧缓冲区对象仍然需要我调用glutInitWindow ..这仍然会导致崩溃。我该如何解决这个问题

0 个答案:

没有答案