Add 3d light env and placed pyramids on desktop1

This commit is contained in:
Willem Cazander 2022-03-15 15:42:15 +01:00
parent 2624eb4dea
commit 14f4cbc2ec
2 changed files with 77 additions and 15 deletions

View file

@ -6,8 +6,11 @@ import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera; import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.ModelBatch; import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController; import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
@ -39,6 +42,7 @@ public abstract class AbstractScreenDesktop extends ScreenAdapter implements Des
private PerspectiveCamera cam; private PerspectiveCamera cam;
private FirstPersonCameraController camController; private FirstPersonCameraController camController;
private DeskAppInputProcessor inputFilter; private DeskAppInputProcessor inputFilter;
private Environment environment;
private ModelBatch modelBatch; private ModelBatch modelBatch;
private Array<ModelInstance> modelInstances = new Array<ModelInstance>(); private Array<ModelInstance> modelInstances = new Array<ModelInstance>();
@ -71,6 +75,10 @@ public abstract class AbstractScreenDesktop extends ScreenAdapter implements Des
camera.update(); camera.update();
batch.setProjectionMatrix(camera.combined); batch.setProjectionMatrix(camera.combined);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
environment.add(new DirectionalLight().set(1, 1, 1, -.2f, -.7f, -.1f));
modelBatch = new ModelBatch(); modelBatch = new ModelBatch();
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
@ -150,7 +158,7 @@ public abstract class AbstractScreenDesktop extends ScreenAdapter implements Des
protected void renderDesktop(float delta, ModelBatch modelBatch, PerspectiveCamera cam, Array<ModelInstance> modelInstances) { protected void renderDesktop(float delta, ModelBatch modelBatch, PerspectiveCamera cam, Array<ModelInstance> modelInstances) {
modelBatch.begin(cam); modelBatch.begin(cam);
for (ModelInstance instance : modelInstances) { for (ModelInstance instance : modelInstances) {
modelBatch.render(instance); modelBatch.render(instance, environment);
} }
modelBatch.end(); modelBatch.end();
} }

View file

@ -4,14 +4,15 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes; import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.Material; import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model; import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance; import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder; import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder.VertexInfo;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
@ -27,6 +28,14 @@ public class ScreenDesktop1 extends AbstractScreenDesktop {
private Model grid; private Model grid;
private Texture pyramidImage; private Texture pyramidImage;
private Model pyramid1; private Model pyramid1;
private Model pyramid1_1;
private Model pyramid1_2;
private Model pyramid1_3;
private Model pyramid2;
private Model pyramid3;
private Model pyramid3_1;
private Model pyramid3_2;
private Model pyramid3_3;
public ScreenDesktop1(SystemGdxBootArgs bootArgs, SystemGdxTerminal terminal, VrGem4DeskAppServiceImpl deskAppService) { public ScreenDesktop1(SystemGdxBootArgs bootArgs, SystemGdxTerminal terminal, VrGem4DeskAppServiceImpl deskAppService) {
super("Desktop1", bootArgs, terminal, deskAppService); super("Desktop1", bootArgs, terminal, deskAppService);
@ -36,28 +45,65 @@ public class ScreenDesktop1 extends AbstractScreenDesktop {
protected void createModel(ModelBuilder modelBuilder, Array<ModelInstance> modelInstances) { protected void createModel(ModelBuilder modelBuilder, Array<ModelInstance> modelInstances) {
backgroundImage = new Texture(Gdx.files.internal("background/temple-os.png")); backgroundImage = new Texture(Gdx.files.internal("background/temple-os.png"));
int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates; int attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;
Material material = new Material(TextureAttribute.createDiffuse(backgroundImage)); Material materialBackground = new Material(TextureAttribute.createDiffuse(backgroundImage));
modelBuilder.begin(); modelBuilder.begin();
MeshPartBuilder mpb = modelBuilder.part("floor", GL20.GL_TRIANGLES, attr, material); MeshPartBuilder mpb = modelBuilder.part("floor", GL20.GL_TRIANGLES, attr, materialBackground);
mpb.rect(-20f,-1f,-20f, -20f,-1f,20f, 20f,-1f,20f, 20f,-1f,-20f, 0,1,0); mpb.rect(-20f,-1f,-20f, -20f,-1f,20f, 20f,-1f,20f, 20f,-1f,-20f, 0,1,0);
background = modelBuilder.end(); background = modelBuilder.end();
pyramidImage = new Texture(Gdx.files.internal("background/doom-credits.png")); pyramidImage = new Texture(Gdx.files.internal("background/doom-credits.png"));
//material = new Material(ColorAttribute.createDiffuse(.2f,.2f,.7f,1f)); //Material material2 = new Material(ColorAttribute.createDiffuse(.2f,.2f,.7f,1f));
material = new Material(TextureAttribute.createDiffuse(pyramidImage)); Material material = new Material(TextureAttribute.createDiffuse(pyramidImage));
modelBuilder.begin();
mpb = modelBuilder.part("pyramid1", GL20.GL_TRIANGLES, attr, material); pyramid1 = buildPyramid(modelBuilder, attr, material, "pyramid1", 2.3f, 2.92f);
mpb.setUVRange(new TextureRegion(pyramidImage)); pyramid2 = buildPyramid(modelBuilder, attr, material, "pyramid2", 2.16f, 2.86f);
mpb.triangle(new Vector3(-3,0, 3), new Vector3(3,0, 3), new Vector3(0, 3, 0)); pyramid3 = buildPyramid(modelBuilder, attr, material, "pyramid3", 1.09f, 1.33f);
mpb.triangle(new Vector3(3,0, 3), new Vector3(3,0, -3), new Vector3(0, 3, 0)); pyramid1_1 = buildPyramid(modelBuilder, attr, material, "pyramid1_1", 0.36f, 0.5f);
mpb.triangle(new Vector3(3,0, -3), new Vector3(-3,0, -3), new Vector3(0, 3, 0)); pyramid1_2 = buildPyramid(modelBuilder, attr, material, "pyramid1_2", 0.36f, 0.5f);
mpb.triangle(new Vector3(-3,0, -3), new Vector3(-3,0, 3), new Vector3(0, 3, 0)); pyramid1_3 = buildPyramid(modelBuilder, attr, material, "pyramid1_3", 0.36f, 0.5f);
pyramid1 = modelBuilder.end(); pyramid3_1 = buildPyramid(modelBuilder, attr, material, "pyramid3_1", 0.36f, 0.5f);
pyramid3_2 = buildPyramid(modelBuilder, attr, material, "pyramid3_2", 0.36f, 0.5f);
pyramid3_3 = buildPyramid(modelBuilder, attr, material, "pyramid3_3", 0.36f, 0.5f);
grid = modelBuilder.createLineGrid(33, 33, 1f, 1f, new Material(ColorAttribute.createDiffuse(.2f,.2f,.2f,1f)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal); grid = modelBuilder.createLineGrid(33, 33, 1f, 1f, new Material(ColorAttribute.createDiffuse(.2f,.2f,.2f,1f)), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
modelInstances.add(new ModelInstance(background, 0, 0, 0)); modelInstances.add(new ModelInstance(background, 0, 0, 0));
modelInstances.add(new ModelInstance(grid, 0, 0, 0)); modelInstances.add(new ModelInstance(grid, 0, 0, 0));
modelInstances.add(new ModelInstance(pyramid1, -8, 0, 8)); modelInstances.add(new ModelInstance(pyramid1, -2, 0, 12));
modelInstances.add(new ModelInstance(pyramid2, -8, 0, 6));
modelInstances.add(new ModelInstance(pyramid3, -14, 0, 2));
modelInstances.add(new ModelInstance(pyramid1_1, -3, 0, 15.5f));
modelInstances.add(new ModelInstance(pyramid1_2, -4, 0, 15.5f));
modelInstances.add(new ModelInstance(pyramid1_3, -5, 0, 15.5f));
modelInstances.add(new ModelInstance(pyramid3_1, -16.5f, 0, 0));
modelInstances.add(new ModelInstance(pyramid3_2, -16.5f, 0, 1));
modelInstances.add(new ModelInstance(pyramid3_3, -16.5f, 0, 2));
}
private Model buildPyramid(ModelBuilder modelBuilder,int attr, Material material, String name, float width, float height) {
Vector3 normal = new Vector3(1,1,1);
VertexInfo v1 = new VertexInfo();
VertexInfo v2 = new VertexInfo();
VertexInfo v3 = new VertexInfo();
modelBuilder.begin();
MeshPartBuilder mpb = modelBuilder.part(name, GL20.GL_TRIANGLES, attr, material);
v1.set(new Vector3(-width,0, width), normal, null, new Vector2(1,0));
v2.set(new Vector3(width,0, width), normal, null, new Vector2(0,1));
v3.set(new Vector3(0, height, 0), normal, null, new Vector2(1,1));
mpb.triangle(v1, v2, v3);
v1.set(new Vector3(width,0, width), normal, null, new Vector2(1,0));
v2.set(new Vector3(width,0, -width), normal, null, new Vector2(0,1));
v3.set(new Vector3(0, height, 0), normal, null, new Vector2(1,1));
mpb.triangle(v1, v2, v3);
v1.set(new Vector3(width,0, -width), normal, null, new Vector2(1,0));
v2.set(new Vector3(-width,0, -width), normal, null, new Vector2(0,1));
v3.set(new Vector3(0, height, 0), normal, null, new Vector2(1,1));
mpb.triangle(v1, v2, v3);
v1.set(new Vector3(-width,0, -width), normal, null, new Vector2(1,0));
v2.set(new Vector3(-width,0, width), normal, null, new Vector2(0,1));
v3.set(new Vector3(0, height, 0), normal, null, new Vector2(1,1));
mpb.triangle(v1, v2, v3);
return modelBuilder.end();
} }
@Override @Override
@ -66,6 +112,14 @@ public class ScreenDesktop1 extends AbstractScreenDesktop {
background.dispose(); background.dispose();
pyramidImage.dispose(); pyramidImage.dispose();
pyramid1.dispose(); pyramid1.dispose();
pyramid2.dispose();
pyramid3.dispose();
pyramid1_1.dispose();
pyramid1_2.dispose();
pyramid1_3.dispose();
pyramid3_1.dispose();
pyramid3_2.dispose();
pyramid3_3.dispose();
grid.dispose(); grid.dispose();
} }
} }