android draw华为手机总是黑屏,android - 当我尝试加载纹理时,Android上的gles20会显示黑屏 - 堆栈内存溢出...

我正在尝试将纹理(480x720px)加载为我的应用程序中的背景,但显示为黑色。 我对GLES20有点陌生,所以我在网上遵循了一些示例来构建我的代码,但这并没有给我带来好运。 如何解决?

这是我的着色器代码:

public class Shaders {

private int mProgram;

private int maPositionHandle;

private int texCoords;

FloatBuffer texBuffer;

int iBaseMap;

private final String vertexShaderCode =

"attribute vec4 a_position;" +

"attribute vec2 a_texCoords;" +

"varying vec2 v_texCoords;" +

"void main()" +

"{" +

"gl_Position = a_position;" +

"v_texCoords = a_texCoords;" +

"}";

private final String fragmentShaderCode =

"precision mediump float;" +

"varying vec2 v_texCoords;" +

"uniform sampler2D u_baseMap;" +

"void main()" +

"{" +

"gl_FragColor = texture2D(u_baseMap, v_texCoords);" +

"}";

private int loadShader(int type, String shaderCode){

// create a vertex shader type (GLES20.GL_VERTEX_SHADER)

// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)

int shader = GLES20.glCreateShader(type);

// add the source code to the shader and compile it

GLES20.glShaderSource(shader, shaderCode);

GLES20.glCompileShader(shader);

return shader;

}

public void initShaders() {

GLES20.glEnable(GLES20.GL_TEXTURE_2D);

GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);

int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program

GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program

GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program

GLES20.glLinkProgram(mProgram); // creates OpenGL program executables

// get handle to the vertex shader's vPosition member

maPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_position");

iBaseMap = GLES20.glGetUniformLocation(mProgram, "u_baseMap");

texCoords = GLES20.glGetAttribLocation(mProgram, "a_texCoords");

GLES20.glDeleteShader(vertexShader);

GLES20.glDeleteShader(fragmentShader);

GLES20.glViewport(0, 0, Settings.width, Settings.height);

}

public int getMPH() {

return maPositionHandle;

}

public int getMProg() {

return mProgram;

}

public int getIBaseMap() {

return iBaseMap;

}

public int getTexCoords() {

return texCoords;

}

public int loadTexture(GLSurfaceView view, int imgResID){

Bitmap img = null;

int textures[] = new int[1];

img = BitmapFactory.decodeResource(view.getResources(), imgResID);

System.out.println(imgResID);

GLES20.glGenTextures(1, textures, 0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);

GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);

img.recycle();

return textures[0];

}

}

还有我画背景的班级:

public class Background {

Shaders shaders;

int texId;

private GLSurfaceView mGLView;

private float BackgroundCoords[] = {

// X, Y, Z

-1f, -1f, 0, 0,0,

-1f, 1f, 0, 0, 1,

1f, 1f, 0, 1, 0,

1f, -1f, 0, 1, 1

};

private FloatBuffer backBuffer;

public void setAct(Shaders s, GLSurfaceView v) {

shaders = s;

mGLView = v;

texId = shaders.loadTexture(mGLView, R.raw.ground_tex);

System.out.println(texId);

}

public Background() {

// a float has 4 bytes so we allocate for each coordinate 4 bytes

ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(BackgroundCoords.length * 4);

vertexByteBuffer.order(ByteOrder.nativeOrder());

// allocates the memory from the byte buffer

backBuffer = vertexByteBuffer.asFloatBuffer();

// fill the vertexBuffer with the vertices

backBuffer.put(BackgroundCoords);

// set the cursor position to the beginning of the buffer

backBuffer.position(0);

}

/** The draw method for the square with the GL context */

public void draw() {

// Add program to OpenGL environment

GLES20.glUseProgram(shaders.getMProg());

// Point to our vertex buffer

backBuffer.position(0);

GLES20.glVertexAttribPointer(shaders.getMPH(), 3, GLES20.GL_FLOAT, false, 5 * 4, backBuffer);

GLES20.glEnableVertexAttribArray(shaders.getMPH());

backBuffer.position(3);

GLES20.glVertexAttribPointer(shaders.getTexCoords(), 2, GLES20.GL_FLOAT, false, 5 * 4, backBuffer);

GLES20.glEnableVertexAttribArray(shaders.getTexCoords());

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);

GLES20.glUniform1i(shaders.getIBaseMap(), 0);

// Draw the vertices as triangle strip

GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 4);

}

}

如果我只尝试使用简单的着色器,则此代码有效,尽管填充背景的两个三角形中只有一个是彩色的。

发生错误“ E / libEGL(8971):在没有当前上下文的情况下调用OpenGL ES API(每个线程记录一次)”源自每次调用GLES时的loadTexture函数。

我真的希望有人能帮助我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值