• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests.g3d;
18 
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.graphics.Camera;
21 import com.badlogic.gdx.graphics.Color;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.PerspectiveCamera;
24 import com.badlogic.gdx.graphics.Texture;
25 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
26 import com.badlogic.gdx.graphics.g3d.Material;
27 import com.badlogic.gdx.graphics.g3d.Model;
28 import com.badlogic.gdx.graphics.g3d.ModelBatch;
29 import com.badlogic.gdx.graphics.g3d.ModelInstance;
30 import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
31 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
32 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
33 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
34 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
35 import com.badlogic.gdx.math.Vector3;
36 import com.badlogic.gdx.tests.utils.GdxTest;
37 
38 public class MaterialTest extends GdxTest {
39 
40 	float angleY = 0;
41 
42 	Model model, backModel;
43 	ModelInstance modelInstance;
44 	ModelInstance background;
45 	ModelBatch modelBatch;
46 
47 	TextureAttribute textureAttribute;
48 	ColorAttribute colorAttribute;
49 	BlendingAttribute blendingAttribute;
50 
51 	Material material;
52 
53 	Texture texture;
54 
55 	Camera camera;
56 
57 	@Override
create()58 	public void create () {
59 		texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true);
60 
61 		// Create material attributes. Each material can contain x-number of attributes.
62 		textureAttribute = new TextureAttribute(TextureAttribute.Diffuse, texture);
63 		colorAttribute = new ColorAttribute(ColorAttribute.Diffuse, Color.ORANGE);
64 		blendingAttribute = new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
65 
66 		ModelBuilder builder = new ModelBuilder();
67 		model = builder.createBox(1, 1, 1, new Material(), Usage.Position | Usage.Normal | Usage.TextureCoordinates);
68 		model.manageDisposable(texture);
69 		modelInstance = new ModelInstance(model);
70 		modelInstance.transform.rotate(Vector3.X, 45);
71 
72 		material = modelInstance.materials.get(0);
73 
74 		builder.begin();
75 		MeshPartBuilder mpb = builder.part("back", GL20.GL_TRIANGLES, Usage.Position | Usage.TextureCoordinates, new Material(
76 			textureAttribute));
77 		mpb.rect(-2, -2, -2, 2, -2, -2, 2, 2, -2, -2, 2, -2, 0, 0, 1);
78 		backModel = builder.end();
79 		background = new ModelInstance(backModel);
80 
81 		modelBatch = new ModelBatch();
82 
83 		camera = new PerspectiveCamera(45, 4, 4);
84 		camera.position.set(0, 0, 3);
85 		camera.direction.set(0, 0, -1);
86 		camera.update();
87 
88 		Gdx.input.setInputProcessor(this);
89 	}
90 
91 	private float counter = 0.f;
92 
93 	@Override
render()94 	public void render () {
95 		counter = (counter + Gdx.graphics.getDeltaTime()) % 1.f;
96 		blendingAttribute.opacity = 0.25f + Math.abs(0.5f - counter);
97 
98 		Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
99 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
100 
101 		modelInstance.transform.rotate(Vector3.Y, 30 * Gdx.graphics.getDeltaTime());
102 		modelBatch.begin(camera);
103 		modelBatch.render(background);
104 		modelBatch.render(modelInstance);
105 		modelBatch.end();
106 	}
107 
108 	@Override
touchUp(int screenX, int screenY, int pointer, int button)109 	public boolean touchUp (int screenX, int screenY, int pointer, int button) {
110 
111 		if (!material.has(TextureAttribute.Diffuse))
112 			material.set(textureAttribute);
113 		else if (!material.has(ColorAttribute.Diffuse))
114 			material.set(colorAttribute);
115 		else if (!material.has(BlendingAttribute.Type))
116 			material.set(blendingAttribute);
117 		else
118 			material.clear();
119 
120 		return super.touchUp(screenX, screenY, pointer, button);
121 	}
122 
123 	@Override
dispose()124 	public void dispose () {
125 		model.dispose();
126 		backModel.dispose();
127 		modelBatch.dispose();
128 	}
129 }
130