1 // Copyright (C) 2007-2008 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.
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
22 // Author : OPEN CASCADE
23 // File: GLViewer_Grid.cxx
24 // Created: November, 2004
25 //#include <GLViewerAfx.h>
27 #include "GLViewer_Grid.h"
28 #include "GLViewer_Defs.h"
30 #include <Precision.hxx>
35 GLViewer_Grid::GLViewer_Grid() :
36 myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
37 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
38 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
39 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ),
40 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
45 myAxisColor[0] = 0.75;
46 myAxisColor[1] = 0.75;
47 myAxisColor[2] = 0.75;
52 \param width and \param height - width and height of grid
53 \param winW and \param winH - width and height of window
54 \param xSize and \param ySize - steps along x and y direction
55 \param xPan and \param yPan - offsets along x and y direction
56 \param xScale and \param yScal - scale factors along x and y direction
58 GLViewer_Grid::GLViewer_Grid( GLfloat width, GLfloat height,
59 GLfloat winW, GLfloat winH,
60 GLfloat xSize, GLfloat ySize,
61 GLfloat xPan, GLfloat yPan,
62 GLfloat xScale, GLfloat yScale ) :
63 myGridList( 0 ), myGridHeight( (GLfloat)0.0 ), myGridWidth( (GLfloat)0.0 ),
64 myWinW( (GLfloat)0.0 ), myWinH( (GLfloat)0.0 ), myXSize( (GLfloat)0.0 ), myYSize( (GLfloat)0.0 ),
65 myXPan( (GLfloat)0.0 ), myYPan( (GLfloat)0.0 ), myXScale( (GLfloat)1.0 ), myYScale( (GLfloat)1.0 ),
66 myLineWidth( (GLfloat)0.05 ), myCenterWidth( (GLfloat)1.5 ), myCenterRadius( (GLfloat)5.0 ),
67 myScaleFactor( 10 ), myIsUpdate( GL_FALSE )
72 myAxisColor[0] = 0.75;
73 myAxisColor[1] = 0.75;
74 myAxisColor[2] = 0.75;
80 GLViewer_Grid::~GLViewer_Grid()
85 Performs OpenGL drawing
87 void GLViewer_Grid::draw()
89 if ( myGridList == 0 || myIsUpdate )
92 glCallList( myGridList );
97 \param r, g, b - components of color
99 void GLViewer_Grid::setGridColor( GLfloat r, GLfloat g, GLfloat b )
101 if( myGridColor[0] == r && myGridColor[1] == g && myGridColor[2] == b )
107 myIsUpdate = GL_TRUE;
111 Changes color of axis
112 \param r, g, b - components of color
114 void GLViewer_Grid::setAxisColor( GLfloat r, GLfloat g, GLfloat b )
116 if( myAxisColor[0] == r && myAxisColor[1] == g && myAxisColor[2] == b )
122 myIsUpdate = GL_TRUE;
127 \param w - new grid width
129 void GLViewer_Grid::setGridWidth( float w )
131 if( myGridWidth == w )
135 myIsUpdate = GL_TRUE;
139 Sets Radius of center point( begin coords )
140 \param r - new radius
142 void GLViewer_Grid::setCenterRadius( int r )
144 if( myCenterRadius == r )
148 myIsUpdate = GL_TRUE;
152 Sets grid size along X and Y axis
153 \param xSize - size along X axis
154 \param ySize - size along Y axis
156 void GLViewer_Grid::setSize( float xSize, float ySize )
158 if( myXSize == xSize && myYSize == ySize )
163 myIsUpdate = GL_TRUE;
168 \param xPan - panning along X axis
169 \param yPan - panning along Y axis
171 void GLViewer_Grid::setPan( float xPan, float yPan )
173 if( myXPan == xPan && myYPan == yPan )
178 myIsUpdate = GL_TRUE;
183 \param zoom - new coefficient of zooming
185 bool GLViewer_Grid::setZoom( float zoom )
191 float bXScale = myXScale;
192 float bYScale = myYScale;
197 if( fabs(myXScale) < Precision::Confusion() || fabs(myYScale) < Precision::Confusion() )
205 myGridHeight /= zoom;
206 myIsUpdate = GL_TRUE;
211 Sets parameters of grid by zoom coefficient and window size
212 \param WinW - window width
213 \param WinH - window height
214 \param zoom - zoom coefficient
216 void GLViewer_Grid::setResize( float WinW, float WinH, float zoom )
218 if( myWinW == WinW && myWinH == WinH && zoom == 1.0 )
221 myGridWidth = myGridWidth + ( WinW - myWinW ) * myXScale;
222 myGridHeight = myGridHeight + ( WinH - myWinH ) * myYScale;
226 myIsUpdate = GL_TRUE;
230 \return grid size along x and y axis
231 \param xSize - for size along x axis
232 \param ySize - for size along y axis
234 void GLViewer_Grid::getSize( float& xSize, float& ySize ) const
241 \return panning along x and y axis
242 \param xPan - for panning along x axis
243 \param yPan - for panning along y axis
245 void GLViewer_Grid::getPan( float& xPan, float& yPan ) const
252 \return scaling along x and y axis
253 \param xScale - for scaling along x axis
254 \param yScale - for scaling along y axis
256 void GLViewer_Grid::getScale( float& xScale, float& yScale ) const
263 Initialize grid display list
265 bool GLViewer_Grid::initList()
267 myIsUpdate = GL_FALSE;
269 if( myXSize == (GLfloat)0.0 )
270 myXSize = (GLfloat)0.1;
271 if( myYSize == (GLfloat)0.0 )
272 myYSize = (GLfloat)0.1;
275 if( ( myXSize >= myGridWidth/5 ) && ( myYSize >= myGridHeight/5 ) )
277 myXSize /= myScaleFactor;
278 myYSize /= myScaleFactor;
281 else if( ( myXSize * myScaleFactor < myGridWidth/5 )
282 || ( myYSize * myScaleFactor < myGridHeight/5 ) )
284 myXSize *= myScaleFactor;
285 myYSize *= myScaleFactor;
289 //int n = myGridWidth / myXSize;
290 //int m = myGridHeight / myYSize;
291 // do not initialise integer by float
292 //if( ( n != 0 ) || ( m != 0 ) )
293 if( ( myGridWidth > 0.5 * myXSize ) || ( myGridHeight > 0.5 * myYSize ) )
295 if ( myGridList != 0 )
297 glDeleteLists( myGridList, 1 );
298 if ( glGetError() != GL_NO_ERROR )
302 float xLoc = (int)(myXPan / myXSize) * myXSize;
303 float yLoc = (int)(myYPan / myYSize) * myYSize;
305 myGridList = glGenLists( 1 );
306 glNewList( myGridList, GL_COMPILE );
308 glColor3f( myGridColor[0], myGridColor[1], myGridColor[2] );
309 glLineWidth( myLineWidth );
312 for( int j = 0; ( j-1 ) * myXSize <= myGridWidth / 2 ; j++ )
314 glVertex2d( -myXSize * j - xLoc, -myGridHeight / 2 - myYSize - yLoc );
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 );
319 for( int i = 0; ( i-1 ) * myYSize <= myGridHeight / 2 ; i++)
321 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, -myYSize * i - yLoc );
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 );
328 glColor3f( myAxisColor[0], myAxisColor[1], myAxisColor[2] );
329 glLineWidth( myCenterWidth );
332 glVertex2d( myGridWidth / 2 + myXSize - xLoc, 0);
333 glVertex2d( -myGridWidth / 2 - myXSize - xLoc, 0);
334 glVertex2d( 0, myGridHeight / 2 + myYSize - yLoc );
335 glVertex2d( 0, -myGridHeight / 2 - myYSize - yLoc );
338 glBegin( GL_LINE_LOOP );
340 for ( int k = 0; k < SEGMENTS; k++ )
342 glVertex2f( cos(angle) * myCenterRadius * myXScale,
343 sin(angle) * myCenterRadius * myYScale );