1 // RenderArea2D.cxx - a class to manage 2D polygon-based drawing
2 // for a complex instrument (eg. GPS).
4 // Written by David Luff, started 2005.
6 // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
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.
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.
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.
29 #include "render_area_2d.hxx"
31 RA2DPrimitive::RA2DPrimitive() {
36 RenderArea2D::RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy) {
48 _backgroundColor[0] = 0.0;
49 _backgroundColor[1] = 0.0;
50 _backgroundColor[2] = 0.0;
51 _backgroundColor[3] = 1.0;
60 void RenderArea2D::draw(osg::State& state) {
62 static osg::ref_ptr<osg::StateSet> renderArea2DStateSet;
63 if(!renderArea2DStateSet.valid()) {
64 renderArea2DStateSet = new osg::StateSet;
65 renderArea2DStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
66 renderArea2DStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
69 state.pushStateSet(renderArea2DStateSet.get());
71 state.setActiveTextureUnit(0);
72 state.setClientActiveTextureUnit(0);
74 // DCL - the 2 lines below are copied verbatim from the hotspot drawing code.
75 // I am not sure if they are needed here or not.
76 glPushAttrib(GL_ENABLE_BIT);
77 glDisable(GL_COLOR_MATERIAL);
79 // FIXME - disabling all clip planes causes bleed-through through the splash screen.
80 glDisable(GL_CLIP_PLANE0);
81 glDisable(GL_CLIP_PLANE1);
82 glDisable(GL_CLIP_PLANE2);
83 glDisable(GL_CLIP_PLANE3);
87 for(unsigned int i = 0; i < drawing_list.size(); ++i) {
88 RA2DPrimitive prim = drawing_list[i];
91 oldDrawLine(prim.x1, prim.y1, prim.x2, prim.y2);
95 //cout << "Clipping = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
96 //cout << "Drawing quad " << prim.x1 << ", " << prim.y1 << " to " << prim.x2 << ", " << prim.y2 << '\n';
98 oldDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
101 oldDrawPixel(prim.x1, prim.y1, prim.invert);
110 state.setActiveTextureUnit(0);
111 state.setClientActiveTextureUnit(0);
114 // Set clipping region in logical units
115 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
120 //cout << "Set clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
123 // Set clip region to be the same as the rendered area (default)
124 void RenderArea2D::ResetClipRegion() {
129 //cout << "Reset clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
132 void RenderArea2D::SetPosition(int posx, int posy) {
137 void RenderArea2D::SetLogicalSize(int logx, int logy) {
142 void RenderArea2D::SetActualSize(int sizex, int sizey) {
147 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
149 if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
156 prim.type = RA2D_PIXEL;
157 prim.invert = invert;
158 drawing_list.push_back(prim);
161 void RenderArea2D::oldDrawPixel(int x, int y, bool invert) {
162 // Clip. In theory this shouldn't be necessary, since all input is clipped before adding
163 // to the drawing list, but it ensures that any errors in clipping lines etc will only
164 // spill over the clip area within the instrument, and still be clipped from straying
165 // outside the instrument.
166 if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
168 // Scale to position within background
169 float fx1 = (float)x, fy1 = (float)y;
170 float rx = (float)_sizex / (float)_logx;
171 float ry = (float)_sizey / (float)_logy;
174 float fx2 = fx1 + rx;
175 float fy2 = fy1 + ry;
177 // Translate to final position
183 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
185 doSetColor(invert ? _backgroundColor : _pixelColor);
186 SGVec2f corners[4] = {
195 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
196 // We need to clip the line to the current region before storing it in the drawing
197 // list, since when we come to actually draw it the clip region may have changed.
199 // Liang-Barsky clipping algorithm
201 float u1 = 0.0f, u2 = 1.0f;
202 p[0] = -(x2 - x1); q[0] = (x1 - _clipx1);
203 p[1] = (x2 - x1); q[1] = (_clipx2 - x1);
204 p[2] = -(y2 - y1); q[2] = (y1 - _clipy1);
205 p[3] = (y2 - y1); q[3] = (_clipy2 - y1);
207 for(int i=0; i<4; ++i) {
210 // Then we have a trivial rejection of a line parallel to a clip plane
211 // completely outside the clip region.
214 } else if(p[i] < 0) {
215 float r = (float)q[i]/(float)p[i];
216 u1 = (u1 > r ? u1 : r);
218 float r = (float)q[i]/(float)p[i];
219 u2 = (u2 < r ? u2 : r);
222 // Then the line is completely outside the clip area.
227 float fx1 = x1 + u1 * (float)(x2 - x1);
228 float fy1 = y1 + u1 * (float)(y2 - y1);
229 float fx2 = x1 + u2 * (float)(x2 - x1);
230 float fy2 = y1 + u2 * (float)(y2 - y1);
231 x1 = (int)(fx1 + 0.5);
232 y1 = (int)(fy1 + 0.5);
233 x2 = (int)(fx2 + 0.5);
234 y2 = (int)(fy2 + 0.5);
241 prim.type = RA2D_LINE;
243 drawing_list.push_back(prim);
246 void RenderArea2D::oldDrawLine(int x1, int y1, int x2, int y2) {
247 // Crude implementation of Bresenham line drawing algorithm.
249 // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
259 bool flip_y = (y1 > y2 ? true : false);
261 int dy = (flip_y ? y1 - y2 : y2 - y1);
266 for(int x=x1; x<=x2; ++x) {
271 y = (flip_y ? y - 1 : y + 1);
278 // Must be a more elegant way to roll the next two cases into one!
280 for(int y=y1; y>=y2; --y) {
289 for(int y=y1; y<=y2; ++y) {
301 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
302 // Force the input to be ordered with x1 < x2 and y1 < y2.
314 // Clip the input to the current drawing region.
315 x1 = x1 < _clipx1 ? _clipx1 : x1;
316 if(x1 > _clipx2) { return; }
317 x2 = x2 > _clipx2 ? _clipx2 : x2;
318 if(x2 < _clipx1) { return; }
319 y1 = y1 < _clipy1 ? _clipy1 : y1;
320 if(y1 > _clipy2) { return; }
321 y2 = y2 > _clipy2 ? _clipy2 : y2;
322 if(y2 < _clipy1) { return; }
329 prim.type = RA2D_QUAD;
330 prim.invert = invert;
331 if(_ra2d_debug) prim.debug = true;
332 drawing_list.push_back(prim);
335 void RenderArea2D::oldDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
336 // Scale to position within background
337 float fx1 = (float)x1, fy1 = (float)y1;
338 float fx2 = (float)x2, fy2 = (float)y2;
339 float rx = (float)_sizex / (float)_logx;
340 float ry = (float)_sizey / (float)_logy;
349 // Translate to final position
355 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
357 doSetColor(invert ? _backgroundColor : _pixelColor);
358 SGVec2f corners[4] = {
367 void RenderArea2D::DrawBackground() {
371 void RenderArea2D::oldDrawBackground() {
372 doSetColor(_backgroundColor);
373 SGVec2f corners[4] = {
374 SGVec2f(_posx, _posy),
375 SGVec2f(_posx + _sizex, _posy),
376 SGVec2f(_posx + _sizex, _posy + _sizey),
377 SGVec2f(_posx, _posy + _sizey)
383 void RenderArea2D::Flush() {
384 drawing_list.clear();
387 // -----------------------------------------
389 // Actual drawing routines copied from Atlas
391 // -----------------------------------------
393 void RenderArea2D::doSetColor( const float *rgba ) {
394 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
398 void RenderArea2D::doDrawQuad( const SGVec2f *p) {
400 glNormal3f(0.0f, 0.0f, 0.0f);
401 glVertex2fv( p[0].data() );
402 glVertex2fv( p[1].data() );
403 glVertex2fv( p[2].data() );
404 glVertex2fv( p[3].data() );
408 void RenderArea2D::doDrawQuad( const SGVec2f *p, const SGVec4f *color ) {
410 glNormal3f(0.0f, 0.0f, 0.0f);
411 glColor4fv( color[0].data() ); glVertex2fv( p[0].data() );
412 glColor4fv( color[1].data() ); glVertex2fv( p[1].data() );
413 glColor4fv( color[2].data() ); glVertex2fv( p[2].data() );
414 glColor4fv( color[3].data() ); glVertex2fv( p[3].data() );