Created
February 17, 2018 01:15
-
-
Save soulslicer/d7839229733f5530905b6716b9c608e8 to your computer and use it in GitHub Desktop.
OpenGL Thread fail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Renderer{ | |
public: | |
GLFWwindow* window_slave; | |
GLuint fb, rbc, rbd, pbo; | |
struct Vertex{ | |
GLfloat position[3]; | |
GLfloat normal[3]; | |
GLfloat texcoord[2]; | |
}; | |
bool createFrameBuffers() //for worker thread | |
{ | |
bool ret; | |
glGenFramebuffers(1, &fb); | |
glBindFramebuffer(GL_FRAMEBUFFER, fb); | |
glGenRenderbuffers(1, &rbc); | |
glBindRenderbuffer(GL_RENDERBUFFER, rbc); | |
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 2, GL_RGBA8, renderWidth, renderHeight); | |
glGenRenderbuffers(1, &rbd); | |
glBindRenderbuffer(GL_RENDERBUFFER, rbd); | |
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 2, GL_DEPTH24_STENCIL8, renderWidth, renderHeight); | |
glBindRenderbuffer(GL_RENDERBUFFER, rbc); | |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbc); | |
glBindRenderbuffer(GL_RENDERBUFFER, rbd); | |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbd); | |
glGenBuffers(1,&pbo); | |
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo); | |
glBufferData(GL_PIXEL_PACK_BUFFER, renderWidth*renderHeight*4, NULL, GL_DYNAMIC_READ); | |
glFlush(); | |
return ret; | |
} | |
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(); | |
glfwInit(); | |
//initWindow(window_slave,true); | |
glEnable(GL_LIGHTING); | |
glEnable( GL_DEPTH_TEST ); | |
glShadeModel( GL_SMOOTH ); | |
glEnable( GL_CULL_FACE ); | |
glClearColor( 1, 1, 1, 1 ); | |
createFrameBuffers(); | |
cout << "ok" << endl; | |
} | |
cv::Mat draw(){ | |
glBindFramebuffer(GL_FRAMEBUFFER,fb); | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
glFlush(); | |
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(); | |
// Draw? | |
glPushMatrix(); | |
glTranslatef(0,-0.5,-0.5); | |
glBegin(GL_POLYGON); | |
glVertex3f(0,0,-3); | |
glVertex3f(-1,-1,-3); | |
glVertex3f(1,-1,-3); | |
glEnd(); | |
glPopMatrix(); | |
// Read buffer test | |
std::vector<std::uint8_t> data(renderWidth*renderHeight*4); | |
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo); | |
glReadBuffer(GL_BACK); | |
glReadPixels(0,0,renderWidth,renderHeight,GL_BGRA,GL_UNSIGNED_BYTE,&data[0]); | |
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo); //Might not be necessary... | |
cv::Mat m(cv::Size(renderWidth, renderHeight),CV_8UC4, &data[0]); | |
cv::flip(m, m, -1); | |
glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
//glutSwapBuffers(); | |
//glutPostRedisplay(); | |
//glutMainLoopEvent(); | |
return m.clone(); | |
} | |
}; | |
void thread_worker(const SMPL& smpl){ | |
Renderer r; | |
r.startOnThread(); | |
while(1){ | |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | |
} | |
} | |
int test(){ | |
Renderer r; | |
r.startOnThread(); | |
cv::Mat a = r.draw(); | |
while(1){ | |
cv::imshow("win",a); | |
cv::waitKey(15); | |
} | |
// const int num_threads = 1; | |
// 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(); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment