tychoBCG3D  0.2
 All Data Structures Namespaces Files Functions Variables
glwidget.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  Copyright (C) 2002-2011 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.3.17.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #include "glwidget.h"
24 #include <math.h>
25 #include <QDebug>
26 
27 // Constructor must call the base class constructor.
28 #if QT_VERSION < 0x040000
29 GLWidget::GLWidget(QWidget *parent, const char *name)
30  : QGLViewer(parent, name)
31  #else
32 GLWidget::GLWidget(QWidget *parent)
33  : QGLViewer(parent)
34  #endif
35 {
36 
37 }
38 
45 {
46 
47  setSceneRadius(10.0);
48  setFPSIsDisplayed(true);
49 
50  setBackgroundColor(backroundColor);
51 
52  //Add ambient light
53  GLfloat ambientColor[] = {redAmbient, greenAmbient, blueAmbient}; //Color (0.2, 0.2, 0.2)
54  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
55 
56  // Light setup
57  glEnable(GL_LIGHTING); //Enable lighting
58  glEnable(GL_NORMALIZE); //Have OpenGL automatically normalize our normals
59  glShadeModel(GL_SMOOTH); //Enable smooth shading
60 }
61 
67 {
68 
69  //to draw point data
70  if ((coordiantes[0].size() > 0)&&(drawer == 1)){
71 
72  glPointSize((float)point_size);
73  glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
74  glEnable(GL_BLEND);
75  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
76  glAlphaFunc(GL_GREATER, 0.5);
77  glEnable(GL_DEPTH_TEST);
78 
79  GLfloat white[] = {redAmbient, greenAmbient, blueAmbient, 1.0f};
80  glMaterialfv(GL_FRONT, GL_SPECULAR, white);
81  GLfloat shininess1[] = {shininess};
82  glMaterialfv(GL_FRONT, GL_SHININESS, shininess1);
83 
84  glBegin(GL_POINTS);
85  for (int i = 0; i < coordiantes[0].size(); i++){
86  glColor3f(redModel,greenModel,blueModel);
87  glNormal3f((GLfloat)coordiantes[3][i], (GLfloat)coordiantes[4][i],(GLfloat)coordiantes[5][i]);
88  glVertex3f((GLfloat)coordiantes[0][i], (GLfloat)coordiantes[1][i],(GLfloat)coordiantes[2][i]);
89 
90  }
91  glEnd();
92 
93  if (coordiantes[0].size() != 0) draw_border(x_min, x_max, y_min, y_max, z_min, z_max);
94 
95  glDisable(GL_DEPTH_TEST);
96  }
97 
98  //to draw stl data
99  if ((stl_data[0].size() > 0)&&(drawer == 1)){
100 
101  glEnable(GL_DEPTH_TEST);
102  glPolygonMode(GL_FRONT_AND_BACK, GL_TRIANGLES );
103 
104  GLfloat white[] = {redAmbient, greenAmbient, blueAmbient, 1.0f};
105  glMaterialfv(GL_FRONT, GL_SPECULAR, white);
106  GLfloat shininess1[] = {shininess};
107  glMaterialfv(GL_FRONT, GL_SHININESS, shininess1);
108 
109  for (int i = 0; i < stl_data[0].size()-4; i = i+4){
110 
111 
112  glBegin(GL_TRIANGLES);
113  glNormal3f((GLfloat)stl_data[0][i], (GLfloat)stl_data[1][i], (GLfloat)stl_data[2][i]);
114  glColor3f(redModel,greenModel,blueModel);
115  glVertex3f((GLfloat)stl_data[0][i+1], (GLfloat)stl_data[1][i+1], (GLfloat)stl_data[2][i+1]);
116  glVertex3f((GLfloat)stl_data[0][i+2], (GLfloat)stl_data[1][i+2], (GLfloat)stl_data[2][i+2]);
117  glVertex3f((GLfloat)stl_data[0][i+3], (GLfloat)stl_data[1][i+3], (GLfloat)stl_data[2][i+3]);
118  glEnd();
119  }
120 
121  if (stl_data[0].size() != 0) draw_border(x_min, x_max, y_min, y_max, z_min, z_max);
122 
123  glDisable(GL_DEPTH_TEST);
124  }
125 
126 
127  //to draw the sampled data
128  if ((sampled_data[0].size() > 0)&&(drawer == 2)){
129 
130  glPointSize((float)point_size);
131  glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
132  glEnable(GL_BLEND);
133  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
134  glAlphaFunc(GL_GREATER, 0.5);
135  glEnable(GL_DEPTH_TEST);
136 
137  GLfloat white[] = {redAmbient, greenAmbient, blueAmbient, 1.0f};
138  glMaterialfv(GL_FRONT, GL_SPECULAR, white);
139  GLfloat shininess1[] = {shininess};
140  glMaterialfv(GL_FRONT, GL_SHININESS, shininess1);
141 
142  glBegin(GL_POINTS);
143  for (int i = 0; i < sampled_data[0].size(); i++){
144  glColor3f(redModel,greenModel,blueModel);
145  glVertex3f((GLfloat)sampled_data[0][i], (GLfloat)sampled_data[1][i],(GLfloat)sampled_data[2][i]);
146 
147  }
148  glEnd();
149 
150  if (sampled_data[0].size() != 0) draw_border(x_min, x_max, y_min, y_max, z_min, z_max);
151 
152  glDisable(GL_DEPTH_TEST);
153  }
154 
155 }
156 
157 
169 void GLWidget::draw_border(float x_min, float x_max, float y_min, float y_max, float z_min, float z_max){
170 
171  glEnable(GL_LINE_SMOOTH);
172  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
173  glLineWidth(1.0f);
174  glColor3f(redModel,greenModel,blueModel);
175 
176  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE );
177  glEnable(GL_DEPTH_TEST);
178  glBegin(GL_QUADS);
179 
180  glVertex3f(x_max, y_max, z_max);
181  glVertex3f(x_min, y_max, z_max);
182  glVertex3f(x_min, y_min, z_max);
183  glVertex3f(x_max, y_min, z_max);
184 
185  glVertex3f(x_max, y_max, z_min);
186  glVertex3f(x_min, y_max, z_min);
187  glVertex3f(x_min, y_min, z_min);
188  glVertex3f(x_max, y_min, z_min);
189 
190  glVertex3f(x_max, y_max, z_max);
191  glVertex3f(x_max, y_min, z_max);
192  glVertex3f(x_max, y_min, z_min);
193  glVertex3f(x_max, y_max, z_min);
194 
195  glVertex3f(x_min, y_max, z_max);
196  glVertex3f(x_min, y_min, z_max);
197  glVertex3f(x_min, y_min, z_min);
198  glVertex3f(x_min, y_max, z_min);
199 
200  glVertex3f(x_max, y_max, z_max);
201  glVertex3f(x_min, y_max, z_max);
202  glVertex3f(x_min, y_max, z_min);
203  glVertex3f(x_max, y_max, z_min);
204 
205  glVertex3f(x_max, y_min, z_max);
206  glVertex3f(x_min, y_min, z_max);
207  glVertex3f(x_min, y_min, z_min);
208  glVertex3f(x_max, y_min, z_min);
209 
210  glEnd();
211 
212  glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
213  glDisable(GL_LINE_SMOOTH);
214  glDisable(GL_DEPTH_TEST);
215 }