opengl中的被动运动
我试图用鼠标的被动运动画一条线(来自一本书),但我无法做到这一点。
float xf, yf, xs, ys;
int flag=0;
void setupmywindow()
{
glClearColor(0,0,0,0);
gluOrtho2D(0,100,0,100);
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
void move(int x, int y)
{
if( flag == 1)
{
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
}
xf = x/500;
yf = (500-y)/500;
xs = x/500;
ys = (500-y)/500;
glBegin(GL_LINES);
glLogicOp(GL_XOR);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glLogicOp(GL_COPY);
glEnd() ;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("My window");
setupmywindow();
glutDisplayFunc(myDisplay);
glutPassiveMotionFunc(move);
glutMainLoop();
}
这里缺少什么吗?
i am trying to draw a line with passive motion of mouse (it's from a book) but i can't manage to do it.
float xf, yf, xs, ys;
int flag=0;
void setupmywindow()
{
glClearColor(0,0,0,0);
gluOrtho2D(0,100,0,100);
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
void move(int x, int y)
{
if( flag == 1)
{
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
}
xf = x/500;
yf = (500-y)/500;
xs = x/500;
ys = (500-y)/500;
glBegin(GL_LINES);
glLogicOp(GL_XOR);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glLogicOp(GL_COPY);
glEnd() ;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("My window");
setupmywindow();
glutDisplayFunc(myDisplay);
glutPassiveMotionFunc(move);
glutMainLoop();
}
Is sth missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个错误是在 glBegin/End 对之间有 glLogicOp 调用。开始和结束之间只允许很少的 OpenGL 调用。这样做:
One error is that you have glLogicOp-calls between glBegin/End pairs. Very few OpenGL calls are allowed between begin and end. Do it like this instead:
如果这段代码确实来自这本书,那么那本书就太可怕了,你应该在它损害你的大脑之前放弃它,转而寻求更好的(免费的)东西,比如 NeHe 教程 代替。
例如,我发现线条存在严重问题...
您将 xf、yf 和 xs、ys 设置为相同的值(因此您的线条长度将为 0)...而且考虑到
x 和
y
是整数,这些除法可能总是得到 0 的结果。If this code is really from the book then that book is horrible, you should just drop it before it damages your brain and go for something better (and free) like NeHe tutorials instead.
I see for example a serious problem with the lines...
you are setting xf, yf and xs, ys to the same values (so your line will be of length 0) ... and moreover given that
x
andy
are integers those divisions will probably always give you a result of 0.这是直接从书上抄来的吗?如果是这样,我建议你停止阅读它!
在几乎所有情况下,任何绘图调用都应在 DisplayFunc 的开始和结束之间完成。所以你应该将你的程序更改为:
编辑:Ville Krumlinde 当然是对的,你不能在 Begin 和 End 子句中执行 glLogicOp,我已经更新了上面的代码以包含修复程序。
Is this copied straight from the book? If so I suggest you stop reading it!
In almost all cases, any drawing calls should be done between the start and the end of DisplayFunc. So you should change your program to:
EDIT: Ville Krumlinde is of course right, you can't do glLogicOp within a Begin and End clause, I have updated the above code to include the fix.