OpenCV_2.2_window_causes_problem_on_OpenGL.cpp

Source code of 'OpenCV 2.2 window causes problem on OpenGL', written on VS2008. however, there is nothing OS depedent except include file - Rudra Poudel, 2011-06-17 09:27 pm

Download (3.4 kB)

 
1
// opengl_example.cpp : Defines the entry point for the console application.
2
//
3
4
#include "stdafx.h"
5
6
//#include <stdlib.h>
7
//#include <windows.h>
8
9
#include "cv.h"
10
#include "highgui.h"
11
12
#include <GL/glut.h>
13
14
15
void init(void) 
16
{
17
   glClearColor (0.0, 0.0, 0.0, 0.0);
18
   glShadeModel (GL_FLAT);
19
   glEnable(GL_CULL_FACE);
20
}
21
22
void display(void)
23
{
24
   glClear (GL_COLOR_BUFFER_BIT);
25
   glColor3f (1.0, 1.0, 1.0);
26
   glLoadIdentity ();             /* clear the matrix */
27
           /* viewing transformation  */
28
  
29
   glTranslated(0, 0, -25);
30
   //glScalef (1.0, 2.0, 1.0);      /* modeling transformation */ 
31
32
   glBegin(GL_QUADS);                // Draw A Quad
33
    glVertex3f(-10.0f, 10.0f, 0.0f);                                // Top Left
34
    glVertex3f(-10.0f,-10.0f, 0.0f);                                // Bottom Left
35
    glVertex3f( 10.0f,-10.0f, 0.0f);                                // Bottom Right
36
    glVertex3f( 10.0f, 10.0f, 0.0f);                                // Top Right
37
  glEnd();
38
   
39
   glFlush ();
40
}
41
42
void reshape (int w, int h)
43
{
44
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
45
   
46
   glMatrixMode (GL_PROJECTION);
47
   glLoadIdentity ();
48
   gluPerspective(90, (double)w/h, 5.0, 400.0);
49
   
50
   glMatrixMode (GL_MODELVIEW);
51
   gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -400.0, 0.0, 1.0, 0.0);
52
}
53
54
void keyboard(unsigned char key, int x, int y)
55
{
56
   switch (key) {
57
      case 27:
58
         exit(0);
59
         break;
60
   }
61
}
62
63
int main(int argc, char** argv)
64
{
65
  int screenWidth = 512, screenHeight = 512;
66
67
  glutInit(&argc, argv);
68
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
69
  glutInitWindowSize (screenWidth, screenHeight); 
70
  glutInitWindowPosition (100, 100);
71
  glutCreateWindow (argv[0]);
72
  init ();
73
  //glutDisplayFunc(display); 
74
  //glutReshapeFunc(reshape);
75
  //glutKeyboardFunc(keyboard);
76
77
  //glutMainLoop();
78
79
  reshape (screenWidth, screenHeight);
80
  
81
  int error_code = glGetError();  // Error Code: 0, NO Error
82
  glDrawBuffer(GL_FRONT);
83
  display();
84
  //glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
85
  cv::Mat image1;
86
  image1.create(screenHeight,screenWidth, CV_8UC3);
87
  glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, (uchar*)image1.data);
88
  int error_code1 = glGetError();  // Error Code: 0, NO Error
89
  //glPopClientAttrib();
90
  const char *title = "glReadPixels";
91
  cv::namedWindow(title);  
92
  int error_code2 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
93
  //cv::flip(image1, image1, 0);
94
  cv::imshow(title, image1);
95
  int error_code3 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
96
  cv::waitKey(2000);
97
  int error_code4 = glGetError();  // Error Code: 0, NO Error
98
  cv::destroyWindow(title);        // IMP: however if I will comment this line then below/2nd display will work!!!
99
  int error_code5 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
100
  image1.release();
101
102
  int error_code6 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
103
104
  glDrawBuffer(GL_FRONT);
105
  int error_code7 = glGetError();// Error Code: 1282, GL_INVALID_OPERATION
106
107
  //Do it again!
108
  display();
109
  //glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
110
  cv::Mat image2;
111
  image2.create(screenHeight,screenWidth, CV_8UC3);
112
  glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, (uchar*)image2.data);
113
  int error_code8 = glGetError();  // Error Code: 0, NO Error
114
  //cv::flip(image2, image2, 0);
115
  cv::imshow(title, image2);
116
  cv::waitKey(5000);
117
118
119
120
   
121
   return 0;
122
}