Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski Tom-Ski/MeshTest.java Secret
Created Sep 13, 2014

Embed
What would you like to do?
Mesh
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
public class MeshTest extends ApplicationAdapter {
private Mesh mesh;
private Texture texture;
private ShaderProgram shader;
Matrix4 projectionMatrix;
@Override
public void create() {
mesh = new Mesh(true, 3, 3,
VertexAttribute.Position(),
VertexAttribute.ColorPacked(),
VertexAttribute.TexCoords(0)); // Create the mesh with position, colour and tex coord vertex attributes
mesh.setVertices(new float[]{-0.5f, -0.5f, 0, Color.toFloatBits(255, 0, 0, 255), 0, 1,
0.5f, -0.5f, 0, Color.toFloatBits(0, 255, 0, 255), 1, 1,
0, 0.5f, 0, Color.toFloatBits(0, 0, 255, 255), 0.5f, 0}); // Setup our mesh with 3 vertices
mesh.setIndices(new short[]{0, 1, 2}); // Define the indices of our mesh, triangle with the first 3 vertices
texture = new Texture(Gdx.files.internal("Space.png")); // Load a texture
shader = SpriteBatch.createDefaultShader(); // Create SpriteBatch's default shader.
projectionMatrix = new Matrix4(); // Create a Matrix4 that we will use for our projection matrix
projectionMatrix.setToOrtho(-1, 1, -1, 1, 0, 1); // Set up our matrix as an orthographic projection, see http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml
}
@Override
public void render() {
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1f); // Clear to colour
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear colour buffer
shader.begin(); // Begin our shader
shader.setUniformMatrix("u_projTrans", projectionMatrix); // Pass in our projection uniform to our shader. Uniform name "u_projTrans"
shader.setUniformi("u_texture", 0); // Pass in our texture uniform to our shader. Uniform name "u_texture".
texture.bind(); // Bind the texture
mesh.render(shader, GL20.GL_TRIANGLES); // Render our mesh with the shader, and primitive type GL_TRIANGLES
shader.end(); // End our shader
}
public static void main(String[] args) {
new LwjglApplication(new MeshTest());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.