Search in sources :

Example 6 with ShaderProgram

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

the class MipMapTest method create.

@Override
public void create() {
    camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(0, 1.5f, 1.5f);
    camera.lookAt(0, 0, 0);
    camera.update();
    controller = new PerspectiveCamController(camera);
    mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
    mesh.setVertices(new float[] { -1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, -1, 1, 0, -1, 0, -1, 0, 0 });
    mesh.setIndices(new short[] { 0, 1, 2, 3 });
    shader = new ShaderProgram(Gdx.files.internal("data/shaders/flattex-vert.glsl").readString(), Gdx.files.internal("data/shaders/flattex-frag.glsl").readString());
    if (!shader.isCompiled())
        throw new GdxRuntimeException("shader error: " + shader.getLog());
    textureHW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
    MipMapGenerator.setUseHardwareMipMap(false);
    textureSW = new Texture(Gdx.files.internal("data/badlogic.jpg"), Format.RGB565, true);
    currTexture = textureHW;
    createUI();
    multiplexer = new InputMultiplexer();
    Gdx.input.setInputProcessor(multiplexer);
    multiplexer.addProcessor(ui);
    multiplexer.addProcessor(controller);
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) PerspectiveCamController(com.badlogic.gdx.tests.utils.PerspectiveCamController) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Mesh(com.badlogic.gdx.graphics.Mesh) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) Texture(com.badlogic.gdx.graphics.Texture)

Example 7 with ShaderProgram

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

the class DistanceFieldFont method createDistanceFieldShader.

/** Returns a new instance of the distance field shader, see https://github.com/libgdx/libgdx/wiki/Distance-field-fonts if the
	 * u_smoothing uniform > 0.0. Otherwise the same code as the default SpriteBatch shader is used. */
public static ShaderProgram createDistanceFieldShader() {
    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" + //
    "varying vec4 v_color;\n" + //
    "varying vec2 v_texCoords;\n" + //
    "\n" + //
    "void main() {\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" + //
    "	precision mediump float;\n" + //
    "	precision mediump int;\n" + //
    "#endif\n" + //
    "\n" + //
    "uniform sampler2D u_texture;\n" + //
    "uniform float u_smoothing;\n" + //
    "varying vec4 v_color;\n" + //
    "varying vec2 v_texCoords;\n" + //
    "\n" + //
    "void main() {\n" + //
    "	if (u_smoothing > 0.0) {\n" + //
    "		float smoothing = 0.25 / u_smoothing;\n" + //
    "		float distance = texture2D(u_texture, v_texCoords).a;\n" + //
    "		float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);\n" + //
    "		gl_FragColor = vec4(v_color.rgb, alpha * v_color.a);\n" + //
    "	} else {\n" + //
    "		gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + //
    "	}\n" + "}\n";
    ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false)
        throw new IllegalArgumentException("Error compiling distance field shader: " + shader.getLog());
    return shader;
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram)

Example 8 with ShaderProgram

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

the class ShaderProgramLoader method loadSync.

@Override
public ShaderProgram loadSync(AssetManager manager, String fileName, FileHandle file, ShaderProgramParameter parameter) {
    String vertFileName = null, fragFileName = null;
    if (parameter != null) {
        if (parameter.vertexFile != null)
            vertFileName = parameter.vertexFile;
        if (parameter.fragmentFile != null)
            fragFileName = parameter.fragmentFile;
    }
    if (vertFileName == null && fileName.endsWith(fragmentFileSuffix)) {
        vertFileName = fileName.substring(0, fileName.length() - fragmentFileSuffix.length()) + vertexFileSuffix;
    }
    if (fragFileName == null && fileName.endsWith(vertexFileSuffix)) {
        fragFileName = fileName.substring(0, fileName.length() - vertexFileSuffix.length()) + fragmentFileSuffix;
    }
    FileHandle vertexFile = vertFileName == null ? file : resolve(vertFileName);
    FileHandle fragmentFile = fragFileName == null ? file : resolve(fragFileName);
    String vertexCode = vertexFile.readString();
    String fragmentCode = vertexFile.equals(fragmentFile) ? vertexCode : fragmentFile.readString();
    if (parameter != null) {
        if (parameter.prependVertexCode != null)
            vertexCode = parameter.prependVertexCode + vertexCode;
        if (parameter.prependFragmentCode != null)
            fragmentCode = parameter.prependFragmentCode + fragmentCode;
    }
    ShaderProgram shaderProgram = new ShaderProgram(vertexCode, fragmentCode);
    if ((parameter == null || parameter.logOnCompileFailure) && !shaderProgram.isCompiled()) {
        manager.getLogger().error("ShaderProgram " + fileName + " failed to compile:\n" + shaderProgram.getLog());
    }
    return shaderProgram;
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 9 with ShaderProgram

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

the class CustomShaderSpriteBatchTest method create.

@Override
public void create() {
    batch = new SpriteBatch(10);
    ShaderProgram.pedantic = false;
    shader = new ShaderProgram(Gdx.files.internal("data/shaders/batch.vert").readString(), Gdx.files.internal("data/shaders/batch.frag").readString());
    batch.setShader(shader);
    texture = new Texture("data/badlogic.jpg");
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Texture(com.badlogic.gdx.graphics.Texture)

Example 10 with ShaderProgram

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

the class EdgeDetectionTest method create.

public void create() {
    ShaderProgram.pedantic = false;
    /*
		 * shader = new ShaderProgram(Gdx.files.internal("data/shaders/default.vert").readString(), Gdx.files.internal(
		 * "data/shaders/depthtocolor.frag").readString()); if (!shader.isCompiled()) { Gdx.app.log("EdgeDetectionTest",
		 * "couldn't compile scene shader: " + shader.getLog()); }
		 */
    batchShader = new ShaderProgram(Gdx.files.internal("data/shaders/batch.vert").readString(), Gdx.files.internal("data/shaders/convolution.frag").readString());
    if (!batchShader.isCompiled()) {
        Gdx.app.log("EdgeDetectionTest", "couldn't compile post-processing shader: " + batchShader.getLog());
    }
    ObjLoader objLoader = new ObjLoader();
    scene = objLoader.loadModel(Gdx.files.internal("data/scene.obj"));
    sceneInstance = new ModelInstance(scene);
    modelBatch = new ModelBatch();
    fbo = new FrameBuffer(Format.RGB565, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(0, 0, 10);
    cam.lookAt(0, 0, 0);
    cam.far = 30;
    batch = new SpriteBatch();
    batch.setShader(batchShader);
    fboRegion = new TextureRegion(fbo.getColorBufferTexture());
    fboRegion.flip(false, true);
    logger = new FPSLogger();
    calculateOffsets();
}
Also used : ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) ModelBatch(com.badlogic.gdx.graphics.g3d.ModelBatch) ObjLoader(com.badlogic.gdx.graphics.g3d.loader.ObjLoader) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) FrameBuffer(com.badlogic.gdx.graphics.glutils.FrameBuffer) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) FPSLogger(com.badlogic.gdx.graphics.FPSLogger)

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