Guest User

OpenGL/Framebuffer

a guest
Apr 9th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/glut.h>
  3. #include <opencv2/core/core.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <opencv2/imgproc/imgproc.hpp>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <string>
  11. #include <list>
  12. #include <vector>
  13. #include <unistd.h>
  14.  
  15. void display();
  16. void idle();
  17. void resize(int w, int h);
  18. void keyboardEvent(unsigned char key, int x, int y);
  19. void init();
  20.  
  21. GLuint framebuffername;
  22. GLuint texturename;
  23. GLuint renderbuffername;
  24. GLuint color;
  25. cv::Mat image;
  26. int windowWidth, windowHeight;
  27.  
  28. GLfloat triangle[] = {
  29.     100.0f, 100.0f, 0.0f, 100.0f/1440.0f, 100.0f/1080.0f,
  30.     500.0f, 900.0f, 0.0f, 500.0f/1440.0f, 900.0f/1080.0f,
  31.     1000.0f, 800.0f, 0.0f, 1000.0f/1440.0f, 800.0f/1080.0f
  32. };
  33.  
  34. GLuint tvbo;
  35.  
  36.  
  37. int main(int argc, char* argv[]) {
  38.  
  39.     if(argc < 2) return 1;
  40.  
  41.     std::string filename = argv[1];
  42.    
  43.     image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_COLOR);
  44.  
  45.     std::cout << image.rows << " " << image.cols << " " << image.step << " " << image.dataend-image.datastart << " " << image.cols*image.rows*3 << std::endl;
  46.  
  47.     glutInit(&argc, argv);
  48.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
  49.     glutInitWindowSize(image.cols, image.rows);
  50.     glutCreateWindow("Test");
  51.  
  52.     glutReshapeFunc(resize);
  53.     glutDisplayFunc(display);
  54.     glutIdleFunc(idle);
  55.     glutKeyboardFunc(keyboardEvent);
  56.  
  57.     glewExperimental = GL_TRUE;
  58.     GLenum glewerr = glewInit();
  59.     if(GLEW_OK != glewerr) {
  60.         std::cerr << glewGetErrorString(glewerr) << std::endl;
  61.     }
  62.  
  63.     init();
  64.     glutMainLoop();
  65.  
  66.     return 0;
  67. }
  68.  
  69. void init() {
  70.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  71.  
  72.     framebuffername = 0;
  73.     glGenFramebuffers(1, &framebuffername);
  74.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
  75.  
  76.     texturename = 0;
  77.     glGenTextures(1, &texturename);
  78.     glBindTexture(GL_TEXTURE_2D, texturename);
  79.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, image.data);
  80.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  81.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  82.  
  83.     glBindTexture(GL_TEXTURE_2D, 0);
  84.  
  85.     color = 0;
  86.     glGenTextures(1, &color);
  87.     glBindTexture(GL_TEXTURE_2D, color);
  88.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  89.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  90.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  91.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
  92.  
  93.     glBindTexture(GL_TEXTURE_2D, 0);
  94.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  95.  
  96.     renderbuffername = 0;
  97.     glGenRenderbuffers(1, &renderbuffername);
  98.     /*glBindRenderbuffer(GL_RENDERBUFFER, renderbuffername);
  99.     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, image.cols, image.rows);
  100.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
  101.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffername);*/
  102.  
  103.     //glGenBuffers(1, &tvbo);
  104.     //glBindBuffer(GL_ARRAY_BUFFER, tvbo);
  105.     //glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
  106. }
  107.  
  108. void display() {
  109.  
  110.     glEnable(GL_TEXTURE_2D);
  111.     glBindTexture(GL_TEXTURE_2D, texturename);
  112.     glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
  113.  
  114.     if(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)) {
  115.     } else std::cout << "Framebuffer is NOT complete" << std::endl;
  116.  
  117.     glPushAttrib(GL_VIEWPORT_BIT);
  118.     glViewport(0, 0, image.cols, image.rows);
  119.  
  120.     glClear(GL_COLOR_BUFFER_BIT);
  121.     glMatrixMode(GL_PROJECTION);
  122.     glLoadIdentity();
  123.     glOrtho(0, image.cols, image.rows, 0, -1, 1);
  124.     glMatrixMode(GL_MODELVIEW);
  125.     glLoadIdentity();
  126.  
  127.     glBegin(GL_TRIANGLES);
  128.     glTexCoord2f(100.0f/image.cols, 100.0f/image.rows);
  129.     glVertex3i(100, 100, 0);
  130.     glTexCoord2f(500.0f/image.cols, 900.0f/image.rows);
  131.     glVertex3i(500, 900, 0);
  132.     glTexCoord2f(1000.0f/image.cols, 800.0f/image.rows);
  133.     glVertex3i(1000, 800, 0);
  134.     glEnd();
  135.     glFlush();
  136.     glPopAttrib();
  137.  
  138.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  139.     glBindTexture(GL_TEXTURE_2D, 0);
  140.  
  141.     std::fill(image.datastart, image.dataend, 0);
  142.     glReadBuffer(GL_COLOR_ATTACHMENT0);
  143.     glReadPixels(0, 0, image.cols, image.rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
  144.     glutSwapBuffers();
  145. }
  146.  
  147. void idle() {
  148.     glutPostRedisplay();
  149. }
  150.  
  151. void resize(int w, int h) {
  152.     windowWidth = w;
  153.     windowHeight = h;
  154.     glutPostRedisplay();
  155. }
  156.  
  157. void keyboardEvent(unsigned char key, int x, int y) {
  158.     (void)x;
  159.     (void)y;
  160.     if(27 == key) {
  161.         //glReadPixels(0, 0, image.cols, image.rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
  162.         //std::fill(image.datastart, image.dataend, 0);
  163.  
  164.         cv::imwrite("./output.jpg", image);
  165.  
  166.         glDeleteBuffers(1, &framebuffername);
  167.         glDeleteBuffers(1, &renderbuffername);
  168.         glDeleteBuffers(1, &texturename);
  169.         glDeleteBuffers(1, &color);
  170.         exit(0);
  171.     }
  172.     return;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment