opengl 模型不能正确跨平台重叠

发布于 2024-10-18 10:47:05 字数 1511 浏览 7 评论 0原文

编辑:我在开头添加了几行新代码以进行深度缓冲,但没有成功。请参阅代码..

编辑 2:我正在通过 wxwidgets 在 wxglcanvas 上构建模型构建器。除了启用深度缓冲和 glClear(GL_DEPTH_BUFFER_BIT) 之外,我还初始化了 WX_GL_DEPTH_SIZE 为 16 和 WX_GL_DOUBLEBUFFER,但没有成功。需要注意的一件事可能有用的是,当我绘制圆锥体时,绘制圆锥体时似乎是在看圆锥体的内部而不是外部,因此它看起来好像是凹进去的。

我有一个奇怪的问题,我的 openGL 模型没有正确重叠。我正在构建一个模型生成器,以便用户可以在画布上创建和旋转模型。

例如:如果我在盒子内有一个圆圈,并且旋转模型,我仍然可以看到盒子内的圆圈,就好像盒子是透明的一样。我的想象是,如果圆圈在盒子里,我应该看不到它。有谁猜测是什么原因导致此类问题?我还没有实现任何类型的透明度,所以我不太确定是什么原因造成的。我对 openGL 还很陌生,所以我的很多努力只是尝试在论坛上找到的代码行并希望它能起作用。此外,此代码的部分内容是从其他程序继承的。

当我在添加两行代码(如下所示)后测试我的程序时,这个问题似乎只出现在Ubuntu中,而不是Win 7中。

这里有一些用于绘制模型的代码...

//Newly Added code
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_GEQUAL);
      glClearDepth(1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//End newly added code  

glGetIntegerv(GL_MATRIX_MODE, &mm);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(identity);
       GrGetInfo();

glViewport(0,0,gw->gc.width,gw->gc.height);
       GrGetInfo();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set some predefined parameters

glOrtho ((GLdouble)tmpOtherAxis.minval, (GLdouble)tmpOtherAxis.maxval, 
               (GLdouble)tmpRangeAxis.minval, (GLdouble)tmpRangeAxis.maxval,  
               -range_dist, range_dist);
glMatrixMode(mm);

//ADDED THESE TWO LINES AND MODEL DISPLAYED
//CORRECTLY IN WIN7 BUT NOT UBUNTU
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
//Draw model here
glPopMatrix();

任何帮助将不胜感激,并且请随时问我您需要的任何问题。

edit: I have added a few new lines of code to the beginning for depth buffering with no luck. See code..

edit 2: I am constructing the model builder on a wxglcanvas via wxwidgets. I have initialized WX_GL_DEPTH_SIZE of 16 and WX_GL_DOUBLEBUFFER on top of enabling depthbuffering and glClear(GL_DEPTH_BUFFER_BIT) with no luck. One thing that might be useful to note is that when I draw a cone, it seems to be drawing the cone as if I am looking at the inside of it rather than the outside portion, so it looks as if it's concaved in.

I have an odd problem where my openGL models do not overlap correctly. I am building a model builder so that the user can create and rotate a model on a canvas.

For example: If I have a circle within a box and I rotate the model around, I can still see the circle within the box as if the box is transparent. How I picture this is that if the circle is in the box, I should not be able to see it. Does anyone have any guesses to what causes this type of problem? I have not implemented any type of transparency, so I'm not really sure what is causing this. I am still very new to openGL so a lot of my efforts have just been trying lines of code I find on the forums and hoping it works. Also, parts of this code are inherited from other programs.

This problem seems to only occur in Ubuntu and not under Win 7 when I test my program after adding two lines of code (shown below).

Here are some lines of code for drawing the models...

//Newly Added code
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_GEQUAL);
      glClearDepth(1.0f);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//End newly added code  

glGetIntegerv(GL_MATRIX_MODE, &mm);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(identity);
       GrGetInfo();

glViewport(0,0,gw->gc.width,gw->gc.height);
       GrGetInfo();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set some predefined parameters

glOrtho ((GLdouble)tmpOtherAxis.minval, (GLdouble)tmpOtherAxis.maxval, 
               (GLdouble)tmpRangeAxis.minval, (GLdouble)tmpRangeAxis.maxval,  
               -range_dist, range_dist);
glMatrixMode(mm);

//ADDED THESE TWO LINES AND MODEL DISPLAYED
//CORRECTLY IN WIN7 BUT NOT UBUNTU
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
//Draw model here
glPopMatrix();

Any help would be greatly appreciated, and feel free to ask me whatever questions you need regarding this.

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

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

发布评论

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

评论(1

公布 2024-10-25 10:47:05

在我看来,你的 z 缓冲区未启用。如果没有 z 缓冲区,则只需按照发出的 OpenGL 命令的顺序绘制几何图形。实际上,不存在“背后”。

要使 z 缓冲区正常工作,您需要:

  1. 为渲染窗口请求 z 缓冲区。这是特定于平台的。
  2. 调用 glEnable( GL_DEPTH_TEST )。
  3. 使用 glClear( ... | GL_DEPTH_BUFFER_BIT );

代码在不同平台上表现不同的两种可能的解释是(a)在其中一个平台上未(正确)分配 z 缓冲区,或者(b)其中一个平台的 GL_DEPTH_TEST 的默认状态为 ON,另一个关闭。

编辑

在 Windows 应用程序中,您将在使用 wglCreateContext() 之前在 HDC 上的某个位置使用 ChoosePixelFormat/SetPixelFormat 对。这是您指定深度缓冲区的地方。

            HDC hdc = GetDC(hwnd);

            PIXELFORMATDESCRIPTOR pfd;
            memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
            pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
            pfd.nVersion = 1;
            pfd.cColorBits = 16;
            pfd.cDepthBits = 16;  // <---- !!! Depth buffer !!!
            pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
            pfd.iPixelType = PFD_TYPE_RGBA;

            int format;
            if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
                    SetPixelFormat(hdc, format, &pfd);

            HGLRC hrc = wglCreateContext(hdc);

Sounds to me like your z-buffer isn't enabled. Without a z-buffer, the geometry is simply drawn in the order of the issued OpenGL commands. Effectivly, there is no "behind".

To get the z-buffer to function properly, you'll need to:

  1. Request a z-buffer for your rendering window. This is platform specific.
  2. Call glEnable( GL_DEPTH_TEST ).
  3. Use glClear( ... | GL_DEPTH_BUFFER_BIT );

Two possible explanations why your code is behaving different on different platforms, is (a) the z-buffer not being allocated (properly) on one of the platforms, or (b) the default state of GL_DEPTH_TEST being ON for one of the platforms, OFF for the other.

Edit

In a windows application, you'll be using a ChoosePixelFormat/SetPixelFormat pair on your HDC somewhere, right before you use wglCreateContext(). That is where you specify your depth buffer.

            HDC hdc = GetDC(hwnd);

            PIXELFORMATDESCRIPTOR pfd;
            memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
            pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
            pfd.nVersion = 1;
            pfd.cColorBits = 16;
            pfd.cDepthBits = 16;  // <---- !!! Depth buffer !!!
            pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
            pfd.iPixelType = PFD_TYPE_RGBA;

            int format;
            if ((format = ChoosePixelFormat(hdc, &pfd)) != 0)
                    SetPixelFormat(hdc, format, &pfd);

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