1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // Author : OPEN CASCADE
24 // File: GLViewer_Grid.cxx
25 // Created: November, 2004
26 //#include <GLViewerAfx.h>
28 #include "GLViewer_Grid.h"
29 #include "GLViewer_Defs.h"
31 #include <Precision.hxx>
36 GLViewer_Grid::GLViewer_Grid() :
37 myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
38 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
39 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
40 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ),
41 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
46 myAxisColor[0] = 0.75;
47 myAxisColor[1] = 0.75;
48 myAxisColor[2] = 0.75;
53 \param width and \param height - width and height of grid
54 \param winW and \param winH - width and height of window
55 \param xSize and \param ySize - steps along x and y direction
56 \param xPan and \param yPan - offsets along x and y direction
57 \param xScale and \param yScal - scale factors along x and y direction
59 GLViewer_Grid::GLViewer_Grid( GLfloat width, GLfloat height,
60 GLfloat winW, GLfloat winH,
61 GLfloat xSize, GLfloat ySize,
62 GLfloat xPan, GLfloat yPan,
63 GLfloat xScale, GLfloat yScale ) :
64 myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
65 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
66 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
67 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ),
68 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
73 myAxisColor[0] = 0.75;
74 myAxisColor[1] = 0.75;
75 myAxisColor[2] = 0.75;
81 GLViewer_Grid::~GLViewer_Grid()
86 Performs OpenGL drawing
88 void GLViewer_Grid::draw()
90 if ( myGridList == 0 || myIsUpdate )
93 glCallList( myGridList );
98 \param r, g, b - components of color
100 void GLViewer_Grid::setGridColor( GLfloat r, GLfloat g, GLfloat b )
102 if( myGridColor[0] == r && myGridColor[1] == g && myGridColor[2] == b )
108 myIsUpdate = GL_TRUE;
112 Changes color of axis
113 \param r, g, b - components of color
115 void GLViewer_Grid::setAxisColor( GLfloat r, GLfloat g, GLfloat b )
117 if( myAxisColor[0] == r && myAxisColor[1] == g && myAxisColor[2] == b )
123 myIsUpdate = GL_TRUE;
128 \param w - new grid width
130 void GLViewer_Grid::setGridWidth( float w )
132 if( myGridWidth == w )
136 myIsUpdate = GL_TRUE;
140 Sets Radius of center point( begin coords )
141 \param r - new radius
143 void GLViewer_Grid::setCenterRadius( int r )
145 if( myCenterRadius == r )
149 myIsUpdate = GL_TRUE;
153 Sets grid size along X and Y axis
154 \param xSize - size along X axis
155 \param ySize - size along Y axis
157 void GLViewer_Grid::setSize( float xSize, float ySize )
159 if( myXSize == xSize && myYSize == ySize )
164 myIsUpdate = GL_TRUE;
169 \param xPan - panning along X axis
170 \param yPan - panning along Y axis
172 void GLViewer_Grid::setPan( float xPan, float yPan )
174 if( myXPan == xPan && myYPan == yPan )
179 myIsUpdate = GL_TRUE;
184 \param zoom - new coefficient of zooming
186 bool GLViewer_Grid::setZoom( float zoom )
192 float bXScale = myXScale;
193 float bYScale = myYScale;
198 if( fabs(myXScale) < Precision::Confusion() || fabs(myYScale) < Precision::Confusion() )
206 myGridHeight /= zoom;
207 myIsUpdate = GL_TRUE;
212 Sets parameters of grid by zoom coefficient and window size
213 \param WinW - window width
214 \param WinH - window height
215 \param zoom - zoom coefficient
217 void GLViewer_Grid::setResize( float WinW, float WinH, float zoom )
219 if( myWinW == WinW && myWinH == WinH && zoom == 1.0 )
222 myGridWidth = myGridWidth + ( WinW - myWinW ) * myXScale;
223 myGridHeight = myGridHeight + ( WinH - myWinH ) * myYScale;
227 myIsUpdate = GL_TRUE;
231 \return grid size along x and y axis
232 \param xSize - for size along x axis
233 \param ySize - for size along y axis
235 void GLViewer_Grid::getSize( float& xSize, float& ySize ) const
242 \return panning along x and y axis
243 \param xPan - for panning along x axis
244 \param yPan - for panning along y axis
246 void GLViewer_Grid::getPan( float& xPan, float& yPan ) const
253 \return scaling along x and y axis
254 \param xScale - for scaling along x axis
255 \param yScale - for scaling along y axis
257 void GLViewer_Grid::getScale( float& xScale, float& yScale ) const
264 Initialize grid display list
266 bool GLViewer_Grid::initList()
268 myIsUpdate = GL_FALSE;
270 if( myXSize == (GLfloat)0.0 )
271 myXSize = (GLfloat)0.1;
272 if( myYSize == (GLfloat)0.0 )
273 myYSize = (GLfloat)0.1;
276 if( ( myXSize >= myGridWidth/5 ) && ( myYSize >= myGridHeight/5 ) )
278 myXSize /= myScaleFactor;
279 myYSize /= myScaleFactor;
282 else if( ( myXSize * myScaleFactor < myGridWidth/5 )
283 || ( myYSize * myScaleFactor < myGridHeight/5 ) )
285 myXSize *= myScaleFactor;
286 myYSize *= myScaleFactor;
290 //int n = myGridWidth / myXSize;
291 //int m = myGridHeight / myYSize;
292 // do not initialise integer by float
293 //if( ( n != 0 ) || ( m != 0 ) )
294 if( ( myGridWidth > 0.5 * myXSize ) || ( myGridHeight > 0.5 * myYSize ) )
296 if ( myGridList != 0 )
298 glDeleteLists( myGridList, 1 );
299 if ( glGetError() != GL_NO_ERROR )
303 float xLoc = (int)(myXPan / myXSize) * myXSize;
304 float yLoc = (int)(myYPan / myYSize) * myYSize;
306 myGridList = glGenLists( 1 );
307 glNewList( myGridList, GL_COMPILE );
309 glColor3f( myGridColor[0], myGridColor[1], myGridColor[2] );
310 glLineWidth( myLineWidth );
313 for( int j = 0; ( j-1 ) * myXSize <= myGridWidth / 2 ; j++ )
315 glVertex2d( -myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
316 glVertex2d( -myXSize * j - xLoc, myGridHeight / 2 + myYSize - yLoc );
317 glVertex2d( myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
318 glVertex2d( myXSize * j - xLoc, myGridHeight / 2 + myYSize - yLoc );
320 for( int i = 0; ( i-1 ) * myYSize <= myGridHeight / 2 ; i++)
322 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, -myYSize * i - yLoc );
323 glVertex2d( myGridWidth / 2 + myXSize - xLoc, -myYSize * i - yLoc );
324 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, myYSize * i - yLoc );
325 glVertex2d( myGridWidth / 2 + myXSize - xLoc, myYSize * i - yLoc );
329 glColor3f( myAxisColor[0], myAxisColor[1], myAxisColor[2] );
330 glLineWidth( myCenterWidth );
333 glVertex2d( myGridWidth / 2 + myXSize - xLoc, 0);
334 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, 0);
335 glVertex2d( 0, myGridHeight / 2 + myYSize - yLoc );
336 glVertex2d( 0, -myGridHeight / 2 - myYSize - yLoc );
339 glBegin( GL_LINE_LOOP );
341 for ( int k = 0; k < SEGMENTS; k++ )
343 glVertex2f( cos(angle) * myCenterRadius * myXScale,
344 sin(angle) * myCenterRadius * myYScale );