1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
int nb_proj;
double screen_width;
double screen_height;
double rect_width = 0;
double rect_height = 0;
double rect_top_left_x = 0;
double rect_top_left_y = 0;
int Win1;
// des points que je vais afficher
std::vector<CvPoint2D64d> * the_points;
// mon main...
// (en fait mon application openGL est appellée par un autre code, c pr ça que c pas un main)
int start(std::vector<CvPoint2D64d> * points, int nb_projectors, double screen_width, double screen_height)
{
// initialize the global variables
nb_proj = nb_projectors;
::screen_width = screen_width;
::screen_height = screen_height;
the_points = new std::vector<CvPoint2D64d> (*points);
// g mis n'importe quoi ici...
int arg1 = 1;
char * arg2 = "window";
glutInit(&arg1, &arg2);
glutInitWindowSize(640, 480);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
Win1 = glutCreateWindow("Display Wall Simlator");
glShadeModel(GL_FLAT);
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
glutKeyboardFunc(myGlutKeyboard);
glutMouseFunc(myGlutMouse);
printf("press 'H' for help\n");
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////////////
void Reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, float(width)/float(height), 1, 5000);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
////////////////////////////////////////////////////////////////////////////
void Draw()
{
glClearColor(.65, .65, .65, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 3200, 0, 0, 0, 0, 1, 0);
glTranslated(-screen_width/2, screen_height/2, 0);
glColor4f(0.0f, 1.0f, 0.0f, 0.0f);
int i = 0;
for (i = 0; i < nb_proj; i++)
{
glBegin(GL_LINE_LOOP);
for (int j = 0; j < 4; j++)
glVertex2d(the_points->at(j + 4*i).x, -the_points->at(j + 4*i).y);
glEnd();
}
glColor4f(0.0f, 0.0f, 1.0f, 0.0f);
glBegin(GL_LINE_LOOP);
glVertex2d(0, 0);
glVertex2d(screen_width, 0);
glVertex2d(screen_width, -screen_height);
glVertex2d(0, -screen_height);
glEnd();
printf("DRAW : rect_top_left_x = %g, rect_top_left_y = %g\n", rect_top_left_x, rect_top_left_y);
printf("\trect_width = %g, rect_height = %g\n\n", rect_width, rect_height);
glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
glTranslated(rect_top_left_x, rect_top_left_y, 0);
glBegin(GL_LINE_LOOP); // GL_LINE_LOOP
glVertex2d(0, 0);
glVertex2d(rect_width, 0);
glVertex2d(rect_width, -rect_height);
glVertex2d(0, -rect_height);
glEnd();
glutSwapBuffers();
}
void myGlutKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
// move the center of the rectangle
case 's':
rect_top_left_x--;
glutPostRedisplay();
break;
case 'd':
rect_top_left_x++;
glutPostRedisplay();
break;
// etc...
default:
break;
}
glutPostRedisplay();
}
void myGlutMouse(int button, int button_state, int x, int y )
{
double ox, oy, oz;
double projectionMatrix[16], modelviewMatrix[16];
int viewport[4];
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
double wy = viewport[3] - y; //le depth buffer est inversé en y
//par rapport à ce que l'on voit à l'écran
double wx = x;
double wz;
glReadPixels((int)wx, (int)wy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &wz);
gluUnProject(wx, wy, wz, modelviewMatrix, projectionMatrix, viewport, &ox, &oy, &oz);
printf("ox = %g, oy = %g, oz = %g\n", ox, oy, oz);
if (button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN)
{
rect_top_left_x = ox;
rect_top_left_y = oy;
}
if (button == GLUT_LEFT_BUTTON && button_state == GLUT_UP)
{
rect_width = fabs(ox - rect_top_left_x);
rect_height = fabs(oy - rect_top_left_y);
}
printf("MOUSE : rect_top_left_x = %g, rect_top_left_y = %g\n", rect_top_left_x, rect_top_left_y);
printf("\trect_width = %g, rect_height = %g\n\n", rect_width, rect_height);
glutPostRedisplay();
} |
Partager