|
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 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
38 glPushMatrix (); |
|
39 glTranslatef ( |
|
40 (g_BBox.v0.x + g_BBox.v1.x) / -2.0, |
|
41 (g_BBox.v0.y + g_BBox.v1.y) / -2.0, |
|
42 (g_BBox.v0.z + g_BBox.v1.z) / -2.0 |
|
43 ); |
|
44 |
|
45 // glTranslatef (0.0f, 0.0f, -5.0f); |
|
46 glTranslatef (0.0f, 0.0f, fZoom); |
|
47 |
|
48 // glScalef (0.75f, 1.15f, 0.0f); |
|
49 glRotatef (fRotX, 1.0f, 0.0f, 0.0f); |
|
50 glRotatef (fRotY, 0.0f, 1.0f, 0.0f); |
|
51 glRotatef (fRotZ, 0.0f, 0.0f, 1.0f); |
|
52 |
|
53 glMatrixMode (GL_MODELVIEW); |
|
54 |
|
55 glCallList (objlist); |
|
56 glColor3f (0.0, 0.5, 1.0); |
|
57 glPopMatrix (); |
|
58 } |
|
59 |
|
60 void renderer::CompileObjects () { |
|
61 printf ("compile all objects\n"); |
|
62 |
|
63 objlist = glGenLists (1); |
|
64 glNewList (objlist, GL_COMPILE); |
|
65 |
|
66 if (!g_CurrentFile) { |
|
67 printf ("renderer: no files loaded, cannot compile anything\n"); |
|
68 return; |
|
69 } |
|
70 |
|
71 for (ulong i = 0; i < g_CurrentFile->objects.size(); i++) |
|
72 CompileOneObject (g_CurrentFile->objects[i]); |
|
73 |
|
74 glEndList (); |
|
75 } |
|
76 |
|
77 #define GL_VERTEX(V) \ |
|
78 glVertex3d (V.x, V.y, V.z); |
|
79 |
|
80 void renderer::CompileOneObject (LDObject* obj) { |
|
81 if (!obj) |
|
82 return; |
|
83 |
|
84 switch (obj->getType ()) { |
|
85 case OBJ_CondLine: // For now, treat condlines like normal lines. |
|
86 case OBJ_Line: |
|
87 { |
|
88 glColor3f (0.0f, 0.0f, 0.0f); // Draw all lines black for now |
|
89 // draw lines |
|
90 LDLine* line = static_cast<LDLine*> (obj); |
|
91 glBegin (GL_LINES); |
|
92 for (short i = 0; i < 2; ++i) |
|
93 GL_VERTEX (line->vaCoords[i]) |
|
94 glEnd (); |
|
95 } |
|
96 break; |
|
97 |
|
98 case OBJ_Triangle: |
|
99 { |
|
100 LDTriangle* tri = static_cast<LDTriangle*> (obj); |
|
101 glColor3f (0.5f, 0.5f, 0.5f); // Draw all polygons gray for now |
|
102 glBegin (GL_TRIANGLES); |
|
103 for (short i = 0; i < 3; ++i) |
|
104 GL_VERTEX (tri->vaCoords[i]) |
|
105 glEnd (); |
|
106 } |
|
107 break; |
|
108 |
|
109 case OBJ_Quad: |
|
110 { |
|
111 LDQuad* quad = static_cast<LDQuad*> (obj); |
|
112 glColor3f (0.5f, 0.5f, 0.5f); |
|
113 glBegin (GL_QUADS); |
|
114 for (short i = 0; i < 4; ++i) |
|
115 GL_VERTEX (quad->vaCoords[i]) |
|
116 glEnd (); |
|
117 } |
|
118 break; |
|
119 |
|
120 default: |
|
121 break; |
|
122 } |
|
123 } |
|
124 |
|
125 void renderer::ClampAngle (double& fAngle) { |
|
126 while (fAngle < 0) |
|
127 fAngle += 360.0; |
|
128 while (fAngle > 360.0) |
|
129 fAngle -= 360.0; |
|
130 } |
|
131 |
|
132 void renderer::mouseMoveEvent (QMouseEvent *event) { |
|
133 int dx = event->x () - lastPos.x (); |
|
134 int dy = event->y () - lastPos.y (); |
|
135 |
|
136 if (event->buttons () & Qt::LeftButton) { |
|
137 fRotX = fRotX + (dy); |
|
138 fRotY = fRotY + (dx); |
|
139 ClampAngle (fRotX); |
|
140 ClampAngle (fRotY); |
|
141 } |
|
142 |
|
143 if (event->buttons () & Qt::RightButton) { |
|
144 fRotX = fRotX + (dy); |
|
145 fRotZ = fRotZ + (dx); |
|
146 ClampAngle (fRotX); |
|
147 ClampAngle (fRotZ); |
|
148 } |
|
149 |
|
150 if (event->buttons () & Qt::MidButton) { |
|
151 fZoom += (dy / 100.0); |
|
152 fZoom = clamp (fZoom, 0.1, 10.0); |
|
153 } |
|
154 |
|
155 printf ("%.3f %.3f %.3f %.3f\n", |
|
156 fRotX, fRotY, fRotZ, fZoom); |
|
157 lastPos = event->pos(); |
|
158 updateGL (); |
|
159 } |