如何在 OpenGL 中绘制旋转棱镜?

发布于 2024-12-06 13:38:45 字数 1230 浏览 0 评论 0原文

我正在尝试绘制一个绕其轴旋转的简单晶体。我可以通过绘制金字塔然后将其颠倒再次绘制来轻松获得正确的形状,但我有两个问题。

首先,尽管我用相同的颜色绘制所有内容,但其中两张脸的颜色与其他两张脸的颜色不同。

其次,它在每个金字塔上放置了一个“底部”,通过水晶的半透明壁可见,这破坏了效果。有什么办法可以摆脱它吗?

这是我用来设置和绘制 GL 场景的代码。当然,还有比这更多的 OpenGL 代码,但这是相关部分。

procedure Initialize;
begin
  glShadeModel(GL_SMOOTH);
  glClearColor(0.0, 0.0, 0.0, 0.5);
  glClearDepth(1.0);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
end;

procedure Draw; //gets called in a loop
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(-1.5,-0.5,-6.0);
  glRotatef(rotation,0.0,1.0,0.0);
  glBegin(GL_TRIANGLE_FAN);
    glColor4f(0, 0, 1, 0.2);
    glVertex3f(0, 3.4, 0);
    glVertex3f(-1, 0, -1);
    glVertex3f(-1, 0, 1);
    glVertex3f(1, 0, 1);
    glVertex3f(1, 0, -1);
    glVertex3f(-1, 0, -1);
  glEnd;
  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0, -3.4, 0);
    glVertex3f(-1, 0, -1);
    glVertex3f(-1, 0, 1);
    glVertex3f(1, 0, 1);
    glVertex3f(1, 0, -1);
    glVertex3f(-1, 0, -1);
  glEnd;
  rotation := rotation + 0.02;
end;

有人知道我做错了什么以及如何解决它吗?

I'm trying to draw a simple crystal that rotates on its axis. I can get the shape right easily enough by drawing a pyramid and then drawing it again upside down, but I've got two problems.

First off, even though I draw everything in the same color, two of the faces come out a different color as the other two.

Second, it's placing a "bottom" on each pyramid that's visible through the translucent walls of the crystal, which ruins the effect. Is there any way to get rid of it?

Here's the code I'm using to set up and draw the GL scene. There's a lot more OpenGL code than this, of course, but this is the relevant part.

procedure Initialize;
begin
  glShadeModel(GL_SMOOTH);
  glClearColor(0.0, 0.0, 0.0, 0.5);
  glClearDepth(1.0);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
end;

procedure Draw; //gets called in a loop
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(-1.5,-0.5,-6.0);
  glRotatef(rotation,0.0,1.0,0.0);
  glBegin(GL_TRIANGLE_FAN);
    glColor4f(0, 0, 1, 0.2);
    glVertex3f(0, 3.4, 0);
    glVertex3f(-1, 0, -1);
    glVertex3f(-1, 0, 1);
    glVertex3f(1, 0, 1);
    glVertex3f(1, 0, -1);
    glVertex3f(-1, 0, -1);
  glEnd;
  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0, -3.4, 0);
    glVertex3f(-1, 0, -1);
    glVertex3f(-1, 0, 1);
    glVertex3f(1, 0, 1);
    glVertex3f(1, 0, -1);
    glVertex3f(-1, 0, -1);
  glEnd;
  rotation := rotation + 0.02;
end;

Anyone know what I'm doing wrong and how to fix it?

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

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

发布评论

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

评论(1

回首观望 2024-12-13 13:38:45

我正在尝试画一个简单的水晶

停止点。晶体是半透明的,当你开始绘制半透明物体的那一刻,你基本上可以放弃任何“简单”效果的概念。渲染真正的棱镜(对不同波长的光进行不同的折射)需要某种形式的光线追踪才能正确完成。有许多光线追踪器甚至无法做到正确,因为它们只追踪 R、G 和 B 波长,而您需要追踪许多波长来近似一个物体的折射和分光模式。棱镜。

你能得到的最好的结果是在像 OpenGL 这样的光栅化器上有一定程度的伪造。

我无法解释这些面孔发生了什么,但看透其他多边形的问题很简单:您没有使用 背面剔除。除非您想看到透明物体的背面,否则您需要确保背面剔除处于活动状态。

I'm trying to draw a simple crystal

Stop. Crystals are translucent, and the moment you start drawing translucent objects, you can basically discard any notion of the effect being "simple". Rendering a true prism (which refracts different wavelengths of light differently) is something that requires raytracing of some form to get right. And there are many ray tracers that can't even get it right, since they only trace R, G and B wavelengths, whereas you need to trace many wavelengths to approximate the refraction and light splitting pattern of a prism.

The best you're going to get is on a rasterizer like OpenGL some level of fakery.

I can't explain what's going on with the faces, but the problem with seeing through to the other polygons is simple: you're not using backface culling. Unless you want to see the back faces of transparent objects, you need to make sure that backface culling is active.

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