用OpenGL ES在安卓上绘制矩形不起作用。

huangapple go评论79阅读模式
英文:

Drawing rectangle with opengl es on android doesn't work

问题

float []translateMatrix = new float[16];
float []targetMatrix = new float[16];

if (rectangle.getTexture().getBitmapName() == null || rectangle.getTexture().getBitmapName().isEmpty())
{
    throw new CustomException("位图名称不能为空!");
} 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;
}
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);
    }
}

请注意,这是您提供的代码的翻译版本。如果您需要更多帮助或有进一步的问题,请随时提问。

英文:

I'm trying to draw a rectangle with opengl es 3.0 on android but it doesn't work. I can fill background with color but objects are not rendered. It's strange becouse i copied most of code from my old project which works. But from this time android was changed fro example ConstraintLayout definition. All what i know is that SurfaceView and Renderer working correctlly. Code of my drawing function below.

    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]); 

My fragment shader

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;
}

vertex shader

uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
attribute vec2 aTexCoordinate;
varying vec2 vTexCoordinate;
void main(){
gl_Position = uMVPMatrix * vPosition;
vTexCoordinate = aTexCoordinate;
}

Rectangle 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);
}
}

答案1

得分: 0

纹理采样器(texture sample uniform)与纹理之间的绑定点是纹理单元的索引。您必须将纹理单元的索引设置到uniform上,并且必须将纹理绑定到相应的纹理单元。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());
英文:

The binding point between a texture sample uniform and the texture is the index of the texture unit. You have to set the index of the texture unit to the uniform and you have to bind the texture to the corresponding texture unit.
glBindTexture bids a texture to the currently active texture unit (glActiveTexture). For instance set the value 1 and activate the texture unit GL_TEXTURE1:

GLES20.glUniform1f(handlers[HandlersUtil.ALPHAMOD_ID], 1);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, rectangle.getTexture().getBitmap());

huangapple
  • 本文由 发表于 2020年9月22日 20:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定