forked from gnudles/stereograma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glmodelview.cpp
312 lines (283 loc) · 9.16 KB
/
glmodelview.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "glmodelview.h"
#include "stereomaker.h"
#include "imageviewer.h"
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>
#include <QGLShader>
#include <QGLShaderProgram>
#include <QMessageBox>
#include <QProcess>
#include <QCoreApplication>
#include "modeldepthviewer.h"
#include "trirender.h"
GlModelView::GlModelView(QWidget *parent) :
QGLWidget(parent),m_zoom(500),m_contrast(100)
{
m_antialias=true;
m_noShaders=false;
m_new_width=1024;
m_new_height=768;
BasicImageWidget::setBasicImageParent(parent);
}
void GlModelView::initializeGL()
{
if (!context()->isValid())
{
QMessageBox::warning(this,"No OpenGL Driver","Depth Map generation is still available");
}
if (QGLShader::hasOpenGLShaders(QGLShader::Vertex,context()) )
{
QString declarations= "//uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ProjectionMatrix; attribute vec4 gl_Vertex;\n";
QString codev= "void main() {gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"\
"float zmax=-1.0;"\
"float zmin=1.0;"\
"float scale=(zmax-zmin);float offs=-zmin;float z=(gl_Position.z/gl_Position.w); gl_FrontColor = vec4((z+offs)/scale,(z+offs)/scale,(z+offs)/scale,1.0); } ";
QGLShader shaderv(QGLShader::Vertex);
bool compile_success=shaderv.compileSourceCode(declarations+codev);
if (!compile_success)
{
compile_success=shaderv.compileSourceCode(codev);
}
if (compile_success)
{
QGLShaderProgram program(context());
program.addShader(&shaderv);
program.link();
program.bind();
}
else
m_noShaders=true;
}
else
m_noShaders=true;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // This Will Clear The Background Color To Black
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
// Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
m_rotx=0;
m_roty=0;
m_rotz=0;
}
void GlModelView::setModel(Model3D * model)
{
m_model = model;
}
void GlModelView::setParams(float xoff,float yoff,float zoom,float contrast,float xrot,float yrot,float zrot,float scale)
{
m_zoom=zoom;
m_contrast=contrast;
m_scale=scale;
m_rotx=xrot;
m_roty=yrot;
m_rotz=zrot;
m_xoffset=xoff;
m_yoffset=yoff;
setPerspective();
}
void GlModelView::setZoom(float zoom)
{
m_zoom=zoom;
setPerspective();
}
void GlModelView::setScale(float scale)
{
m_scale=scale;
setPerspective();
}
void GlModelView::setContrast(float contrast)
{
m_contrast=contrast;
setPerspective();
}
typedef struct
{
unsigned int numPoints;
unsigned int numTriangles;
unsigned int raster_width;
unsigned int raster_height;
float rotate_x;
float rotate_y;
float rotate_z;
float x_offset;
float y_offset;
float zoom;
float contrast;
} raster_prefs_t;
const QImage* GlModelView::snapShot()
{
unsigned int out_width=((ModelDepthViewer*)m_parent)->getOutputImageWidth();
unsigned int out_height=((ModelDepthViewer*)m_parent)->getOutputImageHeight();
out_width=out_width&0xfffffffc;
unsigned int numPoints=m_model->numPoints();
unsigned int numTriangles=m_model->numTriangles();
const float *points=m_model->pointsData();
const unsigned int *triangles=m_model->trianglesData();
unsigned char * buffer=new unsigned char[out_height*out_width];
float real_scale= pow(2,m_scale/1000);
trirender(buffer, out_width, out_height, points, numPoints,triangles, numTriangles, m_rotx, m_roty, m_rotz,m_zoom,m_contrast,m_xoffset,m_yoffset,real_scale);
QByteArray result = QByteArray((const char*)buffer,out_width * out_height);
imdata=QImage(out_width,out_height,QImage::Format_Indexed8);
imdata=imdata.convertToFormat(QImage::Format_Indexed8,StereoMaker::getGrayScale());
memcpy(imdata.bits(),result.data(),result.size());
imdata=imdata.mirrored();
delete [] buffer;
return &imdata;
/*raster_prefs_t rprefs;
int outputSize=out_width * out_height * sizeof(GLubyte);
const float *points=m_model->pointsData();
const unsigned int *triangles=m_model->trianglesData();
rprefs.numPoints=m_model->numPoints();
rprefs.numTriangles=m_model->numTriangles();
unsigned int out_width=((ModelDepthViewer*)m_parent)->getOutputImageWidth();
unsigned int out_height=((ModelDepthViewer*)m_parent)->getOutputImageHeight();
out_width=out_width&0xfffffffc;
rprefs.raster_width=out_width;
rprefs.raster_height=out_height;
rprefs.rotate_x=m_rotx;
rprefs.rotate_y=m_roty;
rprefs.rotate_z=m_rotz;
rprefs.x_offset=m_xoffset;
rprefs.y_offset=m_yoffset;
rprefs.zoom=m_zoom;
rprefs.contrast=m_contrast;
QProcess renderer;
QString rasterizer_path=QCoreApplication::applicationDirPath ()+"/m3draster";
renderer.start(rasterizer_path);
if (!renderer.waitForStarted())
{
QMessageBox::warning(this,"Error","can't open "+rasterizer_path);
return 0;
}
renderer.write((char*)&rprefs,sizeof(raster_prefs_t));
renderer.waitForBytesWritten();
int remaining=rprefs.numPoints*3*sizeof(float);
int bytes_per_write=256*3*sizeof(float);
int written=0;
while (remaining>=bytes_per_write)
{
renderer.write(((char*)points)+written,bytes_per_write);
remaining-=bytes_per_write;
written+=bytes_per_write;
renderer.waitForBytesWritten();
}
if (remaining>0)
{
renderer.write(((char*)points)+written,remaining);
renderer.waitForBytesWritten();
}
remaining=rprefs.numTriangles*3*sizeof(unsigned int);
bytes_per_write=256*3*sizeof(unsigned int);
written=0;
while (remaining>=bytes_per_write)
{
renderer.write(((char*)triangles)+written,bytes_per_write);
remaining-=bytes_per_write;
written+=bytes_per_write;
renderer.waitForBytesWritten();
}
if (remaining>0)
{
renderer.write(((char*)triangles)+written,remaining);
renderer.waitForBytesWritten();
}
imdata=QImage();
if (!renderer.waitForFinished())
return 0;
QByteArray result = renderer.readAll();
int outputSize=out_width * out_height * sizeof(GLubyte);
if ((unsigned int)result.size()==outputSize)
{
imdata=QImage(out_width,out_height,QImage::Format_Indexed8);
imdata=imdata.convertToFormat(QImage::Format_Indexed8,StereoMaker::getGrayScale());
memcpy(imdata.bits(),result.data(),result.size());
imdata.invertPixels();
imdata=imdata.mirrored();
}
return &imdata;*/
}
void print_matrix(int mattype)
{
//usage: print_matrix(GL_MODELVIEW_MATRIX);
//or: print_matrix(GL_PROJECTION_MATRIX);
GLfloat matrix[16];
glGetFloatv (mattype, matrix);
for (int i=0; i<16; ++i)
{
printf("%f ",matrix[(i%4)*4+(i/4)]);
if (i%4==3)
printf("\n");
}
printf("\n");
fflush(0);
}
void GlModelView::setPerspective()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0f,(GLfloat)m_new_width/(GLfloat)m_new_height,m_zoom-m_contrast,m_zoom+m_contrast); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
void GlModelView::resizeGL( int width, int height )
{
unsigned int out_width=((ModelDepthViewer*)m_parent)->getOutputImageWidth();
unsigned int out_height=((ModelDepthViewer*)m_parent)->getOutputImageHeight();
setViewport(out_width,out_height);
}
GlModelView::~GlModelView()
{
if (m_model)
delete m_model;
}
void GlModelView::setViewport(int width_, int height_)
{
if (height_==0) // Prevent A Divide By Zero By
{
height_=1; // Making Height Equal One
}
float hratio=(float)height_/height();
float wratio=(float)width_/width();
float ratio=qMax(hratio,wratio);
m_new_width=width_/ratio;
if (m_new_width>width())
m_new_width=width();
m_new_height=height_/ratio;
if (m_new_height>height())
m_new_height=height();
glViewport((width()-m_new_width)/2,(height()-m_new_height)/2,m_new_width,m_new_height); // Reset The Current Viewport
setPerspective();
}
void GlModelView::paintGL()
{
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);//clear with white...
if (m_antialias)
glEnable(GL_MULTISAMPLE);
//make black bacground...
glPushMatrix();
double large_val=10000;
glTranslatef(0,0,-m_zoom-m_contrast+0.01);
glColor3b(255,0,0);
glBegin(GL_QUADS);
glVertex2d(large_val,large_val);
glVertex2d(-large_val,large_val);
glVertex2d(-large_val,-large_val);
glVertex2d(large_val,-large_val);
glEnd();
glPopMatrix();
//till here
if (m_model)
{
glPushMatrix();
glTranslatef(m_xoffset,m_yoffset,-m_zoom);
float real_scale= pow(2,m_scale/1000);
glScalef(real_scale,real_scale,1.0);
glRotatef(m_rotx,1,0,0);
glRotatef(m_roty,0,1,0);
glRotatef(m_rotz,0,0,1);
m_model->DrawGL(m_noShaders);
glPopMatrix();
}
glDisable(GL_MULTISAMPLE);
}