PDA

View Full Version : problems with drawing mouse cursor!



pran1
03-23-2004, 07:30 AM
Hi! I dont know if this is an advance problem but I am really stuck and was wondering if some could help me. I am trying to draw two mouse cursor that will eventually be assigned to stereo images but for now its just two mouse cursors. I have written the code it compiles well but the problem comes in this function. It says that it cannot compute mouse_cursor as (x / window_height) window_height is always zero and if I use the image's width and height to compute the mouse_cursor then the display is nothing like a mouse should behave.

void Mouse_motion(int x, int y)
{
// check for the window height to be 0 byt if I use this it always crashes saying that cannot divide x by 0 (window Height and width)
// there is no display !
/* if((window_height > 0) && (window_width > 0))
{
// convert mouse coords to openGL coords, guessed on the values.
mouse_current_x = -28 + 28 * (x / window_height) ;
mouse_current_y = 28 - 28 * (y / window_width);
//printf("got it window height not zero\n");
} */

// I try to assign world coordinates to mouse coordinates this way but image dosent display properly then!
mouse_current_x = -28 + 28 * (x / image_height) ;
mouse_current_y = 28 - 28 * (y / image_width);

glutPostRedisplay ( );
//printf("x = %d, y = %d", x,y);
}

The images being used are not being displayed properly. I am pasting the code below and hope if you could help me .

Many thanks for your time and efforts.

Regards

pran

THE MAIN CODE

#include<windows.h>
#include <glut.h>
#include <gl.h>
#include<stdlib.h>
#include <stdio.h>
#include <math.h>
#include "bitmap.h"

int window_height, window_width, image_height, image_width; //image_height1, image_width1;
int mouse_current_x, mouse_current_y;

int offset_x = 0, offset_y = 0; // This would be the offset of the stereo image from the left to right, adjust this value until the cursors are the correct distance apart.


BITMAPINFO *BitmapInfo; // Bitmap information
GLubyte *BitmapBits; // Bitmap data
//BITMAPINFO *BitmapInfo1; // Bitmap information
//GLubyte *BitmapBits1; // Bitmap data
GLuint texture[2];

void Mouse_cursor(int x, int y, int shape)
{

glPushMatrix(); // This saves the matrix stack so that the following translate does not effect other objects
glTranslatef(x, y, 0);

glBindTexture ( GL_TEXTURE_2D, texture[shape]);

// Over here I have tried to assing the coordinates of the mouse instead of they ebing constant!!!

glBegin ( GL_QUADS );
glTexCoord2d ( 0, 0 ); glVertex2f ( mouse_current_x, mouse_current_y);
glTexCoord2d ( 0, 1 ); glVertex2f ( mouse_current_x, mouse_current_y + 64);
glTexCoord2d ( 1, 1 ); glVertex2f ( mouse_current_x + 64, mouse_current_y + 64);
glTexCoord2d ( 1, 0 ); glVertex2f ( mouse_current_x + 64,mouse_current_y);

glEnd ( );
glPopMatrix(); // Restore matrix to before start of routine
}


void Mouse_motion(int x, int y)
{
// check for the window height to be 0 byt if I use this it always crashes saying that cannot divide x by 0 (window Height and width)
// there is no display !
/* if((window_height > 0) && (window_width > 0))
{
// convert mouse coords to openGL coords, guessed on the values.
mouse_current_x = -28 + 28 * (x / window_height) ;
mouse_current_y = 28 - 28 * (y / window_width);
//printf("got it window height not zero\n");
} */

// I try to assign world coordinates to mouse coordinates this way but image dosent display properly then!
mouse_current_x = -28 + 28 * (x / image_height) ;
mouse_current_y = 28 - 28 * (y / image_width);

glutPostRedisplay ( );
//printf("x = %d, y = %d", x,y);
}

int loadbitmap( char *filename, int texture_number)
{
BitmapBits = LoadDIBitmap(filename,&BitmapInfo);
printf("loaded image\n");
image_width = BitmapInfo->bmiHeader.biWidth ;
image_height = BitmapInfo->bmiHeader.biHeight;
glEnable ( GL_TEXTURE_2D );
//glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1, &texture[texture_number]);

glBindTexture(GL_TEXTURE_2D, texture[texture_number]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image_width ,image_height, 0, GL_RGB,GL_UNSIGNED_BYTE, BitmapBits);

free(BitmapInfo);
free (BitmapBits);
return texture[texture_number];
}


int InitGL(GLvoid) // All setup for OpenGL goes here
{


glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); // Black Background
glClearDepth(1.0f);
glEnable ( GL_TEXTURE_2D );
}

void ReshapeGL (int Width, int Height )
{
window_height = Height;
window_width = Width;
glViewport ( 0, 0, Width, Height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
//glOrtho(-28.0, 28.0, -28.0, 28.0, 0.0, 30.0);
gluPerspective ( 45, (float)Width/(float)Height, 1, 100 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}

void DrawGL ( )
{
// static int angle=0;

glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity ( );
gluLookAt ( 0, 0, -20, 0, 0, 0, 0, 1, 0 );
//glRotated( angle++, 1,1,1); // Where 1 = rotate axis, 0 = no rotate axis
//Draw right cursor
Mouse_cursor(mouse_current_x, mouse_current_y, 0); // Draw cursor 0 for image 1
//Draw left cursor
Mouse_cursor(mouse_current_x + 6 , mouse_current_y - 6, 1);

glutSwapBuffers ( );

}

void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}


}


int main( int argc, char *argv[ ], char *envp[ ] )
{

glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition ( 50, 50 );
glutCreateWindow ( "MOUSE CURSOR" );
InitGL ( );
loadbitmap("mouse.bmp", 0);
loadbitmap("tex.bmp", 1);
glutReshapeFunc (ReshapeGL);
glutPassiveMotionFunc(Mouse_motion);

glutDisplayFunc (DrawGL);
glutKeyboardFunc(keyboard);
glutMainLoop ( );
return 0;
}