PDA

View Full Version : How can I texture 2D surface ?



kkebabci
05-21-2016, 01:14 PM
Hello, I want to texture my 2D terrain surface.
I have created the ground mesh and surface mesh separately and everything is okay for ground mesh. But I couldn't texture the surface mesh correctly. Surface was textured linear on x axis but i wanted surface mesh to follow ground curves. For example, at Hill Climb game surfaces are exactly as I want.

Here is how I create my textures and draw my meshes:


private void initializeTextures() {
groundTexture = new Texture(Gdx.files.internal(Constants.SIMPLE_GROUND _FN));
groundTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

surfaceTexture = new Texture(Gdx.files.internal(Constants.SIMPLE_GROUND _SURFACE_FN));
surfaceTexture.setWrap(Texture.TextureWrap.ClampTo Edge, Texture.TextureWrap.Repeat);
surfaceTexture.setFilter(Texture.TextureFilter.Lin ear, Texture.TextureFilter.Linear);
}

public void draw(SpriteBatch batch){

batch.setShader(shader);
shapeRenderer.setProjectionMatrix(GameScreen.getCa mera().combined);
shader.begin();
shader.setUniformMatrix("u_projTrans", GameScreen.getCamera().combined);
groundTexture.bind(0);
shader.setUniformi("u_texture", 0);
groundMesh.render(shader, GL20.GL_TRIANGLES);
surfaceTexture.bind(0);
shader.setUniformi("u_texture", 0);
shader.setUniformMatrix("u_projTrans", GameScreen.getCamera().combined);
surfaceMesh.render(shader, GL20.GL_TRIANGLES);
shader.end();

}

Vertex and fragment shaders:


#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif

varying vec2 v_texCoords;
uniform sampler2D u_texture;

void main() {
gl_FragColor = texture2D(u_texture, v_texCoords);
}


attribute vec4 a_position;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec2 v_texCoords;

void main() {
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}

I am new about programming games using OpenGL(libGdx) and really need your helps to achieve this problem.