forked from baiyang/opengl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
182 lines (132 loc) · 3.58 KB
/
main.cpp
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
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <GL/glut.h>
#include <vector>
using namespace std;
#include "math3d.h"
#include "Selection.h"
Selection selection;
M3DVector2f left_bottom, right_top;
bool bool_select_area = false;
int viewport[4];
static M3DVector3f corners[] = {
-25, 25, 25,
25, 25, 25,
25, -25, 25,
-25, -25, 25,
-25, 25, -25,
25, 25, -25,
25, -25, -25,
-25, -25, -25
};
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
M3DMatrix44f mat_proj, mat_modelview;
int width = glutGet( GLUT_WINDOW_WIDTH ), height = glutGet( GLUT_WINDOW_HEIGHT );
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glGetIntegerv(GL_VIEWPORT, viewport);
glClear (GL_COLOR_BUFFER_BIT);
glPushAttrib(GL_POLYGON_BIT);
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) width/(GLfloat) height, 1.0, 300);
// 获取投影矩阵
glGetFloatv(GL_PROJECTION_MATRIX, mat_proj);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 70, 0, 0, 0, 0, 1, 0);
glColor4f(0.1, 0.4, 0.6, 0.7);
glPushMatrix();
// 获取模型视图矩阵
glGetFloatv(GL_MODELVIEW_MATRIX, mat_modelview);
glBegin( GL_QUADS );
glVertex3fv(corners[0]);
glVertex3fv(corners[1]);
glVertex3fv(corners[5]);
glVertex3fv(corners[4]);
glVertex3fv(corners[4]);
glVertex3fv(corners[7]);
glVertex3fv(corners[3]);
glVertex3fv(corners[0]);
glVertex3fv(corners[3]);
glVertex3fv(corners[2]);
glVertex3fv(corners[6]);
glVertex3fv(corners[7]);
glVertex3fv(corners[7]);
glVertex3fv(corners[6]);
glVertex3fv(corners[5]);
glVertex3fv(corners[4]);
glVertex3fv(corners[5]);
glVertex3fv(corners[1]);
glVertex3fv(corners[2]);
glVertex3fv(corners[6]);
glVertex3fv(corners[1]);
glVertex3fv(corners[2]);
glVertex3fv(corners[3]);
glVertex3fv(corners[0]);
glEnd();
glPopMatrix();
glPopAttrib();
// 配置
selection.set_config( corners, 8, left_bottom, right_top, mat_modelview, mat_proj, viewport);
/************************************************************************/
/* 构造一个新的环境 */
/************************************************************************/
if( bool_select_area ){
selection.draw_area();
selection.highlight_selected_pts();
}
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
}
void mouse(int button, int state, int x, int y)
{
int width = glutGet( GLUT_WINDOW_WIDTH ), height = glutGet( GLUT_WINDOW_HEIGHT );
if( button == GLUT_LEFT_BUTTON ){
if(state == GLUT_DOWN){
bool_select_area = true;
}else if ( state == GLUT_UP)
{
bool_select_area = false;
}
m3dLoadVector2(left_bottom, x, height - y);
m3dLoadVector2(right_top, x, height - y);
}
glutPostRedisplay();
}
void motion(int x, int y)
{
int width = glutGet( GLUT_WINDOW_WIDTH ), height = glutGet( GLUT_WINDOW_HEIGHT );
if( bool_select_area ){
m3dLoadVector2(right_top, x, height - y);
}
glutPostRedisplay();
}
/* ARGSUSED1 */
void keyboard (unsigned char key, int x, int y)
{
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}