1

I am developing some Image-Process program on Android this days . And I want to put some byte and float data into a texture . I did this .

GLfloat test_float[8] = {
            1.0f,2.0f,
            3.0f,4.0f,
            5.0f,6.0f,
            7.0f,8.0f
};
GLbyte test_byte[8] = {
            //1,2,0,0,3,4,0,0,5,6,0,0,7,8,0,0,
            1,2,3,4,5,6,7,8
};
glGenTextures(1, &floatTexture);
glBindTexture(GL_TEXTURE_2D, floatTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, 1, 4, 0, GL_RG,
            GL_UNSIGNED_BYTE, test_byte);

glTexParameterf(GL_TEXTURE_2D,
        GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
        GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUseProgram(program);
GLuint location = glGetUniformLocation(program,"uInputTex");
glUniform1i(location,0);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
GLubyte* datas = new GLubyte[32];
memset(datas,0,32*sizeof(GLubyte));
glReadPixels(0, 0, 1, 4, GL_RGBA, GL_UNSIGNED_BYTE, datas);
for(int i=0;i<32;) {
    __android_log_print(ANDROID_LOG_INFO,"TEST","R %d G %d B %d A %d",datas[i++],datas[i++],datas[i++],datas[i++]);
}

My shader do nothing. Fragment shader code is :

varying vec2 v_texCoord;
uniform sampler2D uInputTex;
void main (){
    gl_FragColor = vec4(texture2D(uInputTex, v_texCoord).rgba);
    //gl_FragColor = vec4(texture2D(uInputTex, v_texCoord).rgba)/255.0;  // if texture data is float this will be used .
});

I run program with the byte data created texture (test_byte)

I got :

 R 1 G 2 B 0 A 255
 R 5 G 6 B 0 A 255
 R 0 G 0 B 0 A 255
 R 0 G 0 B 0 A 255

And I run program with float data created texture (test_float). The texture created by glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, 1, 4, 0, GL_RG,GL_FLOAT, test_float);

I got :

 R 1 G 2 B 0 A 0
 R 3 G 4 B 0 A 0
 R 5 G 6 B 0 A 0
 R 7 G 8 B 0 A 0

why am I missed 3,4 and 7,8 when I use byte-data texture?

Any help will be thanks .

zh18
  • 154
  • 2
  • 13

1 Answers1

0

As @RetoKoradi said,

You need to set GL_UNPACK_ALIGNMENT to 1 if your row size is not a multiple of 4 bytes, which is the default for this value.

Because the 2 floats you have in each row use 2*4=8 bytes, which is a multiple of the default alignment (4 bytes).

Community
  • 1
  • 1
zh18
  • 154
  • 2
  • 13