我试图用鼠标的被动运动绘制一条线(它来自一本书),但我无法做到。
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();
}
在这里失踪了吗?
答案 0 :(得分:1)
一个错误是你在glBegin / End对之间有glLogicOp调用。在开始和结束之间允许很少的OpenGL调用。这样做是这样的:
glLogicOp(GL_XOR);
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
glLogicOp(GL_COPY);
答案 1 :(得分:1)
如果这段代码真的来自书中,那么那本书太可怕了,你应该在它损害你的大脑之前放弃它,然后寻找更好(和免费)的东西,比如NeHe tutorials。
我看到例如线条的严重问题......
xf = x/500;
yf = (500-y)/500;
xs = x/500;
ys = (500-y)/500;
你将xf,yf和xs,ys设置为相同的值(所以你的行的长度为0)......而且假设x
和y
是整数,那么这些划分将是可能总是给你0的结果。
答案 2 :(得分:0)
这是直接从书中复制的吗?如果是这样,我建议你停止阅读! 几乎在所有情况下,任何绘图调用都应该在DisplayFunc的开始和结束之间完成。所以你应该把你的程序改为:
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);
glColor3(1.0f, 1.0f, 1.0f);
if( flag == 1)
{
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
}
glLogicOp(GL_XOR);
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd();
glLogicOp(GL_COPY);
glutSwapBuffers();
}
void move(int x, int y)
{
xf = x/500.0f;
yf = (500.0f-y)/500.0f;
xs = x/500.0f;
ys = (500.0f-y)/500.0f;
}
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();
}
编辑:Ville Krumlinde当然是对的,你不能在Begin和End子句中做glLogicOp,我已经更新了上面的代码来包含修复。