Merge branch 'experiment' of git+ssh://repo.or.cz/srv/git/glgame
[glgame.git] / src / screengrid.cpp
blobf1258fb5c57299ea4d81fdbbcc264246d1d8a158
1 #include "screengrid.h"
3 ScreenGrid::ScreenGrid()
6 regenerate = true;
11 ScreenGrid::~ScreenGrid()
17 /*!
18 \fn ScreenGrid::Init()
20 void ScreenGrid::Init()
22 extern Options options;
23 grid_step = options.getFloatValue( Options::GRID_STEP );
24 bold_step = 10;
25 labels_size = 0.05f;
27 grid_size = GlScreenSize.val.x / 2 + 2 * (grid_step * bold_step);
31 void ScreenGrid::RegenerateGrid()
34 // Creating grid
36 grid_list = glGenLists(1);
37 glNewList(grid_list,GL_COMPILE);
39 GLint bold_step_counter = 0;
41 for ( GLfloat x = - grid_size; x <= grid_size; x+= grid_step ) {
43 if(bold_step_counter) {
45 glColor3f(0.0f, 0.10f, 0.0f);
47 glBegin( GL_LINES );
48 glVertex2f( x, - grid_size);
49 glVertex2f( x, grid_size);
50 glEnd();
52 glBegin( GL_LINES );
53 glVertex2f( - grid_size, x);
54 glVertex2f( grid_size, x);
55 glEnd();
59 bold_step_counter++;
60 if (bold_step_counter >= bold_step) bold_step_counter = 0;
64 for ( GLfloat x = - grid_size; x <= grid_size; x+= grid_step * bold_step ) {
66 glColor3f(0.0,0.3,0.0);
68 glBegin( GL_LINES );
69 glVertex2f( x, - grid_size);
70 glVertex2f( x, grid_size);
71 glEnd();
73 glBegin( GL_LINES );
74 glVertex2f( - grid_size, x);
75 glVertex2f( grid_size, x);
76 glEnd();
79 glEndList();
80 // End creating grid
86 /*!
87 \fn ScreenGrid::display_labels()
89 void ScreenGrid::display_labels()
92 GLfloat gl_width = GlScreenSize.val.x / 2;
93 GLfloat gl_height = - GlScreenSize.val.y / 2;
95 GLfloat otstup = 1;
97 // Show labels - horisontal
98 glColor3f(1.0,0.6,0.0);
100 glPushMatrix();
101 glLoadIdentity();
103 glTranslatef(grid_center_x, -gl_height* otstup, -gl_depth);
105 for ( GLfloat x = - grid_size; x <= grid_size; x+= grid_step * bold_step ) {
107 glBegin( GL_TRIANGLES );
108 glVertex2f( x, 0.0f);
109 glVertex2f( x + labels_size, labels_size);
110 glVertex2f( x - labels_size, labels_size);
111 glEnd();
115 glPopMatrix();
117 // Show labels - vertical
118 glPushMatrix();
119 glLoadIdentity();
121 glTranslatef( - gl_width * otstup, grid_center_y, -gl_depth);
123 for ( GLfloat y = - grid_size; y <= grid_size; y+= grid_step * bold_step ) {
125 glBegin( GL_TRIANGLES );
126 glVertex2f( 0.0f, 0.0f + y);
127 glVertex2f( -labels_size, labels_size + y);
128 glVertex2f( -labels_size, -labels_size +y);
129 glEnd();
133 glPopMatrix();
138 void ScreenGrid::display( Vector2D view_coord )
141 if ( regenerate ) { regenerate = false; RegenerateGrid(); }
143 grid_center_x = - fmodf ( (float)view_coord.val.x, fabs(grid_step * bold_step) );
144 grid_center_y = - fmodf ( (float)view_coord.val.y, fabs(grid_step * bold_step) );
146 glPushMatrix();
147 // glLoadIdentity();
148 glTranslatef(grid_center_x, grid_center_y, 0.0f);
150 // Отключим на время рисования сетки тест глубины и задних плоскостей
151 glDisable(GL_DEPTH_TEST);
152 glDisable(GL_CULL_FACE);
154 glCallList(grid_list); // сетка
155 display_labels(); // сверху рисуем метки
157 // Вернём тест глубины и задних плоскостей
158 glEnable(GL_DEPTH_TEST);
159 glEnable(GL_CULL_FACE);
161 glPopMatrix();