glRotatef问题

发布于 2024-10-11 08:26:10 字数 360 浏览 3 评论 0原文

我正在尝试一个非常基本的代码示例。我绘制一个 opengl 对象,然后用键盘旋转它。我在不使用键盘的情况下尝试了同样的操作。我打开一个线程,每 5 秒调用相同的 glRotatef 函数(与我在键盘函数中使用的相同的 glrotatef)。但没有轮换。问题是什么?下面是我的线程代码:

void movePlayer2()
{
  while(1)
  //if(key == 'a')
  { 
    Sleep(5000);
    glRotatef(25,1,0,0);
    //gluLookAt(0,0,0.01,0,0,-5,0,1,0);
    display();
  }
}

I am trying a very basic code sample. I draw an opengl object and with a keyboard I rotate it. I tried same thing without using keyboard. I open a thread and I invoked same glRotatef function (same glrotatef I use in keyboard function) for every 5 seconds. But there is no rotation. What is the problem? Here is my thread code below:

void movePlayer2()
{
  while(1)
  //if(key == 'a')
  { 
    Sleep(5000);
    glRotatef(25,1,0,0);
    //gluLookAt(0,0,0.01,0,0,-5,0,1,0);
    display();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

椒妓 2024-10-18 08:26:11

这是我的基本代码:

int main(int argc, char **argv)
{
    initGL(argc, argv);
    HANDLE thread_id2=CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)f , NULL , 0 , 0 );
    HANDLE thread_id22=CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)movePlayer2 , NULL , 0 , 0 );

    glutMainLoop();


    return 0;
}



void initGL(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(512, 512);
    glutInit(&argc, argv);

    glutCreateWindow("deneme");
    glutDisplayFunc(display);




}




void display()
{


    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);


    glutWireTeapot(0.5);

    glutSwapBuffers();


}





void f()
{
while(1)

    {   


        Sleep(5000);

        glRotatef(0.2,1,0,0);

        glutPostRedisplay();
        printf("dsdasd\n");


        display();
    }
}

here is my basic code :

int main(int argc, char **argv)
{
    initGL(argc, argv);
    HANDLE thread_id2=CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)f , NULL , 0 , 0 );
    HANDLE thread_id22=CreateThread( NULL , 0 , (LPTHREAD_START_ROUTINE)movePlayer2 , NULL , 0 , 0 );

    glutMainLoop();


    return 0;
}



void initGL(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(512, 512);
    glutInit(&argc, argv);

    glutCreateWindow("deneme");
    glutDisplayFunc(display);




}




void display()
{


    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);


    glutWireTeapot(0.5);

    glutSwapBuffers();


}





void f()
{
while(1)

    {   


        Sleep(5000);

        glRotatef(0.2,1,0,0);

        glutPostRedisplay();
        printf("dsdasd\n");


        display();
    }
}
柠檬心 2024-10-18 08:26:10

OpenGL 上下文与线程绑定,即只要 OpenGL 上下文被绑定(通过 gl*MakeCurrent),只有从上下文所绑定的线程进行的函数调用才会影响该上下文。您可以将上下文绑定到另一个线程(首先将其取消绑定)。或者简单地说,它根本不按照你的想法工作。

然而你的做法无论如何都是完全错误的。 OpenGL 不是场景图。 glRotatef 仅影响模型视图矩阵。 OpenGL 不认识对象,它只认识基元。

OpenGL 中的基本模式是,您在绘制之前设置所需的所有状态。因此,您在循环中调用显示函数,在其中设置视口和投影,然后迭代所有对象,为每个对象设置适当的模型视图矩阵。根据您的应用程序,您可以在显示功能中多次执行这些步骤。在您的情况下,您的计时器应该做的是增加旋转角度,然后在显示函数中使用该角度来创建正确的模型视图矩阵。

OpenGL contexts are tied to threads, i.e. as long as a OpenGL context is bound (by gl*MakeCurrent) only functions calls made from the thread the context has been bound to, will affect that context. You can bind a context to another thread (first unbind it). Or in simple words, it simply doesn't work the way you think.

However you approach is totally wrong anyway. OpenGL is not a scene graph. glRotatef affects just the modelview matrix. And OpenGL doesn't know objects, it just knows primitives.

The basic pattern in OpenGL is, that you set all the state you need, just right before drawing. So you call the display function in a loop, therein set viewport and projection, then iterate through all objects, for each object set the proper modelview matrix. And depending on your application you may do those steps multiple times within the display function. And in your case, what your timer should do is incrementing the rotation angle, which is then used within the display function to create the proper modelview matrix.

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