-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelview.cpp
195 lines (172 loc) · 5.29 KB
/
modelview.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
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "modelview.h"
ModelView::ModelView(MainModel* model,QWidget *parent): QOpenGLWidget(parent)
{
this->setModel(model);
this->camera = new CCamera;
}
ModelView::ModelView(const ModelView& input):QOpenGLWidget(),QOpenGLFunctions()
{
// this->window_height = input.window_height;
// this->window_width = input.window_width;
this->model = input.model;
this->camera = input.camera;
}
void ModelView::setModel(MainModel* model)
{
this->model = model;
}
MainModel* ModelView::getModel_p(void)
{
return this->model;
}
void ModelView::initializeGL()
{
this->initializeOpenGLFunctions();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//Specify the back of the buffer as clear depth
glClearDepth(1.0f);
SetupLighting();
//Enable depth testing
glDepthFunc (GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
void ModelView::SetupLighting()
{
GLfloat light_pos[] = {2.0f, 2.0f, 2.0f, 1.0f};
GLfloat light_Ka[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat light_Kd[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_Ks[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
}
void ModelView::resizeGL(int w, int h)
{
glViewport(0.0f,0.0f,w,h);
window_width = w;
window_height = h;
}
void ModelView::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
for(int loop = 0; loop < 3; loop++)
{
//Initialize backgroud drawing
if(loop == 0&&backgroud_enable)
{
//Set size of background to fill the whole client area
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, window_width, window_height, 0);
}
//Initialize object drawing
if(loop == 1)
{
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.0, 0.01f, 200.0f);
}
//Initialize coordinate indicator drawing
if(loop == 2&& coordinate_enable)
{
glViewport(17*window_width/20, window_height/32,
window_width/6, window_height/6);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.0, 0.01f, 200.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT);
//Draw backgroud
if(loop == 0&&backgroud_enable)
{
glDisable(GL_LIGHTING);
glBegin(GL_POLYGON);
glColor3f(0.4f, 0.7f, 0.55f);
glVertex2f(window_width, 0);
glVertex2f(0, 0);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0, window_height);
glVertex2f(window_width, window_height);
glEnd();
glEnable(GL_LIGHTING);
}
// Draw object
if(loop == 1)
{
camera->UpdateView(1);
if(model->Get_full_index()!=0)
{
QList<uint> ID_list = model->Get_ID_list();
QListIterator<uint>it(ID_list);
while(it.hasNext())
{
glCallList(it.next());
}
}
}
//Draw coordinate indicator
if(loop == 2&& coordinate_enable)
{
camera->UpdateView();
glDisable(GL_LIGHTING);
//Draw x axis
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.2f, 0.0f, 0.0f);
glLineWidth(3);
glEnd();
//Draw y axis
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.2f, 0.0f);
glLineWidth(3);
glEnd();
//Draw z axis
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.2f);
glLineWidth(3);
glEnd();
glColor3ub(129, 129, 129);
glRasterPos3f(1.3f, 0.0f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'X');
glRasterPos3f(0.0f, 1.3f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'Y');
glRasterPos3f(0.0f, 0.0f, 1.3f);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'Z');
glEnable(GL_LIGHTING);
}
}
glFlush();
}
void ModelView::mousePressEvent(QMouseEvent *event)
{
camera->SetMouseDownPoint(event->pos());
}
void ModelView::mouseReleaseEvent(QMouseEvent *event)
{
camera->SetMouseDownPoint(QPoint(0,0));
}
void ModelView::mouseMoveEvent(QMouseEvent *event)
{
camera->UpdateAngle(event->pos());
this->update();
}
void ModelView::wheelEvent(QWheelEvent *event)
{
camera->UpdateDistance(event->delta());
this->update();
}