激活/使用GL_TEXTURE1在OpenGL ES 2.0的Andr​​oid版 [英] Activating/using GL_TEXTURE1 at OpenGL ES 2.0 for Android

查看:662
本文介绍了激活/使用GL_TEXTURE1在OpenGL ES 2.0的Andr​​oid版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用GL_TEXTURE1纹理单元绘制一个简单的形状。我知道如何使用标准GL_TEXTURE0画它,但是当它改变的东西是行不通的。

我想,从我下面的code,我不得不改变如下:

  glActiveTexture(GL_TEXTURE1);
glUniform1i(uTextureLocation,1);
 

什么我失踪?

code:

 公共类RendererClass实现渲染器{

上下文语境;

FloatBuffer verticesInBuffer;

INT aPositionLocation;
INT aTextureLocation;
INT uTextureLocation;

INT方案;

公共RendererClass(上下文的背景下){
    this.context =背景;
}

@覆盖
公共无效onSurfaceCreated(GL10为arg0,EGLConfig配置){

    GLES20.glClearColor(1.0F,0.0,0.0,0.0);

    浮动[]顶点= {

            -0.5f,0.5F,0.5F,0.5F,
            -1.0F,0.0,0.0,0.0,
             0.0,0.0,1.0F,0.0,
             0.0,1.0F,1.0F,1.0F,
            -1.0F,1.0F,0.0,1.0F,
            -1.0F,0.0,0.0,0.0

    };

    verticesInBuffer = ByteBuffer.allocateDirect(vertices.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertices);

    字符串VSS =属性vec4 a_Position; +
                 属性VEC2 a_Texture; +
                 不同VEC2 v_Texture; +
                 无效的主要(){+
                 v_Texture = a_Texture; +
                 GL_POSITION = a_Position; +
                 };

    字符串FSS =precision mediump浮动; +
                 不同VEC2 v_Texture; +
                 统一sampler2D u_Texture; +
                 无效的主要(){+
                 gl_FragColor =的Texture2D(u_Texture,v_Texture); +
                 };

    INT VS = glCreateShader(GL_VERTEX_SHADER);
    INT FS = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(VS,VSS);
    glShaderSource(FS,FSS);

    glCompileShader(VS);
    glCompileShader(FS);

    程序= glCreateProgram();

    glAttachShader(程序,VS);
    glAttachShader(程序,FS);

    glLinkProgram(节目);

    aPositionLocation = glGetAttribLocation(节目a_Position);


    // *****质地的东西开始此处< /< /< /< /


    // FASE 1
    glActiveTexture(GL_TEXTURE0);

    INT [] genTextures =新INT [1];
    glGenTextures(1,genTextures,0);

    glBindTexture(GL_TEXTURE_2D,genTextures [0]);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);


    // FASE 2
    BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inScaled = FALSE;

    位图bitmap1 = BitmapFactory.de codeResource(context.getResources(),R.drawable.res_for_test_1,期权);


    // FASE 3
    texImage2D(GL_TEXTURE_2D,0,bitmap1,0);
    glGenerateMipmap(GL_TEXTURE_2D);


    // FASE 4
    aTextureLocation = glGetAttribLocation(节目a_Texture);
    uTextureLocation = glGetUniformLocation(节目u_Texture);

    glUniform1i(uTextureLocation,0);

    verticesInBuffer.position(2);
    glEnableVertexAttribArray(aTextureLocation);
    glVertexAttribPointer(aTextureLocation,2,GL_FLOAT,假的,16,verticesInBuffer);


 // *****质地的东西两端此处< /< /< /< /


}

@覆盖
公共无效onSurfaceChanged(GL10为arg0,诠释的宽度,高度INT){

    GLES20.glViewport(0,0,宽度,高度);

}

@覆盖
公共无效onDrawFrame(GL10 glUnused){

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    glUseProgram(节目);

    verticesInBuffer.position(0);
    glEnableVertexAttribArray(aPositionLocation);
    glVertexAttribPointer(aPositionLocation,2,GL_FLOAT,假的,16,verticesInBuffer);

    (4,8,GL_TRIANGLE_FAN,0,6);

}

}
 

解决方案

您需要指定活动纹理单元和分配previously载入纹理。

为了方便起见,我创建了一个辅助功能,做了这一切。它激活特定纹理单元,与给定的ID分配纹理,并把着色器的这个值 sampler2D 统一的:

 保护无效setTexture2D(INT textureUnit,诠释textureID,诠释uniformID){
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + textureUnit);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textureID);
    GLES20.glUniform1i(uniformID,textureUnit);
}
 

然后调用它是这样的:

  setTexture2D(0,textureID,uniformID);
 

I'm trying to use the GL_TEXTURE1 texture unit to draw a simple shape. I know how to draw it using the standard GL_TEXTURE0, but when changing it something is not working.

I thought that from my code below, I just had to change the following:

glActiveTexture(GL_TEXTURE1);
glUniform1i(uTextureLocation, 1);

What I'm missing?

Code:

public class RendererClass implements Renderer {

Context context;

FloatBuffer verticesInBuffer;

int aPositionLocation;
int aTextureLocation;
int uTextureLocation;

int program;

public RendererClass(Context context){
    this.context = context;
}

@Override
public void onSurfaceCreated(GL10 arg0, EGLConfig config) {

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

    float[] vertices = {

            -0.5f, 0.5f, 0.5f, 0.5f,
            -1.0f, 0.0f, 0.0f, 0.0f,
             0.0f, 0.0f, 1.0f, 0.0f,
             0.0f, 1.0f, 1.0f, 1.0f,
            -1.0f, 1.0f, 0.0f, 1.0f,
            -1.0f, 0.0f, 0.0f, 0.0f  

    };

    verticesInBuffer = ByteBuffer.allocateDirect(vertices.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertices);

    String vss = "attribute vec4 a_Position;" +
                 "attribute vec2 a_Texture;" +
                 "varying vec2 v_Texture;" +
                 "void main(){" +
                 "    v_Texture = a_Texture;" +
                 "    gl_Position = a_Position;" +
                 "}";

    String fss = "precision mediump float;" +
                 "varying vec2 v_Texture;" +
                 "uniform sampler2D u_Texture;" +
                 "void main(){" +
                 "    gl_FragColor = texture2D(u_Texture, v_Texture);" +
                 "}";

    int vs = glCreateShader(GL_VERTEX_SHADER);
    int fs = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vs, vss);
    glShaderSource(fs, fss);

    glCompileShader(vs);
    glCompileShader(fs);

    program = glCreateProgram();

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);

    aPositionLocation = glGetAttribLocation(program, "a_Position");


    // ***** Texture stuff starts here   </</</</


    // Fase 1
    glActiveTexture(GL_TEXTURE0);

    int[] genTextures = new int[1];
    glGenTextures(1, genTextures, 0);

    glBindTexture(GL_TEXTURE_2D, genTextures[0]);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


    // Fase 2
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;

    Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.res_for_test_1, options);


    // Fase 3
    texImage2D(GL_TEXTURE_2D, 0, bitmap1, 0);
    glGenerateMipmap(GL_TEXTURE_2D);    


    // Fase 4
    aTextureLocation = glGetAttribLocation(program, "a_Texture");
    uTextureLocation = glGetUniformLocation(program, "u_Texture");

    glUniform1i(uTextureLocation, 0);

    verticesInBuffer.position(2);
    glEnableVertexAttribArray(aTextureLocation);
    glVertexAttribPointer(aTextureLocation, 2, GL_FLOAT, false, 16, verticesInBuffer);


 // ***** Texture stuff ends here   </</</</


}

@Override
public void onSurfaceChanged(GL10 arg0, int width, int height) {

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

}

@Override
public void onDrawFrame(GL10 glUnused) {

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    glUseProgram(program);

    verticesInBuffer.position(0);
    glEnableVertexAttribArray(aPositionLocation);
    glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, false, 16, verticesInBuffer);

    glDrawArrays(GL_TRIANGLE_FAN, 0, 6);

}

}

解决方案

You need to specify active texture unit and assign a previously loaded texture to it.

For convenience, I've created a helper function which does all of this. It activates given texture unit, assigns texture with given ID to it and puts this value to sampler2D uniform of shader:

protected void setTexture2D(int textureUnit, int textureID, int uniformID) {
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + textureUnit);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
    GLES20.glUniform1i(uniformID, textureUnit);
}

And then call it like this:

setTexture2D(0, textureID, uniformID);

这篇关于激活/使用GL_TEXTURE1在OpenGL ES 2.0的Andr​​oid版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆