如何绘制彼此独立的三角形?我打算做船游戏,当船与三角形碰撞时,我希望它消失。但我找不到一个很好的方法来做到这一点。创建Mesh并不是最好的解决方案,因为它们以硬编码结束,使用shaperenderer似乎并不好,因为我无法单独控制它们。所以我卡住了,有人有任何想法吗?
答案 0 :(得分:4)
我最终以这种方式解决了我的问题:
我创建了一个创建网格的对象Triangle。在那个对象中我遇到了另一个问题,绑定没有工作,但我想出了如何解决它,我不知道我是如何解决它的,但它确实有效。这是对我有用的最终代码:
import com.badlogic.gdx.Gdx;
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.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class Triangle {
ShaderProgram shader;
Mesh mesh;
Texture texture;
float[] attributes = new float[15];
static int screenWidth;
static int screenHeight;
public Triangle(float[] vertices, float[] color, float[] textureVertices, Texture texture){
this.texture = texture;
createShader();
int j = 0;
int k = 0;
int l = 0;
if ((screenWidth <= 0) || (screenHeight <= 0))
{
throw new NullPointerException("Invalid screen dimensions for triangles.");
}
for (int i = 0; i < vertices.length; i++) {
vertices[i] = (vertices[i]/screenWidth - 1); // (vertices[i] - width/2)/(width/2)
vertices[++i] = (vertices[i]/screenHeight - 1); // (vertices[i] - height/2)/(height/2)
}
for (int i = 0; i < attributes.length;) {
attributes[i++] = vertices[j++];
attributes[i++] = vertices[j++];
attributes[i++] = color[k++];
attributes[i++] = textureVertices[l++];
attributes[i++] = textureVertices[l++];
}
mesh = new Mesh(false, attributes.length, 0, new VertexAttribute(
Usage.Position, 2, "a_position"), new VertexAttribute(
Usage.ColorPacked, 4, "a_color"), new VertexAttribute(
Usage.TextureCoordinates, 2, "a_texCoords"));
mesh.setVertices(attributes);
}
public static void setDimensions(int paramWidth, int paramHeight)
{
screenWidth = paramWidth;
screenHeight = paramHeight;
}
public void createShader()
{
// this shader tells opengl where to put things
String vertexShader = "attribute vec4 a_position; \n"
+ "attribute vec4 a_color; \n"
+ "attribute vec2 a_texCoords; \n"
+ "varying vec4 v_color; \n"
+ "varying vec2 v_texCoords; \n"
+ "void main() \n"
+ "{ \n"
+ " v_color = a_color; \n"
+ " v_texCoords = a_texCoords; \n"
+ " gl_Position = a_position; \n"
+ "} \n";
// this one tells it what goes in between the points (i.e
// colour/texture)
String fragmentShader = "#ifdef GL_ES \n"
+ "precision mediump float; \n"
+ "#endif \n"
+ "varying vec4 v_color; \n"
+ "varying vec2 v_texCoords; \n"
+ "uniform sampler2D u_texture;\n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = v_color * texture2D(u_texture, v_texCoords); \n"
+ "} \n";
shader = new ShaderProgram(vertexShader, fragmentShader);
}
public void render() {
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE);
shader.begin();
texture.bind(0);
shader.setUniformi("u_texture", 0);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
public void dispose() {
texture.dispose();
mesh.dispose();
shader.dispose();
}
}
我希望它有所帮助!