Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 04 April 2012 - 10:29 AM

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

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

Posted Image

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;
}


Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2743

Like
0Likes
Like

Posted 04 April 2012 - 11:17 AM

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?

#3 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 04 April 2012 - 11:56 AM

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?


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.

#4 Ectara   Crossbones+   -  Reputation: 2743

Like
0Likes
Like

Posted 04 April 2012 - 04:18 PM

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 glReadPixels() and gluUnProject(). At least to rule it out.

#5 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 05 April 2012 - 04:03 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS