c++ - glReadPixels is reading from the wrong location -
i trying read color of rectangle drawn on screen, when try read coord, appears reads offset offset not consistent. (i using sdl2 library in case helps) have found inverting coordinates e.g. if y 0 y_max.
i need know how can read pixel correct coord, not care method use faster 1 optimal.
i not understand of code being thrown in here, i'm pretty new images know cast project matrix here,
glenable(gl_texture_2d); gluint texture; glgentextures(1, &texture); glbindtexture(gl_texture_2d, texture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest); glteximage2d(gl_texture_2d, 0, image->format->bytesperpixel, image->w, image->h, 0, gl_rgb, gl_unsigned_byte, image->pixels); glortho(0.0f, 640.0f, 480.0f, 0.0f, -1.0f, 1.0f);
and here drawing rectangle.
glcolor3f(chrome[0], chrome[1], chrome[2]); gltranslatef(0, 0, 0.0f); glbindtexture(gl_texture_2d, texture); glbegin(gl_quads); gltexcoord2f(0.0f, 0.0f); glvertex3f(x*128.0f, 0, 0.0f); gltexcoord2f(1.0f, 0.0f); glvertex3f(128.0f*x+128, 0.0f, 0.0f); gltexcoord2f(1.0f, 1.0f); glvertex3f(128.0f*x+128, 128.0f, 0.0f); gltexcoord2f(0.0f, 1.0f); glvertex3f(x*128.0f, 128.0f, 0.0f); glend(); glpopmatrix(); unsigned char pixel[3]; glreadpixels(5, 5, 1, 1, gl_rgb, gl_unsigned_byte, pixel); std::cout << "r: " << (int)pixel[0] << std::endl; std::cout << "g: " << (int)pixel[1] << std::endl; std::cout << "b: " << (int)pixel[2] << std::endl; std::cout << "x: " << std::endl; std::cout << "y: " << std::endl; std::cout << std::endl;
i accidentally inverting y value, fixed inverting on plane. e.g (screenwidth-mouse_y)
Comments
Post a Comment