简体   繁体   English

LWJGL 2d Lighting(Java)

[英]LWJGL 2d Lighting (Java)

Me and my crew just ported our game from java2d to LWJGL and want to add lighting now. 我和我的工作人员刚将我们的游戏从java2d移植到LWJGL,并希望现在添加照明。 What we exactly want to add: 我们想要添加的内容:

  • items/entities that emit light and are moveable. 发光并且可移动的物品/实体。
  • Static lighting 静态照明

We've googled for quite some time now but are kind of lost because we have no idea where to start. 我们已经搜索了很长一段时间,但有点迷失,因为我们不知道从哪里开始。 We've seen the alpha mapping method but couldn't really get it to work. 我们已经看到了alpha映射方法但却无法真正实现它。

I would be glad if someone could give me an answer. 如果有人能给我一个答案,我会很高兴的。

Thanks! 谢谢!

Creating lights is probably easier than one would think! 创建灯光可能比人们想象的要容易! The following code will create a light and enable lighting, though different parameters can be used also you can change and use things like GL_SPECULAR , etc. 以下代码将创建灯光并启用灯光,但可以使用不同的参数,您也可以更改和使用GL_SPECULAR等内容。

FloatBuffer ambient = BufferUtils.createFloatBuffer(4);
ambient.put(new float[] { 0.05f, 0.05f, 0.05f, 1f, });
ambient.flip();    

FloatBuffer position = BufferUtils.createFloatBuffer(4);
position.put(new float[] { 0f, 0f, 0f, 1f, });
position.flip();    

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModel(GL_LIGHT_MODEL_AMBIENT, ambient);
glLight(GL_LIGHT0, GL_POSITION, position);

Remember that you need to use and calculate normals, because light gets calculated using them. 请记住,您需要使用和计算法线,因为光线是使用它们计算的。

If you don't use textures and want light to be applied to simple colors, then you can add the following where you initialize your OpenGL states. 如果您不使用纹理并希望将光应用于简单颜色,则可以在初始化OpenGL状态的位置添加以下内容。

glEnable(GL_COLOR_MATERIAL);

Also if there are some part you don't want to be affected by lighting like UI, then before rendering the UI you call glDisable(GL_LIGHTING); 此外,如果某些部分您不希望受到像UI这样的光照影响,那么在渲染UI之前,您需要调用glDisable(GL_LIGHTING); and then you call glEnable(GL_LIGHTING); 然后你调用glEnable(GL_LIGHTING); again after then UI if you want lighting to be enabled afterwards. 如果您希望之后启用照明,则在UI之后再次显示。

Remember 记得

If you don't statically import classes, then just to notify you that glEnable(GL_LIGHTING); 如果你没有静态导入类,那么只是通知你glEnable(GL_LIGHTING); would be GL11.glEnable(GL11.GL_LIGHTING); 将是GL11.glEnable(GL11.GL_LIGHTING); like everything else I just showed you. 像我刚给你看的其他一切。

Also take in mind that OpenGL only contains 0 lights from GL_LIGHT0 to GL_LIGHT7 , though more lights can easily be created within different ways. 另外请记住,OpenGL只包含从GL_LIGHT0GL_LIGHT7 0个灯光,尽管可以通过不同方式轻松创建更多灯光。

Important if you think using OpenGL lights will generate shadows, then this code will disappoint you, creating shadows is a combination of OpenGL lights and some advanced FBO work, if I remember correctly. 重要的是,如果您认为使用OpenGL灯会产生阴影,那么此代码会让您失望,如果我没记错的话,创建阴影是OpenGL灯和一些高级FBO工作的组合。

Extra 额外

If you want movable lighting the you just need to update the glLight(GL_LIGHT0, GL_POSITION, position); 如果你想要可移动的照明,你只需要更新glLight(GL_LIGHT0, GL_POSITION, position); each time you move the light. 每次移动灯光。

Here is a little Lighting Types tutorial, I found just by searching "opengl lighting", the tutorial though is written in C++, but OpenGL functions are almost the same no matter which language you're using, only some tiny changes which is easy to spot and change. 这里有一个小灯光类型教程,我发现只是通过搜索“opengl lighting”,虽然教程是用C ++编写的,但无论你使用哪种语言,OpenGL函数几乎都是一样的,只有一些微小的变化很容易现场和变化。 http://www.swiftless.com/tutorials/opengl/lighting_types.html http://www.swiftless.com/tutorials/opengl/lighting_types.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM