use of com.badlogic.gdx.graphics.PerspectiveCamera in project libgdx by libgdx.
the class OcclusionCullingTest method create.
@Override
public void create() {
Gdx.input.setOnscreenKeyboardVisible(true);
super.create();
GLProfiler.enable();
StringBuilder sb = new StringBuilder();
sb.append("Swipe for next test\n");
sb.append("Long press to toggle debug mode\n");
sb.append("Ctrl+drag to rotate\n");
sb.append("Scroll to zoom\n");
sb.append("Tap to spawn dynamic entity, press\n");
sb.append("'0' to spawn ").append(KEY_SPAWN_OCCLUDEE_AMOUNT).append(" static entities\n");
sb.append("'1' to set normal/disabled/occlusion-culling\n");
sb.append("'2' to change camera\n");
sb.append("'3' to toggle camera movement\n");
sb.append("'4' to cycle occlusion buffer sizes\n");
sb.append("'5' to toggle occlusion buffer image\n");
sb.append("'6' to toggle shadows\n");
instructions = sb.toString();
AssetManager assets = new AssetManager();
disposables.add(assets);
for (String modelName : OCCLUDEE_PATHS_DYNAMIC) assets.load(modelName, Model.class);
assets.load(DEFAULT_TEX_PATH, Texture.class);
Camera shadowCamera = ((DirectionalShadowLight) light).getCamera();
shadowCamera.viewportWidth = shadowCamera.viewportHeight = 120;
// User controlled camera
overviewCam = camera;
overviewCam.position.set(overviewCam.direction).nor().scl(-100);
overviewCam.lookAt(Vector3.Zero);
overviewCam.far = camera.far *= 2;
overviewCam.update(true);
// Animated frustum camera model
frustumCam = new PerspectiveCamera(FRUSTUM_CAMERA_FOV, camera.viewportWidth, camera.viewportHeight);
frustumCam.far = FRUSTUM_CAMERA_FAR;
frustumCam.update(true);
final Model frustumModel = FrustumCullingTest.createFrustumModel(frustumCam.frustum.planePoints);
frustumModel.materials.first().set(new ColorAttribute(ColorAttribute.AmbientLight, Color.WHITE));
disposables.add(frustumModel);
frustumInstance = new ModelInstance(frustumModel);
spriteBatch = new SpriteBatch();
disposables.add(spriteBatch);
shapeRenderer = new ShapeRenderer();
disposables.add(shapeRenderer);
oclBuffer = new OcclusionBuffer(OCL_BUFFER_EXTENTS[0], OCL_BUFFER_EXTENTS[0]);
disposables.add(oclBuffer);
occlusionCuller = new OcclusionCuller() {
@Override
public boolean isOccluder(btCollisionObject object) {
return (object.getCollisionFlags() & CF_OCCLUDER_OBJECT) != 0;
}
@Override
public void onObjectVisible(btCollisionObject object) {
visibleEntities.add(world.entities.get(object.getUserValue()));
}
};
disposables.add(occlusionCuller);
// Add occluder walls
final Model occluderModel = modelBuilder.createBox(OCCLUDER_DIM.x, OCCLUDER_DIM.y, OCCLUDER_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
disposables.add(occluderModel);
world.addConstructor("wall", new BulletConstructor(occluderModel, 0, new btBoxShape(tmpV1.set(OCCLUDER_DIM).scl(0.5f))));
float y = OCCLUDER_DIM.y * 0.5f;
addOccluder("wall", 0, tmpV1.set(20, y, 0));
addOccluder("wall", -60, tmpV1.set(10, y, 20));
addOccluder("wall", 60, tmpV1.set(10, y, -20));
addOccluder("wall", 0, tmpV1.set(-20, y, 0));
addOccluder("wall", 60, tmpV1.set(-10, y, 20));
addOccluder("wall", -60, tmpV1.set(-10, y, -20));
// Add ground
final Model groundModel = modelBuilder.createBox(GROUND_DIM.x, GROUND_DIM.y, GROUND_DIM.z, new Material(ColorAttribute.createDiffuse(Color.WHITE)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
btCollisionShape groundShape = new btBoxShape(tmpV1.set(GROUND_DIM).scl(0.5f));
world.addConstructor("big_ground", new BulletConstructor(groundModel, 0, groundShape));
BulletEntity e = world.add("big_ground", 0, -GROUND_DIM.y * 0.5f, 0f);
e.body.setFriction(1f);
e.setColor(Color.FOREST);
// Occludee entity constructors. Scale models uniformly and set a default diffuse texture.
BoundingBox bb = new BoundingBox();
assets.finishLoadingAsset(DEFAULT_TEX_PATH);
TextureAttribute defaultTexture = new TextureAttribute(TextureAttribute.Diffuse, assets.get(DEFAULT_TEX_PATH, Texture.class));
for (int i = 0; i < OCCLUDEE_PATHS_DYNAMIC.length; i++) {
String modelPath = OCCLUDEE_PATHS_DYNAMIC[i];
OCCLUDEE_PATHS_STATIC[i] = "static" + modelPath;
assets.finishLoadingAsset(modelPath);
Model model = assets.get(modelPath, Model.class);
if (!model.materials.first().has(TextureAttribute.Diffuse))
model.materials.first().set(defaultTexture);
Vector3 dim = model.calculateBoundingBox(bb).getDimensions(tmpV1);
float scaleFactor = OCCLUDEE_MAX_EXTENT / Math.max(dim.x, Math.max(dim.y, dim.z));
for (Node node : model.nodes) node.scale.scl(scaleFactor);
btCollisionShape shape = new btBoxShape(dim.scl(scaleFactor * 0.5f));
world.addConstructor(modelPath, new BulletConstructor(model, 1, shape));
world.addConstructor(OCCLUDEE_PATHS_STATIC[i], new BulletConstructor(model, 0, shape));
}
// Add occludees
for (int i = 0; i < STARTING_OCCLUDEE_AMOUNT; i++) addRandomOccludee(false);
}
use of com.badlogic.gdx.graphics.PerspectiveCamera in project libgdx by libgdx.
the class KTXTest method create.
@Override
public void create() {
// Cubemap test
String cubemapVS = //
"" + //
"attribute vec3 a_position;\n" + //
"uniform mat4 u_projViewTrans;\n" + //
"uniform mat4 u_worldTrans;\n" + //
"\n" + //
"varying vec3 v_cubeMapUV;\n" + //
"\n" + //
"void main() {\n" + //
" vec4 g_position = vec4(a_position, 1.0);\n" + //
" g_position = u_worldTrans * g_position;\n" + //
" v_cubeMapUV = normalize(g_position.xyz);\n" + //
" gl_Position = u_projViewTrans * g_position;\n" + "}";
String cubemapFS = //
"" + //
"#ifdef GL_ES\n" + //
"precision mediump float;\n" + //
"#endif\n" + //
"uniform samplerCube u_environmentCubemap;\n" + //
"varying vec3 v_cubeMapUV;\n" + //
"void main() {\n" + //
" gl_FragColor = vec4(textureCube(u_environmentCubemap, v_cubeMapUV).rgb, 1.0);\n" + "}\n";
modelBatch = new ModelBatch(new DefaultShaderProvider(new Config(cubemapVS, cubemapFS)));
cubemap = new Cubemap(new KTXTextureData(Gdx.files.internal("data/cubemap.zktx"), true));
cubemap.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.1f, 0.1f, 0.1f, 1.f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1.0f, -0.8f));
environment.set(new CubemapAttribute(CubemapAttribute.EnvironmentMap, cubemap));
perspectiveCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
perspectiveCamera.position.set(10f, 10f, 10f);
perspectiveCamera.lookAt(0, 0, 0);
perspectiveCamera.near = 0.1f;
perspectiveCamera.far = 300f;
perspectiveCamera.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(perspectiveCamera)));
// 2D texture test
String etc1aVS = //
"" + //
"uniform mat4 u_projTrans;\n" + //
"\n" + //
"attribute vec4 a_position;\n" + //
"attribute vec2 a_texCoord0;\n" + //
"attribute vec4 a_color;\n" + //
"\n" + //
"varying vec4 v_color;\n" + //
"varying vec2 v_texCoord;\n" + //
"\n" + //
"void main() {\n" + //
" gl_Position = u_projTrans * a_position;\n" + //
" v_texCoord = a_texCoord0;\n" + //
" v_color = a_color;\n" + //
"}\n";
String etc1aFS = //
"" + //
"#ifdef GL_ES\n" + //
"precision mediump float;\n" + //
"#endif\n" + //
"uniform sampler2D u_texture;\n" + //
"\n" + //
"varying vec4 v_color;\n" + //
"varying vec2 v_texCoord;\n" + //
"\n" + //
"void main() {\n" + //
" vec3 col = texture2D(u_texture, v_texCoord.st).rgb;\n" + //
" float alpha = texture2D(u_texture, v_texCoord.st + vec2(0.0, 0.5)).r;\n" + //
" gl_FragColor = vec4(col, alpha) * v_color;\n" + //
"}\n";
etc1aShader = new ShaderProgram(etc1aVS, etc1aFS);
orthoCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
image = new Texture("data/egg.zktx");
batch = new SpriteBatch(100, etc1aShader);
}
use of com.badlogic.gdx.graphics.PerspectiveCamera in project libgdx by libgdx.
the class FramebufferToTextureTest method create.
@Override
public void create() {
texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true);
texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
ObjLoader objLoader = new ObjLoader();
mesh = objLoader.loadModel(Gdx.files.internal("data/cube.obj"));
mesh.materials.get(0).set(new TextureAttribute(TextureAttribute.Diffuse, texture));
modelInstance = new ModelInstance(mesh);
modelBatch = new ModelBatch();
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(3, 3, 3);
cam.direction.set(-1, -1, -1);
batch = new SpriteBatch();
font = new BitmapFont();
}
use of com.badlogic.gdx.graphics.PerspectiveCamera in project libgdx by libgdx.
the class MultipleRenderTargetTest method create.
@Override
public void create() {
//use default prepend shader code for batch, some gpu drivers are less forgiving
batch = new SpriteBatch();
//depth texture not currently sampled
ShaderProgram.pedantic = false;
modelCache = new ModelCache();
ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
renderContext = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN));
shaderProvider = new BaseShaderProvider() {
@Override
protected Shader createShader(Renderable renderable) {
return new MRTShader(renderable);
}
};
renderableSorter = new DefaultRenderableSorter() {
@Override
public int compare(Renderable o1, Renderable o2) {
return o1.shader.compareTo(o2.shader);
}
};
mrtSceneShader = new ShaderProgram(Gdx.files.internal("data/g3d/shaders/mrtscene.vert"), Gdx.files.internal("data/g3d/shaders/mrtscene.frag"));
if (!mrtSceneShader.isCompiled()) {
System.out.println(mrtSceneShader.getLog());
}
quad = createFullScreenQuad();
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.near = 1f;
camera.far = 100f;
camera.position.set(3, 5, 10);
camera.lookAt(0, 2, 0);
camera.up.set(0, 1, 0);
camera.update();
cameraController = new FirstPersonCameraController(camera);
cameraController.setVelocity(50);
Gdx.input.setInputProcessor(cameraController);
frameBuffer = new MRTFrameBuffer(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 3);
AssetManager assetManager = new AssetManager();
assetManager.load("data/g3d/materials/cannon.g3db", Model.class);
assetManager.finishLoading();
Model scene = assetManager.get("data/g3d/materials/cannon.g3db");
cannon = new ModelInstance(scene, "Cannon_LP");
cannon.transform.setToTranslationAndScaling(0, 0, 0, 0.001f, 0.001f, 0.001f);
ModelBuilder modelBuilder = new ModelBuilder();
for (int i = 0; i < NUM_LIGHTS; i++) {
modelBuilder.begin();
Light light = new Light();
light.color.set(MathUtils.random(1f), MathUtils.random(1f), MathUtils.random(1f));
light.position.set(MathUtils.random(-10f, 10f), MathUtils.random(10f, 15f), MathUtils.random(-10f, 10f));
light.vy = MathUtils.random(10f, 20f);
light.vx = MathUtils.random(-10f, 10f);
light.vz = MathUtils.random(-10f, 10f);
MeshPartBuilder meshPartBuilder = modelBuilder.part("light", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
meshPartBuilder.setColor(light.color.x, light.color.y, light.color.z, 1f);
meshPartBuilder.sphere(0.2f, 0.2f, 0.2f, 10, 10);
light.lightInstance = new ModelInstance(modelBuilder.end());
lights.add(light);
}
modelBuilder.begin();
MeshPartBuilder meshPartBuilder = modelBuilder.part("floor", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
meshPartBuilder.setColor(0.2f, 0.2f, 0.2f, 1f);
meshPartBuilder.box(0, -0.1f, 0f, 20f, 0.1f, 20f);
floorInstance = new ModelInstance(modelBuilder.end());
Gdx.input.setInputProcessor(new InputMultiplexer(this, cameraController));
}
use of com.badlogic.gdx.graphics.PerspectiveCamera in project libgdx by libgdx.
the class VoxelTest method create.
@Override
public void create() {
spriteBatch = new SpriteBatch();
font = new BitmapFont();
modelBatch = new ModelBatch();
DefaultShader.defaultCullFace = GL20.GL_FRONT;
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.near = 0.5f;
camera.far = 1000;
controller = new FirstPersonCameraController(camera);
Gdx.input.setInputProcessor(controller);
lights = new Environment();
lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
lights.add(new DirectionalLight().set(1, 1, 1, 0, -1, 0));
Texture texture = new Texture(Gdx.files.internal("data/g3d/tiles.png"));
TextureRegion[][] tiles = TextureRegion.split(texture, 32, 32);
MathUtils.random.setSeed(0);
voxelWorld = new VoxelWorld(tiles[0], 20, 4, 20);
PerlinNoiseGenerator.generateVoxels(voxelWorld, 0, 63, 10);
float camX = voxelWorld.voxelsX / 2f;
float camZ = voxelWorld.voxelsZ / 2f;
float camY = voxelWorld.getHighest(camX, camZ) + 1.5f;
camera.position.set(camX, camY, camZ);
}
Aggregations