java 如何在openGL中制作一个光球?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1967923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a ball of light in openGL?
提问by William
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.
我正在尝试制作一个光球(如太阳),但我似乎根本无法看到它。我会给你一些我有的代码片段。它在 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
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?
我还需要做什么才能使光线可见?
回答by Frank Krueger
Lights are never visible.
灯光永远不可见。
However, their affects on the materialsof 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 mistakesone makes when trying to illuminate scenes.
您可能对这篇文章感兴趣,其中提到了人们在尝试照亮场景时所犯的所有常见错误。


You will want to look up the glColorMaterial functions as they are the easiest way to set material properties.
您将需要查找 glColorMaterial 函数,因为它们是设置材料属性的最简单方法。
回答by joshperry
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.
就像 Frank 所说的那样,灯光是不可见的,它们仅用作渲染系统(硬件/软件)用于计算几何体最终颜色的照明方程的输入。
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 codein Java using GLSL to generate a bloom effect.
有关生成光晕的信息,请阅读此OpenGL Bloom 教程,这里是一些使用 GLSL 生成光晕效果的 Java示例代码。
回答by Adam Luchjenbroers
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.
根据您是想要盛开风格的效果(如果是,请查看 Josh Perry 的回答)还是只是一个似乎发光的非黑暗物体,您可能需要考虑这种简单的方法。
- 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 billboardto render an image of your sun in the appropriate spot.
- Re-enable GL Lighting before you display your next frame.
- 在没有实际“太阳”网格的情况下渲染场景,但在“太阳”的中心使用点光源。
- 禁用 GL 照明。
- 渲染一个球体或使用广告牌在适当的位置渲染太阳的图像。
- 在显示下一帧之前重新启用 GL Lighting。
Because you've disabled lighting, the sun will be rendered 'full-bright' with no shading or lighting calculations being performed.
因为您已禁用照明,太阳将被渲染为“全亮”,而不会执行任何阴影或照明计算。

