opengl中的被动运动

发布于 2024-10-27 00:59:37 字数 861 浏览 3 评论 0原文

我试图用鼠标的被动运动画一条线(来自一本书),但我无法做到这一点。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个错误是在 glBegin/End 对之间有 glLogicOp 调用。开始和结束之间只允许很少的 OpenGL 调用。这样做:

glLogicOp(GL_XOR);
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
glLogicOp(GL_COPY);

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:

glLogicOp(GL_XOR);
glBegin(GL_LINES);
glVertex2f(xf, yf);
glVertex2f(xs, ys);
glEnd() ;
glLogicOp(GL_COPY);
看透却不说透 2024-11-03 00:59:37

如果这段代码确实来自这本书,那么那本书就太可怕了,你应该在它损害你的大脑之前放弃它,转而寻求更好的(免费的)东西,比如 NeHe 教程 代替。

例如,我发现线条存在严重问题...

xf = x/500;
yf = (500-y)/500;

xs = x/500;
ys = (500-y)/500;

您将 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...

xf = x/500;
yf = (500-y)/500;

xs = x/500;
ys = (500-y)/500;

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 and y are integers those divisions will probably always give you a result of 0.

晌融 2024-11-03 00:59:37

这是直接从书上抄来的吗?如果是这样,我建议你停止阅读它!
在几乎所有情况下,任何绘图调用都应在 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,我已经更新了上面的代码以包含修复程序。

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:

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();

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文