]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/render_area_2d.cxx
Merge branch 'maint' into next
[flightgear.git] / src / Instrumentation / render_area_2d.cxx
1 // RenderArea2D.cxx - a class to manage 2D polygon-based drawing
2 //                    for a complex instrument (eg. GPS).
3 //
4 // Written by David Luff, started 2005.
5 //
6 // Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include "render_area_2d.hxx"
30
31 static const float dummy_normals[][3] = {{0.0f, 0.0f, 0.0f},
32                                          {0.0f, 0.0f, 0.0f},
33                                          {0.0f, 0.0f, 0.0f},
34                                          {0.0f, 0.0f, 0.0f}};
35
36 RA2DPrimitive::RA2DPrimitive() {
37         invert = false;
38         debug = false;
39 }
40                                                                                  
41 RenderArea2D::RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy) {
42         _logx = logx;
43         _logy = logy;
44         _sizex = sizex;
45         _sizey = sizey;
46         _posx = posx;
47         _posy = posy;
48         _clipx1 = 0;
49         _clipx2 = _logx - 1;
50         _clipy1 = 0;
51         _clipy2 = _logy - 1;
52         
53         _backgroundColor[0] = 0.0;
54         _backgroundColor[1] = 0.0;
55         _backgroundColor[2] = 0.0;
56         _backgroundColor[3] = 1.0;
57         _pixelColor[0] = 1.0;
58         _pixelColor[1] = 0.0;
59         _pixelColor[2] = 0.0;
60         _pixelColor[3] = 1.0;
61         
62         _ra2d_debug = false;
63 }
64
65 void RenderArea2D::draw() {
66 #if 0
67     glDisable(GL_TEXTURE_2D);
68         /*
69     glColor3f(1, 1, 0);
70         
71         float x1 = _posx;
72         float x2 = _posx + _sizex;
73         float y1 = _posy;
74         float y2 = _posy + _sizey;
75         
76         glBegin(GL_LINE_LOOP);
77     glVertex2f(x1, y1);
78     glVertex2f(x1, y2);
79     glVertex2f(x2, y2);
80     glVertex2f(x2, y1);
81     glEnd();
82         */
83         oldDrawBackground();
84         
85         for(unsigned int i = 0; i < drawing_list.size(); ++i) {
86                 RA2DPrimitive prim = drawing_list[i];
87                 switch(prim.type) {
88                 case RA2D_LINE:
89                         oldDrawLine(prim.x1, prim.y1, prim.x2, prim.y2);
90                         break;
91                 case RA2D_QUAD:
92                         if(prim.debug) {
93                                 //cout << "Clipping = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
94                                 //cout << "Drawing quad " << prim.x1 << ", " << prim.y1 << " to " << prim.x2 << ", " << prim.y2 << '\n';
95                         }
96                         oldDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
97                         break;
98                 case RA2D_PIXEL:
99                         oldDrawPixel(prim.x1, prim.y1, prim.invert);
100                         break;
101                 }
102         }
103         
104         glEnable(GL_TEXTURE_2D);
105 #endif
106 }
107
108 // Set clipping region in logical units
109 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
110         _clipx1 = x1;
111         _clipx2 = x2;
112         _clipy1 = y1;
113         _clipy2 = y2;
114         //cout << "Set clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
115 }
116
117 // Set clip region to be the same as the rendered area (default)
118 void RenderArea2D::ResetClipRegion() {
119         _clipx1 = 0;
120         _clipx2 = _logx - 1;
121         _clipy1 = 0;
122         _clipy2 = _logy - 1;
123         //cout << "Reset clip region, clip region = "  << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
124 }
125
126 void RenderArea2D::SetPosition(int posx, int posy) {
127         _posx = posx;
128         _posy = posy;
129 }
130
131 void RenderArea2D::SetLogicalSize(int logx, int logy) {
132         _logx = logx;
133         _logy = logy;
134 }
135
136 void RenderArea2D::SetActualSize(int sizex, int sizey) {
137         _sizex = sizex;
138         _sizey = sizey;
139 }
140
141 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
142         // Clipping is currently performed in oldDrawPixel - could clip here instead though.
143
144         RA2DPrimitive prim;
145         prim.x1 = x;
146         prim.y1 = y;
147         prim.x2 = 0;
148         prim.y2 = 0;
149         prim.type = RA2D_PIXEL;
150         prim.invert = invert;
151         drawing_list.push_back(prim);
152 }
153
154 void RenderArea2D::oldDrawPixel(int x, int y, bool invert) {
155         // Clip
156         if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
157         
158         // Scale to position within background
159         float fx1 = (float)x, fy1 = (float)y;
160         float rx = (float)_sizex / (float)_logx;
161         float ry = (float)_sizey / (float)_logy;
162         fx1 *= rx;
163         fy1 *= ry;
164         float fx2 = fx1 + rx;
165         float fy2 = fy1 + ry;
166         
167         // Translate to final position
168         fx1 += (float)_posx;
169         fx2 += (float)_posx;
170         fy1 += (float)_posy;
171         fy2 += (float)_posy;
172         
173         //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
174         
175         doSetColor(invert ? _backgroundColor : _pixelColor);
176         sgVec2 corners[4];
177         sgSetVec2(corners[0], fx1, fy1);
178         sgSetVec2(corners[1], fx2, fy1);
179         sgSetVec2(corners[2], fx2, fy2);
180         sgSetVec2(corners[3], fx1, fy2);
181         //cout << "Drawing pixel, x,y is " << x << ", " << y << ", fx is [x1,x2,y1,y2] " << fx1 << ", " << fx2 << ", " << fy1 << ", " << fy2 << '\n';
182         doDrawQuad(&corners[0], dummy_normals);
183 }
184
185 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
186         RA2DPrimitive prim;
187         prim.x1 = x1;
188         prim.y1 = y1;
189         prim.x2 = x2;
190         prim.y2 = y2;
191         prim.type = RA2D_LINE;
192         prim.invert = false;
193         drawing_list.push_back(prim);
194 }
195
196 void RenderArea2D::oldDrawLine(int x1, int y1, int x2, int y2) {
197         // Crude implementation of Bresenham line drawing algorithm.
198         
199         // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
200         if(x2 < x1) {
201                 int tmp_x = x1;
202                 int tmp_y = y1;
203                 x1 = x2;
204                 y1 = y2;
205                 x2 = tmp_x;
206                 y2 = tmp_y;
207         }
208         
209         bool flip_y = (y1 > y2 ? true : false);
210         int dx = x2 - x1;
211         int dy = (flip_y ? y1 - y2 : y2 - y1); 
212         if(dx > dy) {
213                 // push the x dir
214                 int y = y1;
215                 int yn = dx/2;
216                 for(int x=x1; x<=x2; ++x) {
217                         DrawPixel(x, y);
218                         yn += dy;
219                         if(yn >= dx) {
220                                 yn -= dx;
221                                 y = (flip_y ? y - 1 : y + 1);
222                         }
223                 }
224         } else {
225                 // push the y dir
226                 int x = x1;
227                 int xn = dy/2;
228                 // Must be a more elegant way to roll the next two cases into one!
229                 if(flip_y) {
230                         for(int y=y1; y>=y2; --y) {
231                                 DrawPixel(x, y);
232                                 xn += dx;
233                                 if(xn >= dy) {
234                                         xn -= dy;
235                                         x++;
236                                 }
237                         }
238                 } else {
239                         for(int y=y1; y<=y2; ++y) {
240                                 DrawPixel(x, y);
241                                 xn += dx;
242                                 if(xn >= dy) {
243                                         xn -= dy;
244                                         x++;
245                                 }
246                         }
247                 }
248         }
249 }
250
251 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
252         // Clip and sanity-check.
253         if(x1 > x2) {
254                 int x = x2;
255                 x2 = x1;
256                 x1 = x;
257         }
258         if(y1 > y2) {
259                 int y = y2;
260                 y2 = y1;
261                 y1 = y;
262         }
263         x1 = x1 < _clipx1 ? _clipx1 : x1;
264         if(x1 > _clipx2) { return; }
265         x2 = x2 > _clipx2 ? _clipx2 : x2;
266         if(x2 < _clipx1) { return; }
267         y1 = y1 < _clipy1 ? _clipy1 : y1;
268         if(y1 > _clipy2) { return; }
269         y2 = y2 > _clipy2 ? _clipy2 : y2;
270         if(y2 < _clipy1) { return; }
271         
272         RA2DPrimitive prim;
273         prim.x1 = x1;
274         prim.y1 = y1;
275         prim.x2 = x2;
276         prim.y2 = y2;
277         prim.type = RA2D_QUAD;
278         prim.invert = invert;
279         if(_ra2d_debug) prim.debug = true;
280         drawing_list.push_back(prim);
281 }
282
283 void RenderArea2D::oldDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
284         // Scale to position within background
285         float fx1 = (float)x1, fy1 = (float)y1;
286         float fx2 = (float)x2, fy2 = (float)y2;
287         float rx = (float)_sizex / (float)_logx;
288         float ry = (float)_sizey / (float)_logy;
289         fx1 *= rx;
290         fy1 *= ry;
291         fx2 *= rx;
292         fy2 *= ry;
293         
294         fx2 += rx;
295         fy2 += ry;
296         
297         // Translate to final position
298         fx1 += (float)_posx;
299         fx2 += (float)_posx;
300         fy1 += (float)_posy;
301         fy2 += (float)_posy;
302         
303         //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
304         
305         doSetColor(invert ? _backgroundColor : _pixelColor);
306         sgVec2 corners[4];
307         sgSetVec2(corners[0], fx1, fy1);
308         sgSetVec2(corners[1], fx2, fy1);
309         sgSetVec2(corners[2], fx2, fy2);
310         sgSetVec2(corners[3], fx1, fy2);
311         doDrawQuad(&corners[0], dummy_normals);
312 }
313
314 void RenderArea2D::DrawBackground() {
315         // TODO
316 }
317
318 void RenderArea2D::oldDrawBackground() {
319         doSetColor(_backgroundColor);
320         sgVec2 corners[4];
321         sgSetVec2(corners[0], (float)_posx, (float)_posy);
322         sgSetVec2(corners[1], (float)(_posx + _sizex), (float)_posy);
323         sgSetVec2(corners[2], (float)(_posx + _sizex), (float)(_posy + _sizey));
324         sgSetVec2(corners[3], (float)_posx, (float)(_posy + _sizey));
325         doDrawQuad(&corners[0], dummy_normals);
326 }
327
328 void RenderArea2D::Flush() {
329         drawing_list.clear();
330 }
331
332 // -----------------------------------------
333 //
334 // Actual drawing routines copied from Atlas
335 //
336 // -----------------------------------------
337
338 void RenderArea2D::doSetColor( const float *rgba ) {
339   //OSGFIXME
340 #if 0
341   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
342   glColor4fv( rgba );
343 #endif
344 }
345
346 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals ) {
347         //cout << "doDrawQuad: " << *p[0] << ", " << *(p[0]+1) << ", " << *p[1] << ", " << *(p[1]+1) << ", " << *p[2] << ", " << *p([2]+1) << ", " << *p[3] << ", " << *p([3]+1) <<'\n';
348   //OSGFIXME
349 #if 0
350   glBegin(GL_QUADS);
351   glNormal3fv( normals[0] ); glVertex2fv( p[0] );
352   glNormal3fv( normals[1] ); glVertex2fv( p[1] );
353   glNormal3fv( normals[2] ); glVertex2fv( p[2] );
354   glNormal3fv( normals[3] ); glVertex2fv( p[3] );
355   glEnd();
356 #endif
357 }
358
359 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals, const sgVec4 *color ) {
360   //OSGFIXME
361 #if 0
362   glBegin(GL_QUADS);
363     glColor4fv( color[0] );glNormal3fv( normals[0] ); glVertex2fv( p[0] );
364     glColor4fv( color[1] );glNormal3fv( normals[1] ); glVertex2fv( p[1] );
365     glColor4fv( color[2] );glNormal3fv( normals[2] ); glVertex2fv( p[2] );
366     glColor4fv( color[3] );glNormal3fv( normals[3] ); glVertex2fv( p[3] );
367   glEnd();
368 #endif
369 }