Java Code Examples for com.badlogic.gdx.graphics.glutils.ShaderProgram#POSITION_ATTRIBUTE

The following examples show how to use com.badlogic.gdx.graphics.glutils.ShaderProgram#POSITION_ATTRIBUTE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Renderer20.java    From fluid-simulator-v2 with Apache License 2.0 6 votes vote down vote up
static private String createVertexShader (boolean hasNormals, boolean hasColors, int numTexCoords) {
	String shader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
		+ (hasNormals ? "attribute vec3 " + ShaderProgram.NORMAL_ATTRIBUTE + ";\n" : "")
		+ (hasColors ? "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" : "");

	for (int i = 0; i < numTexCoords; i++) {
		shader += "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + i + ";\n";
	}

	shader += "uniform mat4 u_projModelView;\n";
	shader += (hasColors ? "varying vec4 v_col;\n" : "");

	for (int i = 0; i < numTexCoords; i++) {
		shader += "varying vec2 v_tex" + i + ";\n";
	}

	shader += "void main() {\n" + "   gl_Position = u_projModelView * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n"
		+ (hasColors ? "   v_col = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" : "");

	for (int i = 0; i < numTexCoords; i++) {
		shader += "   v_tex" + i + " = " + ShaderProgram.TEXCOORD_ATTRIBUTE + i + ";\n";
	}
	shader += "   gl_PointSize = 1.0;\n";
	shader += "}\n";
	return shader;
}
 
Example 2
Source File: Main.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
private Mesh buildMeshFromVertices(List<Double> vertexCoords) {
  if ((vertexCoords.size() % 3) != 0) {
    throw new RuntimeException("Vertex coordinates size is " + vertexCoords.size() + "; must be a multiple of 3");
  }
  float[] data = new float[vertexCoords.size()];
  for (int i = 0; i < vertexCoords.size(); i++) {
    data[i] = (float) vertexCoords.get(i).doubleValue();
  }
  Mesh mesh = new Mesh( true, vertexCoords.size() / 3, 0,
          new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE));
  mesh.setVertices(data);
  return mesh;
}
 
Example 3
Source File: Main.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
private Mesh buildMeshFromVerticesAndTexCoords(List<Double> vertexCoords, List<Double> texCoords) {
  if ((vertexCoords.size() % 3) != 0) {
    throw new RuntimeException("Vertex coordinates size is " + vertexCoords.size() + "; must be a multiple of 3");
  }
  if ((texCoords.size() % 2) != 0) {
    throw new RuntimeException("Texture coordinates size is " + texCoords.size() + "; must be a multiple of 2");
  }
  if (vertexCoords.size() / 3 != texCoords.size() / 2) {
    throw new RuntimeException("There is vertex data for " + vertexCoords.size() / 3 + " triangle(s), "
            + "and texture data for " + texCoords.size() / 2 + " triangle(s) -- these should match");
  }

  float[] data = new float[vertexCoords.size() + texCoords.size()];
  int vertexIndex = 0;
  int texIndex = 0;
  int dataIndex = 0;
  for (int i = 0; i < vertexCoords.size() / 3; i++) {
    data[dataIndex++] = (float) vertexCoords.get(vertexIndex++).doubleValue();
    data[dataIndex++] = (float) vertexCoords.get(vertexIndex++).doubleValue();
    data[dataIndex++] = (float) vertexCoords.get(vertexIndex++).doubleValue();
    data[dataIndex++] = (float) texCoords.get(texIndex++).doubleValue();
    data[dataIndex++] = (float) texCoords.get(texIndex++).doubleValue();
  }
  Mesh mesh = new Mesh( true, vertexCoords.size() / 3, 0,
          new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
          new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
  mesh.setVertices(data);
  return mesh;
}
 
Example 4
Source File: GameWorldRenderer.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
private void createBackPlane () {
	plane = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
		new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));

	// @formatter:off
	float size = 10f;
	float verts[] = {-size / 2, 0, size / 2, size / 2, 0, size / 2, size / 2, 0, -size / 2, -size / 2, 0, -size / 2};
	// float verts[] = {size, 0, size, size, 0, 0, 0, 0, 0, 0, 0, size};

	float normals[] = {0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0};
	// @formatter:on

	int vidx = 0, nidx = 0;
	int length = 6 * 4;
	float[] vertices = new float[length];
	for (int i = 0; i < length;) {
		vertices[i++] = verts[vidx++];
		vertices[i++] = verts[vidx++];
		vertices[i++] = verts[vidx++];
		vertices[i++] = normals[nidx++];
		vertices[i++] = normals[nidx++];
		vertices[i++] = normals[nidx++];
	}

	plane.setVertices(vertices);
	plane.setIndices(new short[] {0, 1, 2, 3});
}
 
Example 5
Source File: G3dtLoader.java    From uracer-kotd with Apache License 2.0 5 votes vote down vote up
private static VertexAttribute[] createVertexAttributes (boolean hasNormals, int uvs) {
	VertexAttribute[] attributes = new VertexAttribute[1 + (hasNormals ? 1 : 0) + uvs];
	int idx = 0;
	attributes[idx++] = new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE);
	if (hasNormals) attributes[idx++] = new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE);
	for (int i = 0; i < uvs; i++) {
		attributes[idx++] = new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + i);
	}
	return attributes;
}
 
Example 6
Source File: FrametimeGraph.java    From Cubes with MIT License 5 votes vote down vote up
private static ShaderProgram createShaderProgram() {
  String vertex = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n";
  vertex += "uniform mat4 u_projModelView;\n";
  vertex += "void main() {\n" + "   gl_Position = u_projModelView * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n";
  vertex += "   gl_PointSize = 1.0;\n";
  vertex += "}\n";
  String fragment = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n";
  fragment += "void main() {\n" + "   gl_FragColor = vec4(1, 0, 0, 1)"; //#00ff00ff
  fragment += ";\n}";
  return new ShaderProgram(vertex, fragment);
}
 
Example 7
Source File: Box2dLightCustomShaderTest.java    From box2dlights with Apache License 2.0 4 votes vote down vote up
private ShaderProgram createNormalShader () {
	String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
		+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
		+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
		+ "uniform mat4 u_projTrans;\n" //
		+ "uniform float u_rot;\n" //
		+ "varying vec4 v_color;\n" //
		+ "varying vec2 v_texCoords;\n" //
		+ "varying mat2 v_rot;\n" //
		+ "\n" //
		+ "void main()\n" //
		+ "{\n" //
		+ "   vec2 rad = vec2(-sin(u_rot), cos(u_rot));\n" //
		+ "   v_rot = mat2(rad.y, -rad.x, rad.x, rad.y);\n" //
		+ "   v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
		+ "   v_color.a = v_color.a * (255.0/254.0);\n" //
		+ "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
		+ "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
		+ "}\n";
	String fragmentShader = "#ifdef GL_ES\n" //
		+ "#define LOWP lowp\n" //
		+ "precision mediump float;\n" //
		+ "#else\n" //
		+ "#define LOWP \n" //
		+ "#endif\n" //
		+ "varying LOWP vec4 v_color;\n" //
		+ "varying vec2 v_texCoords;\n" //
		+ "varying mat2 v_rot;\n" //
		+ "uniform sampler2D u_texture;\n" //
		+ "void main()\n"//
		+ "{\n" //
		+ "  vec4 normal = texture2D(u_texture, v_texCoords).rgba;\n" //
		// got to translate normal vector to -1, 1 range
		+ "  vec2 rotated = v_rot * (normal.xy * 2.0 - 1.0);\n" //
		// and back to 0, 1
		+ "  rotated = (rotated.xy / 2.0 + 0.5 );\n" //
		+ "  gl_FragColor = vec4(rotated.xy, normal.z, normal.a);\n" //
		+ "}";

	ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
	if (!shader.isCompiled()) throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());
	return shader;
}