PDA

View Full Version : 2D grid problem



Ehsan Kamrani
02-05-2010, 11:27 AM
I'm drawing some 2D grids like 3DS Max or maya. The problem is that the viewport is not square and my code converts the square grids to the rectangle grids!
here's my code:
//Draw to the upper left window
glViewport( 0, m_height / 2 , m_width / 2 , m_height / 2 );
glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.

// calculate the aspect ratio of the window.
gluPerspective( 45.f, m_width / m_height , 0.1f, 10000.0f );
//or
//gluOrtho2D( -200. , 200. , -200. , 200. );

glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
gluLookAt( 0.0, 0.0, 500.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
glColor3f( 0.7f, 0.7f, 0.7f );
glLineWidth( 1.0f );
glBegin( GL_LINES );
for( CInt index = -10; index <= 10; index++ )
{
glVertex3f( 0.0f + (CFloat)index * 20.0f, -200.0f, 0.0f );
glVertex3f( 0.0f + (CFloat)index * 20.0f, 200.0f, 0.0f );

glVertex3f( -200.0f ,(CFloat)index * 20.0f, 0.0f );
glVertex3f( 200.0f,(CFloat)index * 20.0f,0.0f );
}
glEnd();


What's the problem?
-Ehsan-

ZbuffeR
02-05-2010, 01:25 PM
According to the settings in gluPerspective, the glViewport params are wrong. Try with :

glViewport( 0, 0, m_width, m_height);

gluPerspective( 45.f, m_width / m_height , 0.1f, 10000.0f );

Ehsan Kamrani
02-05-2010, 01:42 PM
Hi ZBuffer
Yes, My gluPerspective() code had some problems ...
But your code also draws to the whole window and I still have the same problem. My window is not square and so the grids aren't square :(

Ehsan Kamrani
02-05-2010, 02:53 PM
I added "double" keyword and my problem solved :)
gluPerspective( 45.f, double( m_width / 2 ) / double( m_height / 2 ), 1., 10000. );

Thank you ZBuffer for suggestions about the m_width and m_height variables inside gluPerspective().

ZbuffeR
02-05-2010, 03:20 PM
Ah, good old integer divide ...

Iulian B
02-05-2010, 03:46 PM
so the solution would have been to actually read the warnings the compiler spits up and only after that decide wether to ignore them or not ;)