在特定视口中绘制相机位置

发布于 2024-10-10 08:15:59 字数 2887 浏览 5 评论 0原文

大部分代码应该是相当不言自明的。我有一个显示功能和我的视口功能。有两种模式:窗口中 4 个小视口或 1 个大视口。 我有一台可以移动的相机,如果处于 4 视口模式,则只有 3 个固定角度。问题是我希望自由移动的摄像机位置显示在其他 3 个视口中。我尝试通过使用 opengl 绘制球体来实现此目的,但问题是,该位置也会在自由漫游相机中绘制,因为它显示相同的场景。

它不一定是一个球体,只是一个简单的东西,代表相机在这三个其他视图中的空间位置。

使用为三个视口显示的相机对象绘制一次场景,渲染到纹理。在没有相机对象渲染到纹理的情况下清除并绘制场景,然后在实际绘制场景之前将它们缝合在一起,这对于应该很容易的事情来说似乎需要做很多工作。

void display(int what)
    {
        if(what==5){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        camControll();}

        if(what==1){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(75,15,-5,0,5,-5,0,1,0);}

        if(what==2){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0,110,0,0,0,0,1,0,0);}

        if(what==3){
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, float(320) / float(240), 0.1f, 100.0f); 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        camControll();}

        if(what==4){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(185,75,25,0,28,0,0,1,0);}



        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        drawScene();
        drawCamera();
        glutSwapBuffers();
    }

    void viewport(){
    glEnable(GL_SCISSOR_TEST);

        if(!divided_view_port)
        {
        glViewport(0, 0, w, h);
        glScissor(0,0,640,480);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 100.0f);
        display(5);
        }

    else
    {
        ////////////////////// bottom left - working
        glViewport(0, 0, w/2, h/2);
        glScissor(0,0,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(1);
        //////////////////////


        ////////////////////// top right - working
        glViewport(w/2, h/2, w/2, h/2);
        glScissor(w/2,h/2,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(2);
        //////////////////////

        ////////////////////// bottom right -working
        glViewport(w/2, 0, w/2, h/2);
        glScissor(w/2,0,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(3);
        ////////////////////////

        ////////////////////////// top left
        glViewport(0, h/2, w/2, h/2);
        glScissor(0,h/2,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(4);
        ///////////////////////////
    }

    glDisable(GL_SCISSOR_TEST);
    glMatrixMode(GL_MODELVIEW);
    }

所以我基本上需要的是在特定视口中隐藏这个对象。

Most of this code should be fairly self explanatory. I got an display function and my view port function. There are two modes which is 4 small view ports in the window or one large.
I got one camera which can be moved and if in 4 view port mode just 3 fixed angles. The thing is I want the free moving cameras position to be displayed in the 3 other view ports. I tried doing it by drawing spheres using opengl but the problem is that then the position gets draw in the free roaming camera too as it shows the same scene.

It doesn't have to be a sphere, just something simple that represents the cameras spacial position in these three other views.

Drawing the scene once with camera object showing for the three viewports, render to texture. Clear and draw scene without camera object render to texture and then stitch these together before actually drawing the scene seems like a lot o work for something that should be easy.

void display(int what)
    {
        if(what==5){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        camControll();}

        if(what==1){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(75,15,-5,0,5,-5,0,1,0);}

        if(what==2){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0,110,0,0,0,0,1,0,0);}

        if(what==3){
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, float(320) / float(240), 0.1f, 100.0f); 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        camControll();}

        if(what==4){
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(185,75,25,0,28,0,0,1,0);}



        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        drawScene();
        drawCamera();
        glutSwapBuffers();
    }

    void viewport(){
    glEnable(GL_SCISSOR_TEST);

        if(!divided_view_port)
        {
        glViewport(0, 0, w, h);
        glScissor(0,0,640,480);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 100.0f);
        display(5);
        }

    else
    {
        ////////////////////// bottom left - working
        glViewport(0, 0, w/2, h/2);
        glScissor(0,0,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(1);
        //////////////////////


        ////////////////////// top right - working
        glViewport(w/2, h/2, w/2, h/2);
        glScissor(w/2,h/2,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(2);
        //////////////////////

        ////////////////////// bottom right -working
        glViewport(w/2, 0, w/2, h/2);
        glScissor(w/2,0,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(3);
        ////////////////////////

        ////////////////////////// top left
        glViewport(0, h/2, w/2, h/2);
        glScissor(0,h/2,w/2,h/2);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, w / h, 0.1f, 300.0f);
        display(4);
        ///////////////////////////
    }

    glDisable(GL_SCISSOR_TEST);
    glMatrixMode(GL_MODELVIEW);
    }

So what I basically need is to hide this object in specific viewport.

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

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

发布评论

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

评论(3

毁梦 2024-10-17 08:15:59

为什么不让单个 Sphere 对象(或负责绘制球体的实体)了解“当前视口”(恰好是代码中的 what 变量)并让它不可见(如果它是)给定的视口?

这个解决方案与这里涉及​​的逻辑完全对应,听起来既简单又正确。

更通用的解决方案是为每个“相机”提供一个 GUID,并使其可供负责绘制相机的实体检查绑定到当前正在渲染的视口的“相机”的 GUID。如果它们恰好相等,则在此绘制过程中忽略相机对象。

Why not make that single Sphere object (or the entity responsible for drawing the sphere) aware of the "current viewport" (which happens to be the what variable in your code) and let it be invisible if it's the given viewport?

This solution exactly corresponds to the logic involved here sounds both simple and correct.

A more general solution would be to give each "camera" a GUID and make it available for the entity responsible for drawing Cameras to check the GUID of the "camera" bound to the viewport which is being rendered at the moment. If they happen to be equal, ignore the camera object during this draw pass.

夜唯美灬不弃 2024-10-17 08:15:59

我认为如果你只绘制点,这应该很容易,因为如果你想在视口中看到点,它的中心必须在视口中,否则即使你设置巨大的点大小,也不会显示任何内容。然后,您有 2 个选项可以消除闪烁效果(因为当您将 2 个方块放在同一位置时,它们会相互闪烁)。您可以将该点稍微移到相机后面,或者在 glFrustrum/gluPerspective 调用中对近剪裁平面使用非零值。如果每次移动相机时更新点位置,您就没有机会在移动相机中看到该点并且您可以使用单个场景。

第二个选项,我不知道你是否可以只更新单个视口,但也许只是设置场景,将其显示到移动相机,绘制相机位置并为其他 3 个视口显示它也应该很容易。

I think that should be easy if you would just draw point, because if you want to see point in viewport, its center have to be in viewport, otherwise nothing of it is displayed even if you set huge point size. Then you have 2 options to eliminate flickering effect (as when you put 2 squares in the very same possition they will flicker one over another). You can just move that point little behind camera, or use nonzero value for near clipping plane in glFrustrum/gluPerspective call.. and well if you update point position every time you move camera you have no chance of seeing that point in your moving camera and you can use single scene.

And second option, I don't know if you can update just single viewport, but maybe just setting scene, displaying it to moving camera, drawing camera position and displaying it for other 3 viewports should be easy also..

相权↑美人 2024-10-17 08:15:59

为什么不在移动摄像机的近平面后面绘制球体?这应该确保移动的摄像机根本看不到球体,但它的位置为其他摄像机清楚地标记了。

Why don't you draw the sphere behind the moving camera's near plane? That should ensure that the moving camera doesn't see the sphere at all, but its position is clearly marked for the others.

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