use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.
the class ModelCache method end.
/** Finishes creating the cache, must be called after a call to {@link #begin()}, only after this call the cache will be valid
* (until the next call to {@link #begin()}). Calling this method will process all renderables added using one of the add(...)
* methods and will combine them if possible. */
public void end() {
if (!building)
throw new GdxRuntimeException("Call begin() prior to calling end()");
building = false;
if (items.size == 0)
return;
sorter.sort(camera, items);
int itemCount = items.size;
int initCount = renderables.size;
final Renderable first = items.get(0);
VertexAttributes vertexAttributes = first.meshPart.mesh.getVertexAttributes();
Material material = first.material;
int primitiveType = first.meshPart.primitiveType;
int offset = renderables.size;
meshBuilder.begin(vertexAttributes);
MeshPart part = meshBuilder.part("", primitiveType, meshPartPool.obtain());
renderables.add(obtainRenderable(material, primitiveType));
for (int i = 0, n = items.size; i < n; ++i) {
final Renderable renderable = items.get(i);
final VertexAttributes va = renderable.meshPart.mesh.getVertexAttributes();
final Material mat = renderable.material;
final int pt = renderable.meshPart.primitiveType;
final boolean sameMesh = va.equals(vertexAttributes) && // comparing indices and vertices...
renderable.meshPart.size + meshBuilder.getNumVertices() < Short.MAX_VALUE;
final boolean samePart = sameMesh && pt == primitiveType && mat.same(material, true);
if (!samePart) {
if (!sameMesh) {
final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
meshBuilder.begin(vertexAttributes = va);
}
final MeshPart newPart = meshBuilder.part("", pt, meshPartPool.obtain());
final Renderable previous = renderables.get(renderables.size - 1);
previous.meshPart.offset = part.offset;
previous.meshPart.size = part.size;
part = newPart;
renderables.add(obtainRenderable(material = mat, primitiveType = pt));
}
meshBuilder.setVertexTransform(renderable.worldTransform);
meshBuilder.addMesh(renderable.meshPart.mesh, renderable.meshPart.offset, renderable.meshPart.size);
}
final Mesh mesh = meshBuilder.end(meshPool.obtain(vertexAttributes, meshBuilder.getNumVertices(), meshBuilder.getNumIndices()));
while (offset < renderables.size) renderables.get(offset++).meshPart.mesh = mesh;
final Renderable previous = renderables.get(renderables.size - 1);
previous.meshPart.offset = part.offset;
previous.meshPart.size = part.size;
}
use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.
the class PointSpriteParticleBatch method allocParticlesData.
@Override
protected void allocParticlesData(int capacity) {
vertices = new float[capacity * CPU_VERTEX_SIZE];
if (renderable.meshPart.mesh != null)
renderable.meshPart.mesh.dispose();
renderable.meshPart.mesh = new Mesh(false, capacity, 0, CPU_ATTRIBUTES);
}
use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.
the class FloatTextureTest method createQuad.
private void createQuad() {
if (quad != null)
return;
quad = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.ColorUnpacked, 4, "a_color"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));
quad.setVertices(new float[] { -1, -1, 0, 1, 1, 1, 1, 0, 1, 1, -1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 0 });
quad.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });
}
use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.
the class MipMap2D method create.
@Override
public void create() {
String vertexShader = "uniform float u_offset; \n" + "attribute vec4 a_position; \n" + "attribute vec2 a_texCoord; \n" + "varying vec2 v_texCoord; \n" + "void main() \n" + "{ \n" + " gl_Position = a_position; \n" + " gl_Position.x += u_offset;\n" + " v_texCoord = a_texCoord; \n" + "} \n";
String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec2 v_texCoord; \n" + "uniform sampler2D s_texture; \n" + "void main() \n" + "{ \n" + " gl_FragColor = texture2D( s_texture, v_texCoord );\n" + "} \n";
shader = new ShaderProgram(vertexShader, fragmentShader);
mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 4, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
float[] vertices = { // Position 0
-0.5f, // Position 0
0.5f, // Position 0
0.0f, // Position 0
1.5f, // TexCoord 0
0.0f, // TexCoord 0
0.0f, // Position 1
-0.5f, // Position 1
-0.5f, // Position 1
0.0f, // Position 1
0.75f, // TexCoord 1
0.0f, // TexCoord 1
1.0f, // Position 2
0.5f, // Position 2
-0.5f, // Position 2
0.0f, // Position 2
0.75f, // TexCoord 2
1.0f, // TexCoord 2
1.0f, // Position 3
0.5f, // Position 3
0.5f, // Position 3
0.0f, // Position 3
1.5f, // TexCoord 3
1.0f, // TexCoord 3
0.0f };
short[] indices = { 0, 1, 2, 0, 2, 3 };
mesh.setVertices(vertices);
mesh.setIndices(indices);
createTexture();
}
use of com.badlogic.gdx.graphics.Mesh in project libgdx by libgdx.
the class TextureArrayTest method create.
@Override
public void create() {
GL30Profiler.enable();
ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
String[] texPaths = new String[] { "data/g3d/materials/Searing Gorge.jpg", "data/g3d/materials/Lava Cracks.jpg", "data/g3d/materials/Deep Fire.jpg" };
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(8, 10f, 20f);
camera.lookAt(10, 0, 10);
camera.up.set(0, 1, 0);
camera.update();
cameraController = new FirstPersonCameraController(camera);
Gdx.input.setInputProcessor(cameraController);
textureArray = new TextureArray(texPaths);
textureArray.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
shaderProgram = new ShaderProgram(Gdx.files.internal("data/shaders/texturearray.vert"), Gdx.files.internal("data/shaders/texturearray.frag"));
System.out.println(shaderProgram.getLog());
int vertexStride = 6;
int vertexCount = 100 * 100;
terrain = new Mesh(false, vertexCount * 6, 0, new VertexAttributes(VertexAttribute.Position(), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 3, ShaderProgram.TEXCOORD_ATTRIBUTE + 0)));
Pixmap data = new Pixmap(Gdx.files.internal("data/g3d/heightmap.png"));
float[] vertices = new float[vertexCount * vertexStride * 6];
int idx = 0;
for (int i = 0; i < 100 - 1; i++) {
for (int j = 0; j < 100 - 1; j++) {
idx = addVertex(i, j, vertices, data, idx);
idx = addVertex(i, j + 1, vertices, data, idx);
idx = addVertex(i + 1, j, vertices, data, idx);
idx = addVertex(i, j + 1, vertices, data, idx);
idx = addVertex(i + 1, j + 1, vertices, data, idx);
idx = addVertex(i + 1, j, vertices, data, idx);
}
}
terrain.setVertices(vertices);
data.dispose();
}
Aggregations