Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! it's my first post here and I'm a rookie with OpenGL

I use the function "glutPassiveMotionFunc" to track my mouse.But it seems to have an initial position in (0,0).
Here is short program that can demonstrate my problem:
#include<GL/glut.h>

GLint viewWidth = 400, viewHeight = 400;
int centerX = 200, centerY = 200;
int tempX, tempY;

void drawLine( int x, int y)
{
	glBegin( GL_LINES);
		glVertex2i( centerX, centerY);
		glVertex2i( x, y);
	glEnd();
}

void init()
{
	glClearColor( 0.0, 0.0, 1.0, 1.0);
	glMatrixMode( GL_PROJECTION);
	gluOrtho2D( 0.0, 400.0, 0.0, 400.0);
}

void myDisplay()
{
	glClear( GL_COLOR_BUFFER_BIT);
	glColor3f( 0.0, 0.0, 0.0);

	drawLine( tempX, tempY);

	glutSwapBuffers();
}

void myMouseMove( int x, int y)
{
	tempX = x;
	tempY = viewHeight - y;

	glutPostRedisplay();
}

void main( int argc, char ** argv)
{
	glutInit( &argc, argv);
	glutInitDisplayMode( GLUT_DOUBLE| GLUT_RGB);
	glutInitWindowPosition( 100, 100);
	glutInitWindowSize( viewWidth, viewHeight);
	glutCreateWindow( "problem demonstration");

	init();
	glutDisplayFunc( myDisplay);
	glutPassiveMotionFunc( myMouseMove);

	glutMainLoop();
}


You may run this program with the pointer outside the program window. And before you move you mouse into the program window, you can already see a black line which is the problem that confuses me.

Thanks in advance!
Posted
Updated 9-Jul-11 16:33pm
v2

1 solution

Please initialise tempX and tempY with Center values.

C#
int centerX = 200, centerY = 200;


int tempX = 200, tempY= 200;

When your application starts, it draws your screen with myDisplay() function.
In myDisplay() one line draw code exists. Which will draw a line from Center of window to an un-intialised position.
And it create an unexpected line at startup :)
 
Share this answer
 
Comments
arcodist 19-Jul-11 14:04pm    
thank you, I have already fixed it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900