在libgdx中绘制独立三角形

在libgdx中绘制独立三角形,libgdx,Libgdx,如何绘制彼此独立的三角形?我想做一个船形游戏,当船与三角形相撞时,我想让它消失。但我找不到好办法。创建网格并不是最好的解决方案,因为它们最终是硬编码的,使用ShaperEnder似乎也不太好,因为我无法单独控制它们。所以我被卡住了,有人知道吗?我最终以这种方式解决了我的问题: 我创建了一个创建网格的对象三角形。在这个对象中,我遇到了另一个问题,绑定不起作用,但我知道如何解决它,我不确定如何解决它,但它起作用了。下面是对我有用的最后一个代码: import com.badlogic.gdx.Gdx

如何绘制彼此独立的三角形?我想做一个船形游戏,当船与三角形相撞时,我想让它消失。但我找不到好办法。创建网格并不是最好的解决方案,因为它们最终是硬编码的,使用ShaperEnder似乎也不太好,因为我无法单独控制它们。所以我被卡住了,有人知道吗?

我最终以这种方式解决了我的问题:

我创建了一个创建网格的对象三角形。在这个对象中,我遇到了另一个问题,绑定不起作用,但我知道如何解决它,我不确定如何解决它,但它起作用了。下面是对我有用的最后一个代码:

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();
    }

}
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Mesh;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.VertexAttribute;
导入com.badlogic.gdx.graphics.VertexAttributes.Usage;
导入com.badlogic.gdx.graphics.glutils.ShaderProgram;
公共阶级三角{
着色器程序着色器;
网目;
纹理;
float[]属性=新的float[15];
静态屏幕宽度;
静态屏幕高度;
公共三角形(浮动[]顶点、浮动[]颜色、浮动[]纹理、纹理){
这个纹理=纹理;
createShader();
int j=0;
int k=0;
int l=0;

如果((屏幕宽度我最终以这种方式解决了问题:

我创建了一个创建网格的对象三角形。在该对象中,我遇到了另一个问题,绑定不起作用,但我知道如何解决它,我不确定如何解决它,但它起了作用。以下是对我有效的最终代码:

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();
    }

}
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Mesh;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.VertexAttribute;
导入com.badlogic.gdx.graphics.VertexAttributes.Usage;
导入com.badlogic.gdx.graphics.glutils.ShaderProgram;
公共阶级三角{
着色器程序着色器;
网目;
纹理;
float[]属性=新的float[15];
静态屏幕宽度;
静态屏幕高度;
公共三角形(浮动[]顶点、浮动[]颜色、浮动[]纹理、纹理){
这个纹理=纹理;
createShader();
int j=0;
int k=0;
int l=0;

如果((screenWidth Mesh和ShaperEnder是绘制三角形的好选择。什么特别不适合您?我的大问题是绘制许多三角形并控制它们。但是我创建了一个三角形对象,为自己创建了一个特定的网格。我也有绑定问题,但我已经解决了。无论如何,谢谢!!Mesh和ShapeRenderer是绘制三角形的好选择。什么特别不适合你?我的大问题是绘制许多三角形并控制它们。但是我创建了一个三角形对象,它为自己创建了一个特定的网格。我也有绑定问题,但我已经解决了。无论如何,谢谢!!