开发者

passive motion in opengl

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);
    glutInitWindowSi开发者_JAVA百科ze(500,500);
    glutCreateWindow("My window");
    setupmywindow();
    glutDisplayFunc(myDisplay);
    glutPassiveMotionFunc(move);
    glutMainLoop();

}

Is sth missing here?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜