Sign in to follow this  
Polas22

[C++/SDL/OpenGL] Wrong mouse information in specific area.

Recommended Posts

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

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

[img]http://img26.imageshack.us/img26/4752/lolmh.png[/img]

The code:

[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;
}
[/CODE]

Share this post


Link to post
Share on other sites
Just a quick question, is the origin for your screen at the top, as one might guess for a plain array of pixels, or at the bottom, which one might expect from a Cartesian sense?

Share this post


Link to post
Share on other sites
[quote name='Ectara' timestamp='1333559867' post='4928246']
Just a quick question, is the origin for your screen at the top, as one might guess for a plain array of pixels, or at the bottom, which one might expect from a Cartesian sense?
[/quote]

The cube is rendered but under the cube mouse coordinates are wrong, in that area witch is in the picture the coordinates are X from 0 to 1 and, y from 0 to 1, but when the cursor goes out of hat are the coordinates sudenly become much bigger.

Share this post


Link to post
Share on other sites
How much bigger? Are the coordinates still from 0 to 1?

There isn't much information in the original post, and the red square is likely at position (x, y), while the cube looks to be rasterized at position (x, 1 - y). As pointed out in item 12 of the list of pitfalls here: http://www.opengl.org/archives/resources/features/KilgardTechniques/oglpitfall/, the origin is in the lower left corner if not moved. I can't run the code since I'm not at home right now, but try seeing if this at least moves the rectangle, if it represents what I think it does. Perhaps pass (1 - y) instead of y to[font=monospace] [/font]glReadPixels() and gluUnProject(). At least to rule it out.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this