Sunday, 25 August 2013

Is there a need to set the parameters again when updating a texture?

Is there a need to set the parameters again when updating a texture?

Suppose that I'm developing the class COpenGLControl here in code guru to
use it for showing a 2D texture in an MFC CPictureControl. and after
applying some filters on the image, I will update the texture to show the
filtered image on the picture control.
I have set a rule for the clients of my class as follows:
Call function setImageWidthHeightType just first time of calling OnTimer
and after setting pImage(the rastetr data that will be provided by another
classes that have been developed using OpenCV and GDAL libraries)for the
next calls of OnTimer and when updating pImage there's no need to call
this function. If you do so, your client code slows down because the
function InitializeTextureObject() is called in the
setImageWidthHeightType function
For second and more calls of OnTimer you should call updataTextureObject()
function after setting pImage. In the first call of OnTimer and before
calling setImageWidthHeightType, the function updataTextureObject() should
not be called and if so, the program will encounter an unhandled exception
because there you still don't have any texture object to be updated.
any way I have used the following codes for the functions that are
mentioned above:
OpenGLControl.h
allocate texture name which will be an unsigned integer
GLuint texture;
OpenGLControl.cpp
Generate just one texture and bind it to the target:
void COpenGLControl::InitializeTextureObject()
{
wglMakeCurrent(hdc, hrc);
glEnable(target);
glGenTextures(1,&texture);
glBindTexture(target,texture);
glTexParameteri(target,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(target,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(target,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(target,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(target,level,internalformat,ImageWidth,ImageHeight,border,format,type,&pImage[0]);
wglMakeCurrent(NULL, NULL);
}
the question that roses up is that Is there a need to set the parameters
again when updating texture because of using wglMakeCurrent(NULL,NULL) at
the end of the function above?
In other words should I write the function updataTextureObject() as this:
void COpenGLControl::updataTextureObject()
{
wglMakeCurrent(hdc, hrc);
glEnable(target);
glBindTexture(target,texture);
glTexParameteri(target,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(target,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(target,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(target,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexSubImage2D(target,level,xOffset,yOffset,ImageWidth,ImageHeight,format,type,&pImage[0]);
wglMakeCurrent(NULL, NULL);
}
Or just as this:
void COpenGLControl::updataTextureObject()
{
wglMakeCurrent(hdc, hrc);
glTexSubImage2D(target,level,xOffset,yOffset,ImageWidth,ImageHeight,format,type,&pImage[0]);
wglMakeCurrent(NULL, NULL);
}
What way is the correct one?

No comments:

Post a Comment