Problem in mouse position

This is a discussion on Problem in mouse position within the Game Programming forums, part of the General Programming Boards category; I have been working on this project for a few days, I borrowed most of the code from Nehe (about ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    Problem in mouse position

    I have been working on this project for a few days, I borrowed most of the code from Nehe (about 4/5) and wrote the rest myself.
    I have a problem with mouse position. I'm trying to do so it will, when clicked on the screen, it will take the mouse position after the click and add a sphere on the mouse position.

    I have 2 problems, my first is, when I click on the screen, it adds a sphere in the middle of the screen instead of in the mouse position after click.

    And my second problem is that it just adds the sphere as long as I hold down the mouse button, when I release it it disapears.

    Here is the important page in it (there are more of them, but they are pretty long so if you need them too, I will post them):
    And Im just 13 years old, be nice


    Page one:
    Code:
    #include <stdio.h>			// Header File For Standard Input/Output
    #include <stdlib.h>
    #include <windows.h>												// Header File For Windows
    #include <gl\gl.h>													// Header File For The OpenGL32 Library
    #include <gl\glu.h>													// Header File For The GLu32 Library
    #include <gl\glaux.h>												// Header File For The GLaux Library
    #include "NeHeGL.h"													// Header File For NeHeGL
    #include <iostream>
    #include "math.h"												    //  Needed For Sqrtf
    #include "ArcBall.h"												//  ArcBall Header
    
    #pragma comment( lib, "opengl32.lib" )								// Search For OpenGL32.lib While Linking
    #pragma comment( lib, "glu32.lib" )									// Search For GLu32.lib While Linking
    #pragma comment( lib, "glaux.lib" )									// Search For GLaux.lib While Linking
    
    #ifndef CDS_FULLSCREEN												// CDS_FULLSCREEN Is Not Defined By Some
    #define CDS_FULLSCREEN 4											// Compilers. By Defining It This Way,
    #endif																// We Can Avoid Errors
    
    GL_Window*	g_window;
    Keys*		g_keys;
    
    //Defined Variables
    GLUquadricObj *quadratic;											// Used For Our Quadric
    
    const float PI2 = 2.0*3.1415926535f;								// PI Squared
    
    Matrix4fT   Transform   = {  1.0f,  0.0f,  0.0f,  0.0f,				//  Final Transform
                                 0.0f,  1.0f,  0.0f,  0.0f,
                                 0.0f,  0.0f,  1.0f,  0.0f,
                                 0.0f,  0.0f,  0.0f,  1.0f };
    
    Matrix3fT   LastRot     = {  1.0f,  0.0f,  0.0f,					// Last Rotation
                                 0.0f,  1.0f,  0.0f,
                                 0.0f,  0.0f,  1.0f };
    
    Matrix3fT   ThisRot     = {  1.0f,  0.0f,  0.0f,					//  This Rotation
                                 0.0f,  1.0f,  0.0f,
                                 0.0f,  0.0f,  1.0f };
    
    ArcBallT    ArcBall(860.0f, 640.0f);				                //  ArcBall Instance
    Point2fT    MousePt;												//  Current Mouse Point
    Point2fT    isClickPoint;
    
    bool        isClicked  = false;										//  Clicking The Mouse?
    bool        isRClicked = false;	
    bool        isMClicked = false;									//  Clicking The Right Mouse Button?
    bool        isDragging = false;					                    // NEW: Dragging The Mouse?
    
    bool    blend;						// Blending OFF/ON? 
    bool    bp;
    bool    isAdded;
    bool    IA;
    int xpos,ypos,zpos;   
    int xposition,yposition;                   //Mouse position;
    POINT Pt;                           //Mouse position;
    
    GLuint	texture[3];			// Storage For 3 Textures
    
    GLfloat LightAmbient[]=		{ 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat LightDiffuse[]=		{ 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat LightPosition[]=	{ 0.0f, 0.0f, 2.0f, 1.0f };
    
    
    AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image
    {
             
    	FILE *File=NULL;									// File Handle
    
    	if (!Filename)										// Make Sure A Filename Was Given
    	{
    		return NULL;									// If Not Return NULL
    	}
    
    	File=fopen(Filename,"r");							// Check To See If The File Exists
    
    	if (File)											// Does The File Exist?
    	{
    		fclose(File);									// Close The Handle
    		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
    	}
    
    	return NULL;										// If Load Failed Return NULL
    }
    
    int LoadGLTextures()									// Load Bitmaps And Convert To Textures
    {
    	int Status=FALSE;									// Status Indicator
    
    	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture
    
    	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
    
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage[0]=LoadBMP("Data/Glass.bmp"))
    	{
    		Status=TRUE;									// Set The Status To TRUE
    
    		glGenTextures(3, &texture[0]);					// Create Three Textures
    
    		// Create Nearest Filtered Texture
    		glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        }
        if (TextureImage[0])								// If Texture Exists
    	{
    		if (TextureImage[0]->data)						// If Texture Image Exists
    		{
    			free(TextureImage[0]->data);				// Free The Texture Image Memory
    		}
    
    		free(TextureImage[0]);							// Free The Image Structure
    	}
    
    	return Status;										// Return The Status
    }
    
    BOOL Initialize (GL_Window* window, Keys* keys)						// Any GL Init Code & User Initialiazation Goes Here
    {
    
    	g_window	= window;
    	g_keys		= keys;
    
    	// Start Of User Initialization
        isClicked   = false;								            // NEW: Clicking The Middle Mouse?
        isDragging  = false;							                // NEW: Dragging The Mouse?
    
    	glEnable(GL_TEXTURE_2D);							// Enable Texture Mapping
    	glClearColor (0.0f, 0.0f, 0.0f, 0.5f);							// Black Background
    	glClearDepth (1.0f);											// Depth Buffer Setup
    	glDepthFunc (GL_LEQUAL);										// The Type Of Depth Testing (Less Or Equal)
    	glEnable (GL_DEPTH_TEST);										// Enable Depth Testing
    	glShadeModel (GL_SMOOTH);									    // Select Flat Shading (Nice Definition Of Objects)
    	glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);				// Set Perspective Calculations To Most Accurate
        glClearDepth(1.0f);									// Depth Buffer Setup
    
    	quadratic=gluNewQuadric();										// Create A Pointer To The Quadric Object
    	gluQuadricNormals(quadratic, GLU_SMOOTH);						// Create Smooth Normals
    	gluQuadricTexture(quadratic, GL_TRUE);							// Create Texture Coords
    
    	glEnable(GL_LIGHT0);											// Enable Default Light
    	glEnable(GL_LIGHTING);											// Enable Lighting
    
    	glEnable(GL_COLOR_MATERIAL);									// Enable Color Material
    	
        glColor4f(1.0f, 1.0f, 1.0f, 0.5f);					// Full Brightness.  50% Alpha
    	glBlendFunc(GL_SRC_ALPHA,GL_ONE);					// Set The Blending Function For Translucency
    	return TRUE;													// Return TRUE (Initialization Successful)
    }
    
    void Deinitialize (void)											// Any User DeInitialization Goes Here
    {
    	gluDeleteQuadric(quadratic);
    }
    
    void Update (DWORD milliseconds)									// Perform Motion Updates Here
    {
        
    	if (g_keys->keyDown [VK_ESCAPE] == TRUE)						// Is ESC Being Pressed?
    		TerminateApplication (g_window);							// Terminate The Program
    
    	if (g_keys->keyDown [VK_F1] == TRUE)							// Is F1 Being Pressed?
    		ToggleFullscreen (g_window);								// Toggle Fullscreen Mode
    	
        
    	if(isRClicked && !bp)
       {
    
    					bp=TRUE;
    					blend = !blend;
    					if(blend)
    					{
    						glEnable(GL_BLEND);			// Turn Blending On
    						glDisable(GL_DEPTH_TEST);	// Turn Depth Testing Off
    					}
    					else
    					{
    						glDisable(GL_BLEND);		// Turn Blending Off
    						glEnable(GL_DEPTH_TEST);	// Turn Depth Testing On
    					}
    				}
    				if (!isRClicked)
    				{
    					bp=FALSE;
                    }
    
        
    
        if (!isDragging)												// Not Dragging
        {
            if (isMClicked)												// First Click
            {
    			isDragging = true;										// Prepare For Dragging
    			LastRot = ThisRot;										// Set Last Static Rotation To Last Dynamic One
    			ArcBall.click(&MousePt);								// Update Start Vector And Prepare For Dragging
            }
        }
        else
        {
            if (isMClicked)												// Still Clicked, So Still Dragging
            {
                Quat4fT     ThisQuat;
    
                ArcBall.drag(&MousePt, &ThisQuat);						// Update End Vector And Get Rotation As Quaternion
                Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat);		// Convert Quaternion Into Matrix3fT
                Matrix3fMulMatrix3f(&ThisRot, &LastRot);				// Accumulate Last Rotation Into This One
                Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);	// Set Our Final Transform's Rotation From This One
            }
            else														// No Longer Dragging
                isDragging = false;
        }
    }
    
    
    
    void Draw (void)
    {
         
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);				// Clear Screen And Depth Buffer
    	
     (isClicked)         //HERE IS MY PROBLEM
        {      
               
               GetCursorPos(&Pt); 
               Pt.x=xpos;  
               Pt.y=ypos;
               
               std::cout<<xpos<<" "<<ypos<<"\n";    //It just gives 00 00 all the time
               glLoadIdentity();
               glTranslatef(xpos,ypos,-10.0f);             // I don't know how to get the xpos and ypos 
               glColor3f(0.7f,0.8f,0.8f);                       // be the right cordinates
               gluSphere(quadratic,0.7f,15,15);
               
        }
    
       
    
     
    	glLoadIdentity();												// Reset The Current Modelview Matrix
    	glTranslatef(2.0f,-0.5f,-10.0f);									// Move Right 1.5 Units And Into The Screen 7.0
    
        glPushMatrix();													// NEW: Prepare Dynamic Transform
        glMultMatrixf(Transform.M);										// NEW: Apply Dynamic Transform
    	glColor3f(0.7f,0.8f,0.8f);
    	glBindTexture(GL_TEXTURE_2D, texture[0]);
    	gluSphere(quadratic,1.3f,25,25);
    	
    	
    	glBegin(GL_QUADS);
    	    
    		glNormal3f( 0.0f, 0.0f, 0.5f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f,  0.5f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f,  0.5f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f,  0.5f,  0.5f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  0.5f,  0.5f);
    		// Back Face
    		glNormal3f( 0.0f, 0.0f,-0.5f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f,  0.5f, -0.5f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f,  0.5f, -0.5f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
    		// Top Face
    		glNormal3f( 0.0f, 0.5f, 0.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  0.5f, -0.5f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f,  0.5f,  0.5f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f,  0.5f,  0.5f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f,  0.5f, -0.5f);
    		// Bottom Face
    		glNormal3f( 0.0f,-1.0f, 0.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f,  0.5f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f,  0.5f);
    		// Right face
    		glNormal3f( 0.5f, 0.0f, 0.0f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f, -0.5f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f,  0.5f, -0.5f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.5f,  0.5f,  0.5f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.5f, -0.5f,  0.5f);
    		// Left Face
    		glNormal3f(-0.5f, 0.0f, 0.0f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f,  0.5f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f,  0.5f,  0.5f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  0.5f, -0.5f);        
    	glEnd();
        glPopMatrix();													// NEW: Unapply Dynamic Transform
    
    	glFlush ();														// Flush The GL Rendering Pipeline
    }
    Thanks for reading this.

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    It doesn't look like xpos and ypos are ever set, which could be why they are always zero.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >GetCursorPos(&Pt);
    >Pt.x=xpos;
    >Pt.y=ypos;

    You get the cursor position, and then you immediately re-assign it to some other value. I think your assignment is backwards.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    I fixed the cordinates. Now I just need help to get the thing right, that when I click I want the sphere to stay there and not disapear when I release the button, and when I click again it will add another ball and the first one will not disapear.
    Right now it adds a sphere when I hold down the button, and then the sphere diasapears when I release the button.

    Thanks

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You need to keep a list of spheres and draw each one every frame.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Any example, even a small one would be enough?

    Thanks

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Code:
    #define NUM_SPHERES 100 //change this to whatever you want
    
    struct Sphere{
         float x,y,z;
    };
    
    class CSphere : Sphere{
         public:
         void NewSphere(float x, float y, float z);
    };
    
    //..//
    
    void NewSphere(float x, float y, float z){
         sphereList[currentSphere] = new CSphere();
         sphereList[currentSphere].x = x;
         //etc etc...
         currentSphere++;
    }
    
    CSphere *sphereList[NUM_SPHERES];
    This code is incomplete, and probably won't compile, but it should give you an idea of how this works. I've also left writing a drawing function that uses the CSphere info to you.

    EDIT: the header and source is kind of mashed together too. You should be able to seperate that yourself though.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the position of the mouse cursor
    By Mavix in forum Game Programming
    Replies: 5
    Last Post: 12-27-2007, 03:02 PM
  2. simple mouse movement program
    By skankles in forum Windows Programming
    Replies: 3
    Last Post: 05-27-2007, 07:54 PM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 01:10 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. Replies: 0
    Last Post: 02-05-2002, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21