private abstract class GroundShape extends Shape {
protected class Vertex { float x, y; float u, v; }; protected class MorphVertexBuffer extends VertexBuffer { public MorphVertexBuffer(int capacity) { // , . super(capacity, GL11.GL_STATIC_DRAW, true); } // . public void update(Vertex[] vertexes) { int j = 0; final float[] bufferData = new float[vertexes.length*2]; for (int i = 0; i < vertexes.length; ++i) { bufferData[j++] = vertexes[i].x; bufferData[j++] = vertexes[i].y; } final FastFloatBuffer buffer = this.getFloatBuffer(); buffer.position(0); buffer.put(bufferData); buffer.position(0);//, :) super.setHardwareBufferNeedsUpdate(); } }
protected class MorphTexture extends BufferObject { //ITexture , . final ITexture mTexture; public MorphTexture(ITexture tex, int pCapacity) { super(pCapacity, GL11.GL_STATIC_DRAW, true); mTexture = tex; } public void ApplyUV(Vertex [] vertexes) { final float[] bufferData = new float[vertexes.length*2]; for (int i = 0, j = 0; i < vertexes.length; ++i) { bufferData[j++] = vertexes[i].u; bufferData[j++] = vertexes[i].v; } final FastFloatBuffer buffer = this.getFloatBuffer(); buffer.position(0); buffer.put(bufferData); buffer.position(0);//, :) super.setHardwareBufferNeedsUpdate(); }
public void onApply(final GL10 pGL) { this.mTexture.bind(pGL);// , glBindTexture() if(GLHelper.EXTENSIONS_VERTEXBUFFEROBJECTS) { final GL11 gl11 = (GL11)pGL; selectOnHardware(gl11); GLHelper.texCoordZeroPointer(gl11); } else { GLHelper.texCoordPointer(pGL, getFloatBuffer()); } } }
MorphVertexBuffer m_Buffer; MorphTexture m_TextureRegion; int vertexesLimit; // . protected BitmapTextureAtlas m_Texture;//,
public GroundShape(BitmapTextureAtlas texture) { super(0, 0); m_Texture = texture; }
protected void Init() { Vertex[] vertexes = buildVertexBuffer();// . if (vertexes == null) return; // . vertexesLimit = vertexes.length; m_Buffer = new MorphVertexBuffer(vertexesLimit*2); m_Buffer.update(vertexes); m_TextureRegion = new MorphTexture(m_Texture, vertexesLimit*2); m_TextureRegion.ApplyUV(vertexes); }
protected abstract Vertex[] buildVertexBuffer();
@Override protected void doDraw(final GL10 pGL, final Camera pCamera) { // m_TextureRegion.onApply(pGL); // super.doDraw(pGL, pCamera); } @Override protected void onInitDraw(final GL10 pGL) { // UV . //GLHelper - . super.onInitDraw(pGL); GLHelper.enableTextures(pGL); GLHelper.enableTexCoordArray(pGL); } @Override protected void drawVertices(GL10 pGL, Camera arg1) { // . pGL.glDrawArrays(GL10.GL_TRIANGLES, 0, vertexesLimit); }
@Override protected VertexBuffer getVertexBuffer() { return m_Buffer; }
@Override public boolean collidesWith(IShape arg0) { return false; } @Override public float getBaseHeight() { return 0; } @Override public float getBaseWidth() { return 0; } @Override public float getHeight() { return 0; } @Override public float getWidth() { return 0; } @Override public boolean contains(float arg0, float arg1) { return false; } @Override protected boolean isCulled(Camera arg0) { return false; } @Override protected void onUpdateVertexBuffer() { } }
private class GroundSelf extends GroundShape { public GroundSelf(List<Section> sec, BitmapTextureAtlas texture) { super(texture); sections = sec;// , . Init();// GroundShape' }
@Override protected Vertex[] buildVertexBuffer() { int vertexesCount = 0, i, j, k = 0; float hellY = 800.0f;// "". final float maxU = 4.0f;// U final float maxV = 2.0f;// V float stepU; // // - , . // V. float startV = sections.get(0).lines.get(0).line.getY1(); float valueV = hellY - sections.get(0).lines.get(0).line.getY1(); for (i = 0; i < sections.size(); ++i) vertexesCount += sections.get(i).lines.size()*6; Vertex[] res = new Vertex[vertexesCount]; Section tmpSection; Line tmpLine; for (i = 0; i < sections.size(); ++i) { tmpSection = sections.get(i); // //. . // 6 . for (j = 0; j < tmpSection.lines.size(); ++j) { tmpLine = tmpSection.lines.get(j).line; stepU = maxU/(float)tmpSection.lines.size(); res[k] = new Vertex(); res[k].x = tmpLine.getX1(); res[k].y = tmpLine.getY1(); res[k].u = (float)j*stepU; res[k++].v = maxV + ((startV - tmpLine.getY1())/valueV)*maxV; res[k] = new Vertex(); res[k].x = tmpLine.getX1(); res[k].y = hellY; res[k].u = (float)j*stepU; res[k++].v = 0.0f; res[k] = new Vertex(); res[k].x = tmpLine.getX2(); res[k].y = tmpLine.getY2(); res[k].u = (float)(j + 1)*stepU; res[k++].v = maxV + ((startV - tmpLine.getY2())/valueV)*maxV; res[k] = new Vertex(); res[k].x = tmpLine.getX2(); res[k].y = tmpLine.getY2(); res[k].u = (float)(j + 1)*stepU; res[k++].v = maxV + ((startV - tmpLine.getY2())/valueV)*maxV; res[k] = new Vertex(); res[k].x = tmpLine.getX1(); res[k].y = hellY; res[k].u = (float)j*stepU; res[k++].v = 0.0f; res[k] = new Vertex(); res[k].x = tmpLine.getX2(); res[k].y = hellY; res[k].u = (float)(j + 1)*stepU; res[k++].v = 0.0f; } } // , . return res; } List<Section> sections; }
// grndSelf = new GroundSelf(sections, EvoGlobal.getTextureCache().get(EvoTextureCache.tex_ground).texture); // AndEngine EvoGlobal.getWorld().getScene().attachChild(grndSelf);
Source: https://habr.com/ru/post/150042/