Search in sources :

Example 26 with ShaderProgram

use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.

the class IndexBufferObjectShaderTest method create.

@Override
public void create() {
    String vertexShader = "attribute vec4 a_position;    \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoords;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main()                  \n" + "{                            \n" + "   v_color = vec4(a_color.x, a_color.y, a_color.z, 1); \n" + "   v_texCoords = a_texCoords; \n" + "   gl_Position =  a_position;  \n" + "}                            \n";
    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" + "}";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    vbo = new VertexBufferObject(true, 3, new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoords"), new VertexAttribute(VertexAttributes.Usage.ColorPacked, 4, "a_color"));
    float[] vertices = new float[] { -1, -1, 0, 0, Color.toFloatBits(1f, 0f, 0f, 1f), 0, 1, 0.5f, 1.0f, Color.toFloatBits(0f, 1f, 0f, 1f), 1, -1, 1, 0, Color.toFloatBits(0f, 0f, 1f, 1f) };
    vbo.setVertices(vertices, 0, vertices.length);
    ibo = new IndexBufferObject(true, 3);
    ibo.setIndices(new short[] { 0, 1, 2 }, 0, 3);
    texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) IndexBufferObject(com.badlogic.gdx.graphics.glutils.IndexBufferObject) VertexBufferObject(com.badlogic.gdx.graphics.glutils.VertexBufferObject) Texture(com.badlogic.gdx.graphics.Texture)

Example 27 with ShaderProgram

use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.

the class FloatTextureTest method create.

@Override
public void create() {
    fb = new FrameBuffer(Format.RGBA8888, 200, 100, false);
    ffb = new FloatFrameBuffer(200, 100, false);
    // @off
    String vertexShader = "attribute vec4 a_position; " + "varying vec2 v_position; " + "void main(){ " + "    v_position = a_position.xy; " + "    gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); " + "}";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "uniform vec3 u_color;" + "uniform vec2 u_viewport; " + "void main(void){ " + "    vec2 uv = gl_FragCoord.xy/u_viewport; " + // <--- // regular (non-float) texture loses precision here, res == 0 for every fragment
    "    float res = mix(0.0, 0.0001, uv.x); " + "    gl_FragColor = vec4(u_color, res); " + "}";
    fbshader = new ShaderProgram(vertexShader, fragmentShader);
    vertexShader = "attribute vec4 a_position; " + "attribute vec4 a_color; " + "attribute vec2 a_texCoords; " + "uniform mat4 u_worldView; " + "varying vec4 v_color; " + "varying vec2 v_texCoords; " + "void main() " + "{ " + "    v_color = a_color; " + "    v_texCoords = a_texCoords; " + "    gl_Position =  u_worldView * a_position; " + "}";
    fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec2 v_texCoords; " + "uniform sampler2D u_fbtex, u_ffbtex; " + "vec4 getValue(vec4 col) {" + "    if (col.a > 0.00005)" + "        return vec4(col.rgb, 1.0);" + "    else" + "        return vec4(0.0, 0.0, 0.0, 1.0);" + "}" + "void main() " + "{ " + "    if (v_texCoords.y < 0.45)" + "        gl_FragColor = getValue(texture2D(u_fbtex, v_texCoords)); " + "    else if (v_texCoords.y > 0.55)" + "        gl_FragColor = getValue(texture2D(u_ffbtex, v_texCoords)); " + "}";
    // @on
    shader = new ShaderProgram(vertexShader, fragmentShader);
    createQuad();
    screenCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    createScreenQuad();
}
Also used : FloatFrameBuffer(com.badlogic.gdx.graphics.glutils.FloatFrameBuffer) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) FloatFrameBuffer(com.badlogic.gdx.graphics.glutils.FloatFrameBuffer) FrameBuffer(com.badlogic.gdx.graphics.glutils.FrameBuffer)

Example 28 with ShaderProgram

use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.

the class MultipleRenderTargetTest method create.

@Override
public void create() {
    //use default prepend shader code for batch, some gpu drivers are less forgiving
    batch = new SpriteBatch();
    //depth texture not currently sampled
    ShaderProgram.pedantic = false;
    modelCache = new ModelCache();
    ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
    ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_ARB_explicit_attrib_location : enable\n" : "#version 300 es\n";
    renderContext = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN));
    shaderProvider = new BaseShaderProvider() {

        @Override
        protected Shader createShader(Renderable renderable) {
            return new MRTShader(renderable);
        }
    };
    renderableSorter = new DefaultRenderableSorter() {

        @Override
        public int compare(Renderable o1, Renderable o2) {
            return o1.shader.compareTo(o2.shader);
        }
    };
    mrtSceneShader = new ShaderProgram(Gdx.files.internal("data/g3d/shaders/mrtscene.vert"), Gdx.files.internal("data/g3d/shaders/mrtscene.frag"));
    if (!mrtSceneShader.isCompiled()) {
        System.out.println(mrtSceneShader.getLog());
    }
    quad = createFullScreenQuad();
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.near = 1f;
    camera.far = 100f;
    camera.position.set(3, 5, 10);
    camera.lookAt(0, 2, 0);
    camera.up.set(0, 1, 0);
    camera.update();
    cameraController = new FirstPersonCameraController(camera);
    cameraController.setVelocity(50);
    Gdx.input.setInputProcessor(cameraController);
    frameBuffer = new MRTFrameBuffer(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 3);
    AssetManager assetManager = new AssetManager();
    assetManager.load("data/g3d/materials/cannon.g3db", Model.class);
    assetManager.finishLoading();
    Model scene = assetManager.get("data/g3d/materials/cannon.g3db");
    cannon = new ModelInstance(scene, "Cannon_LP");
    cannon.transform.setToTranslationAndScaling(0, 0, 0, 0.001f, 0.001f, 0.001f);
    ModelBuilder modelBuilder = new ModelBuilder();
    for (int i = 0; i < NUM_LIGHTS; i++) {
        modelBuilder.begin();
        Light light = new Light();
        light.color.set(MathUtils.random(1f), MathUtils.random(1f), MathUtils.random(1f));
        light.position.set(MathUtils.random(-10f, 10f), MathUtils.random(10f, 15f), MathUtils.random(-10f, 10f));
        light.vy = MathUtils.random(10f, 20f);
        light.vx = MathUtils.random(-10f, 10f);
        light.vz = MathUtils.random(-10f, 10f);
        MeshPartBuilder meshPartBuilder = modelBuilder.part("light", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
        meshPartBuilder.setColor(light.color.x, light.color.y, light.color.z, 1f);
        meshPartBuilder.sphere(0.2f, 0.2f, 0.2f, 10, 10);
        light.lightInstance = new ModelInstance(modelBuilder.end());
        lights.add(light);
    }
    modelBuilder.begin();
    MeshPartBuilder meshPartBuilder = modelBuilder.part("floor", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked | VertexAttributes.Usage.Normal, new Material());
    meshPartBuilder.setColor(0.2f, 0.2f, 0.2f, 1f);
    meshPartBuilder.box(0, -0.1f, 0f, 20f, 0.1f, 20f);
    floorInstance = new ModelInstance(modelBuilder.end());
    Gdx.input.setInputProcessor(new InputMultiplexer(this, cameraController));
}
Also used : RenderContext(com.badlogic.gdx.graphics.g3d.utils.RenderContext) AssetManager(com.badlogic.gdx.assets.AssetManager) Material(com.badlogic.gdx.graphics.g3d.Material) DefaultRenderableSorter(com.badlogic.gdx.graphics.g3d.utils.DefaultRenderableSorter) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) Shader(com.badlogic.gdx.graphics.g3d.Shader) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) BaseShaderProvider(com.badlogic.gdx.graphics.g3d.utils.BaseShaderProvider) ModelCache(com.badlogic.gdx.graphics.g3d.ModelCache) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) Renderable(com.badlogic.gdx.graphics.g3d.Renderable) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) Model(com.badlogic.gdx.graphics.g3d.Model) FirstPersonCameraController(com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController) DefaultTextureBinder(com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder)

Example 29 with ShaderProgram

use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.

the class MipMap2D method create.

@Override
public void create() {
    String vertexShader = "uniform float u_offset;      \n" + "attribute vec4 a_position;   \n" + "attribute vec2 a_texCoord;   \n" + "varying vec2 v_texCoord;     \n" + "void main()                  \n" + "{                            \n" + "   gl_Position = a_position; \n" + "   gl_Position.x += u_offset;\n" + "   v_texCoord = a_texCoord;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec2 v_texCoord;                            \n" + "uniform sampler2D s_texture;                        \n" + "void main()                                         \n" + "{                                                   \n" + "  gl_FragColor = texture2D( s_texture, v_texCoord );\n" + "}                                                   \n";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 4, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
    float[] vertices = { // Position 0
    -0.5f, // Position 0
    0.5f, // Position 0
    0.0f, // Position 0
    1.5f, // TexCoord 0
    0.0f, // TexCoord 0
    0.0f, // Position 1
    -0.5f, // Position 1
    -0.5f, // Position 1
    0.0f, // Position 1
    0.75f, // TexCoord 1
    0.0f, // TexCoord 1
    1.0f, // Position 2
    0.5f, // Position 2
    -0.5f, // Position 2
    0.0f, // Position 2
    0.75f, // TexCoord 2
    1.0f, // TexCoord 2
    1.0f, // Position 3
    0.5f, // Position 3
    0.5f, // Position 3
    0.0f, // Position 3
    1.5f, // TexCoord 3
    1.0f, // TexCoord 3
    0.0f };
    short[] indices = { 0, 1, 2, 0, 2, 3 };
    mesh.setVertices(vertices);
    mesh.setIndices(indices);
    createTexture();
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh)

Example 30 with ShaderProgram

use of com.badlogic.gdx.graphics.glutils.ShaderProgram in project libgdx by libgdx.

the class TextureArrayTest method create.

@Override
public void create() {
    GL30Profiler.enable();
    ShaderProgram.prependVertexCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
    ShaderProgram.prependFragmentCode = Gdx.app.getType().equals(Application.ApplicationType.Desktop) ? "#version 140\n #extension GL_EXT_texture_array : enable\n" : "#version 300 es\n";
    String[] texPaths = new String[] { "data/g3d/materials/Searing Gorge.jpg", "data/g3d/materials/Lava Cracks.jpg", "data/g3d/materials/Deep Fire.jpg" };
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(8, 10f, 20f);
    camera.lookAt(10, 0, 10);
    camera.up.set(0, 1, 0);
    camera.update();
    cameraController = new FirstPersonCameraController(camera);
    Gdx.input.setInputProcessor(cameraController);
    textureArray = new TextureArray(texPaths);
    textureArray.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
    shaderProgram = new ShaderProgram(Gdx.files.internal("data/shaders/texturearray.vert"), Gdx.files.internal("data/shaders/texturearray.frag"));
    System.out.println(shaderProgram.getLog());
    int vertexStride = 6;
    int vertexCount = 100 * 100;
    terrain = new Mesh(false, vertexCount * 6, 0, new VertexAttributes(VertexAttribute.Position(), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 3, ShaderProgram.TEXCOORD_ATTRIBUTE + 0)));
    Pixmap data = new Pixmap(Gdx.files.internal("data/g3d/heightmap.png"));
    float[] vertices = new float[vertexCount * vertexStride * 6];
    int idx = 0;
    for (int i = 0; i < 100 - 1; i++) {
        for (int j = 0; j < 100 - 1; j++) {
            idx = addVertex(i, j, vertices, data, idx);
            idx = addVertex(i, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j, vertices, data, idx);
            idx = addVertex(i, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j + 1, vertices, data, idx);
            idx = addVertex(i + 1, j, vertices, data, idx);
        }
    }
    terrain.setVertices(vertices);
    data.dispose();
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) TextureArray(com.badlogic.gdx.graphics.TextureArray) Mesh(com.badlogic.gdx.graphics.Mesh) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) FirstPersonCameraController(com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)31 Texture (com.badlogic.gdx.graphics.Texture)10 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)9 Mesh (com.badlogic.gdx.graphics.Mesh)8 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)7 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)5 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)3 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)3 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 Material (com.badlogic.gdx.graphics.g3d.Material)2 ModelBatch (com.badlogic.gdx.graphics.g3d.ModelBatch)2 FirstPersonCameraController (com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController)2 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)2 FrameBuffer (com.badlogic.gdx.graphics.glutils.FrameBuffer)2 IndexBufferObject (com.badlogic.gdx.graphics.glutils.IndexBufferObject)2 VertexBufferObject (com.badlogic.gdx.graphics.glutils.VertexBufferObject)2