[英]Drawing rectangle with opengl es on android doesn't work
我正在嘗試在 android 上繪制一個 opengl es 3.0 的矩形,但它不起作用。 我可以用顏色填充背景,但不渲染對象。 這很奇怪,因為我從我的舊項目中復制了大部分代碼。 但從此時起,android 已從示例 ConstraintLayout 定義更改。 我所知道的是 SurfaceView 和 Renderer 工作正常。 我的繪圖代碼 function 下面。
float []translateMatrix = new float[16];
float []targetMatrix = new float[16];
if (rectangle.getTexture().getBitmapName() == null || rectangle.getTexture().getBitmapName().isEmpty())
{
throw new CustomException("bitmap name cannot be empty!");
} else if (rectangle.getTexture().getBitmap() == null)
{
rectangle.getTexture().setBitmap(
BitmapUtil.getBitmap(
bitmaps.getHashMap().get(rectangle.getTexture().getBitmapName()
)
)
);
}
Matrix.multiplyMM(
targetMatrix,
0,
vPMatrix,
0,
translateMatrix,
0
);
GLES20.glUniform1f(handlers[HandlersUtil.ALPHAMOD_ID], 1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());
GLES20.glEnableVertexAttribArray(handlers[HandlersUtil.VPOSITION_ID]);
GLES20.glVertexAttribPointer(
handlers[HandlersUtil.VPOSITION_ID],
VERTICE_VERTEX,
GLES20.GL_FLOAT,
NORMALIZED,
OBJ_STRIDE,
rectangle.getCoordinatesBuffer()
);
GLES20.glEnableVertexAttribArray(handlers[HandlersUtil.ATEXCOORDINATE_ID]);
GLES20.glVertexAttribPointer(
handlers[HandlersUtil.ATEXCOORDINATE_ID],
2,
GLES20.GL_FLOAT,
NORMALIZED,
TEX_STRIDE,
rectangle.getTexture().getBuffer()
);
GLES20.glUniformMatrix4fv(
handlers[HandlersUtil.UMVPMATRIX_ID],
COUNT,
TRANSPOSE,
targetMatrix,
0
);
GLES20.glDrawElements(
GLES20.GL_TRIANGLE_STRIP,
rectangle.getIndices(),
GLES20.GL_UNSIGNED_SHORT,
rectangle.getOrderBuffer()
);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glUniform1i(handlers[HandlersUtil.UTEXTURE_ID], X);
GLES20.glDisableVertexAttribArray(handlers[HandlersUtil.ATEXCOORDINATE_ID]);
GLES20.glDisableVertexAttribArray(handlers[HandlersUtil.VPOSITION_ID]);
我的片段着色器
precision mediump float;
uniform vec4 vColor;
uniform sampler2D uTexture;
varying vec2 vTexCoordinate;
uniform float alphaMod;
void main(){
gl_FragColor = texture2D(uTexture, vTexCoordinate);
gl_FragColor.a *= alphaMod;
}
頂點着色器
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
attribute vec2 aTexCoordinate;
varying vec2 vTexCoordinate;
void main(){
gl_Position = uMVPMatrix * vPosition;
vTexCoordinate = aTexCoordinate;
}
長方形 class
public final class Rectangle
{
public final static int INDICES = 4;
public final static int MATRIX_SIZE = 16;
public final static int OFFSET = 0;
public final static int VERTICES = 12;
public final static int LEFT_TOP_ID = 0;
public final static int LEFT_BOTTOM_ID = 1;
public final static int RIGHT_TOP_ID = 2;
public final static int RIGHT_BOTTOM_ID = 3;
public final static int X = 0;
public final static int Y = 1;
public final static int Z = 2;
public final static int VERTICE_VERTEX = 3;
public final static float DEFAULT_ALPHA = 1.0f;
private final int indices;
private float alphaMod;
private final short[] order;
private final float[] coordinates;
private final float[] translateMatrix;
private final float[] targetMatrix;
private FloatBuffer coordinatesBuffer;
private final ShortBuffer orderBuffer;
private final Texture texture;
public Rectangle()
{
this.indices = INDICES;
this.alphaMod = DEFAULT_ALPHA;
this.order = new short[]{
0, 1, 2, 3
};
this.coordinates = new float[VERTICES];
this.translateMatrix = new float[MATRIX_SIZE];
this.targetMatrix = new float[MATRIX_SIZE];
this.coordinatesBuffer = BufferUtil.getBuffer(coordinates);
this.orderBuffer = BufferUtil.getBuffer(order);
this.texture = new Texture();
Matrix.setIdentityM(translateMatrix, OFFSET);
}
}
紋理樣本統一和紋理之間的綁定點是紋理單元的索引。 您必須將紋理單元的索引設置為統一,並且必須將紋理綁定到相應的紋理單元。
glBindTexture
為當前活動的紋理單元 ( glActiveTexture
) 提供紋理。 例如設置值1並激活紋理單元GL_TEXTURE1
:
GLES20.glUniform1f(handlers[HandlersUtil.ALPHAMOD_ID], 1);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.