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 - david.luff@nottingham.ac.uk
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 static const float dummy_normals[][3] = {{0.0f, 0.0f, 0.0f},
36 RA2DPrimitive::RA2DPrimitive() {
41 RenderArea2D::RenderArea2D(int logx, int logy, int sizex, int sizey, int posx, int posy) {
53 _backgroundColor[0] = 0.0;
54 _backgroundColor[1] = 0.0;
55 _backgroundColor[2] = 0.0;
56 _backgroundColor[3] = 1.0;
65 void RenderArea2D::draw(osg::State& state) {
67 static osg::ref_ptr<osg::StateSet> renderArea2DStateSet;
68 if(!renderArea2DStateSet.valid()) {
69 renderArea2DStateSet = new osg::StateSet;
70 renderArea2DStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
71 renderArea2DStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
74 state.pushStateSet(renderArea2DStateSet.get());
76 state.setActiveTextureUnit(0);
77 state.setClientActiveTextureUnit(0);
79 // DCL - the 2 lines below are copied verbatim from the hotspot drawing code.
80 // I am not sure if they are needed here or not.
81 glPushAttrib(GL_ENABLE_BIT);
82 glDisable(GL_COLOR_MATERIAL);
84 // FIXME - disabling all clip planes causes bleed-through through the splash screen.
85 glDisable(GL_CLIP_PLANE0);
86 glDisable(GL_CLIP_PLANE1);
87 glDisable(GL_CLIP_PLANE2);
88 glDisable(GL_CLIP_PLANE3);
92 for(unsigned int i = 0; i < drawing_list.size(); ++i) {
93 RA2DPrimitive prim = drawing_list[i];
96 oldDrawLine(prim.x1, prim.y1, prim.x2, prim.y2);
100 //cout << "Clipping = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
101 //cout << "Drawing quad " << prim.x1 << ", " << prim.y1 << " to " << prim.x2 << ", " << prim.y2 << '\n';
103 oldDrawQuad(prim.x1, prim.y1, prim.x2, prim.y2, prim.invert);
106 oldDrawPixel(prim.x1, prim.y1, prim.invert);
115 state.setActiveTextureUnit(0);
116 state.setClientActiveTextureUnit(0);
119 // Set clipping region in logical units
120 void RenderArea2D::SetClipRegion(int x1, int y1, int x2, int y2) {
125 //cout << "Set clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
128 // Set clip region to be the same as the rendered area (default)
129 void RenderArea2D::ResetClipRegion() {
134 //cout << "Reset clip region, clip region = " << _clipx1 << ", " << _clipy1 << " to " << _clipx2 << ", " << _clipy2 << '\n';
137 void RenderArea2D::SetPosition(int posx, int posy) {
142 void RenderArea2D::SetLogicalSize(int logx, int logy) {
147 void RenderArea2D::SetActualSize(int sizex, int sizey) {
152 void RenderArea2D::DrawPixel(int x, int y, bool invert) {
153 // Clipping is currently performed in oldDrawPixel - could clip here instead though.
160 prim.type = RA2D_PIXEL;
161 prim.invert = invert;
162 drawing_list.push_back(prim);
165 void RenderArea2D::oldDrawPixel(int x, int y, bool invert) {
167 if(x < _clipx1 || x > _clipx2 || y < _clipy1 || y > _clipy2) return;
169 // Scale to position within background
170 float fx1 = (float)x, fy1 = (float)y;
171 float rx = (float)_sizex / (float)_logx;
172 float ry = (float)_sizey / (float)_logy;
175 float fx2 = fx1 + rx;
176 float fy2 = fy1 + ry;
178 // Translate to final position
184 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
186 doSetColor(invert ? _backgroundColor : _pixelColor);
188 sgSetVec2(corners[0], fx1, fy1);
189 sgSetVec2(corners[1], fx2, fy1);
190 sgSetVec2(corners[2], fx2, fy2);
191 sgSetVec2(corners[3], fx1, fy2);
192 //cout << "Drawing pixel, x,y is " << x << ", " << y << ", fx is [x1,x2,y1,y2] " << fx1 << ", " << fx2 << ", " << fy1 << ", " << fy2 << '\n';
193 doDrawQuad(&corners[0], dummy_normals);
196 void RenderArea2D::DrawLine(int x1, int y1, int x2, int y2) {
202 prim.type = RA2D_LINE;
204 drawing_list.push_back(prim);
207 void RenderArea2D::oldDrawLine(int x1, int y1, int x2, int y2) {
208 // Crude implementation of Bresenham line drawing algorithm.
210 // Our lines are non directional, so first order the points x-direction-wise to leave only 4 octants to consider.
220 bool flip_y = (y1 > y2 ? true : false);
222 int dy = (flip_y ? y1 - y2 : y2 - y1);
227 for(int x=x1; x<=x2; ++x) {
232 y = (flip_y ? y - 1 : y + 1);
239 // Must be a more elegant way to roll the next two cases into one!
241 for(int y=y1; y>=y2; --y) {
250 for(int y=y1; y<=y2; ++y) {
262 void RenderArea2D::DrawQuad(int x1, int y1, int x2, int y2, bool invert) {
263 // Clip and sanity-check.
274 x1 = x1 < _clipx1 ? _clipx1 : x1;
275 if(x1 > _clipx2) { return; }
276 x2 = x2 > _clipx2 ? _clipx2 : x2;
277 if(x2 < _clipx1) { return; }
278 y1 = y1 < _clipy1 ? _clipy1 : y1;
279 if(y1 > _clipy2) { return; }
280 y2 = y2 > _clipy2 ? _clipy2 : y2;
281 if(y2 < _clipy1) { return; }
288 prim.type = RA2D_QUAD;
289 prim.invert = invert;
290 if(_ra2d_debug) prim.debug = true;
291 drawing_list.push_back(prim);
294 void RenderArea2D::oldDrawQuad(int x1, int y1, int x2, int y2, bool invert) {
295 // Scale to position within background
296 float fx1 = (float)x1, fy1 = (float)y1;
297 float fx2 = (float)x2, fy2 = (float)y2;
298 float rx = (float)_sizex / (float)_logx;
299 float ry = (float)_sizey / (float)_logy;
308 // Translate to final position
314 //cout << "DP: " << fx1 << ", " << fy1 << " ... " << fx2 << ", " << fy2 << '\n';
316 doSetColor(invert ? _backgroundColor : _pixelColor);
318 sgSetVec2(corners[0], fx1, fy1);
319 sgSetVec2(corners[1], fx2, fy1);
320 sgSetVec2(corners[2], fx2, fy2);
321 sgSetVec2(corners[3], fx1, fy2);
322 doDrawQuad(&corners[0], dummy_normals);
325 void RenderArea2D::DrawBackground() {
329 void RenderArea2D::oldDrawBackground() {
330 doSetColor(_backgroundColor);
332 sgSetVec2(corners[0], (float)_posx, (float)_posy);
333 sgSetVec2(corners[1], (float)(_posx + _sizex), (float)_posy);
334 sgSetVec2(corners[2], (float)(_posx + _sizex), (float)(_posy + _sizey));
335 sgSetVec2(corners[3], (float)_posx, (float)(_posy + _sizey));
336 doDrawQuad(&corners[0], dummy_normals);
339 void RenderArea2D::Flush() {
340 drawing_list.clear();
343 // -----------------------------------------
345 // Actual drawing routines copied from Atlas
347 // -----------------------------------------
349 void RenderArea2D::doSetColor( const float *rgba ) {
350 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgba);
354 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals ) {
356 glNormal3fv( normals[0] ); glVertex2fv( p[0] );
357 glNormal3fv( normals[1] ); glVertex2fv( p[1] );
358 glNormal3fv( normals[2] ); glVertex2fv( p[2] );
359 glNormal3fv( normals[3] ); glVertex2fv( p[3] );
363 void RenderArea2D::doDrawQuad( const sgVec2 *p, const sgVec3 *normals, const sgVec4 *color ) {
365 glColor4fv( color[0] );glNormal3fv( normals[0] ); glVertex2fv( p[0] );
366 glColor4fv( color[1] );glNormal3fv( normals[1] ); glVertex2fv( p[1] );
367 glColor4fv( color[2] );glNormal3fv( normals[2] ); glVertex2fv( p[2] );
368 glColor4fv( color[3] );glNormal3fv( normals[3] ); glVertex2fv( p[3] );