-
Notifications
You must be signed in to change notification settings - Fork 0
/
glwidget.cpp
155 lines (137 loc) · 3.56 KB
/
glwidget.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
#include "glwidget.h"
#include <QDebug>
std::vector<Layer<double> > layers;
Layer<double>* selected_layer;
double rotate_y;
double rotate_x;
bool show_axis;
bool show_grid;
bool show_wave;
double cam_radius;
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL(){
rotate_y=0;
rotate_x=0;
show_axis=true;
show_grid=true;
show_wave=false;
cam_radius=2.0;
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST | GL_LINE_SMOOTH);
}
void GLWidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,0.01f,650.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0.0, cam_radius, cam_radius,
0.0, 0.0, 0.0,
0.0, 0.0, 1.0);
glViewport(0, 0, width, height);
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 0.0, 1.0 );
draw_layers();
if (show_grid)
draw_grid(-0.5,-0.5,0.1,0.1,11,11,&guide_z,&guide_col);
if (show_axis)
draw_axis();
glFlush();
}
void GLWidget::resizeGL(int width_, int height_){
width = width_;
height = height_;
}
void GLWidget::open_graphic(std::string file){
std::string line;
std::ifstream infile(file.c_str());
while (std::getline(infile, line)){
std::istringstream iss(line);
std::string layer_file;
std::getline(iss,layer_file,';');
layer_file = file.substr(0,file.rfind("/"))+"/"+layer_file;
std::string color_string;
std::getline(iss,color_string,',');
int rgb = (int)strtol(color_string.c_str(), NULL, 16);
int r = double((rgb&0xFF0000)>>16)/255.0;
int g = double((rgb&0x00FF00)>>8)/255.0;
int b = double(rgb&0x0000FF)/255.0;
open_layer(layer_file,r,g,b);
}
}
void GLWidget::open_layer(std::string file, double r, double g, double b){
Layer<double> layer(file, r, g, b);
layers.push_back(layer);
updateGL();
}
void GLWidget::draw_quad(vec a, vec b, vec c, vec d){
glBegin(GL_POLYGON);
glVertex3f(a.x, a.y, a.z);
glVertex3f(b.x, b.y, b.z);
glVertex3f(c.x, c.y, c.z);
glVertex3f(d.x, d.y, d.z);
glEnd();
};
void GLWidget::draw_grid(double x,double y,double dx,double dy,int w,int h,double(*z)(const vec&),vec(*col)(const vec&)){
for (int i=0; i<w - 1; i++){
for (int j=0; j<h - 1; j++){
vec color(col(vec(i,j,0)));
glColor3f(color.x, color.y, color.z);
int I = i+1;
int J = j+1;
float pi = x+i*dx;
float pj = y+j*dy;
float pI = x+I*dx;
float pJ = y+J*dy;
draw_quad(
vec(pi,pj,z(vec(i,j))),
vec(pI,pj,z(vec(I,j))),
vec(pI,pJ,z(vec(I,J))),
vec(pi,pJ,z(vec(i,J))));
};
};
}
void GLWidget::draw_line(const vec & a, const vec & b){
glBegin(GL_LINES);
glVertex3d(a.x,a.y,a.z);
glVertex3d(b.x,b.y,b.z);
glEnd();
}
void GLWidget::draw_axis(){
glColor3f(1.0,0.0,0.0); draw_line(vec(0.0,0.0,0.0),vec(1.0,0.0,0.0));
glColor3f(0.0,1.0,0.0); draw_line(vec(0.0,0.0,0.0),vec(0.0,1.0,0.0));
glColor3f(0.0,0.0,1.0); draw_line(vec(0.0,0.0,0.0),vec(0.0,0.0,1.0));
}
double layer_z(const vec& pos){
return selected_layer->z(pos.x,pos.y);
}
vec layer_col(const vec & pos){
double c=0.5+double(pos.x)*0.05 + double(int(pos.x+pos.y)%2)*0.1;
return vec(selected_layer->r*c,selected_layer->g*c,selected_layer->b*c);
}
double guide_z(const vec & pos){
return 0;
}
vec guide_col(const vec & pos){
double add=int(pos.x+pos.y)%2*0.2;
return vec(0.5+add,0.5+add,0.5+add);
}
void GLWidget::draw_layers(){
for (unsigned int i=0; i<layers.size(); ++i){
selected_layer = &layers[i];
draw_grid(
layers[i].x,
layers[i].y,
layers[i].dx,
layers[i].dy,
layers[i].dimx,
layers[i].dimy,
layer_z,
layer_col);
}
}