帮助使用glutPassiveMotionFunc [英] help with glutPassiveMotionFunc

查看:197
本文介绍了帮助使用glutPassiveMotionFunc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!这是我在这里的第一篇文章,我是OpenGL的新手



我使用函数glutPassiveMotionFunc来跟踪我的鼠标。但它似乎有一个初始位置(0,0)。

这是一个可以证明我的问题的简短程序:

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!

推荐答案

请使用中心值初始化tempX和tempY。



Please initialise tempX and tempY with Center values.

int centerX = 200, centerY = 200;





int tempX = 200,tempY = 200;



申请开始时,它使用myDisplay()函数绘制屏幕。

在myDisplay()中,存在一行绘制代码。这将从窗口中心到非主动位置绘制一条线。

它在启动时创建一条意外的线:)



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 :)


这篇关于帮助使用glutPassiveMotionFunc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆