[C++/SDL/OpenGL] Wrong mouse information

As the title says, I am getting wrong mouse information in specific area

Here is the picture in wich are I get wrong coordinates:

Normali the coordinates should be from 0 to 64 in that arena, but not from 0 to 1. The are which shows coordinates from 0 to 1 is the same size as the white cube.

The code:

#include "SDL.h"
#include <SDL_opengl.h>
#include <GL/glu.h>
#include <sstream>
int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 640;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 60;
SDL_Surface *surface;
SDL_Event event;

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
         if (height==0)                                                   // Prevent A Divide By Zero By
        {
                height=1;                                                  // Making Height Equal One
        }
        glViewport( 0, 0, width, height );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity( );
        gluPerspective( 45.0f, ( GLfloat )width / ( GLfloat )height, 0.1f, 100.0f );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
}

int initGL( GLvoid )
{
         glEnable(GL_TEXTURE_2D);          // Enable Texture Mapping ( NEW )
         glShadeModel(GL_SMOOTH);          // Enable Smooth Shading
         glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // Black Background
         glClearDepth(1.0f);             // Depth Buffer Setup
         glEnable(GL_DEPTH_TEST);          // Enables Depth Testing
         glDepthFunc(GL_LEQUAL);                // The Type Of Depth Testing To Do
         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
        return TRUE ;
}

void clean_up()
{
        //Quit SDL
        SDL_Quit();
}

void GLDRAW(GLvoid)
{
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glLoadIdentity();
         glTranslatef(0.0f,0.0f,-10.0f);
         glBegin(GL_QUADS);              // Draw A Quad
                 glVertex3f(0.0f , 0.64f, 0.0f);         // Top Left
                 glVertex3f(0.64f ,0.64f, 0.0f);         // Top Right
                 glVertex3f(0.64f ,0.0f , 0.0f);         // Bottom Right
                  glVertex3f(0.0f , 0.0f , 0.0f);        // Bottom Left
         glEnd();
}

int Timer()
{
         int startTicks = 0;
         startTicks = SDL_GetTicks();
         return SDL_GetTicks() - startTicks;
}

int main( int argc, char *argv[] )
{
        //Quit flag
        bool quit = false;
        if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
        {
                return false;
        }

         if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL ) == NULL )
        {
                return false;
        }

         initGL( );
         ReSizeGLScene( SCREEN_WIDTH, SCREEN_HEIGHT );
         int x = 0, y = 0;
         float x3d,y3d,z3d;
         //Wait for user exit
         while( quit == false )
         {
                //Start the frame timer
                //While there are events to handle
           while( SDL_PollEvent( &event ) )
           {
                 if( event.type == SDL_MOUSEMOTION )
                         {
                                 x = event.motion.x;
                                  y = event.motion.y;
                                 GLdouble model_view[16];
                                 glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
                                 GLdouble projection[16];
                                 glGetDoublev(GL_PROJECTION_MATRIX, projection);
                                 GLint viewport[4];
                                 glGetIntegerv(GL_VIEWPORT, viewport);
                                 double dx; double dy; double dz;
                                 GLfloat depth[2];
                                 glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
                                 gluUnProject(x, y, depth[0], model_view, projection, viewport, &dx, &dy, &dz);
                                 x3d = float(dx);
                                 y3d = float(dy);
                                 z3d = float(dz);
           }

           if( event.type == SDL_QUIT )
           {
                                quit = true;
                  }
}
                   //Clear the screen
                   glClear( GL_COLOR_BUFFER_BIT );
                   //Show the square
                   GLDRAW();
                        //Update screen
                 SDL_GL_SwapBuffers();
                 std::stringstream time;
                 time << "X: " << x3d << " Y: " << y3d;
                 SDL_WM_SetCaption( time.str().c_str(), NULL );
                 }
//Clean up
clean_up();
return 0;
}

The origin for glReadPixels is at the bottom left of the frame buffer, so you would need to be using “height-y” instead of “y” if the coordinates provided start from the upper left:


y = height - y;
glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);

height is the height of the screen?
and shoul I do the same thing with gluUnProject?