Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GL/glut.h>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <list>
- #include <vector>
- #include <unistd.h>
- void display();
- void idle();
- void resize(int w, int h);
- void keyboardEvent(unsigned char key, int x, int y);
- void init();
- GLuint framebuffername;
- GLuint texturename;
- GLuint renderbuffername;
- GLuint color;
- cv::Mat image;
- int windowWidth, windowHeight;
- GLfloat triangle[] = {
- 100.0f, 100.0f, 0.0f, 100.0f/1440.0f, 100.0f/1080.0f,
- 500.0f, 900.0f, 0.0f, 500.0f/1440.0f, 900.0f/1080.0f,
- 1000.0f, 800.0f, 0.0f, 1000.0f/1440.0f, 800.0f/1080.0f
- };
- GLuint tvbo;
- int main(int argc, char* argv[]) {
- if(argc < 2) return 1;
- std::string filename = argv[1];
- image = cv::imread(filename.c_str(), CV_LOAD_IMAGE_COLOR);
- std::cout << image.rows << " " << image.cols << " " << image.step << " " << image.dataend-image.datastart << " " << image.cols*image.rows*3 << std::endl;
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
- glutInitWindowSize(image.cols, image.rows);
- glutCreateWindow("Test");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutIdleFunc(idle);
- glutKeyboardFunc(keyboardEvent);
- glewExperimental = GL_TRUE;
- GLenum glewerr = glewInit();
- if(GLEW_OK != glewerr) {
- std::cerr << glewGetErrorString(glewerr) << std::endl;
- }
- init();
- glutMainLoop();
- return 0;
- }
- void init() {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- framebuffername = 0;
- glGenFramebuffers(1, &framebuffername);
- glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
- texturename = 0;
- glGenTextures(1, &texturename);
- glBindTexture(GL_TEXTURE_2D, texturename);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, image.data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glBindTexture(GL_TEXTURE_2D, 0);
- color = 0;
- glGenTextures(1, &color);
- glBindTexture(GL_TEXTURE_2D, color);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.cols, image.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
- glBindTexture(GL_TEXTURE_2D, 0);
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- renderbuffername = 0;
- glGenRenderbuffers(1, &renderbuffername);
- /*glBindRenderbuffer(GL_RENDERBUFFER, renderbuffername);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, image.cols, image.rows);
- glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffername);*/
- //glGenBuffers(1, &tvbo);
- //glBindBuffer(GL_ARRAY_BUFFER, tvbo);
- //glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
- }
- void display() {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texturename);
- glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
- if(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)) {
- } else std::cout << "Framebuffer is NOT complete" << std::endl;
- glPushAttrib(GL_VIEWPORT_BIT);
- glViewport(0, 0, image.cols, image.rows);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, image.cols, image.rows, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glBegin(GL_TRIANGLES);
- glTexCoord2f(100.0f/image.cols, 100.0f/image.rows);
- glVertex3i(100, 100, 0);
- glTexCoord2f(500.0f/image.cols, 900.0f/image.rows);
- glVertex3i(500, 900, 0);
- glTexCoord2f(1000.0f/image.cols, 800.0f/image.rows);
- glVertex3i(1000, 800, 0);
- glEnd();
- glFlush();
- glPopAttrib();
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- glBindTexture(GL_TEXTURE_2D, 0);
- std::fill(image.datastart, image.dataend, 0);
- glReadBuffer(GL_COLOR_ATTACHMENT0);
- glReadPixels(0, 0, image.cols, image.rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
- glutSwapBuffers();
- }
- void idle() {
- glutPostRedisplay();
- }
- void resize(int w, int h) {
- windowWidth = w;
- windowHeight = h;
- glutPostRedisplay();
- }
- void keyboardEvent(unsigned char key, int x, int y) {
- (void)x;
- (void)y;
- if(27 == key) {
- //glReadPixels(0, 0, image.cols, image.rows, GL_RGB, GL_UNSIGNED_BYTE, image.data);
- //std::fill(image.datastart, image.dataend, 0);
- cv::imwrite("./output.jpg", image);
- glDeleteBuffers(1, &framebuffername);
- glDeleteBuffers(1, &renderbuffername);
- glDeleteBuffers(1, &texturename);
- glDeleteBuffers(1, &color);
- exit(0);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment