summaryrefslogtreecommitdiff
path: root/src/gui/GLWidget.cc
blob: 391c5f5f6d727f22151a5b103a95dc89945bdbd6 (plain) (blame)
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
#include <QtOpenGL>
#include <math.h>
#include "GLWidget.h"
#include "util.h"

void torus(int numc, int numt, double fraction = 1 , double R = 1, double r = 0.1) {
   int i, j, k;
   double s, t, x, y, z, twopi;

   twopi = 2 * M_PI;
   for (i = 0; i < numc; i++) {
      glBegin(GL_QUAD_STRIP);
      for (j = 0; j <= numt / fraction ; j++) {
         for (k = 1; k >= 0; k--) {
            s = (i + k) % numc + 0.5;
            t = j % numt;

            x = (R+r*cos(s*twopi/numc))*cos(t*twopi/numt);
            y = (R+r*cos(s*twopi/numc))*sin(t*twopi/numt);
            z = r * sin(s * twopi / numc);
            glColor3d(0, s/numc, t/numc);
            glVertex3f(x, y, z);
         }
      }
      glEnd();
   }
}



void axes() {
	glBegin(GL_LINES);
	glColor3d(1, 0, 0);
	glVertex3d(0, 0, 0); // origin of the line
	glVertex3d(100, 0, 0); // ending point of the line
	glEnd();
	glBegin(GL_LINES);
	glColor3d(0, 1, 0);
	glVertex3d(0, 0, 0); // origin of the line
	glVertex3d(0, 100, 0); // ending point of the line
	glEnd();
	glBegin(GL_LINES);
	glColor3d(0, 0, 1);
	glVertex3d(0, 0, 0); // origin of the line
	glVertex3d(0, 0, 100); // ending point of the line
	glEnd();

}


GLWidget::GLWidget (QWidget* parent)
	: QGLWidget (parent)
{
	eyeX = 0;
	eyeY = 0;
	eyeZ = 10;
	resize (sizeHint ());
}

GLWidget::~GLWidget () {}

QSize GLWidget::minimumSizeHint () const {
	return QSize (50, 50);
}

QSize GLWidget::sizeHint () const {
	return QSize (800, 600);
}

void GLWidget::initializeGL () {
	glClearColor (0, 0, 0, 1.0);
	glEnable (GL_DEPTH_TEST);
	gluPerspective(65.0, 4.0/3, 1.0, 1000.0);
}

void GLWidget::paintGL () {
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity ();
	gluLookAt(eyeX,eyeY,eyeZ,
		0,0,0,
		0,1000,0);
	//glTranslated (0.0, 0.0, -10.0);
	//glColor3d (0.0, 0.0, 1.0);
	//glScaled (300.0, 300.0, 300.0);

	glBegin (GL_POLYGON);
	glColor3d(1,0,0);
	glVertex3d (-300, -300, 0.0);
	glColor3d(0,1,0);
	glVertex3d (300 + 10, -300, 0.0);
	glColor3d(0,0,1);
	glVertex3d (0, 300, 0.0);
	glColor3d(1,1,1);
	glVertex3d (0, 0, 100);
	glColor3d(0,1,0);
	glVertex3d (300 + 10, -300, 0.0);
	glEnd ();


	axes();

	glColor3d(0,0,1);
	glScaled(300,300,300);

	glPolygonMode(GL_FRONT, GL_LINE);
	glPolygonMode(GL_BACK, GL_LINE);
	torus(12, 20, 4, 1, 0.1);

	vhc::util::cylinder(0.1, 0, 2, 12, 20);

	glPolygonMode(GL_FRONT, GL_FILL);
	glPolygonMode(GL_BACK, GL_FILL);

}

void GLWidget::resizeGL (int width, int height) {
	glViewport (0, 0, width, height);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho (-width, width, -height, height, -1000.0, 1000.0);
	glMatrixMode (GL_MODELVIEW);
}

void GLWidget::mousePressEvent (QMouseEvent* event) {
}

void GLWidget::mouseReleaseEvent (QMouseEvent* event) {
}

void GLWidget::keyPressEvent (QKeyEvent* event) {
	switch (event->key()) {
		case Qt::Key_Escape:
			qApp->quit();
			break;
		case Qt::Key_Up:
			eyeZ += 10;
			break;
		case Qt::Key_Down:
			eyeZ -= 10;
			break;
		case Qt::Key_Right:
			eyeX += 10;
			break;
		case Qt::Key_Left:
			eyeX -= 10;
			break;
		case Qt::Key_R:
			eyeX = 0;
			eyeY = 0;
			eyeZ = 10;
			break;
		default:
			break;
	}
	repaint();
}

void GLWidget::keyReleaseEvent (QKeyEvent* event) {

}