Technique for moving texture at mouse move coordinates

Started by
16 comments, last by aewarnick 20 years, 2 months ago
How do I draw a texture at the mouse move coordinates. I know I may need to use glOrtho but I do not see how I can plug the mouse coordinates into it and have the texture drawn at the cursor.
*C++*->
Advertisement
glTranslatef(mouseX, mouseY, 0);

Enigma
I don''t see how that can be done. All I can put in translate are low numbers. Or else the image is way off the screen.
*C++*->
use an ortho projection which has a size which matches your window size (or full screen if in fullscreen mode), then your mouse coords will match
Like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, this->W, this->H, 0, 0, 0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(mouseX, mouseY, 0);

It is a tiny little dot in the back of the window. Something is wrong. Here is the initialization code too:

glViewport(0,0, 892, 672);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100, (GLfloat)892/(GLfloat)672, .1f, 100);
*C++*->
Have you scaled up your quad?

Enigma
I don''t know. Here is the drawing code:
glBegin(GL_QUADS);        glTexCoord2f (0.0f,0.0f); /* lower left corner of image */glVertex3f (-1.0f, -1.0f, 0);        glTexCoord2f (1.0f, 0.0f); /* lower right corner of image */glVertex3f (1.0f, -1.0f, 0);        glTexCoord2f (1.0f, 1.0f); /* upper right corner of image */glVertex3f (1.0f, 1.0f, 0);        glTexCoord2f (0.0f, 1.0f); /* upper left corner of image */glVertex3f (-1.0f, 1.0f, 0);    glEnd ();	SwapBuffers(this->DrawDC); 
*C++*->
the textures size is 512x512. I put the size of the image W and H in for the x and y of the Vertex for each corner. It draws the image over twice the size that it really is. How can I fix that?
glBegin(GL_QUADS);	glTexCoord2f (0.0f,0.0f); /* lower left corner of image */glVertex3f (-abmp.W, -abmp.H, 0);        glTexCoord2f (1.0f, 0.0f); /* lower right corner of image */glVertex3f (abmp.W, -abmp.H, 0);        glTexCoord2f (1.0f, 1.0f); /* upper right corner of image */glVertex3f (abmp.W, abmp.H, 0);        glTexCoord2f (0.0f, 1.0f); /* upper left corner of image */glVertex3f (-abmp.W, abmp.H, 0);    glEnd ();	SwapBuffers(this->DrawDC); 
*C++*->
You need to make your quad bigger. Setting your orthographic projection to the size of the screen means each unit is one pixel, so your quad is 2 pixels by 2 pixels! Try changing it to:
glBegin(GL_QUADS);	glTexCoord2f (0.0f,0.0f); /* lower left corner of image */glVertex3f (-32.0f, -32.0f, 0);	glTexCoord2f (1.0f, 0.0f); /* lower right corner of image */glVertex3f (32.0f, -32.0f, 0);	glTexCoord2f (1.0f, 1.0f); /* upper right corner of image */glVertex3f (32.0f, 32.0f, 0);	glTexCoord2f (0.0f, 1.0f); /* upper left corner of image */glVertex3f (-32.0f, 32.0f, 0);glEnd (); 


This will centre the quad at the position of the cursor. To place the quad such that the top left hand corner is in the position of the cursor, either add a glTranslatef(32, 32, 0) before it or change all the -32.0f''s to 0.0f and the 32.0f''s to 64.0f''s. Adjust values for effect.

Enigma
That worked, although now it is way too small. It is about 50x50 now when it should be 512x512.
*C++*->

This topic is closed to new replies.

Advertisement