Example usage for com.badlogic.gdx.graphics GL20 GL_TRIANGLES

List of usage examples for com.badlogic.gdx.graphics GL20 GL_TRIANGLES

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics GL20 GL_TRIANGLES.

Prototype

int GL_TRIANGLES

To view the source code for com.badlogic.gdx.graphics GL20 GL_TRIANGLES.

Click Source Link

Usage

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static ModelInstance createBox(Material material1, Material material2, Material material3) {

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();/*from ww w  . ja va 2s.c o  m*/

    modelBuilder.part("1", createRectX(-2.5f, -2.5f, 2.5f, 5f, 5f, 1), GL20.GL_TRIANGLES, material1);
    modelBuilder.part("2", createRectX(-2.5f, -2.5f, -2.5f, 5f, 5f, 0), GL20.GL_TRIANGLES, material1);

    modelBuilder.part("3", createRectY(-2.5f, -2.5f, -2.5f, 5f, 5f, 3), GL20.GL_TRIANGLES, material2);
    modelBuilder.part("4", createRectY(-2.5f, 2.5f, -2.5f, 5f, 5f, 2), GL20.GL_TRIANGLES, material2);

    modelBuilder.part("5", createRectZ(-2.5f, -2.5f, -2.5f, 5f, 5f, 5), GL20.GL_TRIANGLES, material3);
    modelBuilder.part("6", createRectZ(2.5f, -2.5f, -2.5f, 5f, 5f, 4), GL20.GL_TRIANGLES, material3);

    Model model = modelBuilder.end();

    ModelInstance instance = new ModelInstance(model);

    return instance;
}

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static ModelInstance createCircle(Material material) {

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();//  ww w .  j a v a 2s  .  c o m

    float outerDiameter = 5f;
    float innerDiameter = 2f;

    int sections = 20;
    float angle = FULL_CIRCLE / sections;

    Mesh mesh = new Mesh(true, sections * 2, sections * 12, VertexAttribute.Position(),
            VertexAttribute.ColorUnpacked(), VertexAttribute.Normal(), VertexAttribute.TexCoords(0));

    float[] vertices = new float[sections * 24];

    for (int i = 0; i < sections; i++) {
        int v = i * 24;
        // ======== outer =========
        vertices[v] = (float) (Math.sin(angle * i)) * outerDiameter;
        vertices[v + 1] = (float) (Math.cos(angle * i)) * outerDiameter;
        vertices[v + 2] = 0;

        // Colours
        vertices[v + 3] = 1;
        vertices[v + 4] = 1;
        vertices[v + 5] = 1;
        vertices[v + 6] = 1;

        // Normal
        vertices[v + 7] = 0;
        vertices[v + 8] = 0;
        vertices[v + 9] = 1;

        // Text coordinates
        vertices[v + 10] = (float) (Math.sin(angle * i) / 2) + 0.5f;
        vertices[v + 11] = (float) (Math.cos(angle * i) / 2) + 0.5f;

        // ======== inner =========
        vertices[v + 12] = (float) (Math.sin(angle * i)) * innerDiameter;
        vertices[v + 13] = (float) (Math.cos(angle * i)) * innerDiameter;
        vertices[v + 14] = 0;

        // Colours
        vertices[v + 15] = 1;
        vertices[v + 16] = 1;
        vertices[v + 17] = 1;
        vertices[v + 18] = 1;

        // Normal
        vertices[v + 19] = 0;
        vertices[v + 20] = 0;
        vertices[v + 21] = 1;

        // Text coordinates
        vertices[v + 22] = (float) (Math.sin(angle * i) / 2) * (innerDiameter / outerDiameter) + 0.5f;
        vertices[v + 23] = (float) (Math.cos(angle * i) / 2) * (innerDiameter / outerDiameter) + 0.5f;
    }

    mesh.setVertices(vertices);

    short[] indices = new short[sections * 12];

    for (int i = 0; i < sections - 1; i++) {
        int j = i * 12;
        indices[j + 0] = (short) (i * 2 + 0);
        indices[j + 1] = (short) (i * 2 + 1);
        indices[j + 2] = (short) (i * 2 + 2);

        indices[j + 3] = (short) (i * 2 + 0);
        indices[j + 4] = (short) (i * 2 + 2);
        indices[j + 5] = (short) (i * 2 + 1);

        indices[j + 6] = (short) (i * 2 + 1);
        indices[j + 7] = (short) (i * 2 + 2);
        indices[j + 8] = (short) (i * 2 + 3);

        indices[j + 9] = (short) (i * 2 + 1);
        indices[j + 10] = (short) (i * 2 + 3);
        indices[j + 11] = (short) (i * 2 + 2);
    }

    int i = (sections - 1) * 2;
    int j = (sections - 1) * 12;
    indices[j] = (short) (i + 0);
    indices[j + 1] = (short) (i + 1);
    indices[j + 2] = (short) (0);
    indices[j + 3] = (short) (i + 0);
    indices[j + 4] = (short) (0);
    indices[j + 5] = (short) (i + 1);
    indices[j + 6] = (short) (i + 1);
    indices[j + 7] = (short) (0);
    indices[j + 8] = (short) (1);
    indices[j + 9] = (short) (i + 1);
    indices[j + 10] = (short) (1);
    indices[j + 11] = (short) (0);

    mesh.setIndices(indices);

    modelBuilder.part("circle", mesh, GL20.GL_TRIANGLES, material);
    Model model = modelBuilder.end();

    ModelInstance instance = new ModelInstance(model);

    return instance;
}

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static Model createTorus(Material material, float zDiameter, float xDiameter, int zSections,
        int xSections) {
    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();/*from w ww.j  a v a  2 s .c  o  m*/

    float zAngle = FULL_CIRCLE / zSections;
    float xAngle = FULL_CIRCLE / xSections;

    Mesh mesh = new Mesh(true, zSections * xSections, zSections * xSections * 6, VertexAttribute.Position(),
            VertexAttribute.ColorUnpacked(), VertexAttribute.Normal(), VertexAttribute.TexCoords(0));

    float[] vertices = new float[zSections * xSections * 12];

    for (int j = 0; j < xSections; j++) {
        for (int i = 0; i < zSections; i++) {
            int v = (j * zSections * 12) + i * 12;

            createTorusVertice(zDiameter, xDiameter, zSections, xSections, zAngle, xAngle, vertices, j, i, v);
        }
    }
    mesh.setVertices(vertices);

    short[] indices = new short[zSections * xSections * 6];

    createTorusIndices(zSections, xSections, indices);

    mesh.setIndices(indices);

    modelBuilder.part("torus", mesh, GL20.GL_TRIANGLES, material);
    Model model = modelBuilder.end();

    return model;
}

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static void createTorus2(ModelBuilder modelBuilder, Material material, float zDiameter, float xDiameter,
        int zSections, int xSections) {

    float zAngle = FULL_CIRCLE / zSections;
    float xAngle = FULL_CIRCLE / xSections;

    Mesh mesh = new Mesh(true, (zSections + 1) * (xSections + 1), zSections * xSections * 6,
            VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.Normal(),
            VertexAttribute.TexCoords(0));

    float[] vertices = new float[(zSections + 1) * (xSections + 1) * 12];

    for (int j = 0; j < xSections + 1; j++) {
        for (int i = 0; i < zSections + 1; i++) {
            int v = (j * (zSections + 1) * 12) + i * 12;

            createTorusVertice2(zDiameter, xDiameter, zSections, xSections, zAngle, xAngle, vertices, j, i, v);
        }/*from w ww . j av  a  2 s .  c o m*/
    }
    mesh.setVertices(vertices);

    short[] indices = new short[zSections * xSections * 6];

    createTorusIndices2(zSections, xSections, indices);

    mesh.setIndices(indices);

    modelBuilder.part("torus", mesh, GL20.GL_TRIANGLES, material);
}

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static void createTorus3(ModelBuilder modelBuilder, Material material, float zDiameter, float xDiameter,
        int zSections, int xSections, int bones) {

    float zAngle = FULL_CIRCLE / zSections;
    float xAngle = FULL_CIRCLE / xSections;
    float boneAngle = FULL_CIRCLE / bones;

    Mesh mesh = new Mesh(true, (zSections + 1) * (xSections + 1), zSections * xSections * 6,
            VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.Normal(),
            VertexAttribute.TexCoords(0), VertexAttribute.BoneWeight(0), VertexAttribute.BoneWeight(1));

    float[] vertices = new float[(zSections + 1) * (xSections + 1) * 16];

    int bone1Index = 0;
    int bone2Index = 0;
    float bone1Weight = 0.0f;
    float bone2Weight = 0.0f;

    float zSectionToBoneRatio = (float) (zSections + 1) / (float) bones;
    float zSectionAngle = 0.0f;
    float zSectionWeight = 0.0f;

    for (int i = 0; i < zSections + 1; i++) {

        bone1Index = (int) (i / zSectionToBoneRatio);
        if (bone1Index >= bones) {
            throw new IllegalStateException();
        }/*from ww w .  j  a  v a  2  s .  com*/
        bone2Index = bone1Index + 1;
        if (bone2Index >= bones) {
            bone2Index = 0;
        }

        zSectionAngle = i * zAngle - bone1Index * boneAngle;
        if (zSectionAngle > FULL_CIRCLE) {
            zSectionAngle = zSectionAngle - FULL_CIRCLE;
        }
        zSectionWeight = zSectionAngle / boneAngle;
        bone1Weight = 1 - zSectionWeight;
        bone2Weight = zSectionWeight;

        for (int j = 0; j < xSections + 1; j++) {
            int v = (j * (zSections + 1) * 16) + i * 16;
            createTorusVertice3(zDiameter, xDiameter, zSections, xSections, zAngle, xAngle, vertices, j, i, v,
                    bone1Index, bone1Weight, bone2Index, bone2Weight);
        }
    }
    mesh.setVertices(vertices);

    short[] indices = new short[zSections * xSections * 6];

    createTorusIndices2(zSections, xSections, indices);

    mesh.setIndices(indices);

    Node node = modelBuilder.node();
    node.id = "torus";
    modelBuilder.part("torus", mesh, GL20.GL_TRIANGLES, material);
    Matrix4[] boneMatrixes = new Matrix4[bones];
    for (int i = 0; i < boneMatrixes.length; i++) {
        boneMatrixes[i] = new Matrix4().idt();
    }

    Array<NodePart> parts = node.parts;
    Object[] items = parts.items;
    NodePart item = (NodePart) items[0];
    item.bones = boneMatrixes;
    //node.parts.items[0].bones = boneMatrixes;
}

From source file:org.ah.gcode.preview.utils.ModelBuilders.java

License:Open Source License

public static Model createUnevenTorus(Material material, float zDiameter, float xDiameter, int zSections,
        int xSections, float deviation) {
    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();/* w ww .  j a  v  a 2 s.c o m*/

    float zAngle = FULL_CIRCLE / zSections;
    float xAngle = FULL_CIRCLE / xSections;

    Mesh mesh = new Mesh(true, (zSections + 1) * (xSections + 1), zSections * xSections * 6,
            VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.Normal(),
            VertexAttribute.TexCoords(0));

    float[] vertices = new float[(zSections + 1) * (xSections + 1) * 12];

    for (int j = 0; j < xSections + 1; j++) {
        for (int i = 0; i < zSections + 1; i++) {
            int v = (j * (zSections + 1) * 12) + i * 12;

            createUnevenTorusVertice(zDiameter, xDiameter, zSections, xSections, zAngle, xAngle, vertices, j, i,
                    v, deviation);
            //createTorusVertice2(zDiameter, xDiameter, zSections, xSections, zAngle, xAngle, vertices, j, i, v);
        }
    }
    mesh.setVertices(vertices);

    short[] indices = new short[zSections * xSections * 6];

    createTorusIndices2(zSections, xSections, indices);

    mesh.setIndices(indices);

    modelBuilder.part("torus", mesh, GL20.GL_TRIANGLES, material);
    Model model = modelBuilder.end();

    return model;
}

From source file:org.bladecoder.bladeengine.util.Utils3D.java

License:Apache License

public static void createFloor() {

    ModelBuilder modelBuilder = new ModelBuilder();
    modelBuilder.begin();//www . java 2s.c o  m
    MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES,
            Usage.Position | Usage.Normal | Usage.Color,
            new Material(ColorAttribute.createDiffuse(Color.WHITE)));
    mpb.setColor(1f, 1f, 1f, 1f);
    //      mpb.box(0, -0.1f, 0, 10, .2f, 10);
    mpb.rect(-10, 0, -10, -10, 0, 10, 10, 0, 10, 10, 0, -10, 0, 1, 0);
    floorModel = modelBuilder.end();
    floorInstance = new ModelInstance(floorModel);

    // TODO Set only when FBO is active
    floorInstance.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));
}

From source file:scene3d.demo.Scene3dDemo.java

License:Apache License

@Override
public void create() {
    //2d stuff/*from   w  ww.j  ava2 s .c o m*/
    stage2d = new Stage();
    skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
    fpsLabel = new Label("ff", skin);
    fpsLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 40);
    visibleLabel = new Label("ff", skin);
    visibleLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 60);
    positionLabel = new Label("Position", skin);
    positionLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 80);
    rotationLabel = new Label("Rotation", skin);
    rotationLabel.setPosition(Gdx.graphics.getWidth() - 260, Gdx.graphics.getHeight() - 100);
    positionCameraLabel = new Label("Position", skin);
    positionCameraLabel.setPosition(20, Gdx.graphics.getHeight() - 40);
    rotationCameraLabel = new Label("Rotation", skin);
    rotationCameraLabel.setPosition(20, Gdx.graphics.getHeight() - 60);
    stage2d.addActor(fpsLabel);
    stage2d.addActor(visibleLabel);
    stage2d.addActor(positionLabel);
    stage2d.addActor(rotationLabel);
    stage2d.addActor(positionCameraLabel);
    stage2d.addActor(rotationCameraLabel);
    stage2d.addListener(new InputListener() {
        @Override
        public boolean keyUp(InputEvent event, int keycode) {
            if (keycode == Keys.LEFT)
                leftKey = false;
            if (keycode == Keys.RIGHT)
                rightKey = false;
            if (keycode == Keys.UP)
                upKey = false;
            if (keycode == Keys.DOWN)
                downKey = false;
            if (keycode == Keys.SPACE)
                spaceKey = false;
            return super.keyUp(event, keycode);
        }

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            if (keycode == Keys.LEFT)
                leftKey = true;
            if (keycode == Keys.RIGHT)
                rightKey = true;
            if (keycode == Keys.UP)
                upKey = true;
            if (keycode == Keys.DOWN)
                downKey = true;
            if (keycode == Keys.SPACE)
                spaceKey = true;
            return super.keyDown(event, keycode);
        }
    });

    //3dstuff
    stage3d = new Stage3d();
    modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(5f, 5f, 5f, new Material("Color", ColorAttribute.createDiffuse(Color.WHITE)),
            Usage.Position | Usage.Normal);

    model2 = modelBuilder.createBox(2f, 2f, 2f,
            new Material("Color", ColorAttribute.createDiffuse(Color.WHITE)), Usage.Position | Usage.Normal);
    actor2 = new Actor3d(model2, 10f, 0f, 0f);
    model3 = modelBuilder.createBox(2f, 2f, 2f,
            new Material("Color", ColorAttribute.createDiffuse(Color.ORANGE)), Usage.Position | Usage.Normal);
    actor3 = new Actor3d(model3, -10f, 0f, 0f);
    actor2.setColor(Color.RED);
    actor2.setName("actor2");
    actor3.setName("actor3");
    camController = new CameraInputController(stage3d.getCamera());
    InputMultiplexer im = new InputMultiplexer();
    im.addProcessor(stage2d);// 2d should get click events first
    //im.addProcessor(stage3d);
    im.addProcessor(camController);
    Gdx.input.setInputProcessor(im);
    stage3d.touchable = Touchable.enabled; // only then will it detect hit actor3d

    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    MeshPartBuilder part = builder.part("floor", GL20.GL_TRIANGLES,
            Usage.Position | Usage.TextureCoordinates | Usage.Normal, new Material());
    for (float x = -200f; x < 200f; x += 10f) {
        for (float z = -200f; z < 200f; z += 10f) {
            part.rect(x, 0, z + 10f, x + 10f, 0, z + 10f, x + 10f, 0, z, x, 0, z, 0, 1, 0);
        }
    }
    floor = new Actor3d(builder.end());
    AssetManager am = new AssetManager();
    am.load("data/g3d/knight.g3db", Model.class);
    am.load("data/g3d/skydome.g3db", Model.class);
    am.load("data/g3d/concrete.png", Texture.class);
    am.finishLoading();
    knight = new Actor3d(am.get("data/g3d/knight.g3db", Model.class), 0f, 18f, 0f);
    knight.getAnimation().inAction = true;
    knight.getAnimation().animate("Walk", -1, 1f, null, 0.2f);
    skydome = new Actor3d(am.get("data/g3d/skydome.g3db", Model.class));
    floor.materials.get(0).set(TextureAttribute.createDiffuse(am.get("data/g3d/concrete.png", Texture.class)));
    stage3d.addActor3d(skydome);
    stage3d.addActor3d(floor);
    knight.setPitch(-90f);
    knight.setYaw(-130f);
    testActor3d();
    //stage3d.addAction3d(Actions3d.rotateBy(0f, 90f, 0f, 2f));
    //testGroup3d();
    //testStage3d();
}

From source file:ta.shape3D.mesh.MeshTA.java

License:Apache License

/**
 * draw all triangles of the Mesh in the OpenGL environment.
 * @param rendu the renderer which is used for drawing triangle, you don't need to call the begin() method of this renderer
 * before using this method.//w  w w .  j av  a2 s  .  c o  m
 * @param rendu 
 * @param projectionMatrix The projection matrix in which the mesh will be represented 
 */
final public void render(ImmediateModeRenderer20 rendu, Matrix4 projectionMatrix) {
    projectionMatrix.translate(tX, tY, tZ);
    projectionMatrix.rotateRad(Vector3.X, rX);
    projectionMatrix.rotateRad(Vector3.Y, rY);
    projectionMatrix.rotateRad(Vector3.Z, rZ);
    projectionMatrix.scale(scaleX, scaleY, scaleZ);
    if (image != null) {
        Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
        Gdx.gl20.glBindTexture(image.glTarget, image.getTextureObjectHandle());
    }
    rendu.begin(projectionMatrix, GL20.GL_TRIANGLES);
    for (Triangle3D t : triangles) {
        t.render(rendu);
    }
    rendu.end();
    Gdx.gl20.glDisable(GL20.GL_TEXTURE_BINDING_2D);
    for (MeshTA m : sousMesh) {
        m.render(rendu, projectionMatrix);
    }
    projectionMatrix.scale(1 / scaleX, 1 / scaleY, 1 / scaleZ);
    projectionMatrix.rotateRad(Vector3.X, -rX);
    projectionMatrix.rotateRad(Vector3.Y, -rY);
    projectionMatrix.rotateRad(Vector3.Z, -rZ);
    projectionMatrix.translate(-tX, -tY, -tZ);
}

From source file:tools.SpriteBatch.java

License:Apache License

private void renderMesh() {
    if (idx == 0) {
        return;/*from ww w .  ja  v  a2 s  . c o  m*/
    }

    renderCalls++;
    totalRenderCalls++;
    int spritesInBatch = idx / 24;
    if (spritesInBatch > maxSpritesInBatch) {
        maxSpritesInBatch = spritesInBatch;
    }

    lastTexture.bind();
    mesh.setVertices(vertices, 0, idx);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(spritesInBatch * 6);

    if (blendingDisabled) {
        Gdx.gl.glDisable(GL20.GL_BLEND);
    } else {
        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(blendSrcFunc, blendDstFunc);
    }

    if (customShader != null) {
        mesh.render(customShader, GL20.GL_TRIANGLES, 0, spritesInBatch * 6);
    } else {
        mesh.render(shader, GL20.GL_TRIANGLES, 0, spritesInBatch * 6);
    }

    idx = 0;
    currBufferIdx++;
    if (currBufferIdx == buffers.length) {
        currBufferIdx = 0;
    }
    mesh = buffers[currBufferIdx];
}