package com.nanshaws.lwjgl3; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;…
- User
package com.nanshaws.lwjgl3;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; import com.nanshaws.Box2DLightsDemo; import com.nanshaws.Nanshaws;
/** Launches the desktop (LWJGL3) application. */ public class Lwjgl3Launcher { public static void main(String[] args) { if (StartupHelper.startNewJvmIfRequired()) return; // This handles macOS support and helps on Windows. createApplication(); }
private static Lwjgl3Application createApplication() { return new Lwjgl3Application(new Box2DLightsDemo(), getDefaultConfiguration()); } private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() { Lwjgl3ApplicationConfiguration configuration = new Lwjgl3ApplicationConfiguration(); configuration.setTitle("thefristGdx-lift"); //// Vsync limits the frames per second to what your hardware can display, and helps eliminate //// screen tearing. This setting doesn't always work on Linux, so the line after is a safeguard. configuration.useVsync(true); //// Limits FPS to the refresh rate of the currently active monitor, plus 1 to try to match fractional //// refresh rates. The Vsync setting above should limit the actual FPS to match the monitor. configuration.setForegroundFPS(Lwjgl3ApplicationConfiguration.getDisplayMode().refreshRate + 1); //// If you remove the above line and set Vsync to false, you can get unlimited FPS, which can be //// useful for testing performance, but can also be very stressful to some hardware. //// You may also need to configure GPU drivers to fully disable Vsync; this can cause screen tearing. configuration.setWindowedMode(640, 480); //// You can change these files; they are in lwjgl3/src/main/resources/ . configuration.setWindowIcon("libgdx128.png", "libgdx64.png", "libgdx32.png", "libgdx16.png"); return configuration; }} 和package com.nanshaws;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.Box2D; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.PolygonShape;
import box2dLight.PointLight; import box2dLight.RayHandler;
public class Box2DLightsDemo extends ApplicationAdapter { private World world; private Box2DDebugRenderer debugRenderer; private OrthographicCamera camera; private RayHandler rayHandler; private Body groundBody;
@Override public void create() { Box2D.init(); world = new World(new Vector2(0,-9.8f), true); debugRenderer = new Box2DDebugRenderer(); camera = new OrthographicCamera(Gdx.graphics.getWidth() / 32f, Gdx.graphics.getHeight() / 32f); // Create ground body BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position.set(0, -10); groundBody = world.createBody(groundBodyDef); PolygonShape groundBox = new PolygonShape(); groundBox.setAsBox(50, 10); groundBody.createFixture(groundBox, 0.0f); groundBox.dispose(); // Initialize RayHandler RayHandler.useDiffuseLight(true); rayHandler = new RayHandler(world); rayHandler.setCombinedMatrix(camera); rayHandler.setAmbientLight(0.5f); // Create a point light PointLight light = new PointLight(rayHandler, 128); // 128 rays light.setPosition(5, 5); light.setDistance(10); } @Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); debugRenderer.render(world, camera.combined); rayHandler.updateAndRender(); } @Override public void dispose() { world.dispose(); debugRenderer.dispose(); rayHandler.dispose(); } - Assistant
在代码中,光和影的效果是通过 Box2D 和 box2dLight 库的结合来实现的。以下是具体原因:
-
物理引擎 (Box2D):
Box2D是一个2D物理引擎,用于模拟刚体物理。这对模拟物体的交互及其运动非常有用。在代码中,World对象用来管理物理世界,包括所有的物体及其交互。Box2DDebugRenderer用于渲染物理世界的轮廓,帮助开发者可视化和调试。
-
光影处理 (box2dLight):
box2dLight是一个用于在 Box2D 基础上生成光影效果的库。RayHandler是负责处理光线投射和渲染的主要类,它利用了光线追踪的原理来生成光影效果。PointLight是一种光源,它从一个点向四周发射光线。在代码中,PointLight的位置和距离属性定义了光源的位置和照射范围。
-
相机和矩阵组合:
OrthographicCamera用于渲染场景中的2D视图。setCombinedMatrix方法将相机的投影矩阵与光线处理程序结合,确保光影效果与相机视图一致。- 这样可以使得光影效果随着相机视角的变化而动态更新。
-
环境光和光线投射:
rayHandler.setAmbientLight(0.5f)设置了环境光的亮度,影响场景的全局光照效果。light.setDistance(10)设置光源的影响范围,即光线的投射距离。
通过这些类和方法的结合,代码能够生成逼真的光影效果,模拟光线的传播和其对场景中物体产生的阴影。例如,
PointLight通过从点向四周发射光线,并在与物体相交后产生阴影,结合相机设置和调试渲染,出现你在应用中看到的效果。 -