Sat, 16 Mar 2013 03:12:31 +0200
oops, forgot the new dialog files out
0 | 1 | #include <QtGui> |
2 | #include <QGLWidget> | |
3 | #include "common.h" | |
4 | #include "io.h" | |
5 | #include "draw.h" | |
6 | #include "bbox.h" | |
7 | ||
8 | renderer::renderer (QWidget* parent) { | |
9 | parent = parent; // shhh, GCC | |
10 | fRotX = fRotY = fRotZ = 0.0; | |
11 | fZoom = 1.0; | |
12 | } | |
13 | ||
14 | void renderer::initializeGL () { | |
15 | glLoadIdentity(); | |
16 | glMatrixMode (GL_MODELVIEW); | |
17 | glClearColor (0.8f, 0.8f, 0.85f, 1.0f); | |
18 | glEnable (GL_DEPTH_TEST); | |
19 | glShadeModel (GL_SMOOTH); | |
20 | glEnable (GL_MULTISAMPLE); | |
21 | ||
22 | CompileObjects (); | |
23 | } | |
24 | ||
25 | void renderer::hardRefresh () { | |
26 | CompileObjects (); | |
27 | paintGL (); | |
28 | } | |
29 | ||
30 | void renderer::resizeGL (int w, int h) { | |
31 | glViewport (0, 0, w, h); | |
32 | } | |
33 | ||
34 | void renderer::paintGL () { | |
35 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
36 | ||
37 | glPushMatrix (); | |
38 | glTranslatef ( | |
39 | (g_BBox.v0.x + g_BBox.v1.x) / -2.0, | |
40 | (g_BBox.v0.y + g_BBox.v1.y) / -2.0, | |
41 | (g_BBox.v0.z + g_BBox.v1.z) / -2.0 | |
42 | ); | |
43 | ||
44 | // glTranslatef (0.0f, 0.0f, -5.0f); | |
45 | glTranslatef (0.0f, 0.0f, fZoom); | |
46 | ||
47 | // glScalef (0.75f, 1.15f, 0.0f); | |
48 | glRotatef (fRotX, 1.0f, 0.0f, 0.0f); | |
49 | glRotatef (fRotY, 0.0f, 1.0f, 0.0f); | |
50 | glRotatef (fRotZ, 0.0f, 0.0f, 1.0f); | |
51 | ||
52 | glMatrixMode (GL_MODELVIEW); | |
53 | ||
54 | glCallList (objlist); | |
55 | glColor3f (0.0, 0.5, 1.0); | |
56 | glPopMatrix (); | |
57 | } | |
58 | ||
59 | void renderer::CompileObjects () { | |
60 | printf ("compile all objects\n"); | |
61 | ||
62 | objlist = glGenLists (1); | |
63 | glNewList (objlist, GL_COMPILE); | |
64 | ||
65 | if (!g_CurrentFile) { | |
66 | printf ("renderer: no files loaded, cannot compile anything\n"); | |
67 | return; | |
68 | } | |
69 | ||
70 | for (ulong i = 0; i < g_CurrentFile->objects.size(); i++) | |
71 | CompileOneObject (g_CurrentFile->objects[i]); | |
72 | ||
73 | glEndList (); | |
74 | } | |
75 | ||
76 | #define GL_VERTEX(V) \ | |
77 | glVertex3d (V.x, V.y, V.z); | |
78 | ||
79 | void renderer::CompileOneObject (LDObject* obj) { | |
80 | if (!obj) | |
81 | return; | |
82 | ||
83 | switch (obj->getType ()) { | |
84 | case OBJ_CondLine: // For now, treat condlines like normal lines. | |
85 | case OBJ_Line: | |
86 | { | |
87 | glColor3f (0.0f, 0.0f, 0.0f); // Draw all lines black for now | |
88 | // draw lines | |
89 | LDLine* line = static_cast<LDLine*> (obj); | |
90 | glBegin (GL_LINES); | |
91 | for (short i = 0; i < 2; ++i) | |
92 | GL_VERTEX (line->vaCoords[i]) | |
93 | glEnd (); | |
94 | } | |
95 | break; | |
96 | ||
97 | case OBJ_Triangle: | |
98 | { | |
99 | LDTriangle* tri = static_cast<LDTriangle*> (obj); | |
100 | glColor3f (0.5f, 0.5f, 0.5f); // Draw all polygons gray for now | |
101 | glBegin (GL_TRIANGLES); | |
102 | for (short i = 0; i < 3; ++i) | |
103 | GL_VERTEX (tri->vaCoords[i]) | |
104 | glEnd (); | |
105 | } | |
106 | break; | |
107 | ||
108 | case OBJ_Quad: | |
109 | { | |
110 | LDQuad* quad = static_cast<LDQuad*> (obj); | |
111 | glColor3f (0.5f, 0.5f, 0.5f); | |
112 | glBegin (GL_QUADS); | |
113 | for (short i = 0; i < 4; ++i) | |
114 | GL_VERTEX (quad->vaCoords[i]) | |
115 | glEnd (); | |
116 | } | |
117 | break; | |
118 | ||
119 | default: | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | void renderer::ClampAngle (double& fAngle) { | |
125 | while (fAngle < 0) | |
126 | fAngle += 360.0; | |
127 | while (fAngle > 360.0) | |
128 | fAngle -= 360.0; | |
129 | } | |
130 | ||
131 | void renderer::mouseMoveEvent (QMouseEvent *event) { | |
132 | int dx = event->x () - lastPos.x (); | |
133 | int dy = event->y () - lastPos.y (); | |
134 | ||
135 | if (event->buttons () & Qt::LeftButton) { | |
136 | fRotX = fRotX + (dy); | |
137 | fRotY = fRotY + (dx); | |
138 | ClampAngle (fRotX); | |
139 | ClampAngle (fRotY); | |
140 | } | |
141 | ||
142 | if (event->buttons () & Qt::RightButton) { | |
143 | fRotX = fRotX + (dy); | |
144 | fRotZ = fRotZ + (dx); | |
145 | ClampAngle (fRotX); | |
146 | ClampAngle (fRotZ); | |
147 | } | |
148 | ||
149 | if (event->buttons () & Qt::MidButton) { | |
150 | fZoom += (dy / 100.0); | |
151 | fZoom = clamp (fZoom, 0.1, 10.0); | |
152 | } | |
153 | ||
154 | printf ("%.3f %.3f %.3f %.3f\n", | |
155 | fRotX, fRotY, fRotZ, fZoom); | |
156 | lastPos = event->pos(); | |
157 | updateGL (); | |
158 | } |