Question

i have rendered an image of size 480 * 800 into openGL Texture using below code, which rendered the image nicely in Samsung S3, which is of 720*1280 resolution in portrait mode.

//initialization
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &s_texture);
glBindTexture(GL_TEXTURE_2D, s_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glShadeModel(GL_FLAT);
check_gl_error("glShadeModel");

glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
check_gl_error("glColor4x");
glTexImage2D(GL_TEXTURE_2D, /* target*/
    0, /* level*/
    GL_RGBA, /* internal format*/
    480, /* width*/
    800, /* height*/
    0, /* border*/
    GL_RGBA, /* format*/
    GL_UNSIGNED_BYTE,/* type*/ //can it be GL_UNSIGNED_SHORT_5_6_5??
    0); /*source pixels*/ 
    check_gl_error("glTexImage2D");

//rendering loop
glClear(GL_COLOR_BUFFER_BIT);
glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_WRAP_S,
                       GL_REPEAT);
    check_gl_error("glTexParameterf:GL_TEXTURE_WRAP_S ");
    glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_WRAP_T,
                       GL_REPEAT);
    check_gl_error("glTexParameterf:GL_TEXTURE_WRAP_T");
int rect[4] = { 0, 800, 480, -800 };
    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
glTexSubImage2D(GL_TEXTURE_2D, 0,0, 0, 480, 800, GL_RGBA, GL_UNSIGNED_BYTE, s_pixels);
check_gl_error("glTexSubImage2D");


glDrawTexiOES(0, 0, 0, screen_width, screen_height); //draw into full available window

Now i changed source image to size 720 * 1280 , Also the rendering target which is of 720*1360 resolution and is a tablet.

Now changing the above code like below doesn't seem to be working as it worked on a portrait mode device(above case). The image is not fully displayed and the partial image is repeated many times.

//initialization
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &s_texture);
glBindTexture(GL_TEXTURE_2D, s_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glShadeModel(GL_FLAT);
check_gl_error("glShadeModel");

glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
check_gl_error("glColor4x");
glTexImage2D(GL_TEXTURE_2D, /* target*/
    0, /* level*/
    GL_RGBA, /* internal format*/
    720, /* width*/
    1280, /* height*/
    0, /* border*/
    GL_RGBA, /* format*/
    GL_UNSIGNED_BYTE,/* type*/ //can it be GL_UNSIGNED_SHORT_5_6_5??
    /*s_pixels*/0); /*source pixels*/ 
    check_gl_error("glTexImage2D");

//rendering loop
glClear(GL_COLOR_BUFFER_BIT);
glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_WRAP_S,
                       GL_REPEAT);
    check_gl_error("glTexParameterf:GL_TEXTURE_WRAP_S ");
    glTexParameterf(GL_TEXTURE_2D,
                       GL_TEXTURE_WRAP_T,
                       GL_REPEAT);
    check_gl_error("glTexParameterf:GL_TEXTURE_WRAP_T");
int rect[4] = { 0, 720, 1280, -720};
    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
glTexSubImage2D(GL_TEXTURE_2D, 0,0, 0, 720, 1280, GL_RGBA, GL_UNSIGNED_BYTE, s_pixels);
check_gl_error("glTexSubImage2D");


glDrawTexiOES(0, 0, 0, screen_width, screen_height); //draw into full available window

1 .Can someone explain me where i do mistake ?

2 .Can someone explain me how to deal with rendering various resolution images in various dimension displays, cos all examples i saw showed that using glDrawTexiOES(0, 0, 0, screen_width, screen_height); will do it correctly, but it's not the case.

3 .If i remove int rect[4] = { 0, 720, 1280, -720}; glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect); i dont' see anything on screen, that's why i am using it, but i really know not the purpose of it and how to use it, can anyone tell me it's purpose and proper usage?

Was it helpful?

Solution

Your code looks like right, but I am not a openGL expert. Maybe that screen_width and screen_height are not right. Anyway some tips:

  • It is not a good idea to create not pow to 2 textures. In some devices it will be ignored. If you want to work with a 780x1280 image, you will need to create a 1024x2048 texture. In glTexImage2D you will set "1024,2048" and in rect and glTexSubImage you will use the real image size (780x1280).
  • glDrawTexiOES (and glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,) are right.
  • Change GL_REPEAT to GL_CLAMP_TO_EDGE, so you will avoid that the image will get repeated.

Remember in glTexImage2D you define the texture, but the image can a part of it. In glTexSubImage and GL_TEXTURE_CROP_RECT_OES you define the part of the texture to draw, and in glDrawTexiOES you define the rect of the screen which you want to draw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top