Libgdx - 2D Mesh rendering overlap glitch

Posted by user46858 on Game Development See other posts from Game Development or by user46858
Published on 2014-06-07T02:11:15Z Indexed on 2014/06/07 3:49 UTC
Read the original article Hit count: 454

Filed under:
|
|
|

I am trying to render a 2D circle segment mesh (quarter circle)using Libgdx/Opengl ES 2.0 but I seem to be getting an overlapping issue as seen in the picture attached.

I cant seem to find the cause of the problem but the overlapping disappears/reappears if I drag and resize the window to random sizes. The problem occurs on both pc and android.

The strange thing is the first two segments atleast dont seem to be causing any overlapping only the third and/or forth segment.......even though they are all rendered using the same mesh object.....

I have spent ages trying to find the cause of the problem before posting here for help so ANY help/advice in finding the cause of this problem would be really appreciated.


public class MyGdxGame extends Game {

private SpriteBatch batch; private Texture texture; private OrthographicCamera myCamera; private float w; private float h; private ShaderProgram circleSegShader; private Mesh circleScaleSegMesh; private Stage stage; private float TotalSegments; Vector3 virtualres;

@Override public void create() {
w = Gdx.graphics.getWidth(); h = Gdx.graphics.getHeight();

  batch = new SpriteBatch();

  ViewPortsize = new Vector2();
  TotalSegments = 4.0f;
  virtualres = new Vector3(1280.0f, 720.0f, 0.0f);
  myCamera = new OrthographicCamera();
  myCamera.setToOrtho(false, w, h);

  texture = new Texture(Gdx.files.internal("data/libgdx.png"));
  texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

  circleScaleSegMesh = createCircleMesh_V3(0.0f,0.0f,200.0f, 30.0f,3, (360.0f /TotalSegments) );
  circleSegShader = loadShaderFromFile(new String("circleseg.vert"), new String("circleseg.frag"));
  shaderProgram.pedantic = false;

  stage = new Stage();
  stage.setViewport(new ExtendViewport(w, h));

  Gdx.input.setInputProcessor(stage);

}

@Override public void render() {

  ....

  //render
  renderInit();
  renderCircleScaledSegment();

}

@Override public void resize(int width, int height) {

  stage.getViewport().update(width, height, true);

  myCamera.position.set( virtualres.x/2.0f, virtualres.y/2.0f, 0.0f);
  myCamera.update();

}

public void renderInit(){

  Gdx.gl20.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

  batch.setShader(null);   
  batch.setProjectionMatrix(myCamera.combined);

}

public void renderCircleScaledSegment(){

  Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
  Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  Gdx.gl20.glEnable(GL20.GL_BLEND);

  batch.begin();
  circleSegShader.begin();

  Matrix4 modelMatrix = new Matrix4();
  Matrix4 cameraMatrix = new Matrix4();
  Matrix4 cameraMatrix2 = new Matrix4();
  Matrix4 cameraMatrix3 = new Matrix4();
  Matrix4 cameraMatrix4 = new Matrix4();

  cameraMatrix = myCamera.combined.cpy();
  modelMatrix.idt().rotate(new Vector3(0.0f,0.0f,1.0f), 0.0f - ((360.0f /TotalSegments)/ 2.0f)).trn(virtualres.x/2.0f,virtualres.y/2.0f, 0.0f);
  cameraMatrix.mul(modelMatrix);

  cameraMatrix2 = myCamera.combined.cpy();
  modelMatrix.idt().rotate(new Vector3(0.0f,0.0f,1.0f), 0.0f - ((360.0f /TotalSegments)/ 2.0f) +(360.0f /TotalSegments) ).trn(virtualres.x/2.0f,virtualres.y/2.0f, 0.0f);
  cameraMatrix2.mul(modelMatrix);

  cameraMatrix3 = myCamera.combined.cpy();
  modelMatrix.idt().rotate(new Vector3(0.0f,0.0f,1.0f), 0.0f - ((360.0f /TotalSegments)/ 2.0f) +(2*(360.0f /TotalSegments))).trn(virtualres.x/2.0f,virtualres.y/2.0f, 0.0f);
  cameraMatrix3.mul(modelMatrix);

  cameraMatrix4 = myCamera.combined.cpy();
  modelMatrix.idt().rotate(new Vector3(0.0f,0.0f,1.0f),0.0f - ((360.0f /TotalSegments)/ 2.0f) +(3*(360.0f /TotalSegments)) ).trn(virtualres.x/2.0f,virtualres.y/2.0f, 0.0f);
  cameraMatrix4.mul(modelMatrix);

  Vector3 box2dpos = new Vector3(0.0f, 0.0f, 0.0f);

  circleSegShader.setUniformMatrix("u_projTrans",  cameraMatrix);
  circleSegShader.setUniformf("u_box2dpos", box2dpos);
  circleSegShader.setUniformi("u_texture",   0);
  texture.bind();
  circleScaleSegMesh.render(circleSegShader, GL20.GL_TRIANGLES);

  circleSegShader.setUniformMatrix("u_projTrans",  cameraMatrix2);
  circleSegShader.setUniformf("u_box2dpos", box2dpos);
  circleSegShader.setUniformi("u_texture",   0);
  texture.bind();
  circleScaleSegMesh.render(circleSegShader, GL20.GL_TRIANGLES);

  circleSegShader.setUniformMatrix("u_projTrans",  cameraMatrix3);
  circleSegShader.setUniformf("u_box2dpos", box2dpos);
  circleSegShader.setUniformi("u_texture",   0);
  texture.bind();
  circleScaleSegMesh.render(circleSegShader, GL20.GL_TRIANGLES);

  circleSegShader.setUniformMatrix("u_projTrans",  cameraMatrix4);
  circleSegShader.setUniformf("u_box2dpos", box2dpos);
  circleSegShader.setUniformi("u_texture",   0);
  texture.bind();
  circleScaleSegMesh.render(circleSegShader, GL20.GL_TRIANGLES);

  circleSegShader.end();
  batch.flush();
  batch.end();

  Gdx.gl20.glDisable(GL20.GL_DEPTH_TEST);
  Gdx.gl20.glDisable(GL20.GL_BLEND);

}

public Mesh createCircleMesh_V3(float cx, float cy, float r_out, float r_in, int num_segments, float segmentSizeDegrees){

float theta = (float) (2.0f * MathUtils.PI / (num_segments * (360.0f / segmentSizeDegrees))); float c = MathUtils.cos(theta);//precalculate the sine and cosine float s = MathUtils.sin(theta); float t,t2; float x = r_out;//we start at angle = 0 float y = 0; float x2 = r_in;//we start at angle = 0 float y2 = 0; float[] meshCoords = new float[num_segments *2 *3 *7]; int arrayIndex = 0; //array for triangles without indices for(int ii = 0; ii < num_segments; ii++) { meshCoords[arrayIndex] = x2+cx; meshCoords[arrayIndex +1] = y2+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; meshCoords[arrayIndex] = x+cx; meshCoords[arrayIndex +1] = y+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; t = x; x = c * x - s * y; y = s * t + c * y; meshCoords[arrayIndex] = x+cx; meshCoords[arrayIndex +1] = y+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; meshCoords[arrayIndex] = x2+cx; meshCoords[arrayIndex +1] = y2+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; meshCoords[arrayIndex] = x+cx; meshCoords[arrayIndex +1] = y+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; t2 = x2; x2 = c * x2 - s * y2; y2 = s * t2 + c * y2; meshCoords[arrayIndex] = x2+cx; meshCoords[arrayIndex +1] = y2+cy; meshCoords[arrayIndex +2] = 0.0f; meshCoords[arrayIndex +3] = 63.0f/255.0f; meshCoords[arrayIndex +4] = 139.0f/255.0f; meshCoords[arrayIndex +5] = 217.0f/255.0f; meshCoords[arrayIndex +6] = 0.7f; arrayIndex = arrayIndex + 7; } Mesh myMesh = new Mesh(VertexDataType.VertexArray, false, meshCoords.length, 0, new VertexAttribute(VertexAttributes.Usage.Position, 3, "a_position"), new VertexAttribute(VertexAttributes.Usage.Color, 4, "a_color")); myMesh.setVertices(meshCoords); return myMesh; } }

Output Rendering

© Game Development or respective owner

Related posts about opengl

Related posts about libgdx