如何在 openGL 中制作一个光球?

发布于 2024-08-15 23:29:01 字数 1254 浏览 8 评论 0原文

我正在尝试制作一个光球(如太阳),但我似乎根本无法使其可见。我会给你一些我拥有的代码片段。它是用 Java LWJGL 编写的,所以看起来可能有点不同。

private float lightAmbient[] = { 0.0f, 1.0f, 1.0f, 1.0f };  // Ambient Light Values ( NEW )
    private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values ( NEW )
    private float lightPosition[] = { 0.0f, 0.0f, -5.0f, 1.0f }; // Light Position ( NEW )
    float lightSpecular[] = { 0f, 0f, 0.5f, 1.0f };  // highlight

ByteBuffer temp = ByteBuffer.allocateDirect(16);
        temp.order(ByteOrder.nativeOrder());
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_SPECULAR,(FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());         // Position The Light

        GL11.glEnable(GL11.GL_LIGHT1); 

我还需要做什么才能使光线可见

I'm trying to make a orb of light (Like a sun) but I can't seem to make it visible at all. I'll give you some snipets of code I have. It's in Java LWJGL, so it might look a little different.

private float lightAmbient[] = { 0.0f, 1.0f, 1.0f, 1.0f };  // Ambient Light Values ( NEW )
    private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values ( NEW )
    private float lightPosition[] = { 0.0f, 0.0f, -5.0f, 1.0f }; // Light Position ( NEW )
    float lightSpecular[] = { 0f, 0f, 0.5f, 1.0f };  // highlight

and

ByteBuffer temp = ByteBuffer.allocateDirect(16);
        temp.order(ByteOrder.nativeOrder());
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
        GL11.glLight(GL11.GL_LIGHT1, GL11.GL_SPECULAR,(FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());         // Position The Light

        GL11.glEnable(GL11.GL_LIGHT1); 

What else do I have to do to make the light visible?

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

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

发布评论

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

评论(3

池木 2024-08-22 23:29:01

灯永远不可见。

然而,它们对其他物体材质的影响却很大。因此,场景中的所有对象都设置适当的材质属性非常重要。

您可能会对这篇文章感兴趣,其中提到了人们在以下情况下犯的所有常见错误试图照亮场景。

alt text

您将需要查找 glColorMaterial 函数,因为它们是设置材质属性的最简单方法。

Lights are never visible.

However, their affects on the materials of other objects are. It's therefore important that all objects in the scene have appropriate material properties set.

You may be interested in this article that mentions all the common mistakes one makes when trying to illuminate scenes.

alt text

You will want to look up the glColorMaterial functions as they are the easiest way to set material properties.

随梦而飞# 2024-08-22 23:29:01

正如弗兰克所说,灯光是不可见的,它们仅用作渲染系统(硬件/软件)用于计算几何体的最终颜色的照明方程的输入。

如果您希望某些东西看起来像光,您需要模仿您在观看明亮光源时所看到的效果。对于太阳来说,最明显的效果之一就是光晕。

有关生成光绽放的信息,请阅读OpenGL Bloom 教程,这里有一些Java 中使用 GLSL 生成光晕效果的示例代码

Like Frank said, lights are invisible, they are merely used as an input to the lighting equation used by the rendering system (hardware/software) to calculate the final colors of geometry.

If you want something to look like a light you need to mimic the effects that you are used to seeing when looking at a bright light source. For the sun one of the most noticeable effects is light bloom.

For information on generating light bloom read this OpenGL Bloom Tutorial, and here is some sample code in Java using GLSL to generate a bloom effect.

清风无影 2024-08-22 23:29:01

根据您是否想要盛开风格的效果(如果是这样,请查看 Josh Perry 的答案)或只是一个看起来发光的非暗对象,您可能需要考虑这种简单的方法。

  • 在没有实际“太阳”网格的情况下渲染场景,但在“太阳”中心使用点光源。
  • 禁用 GL 照明。
  • 渲染球体或使用广告牌在适当的位置渲染太阳的图像。
  • 在显示下一帧之前重新启用 GL 光照。

由于您已禁用照明,因此太阳将被渲染为“全亮”,并且不会执行任何着色或照明计算。

Depending on if you want full bloom style effects (if so, look at Josh Perry's answer) or just a not-dark object object that seems to emit light you might want to consider this simple approach.

  • Render the scene without the actual 'sun' mesh but with a point light at the centre of your 'sun'.
  • Disable GL Lighting.
  • Render a sphere or use a billboard to render an image of your sun in the appropriate spot.
  • Re-enable GL Lighting before you display your next frame.

Because you've disabled lighting, the sun will be rendered 'full-bright' with no shading or lighting calculations being performed.

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