PDA

View Full Version : A problem with rendering to a texture



ximian
01-31-2005, 08:34 AM
I have created a texture with the dimensions 512x512x3 (GL_RGB) with the purpose to do motion blur. The ideea was to render the previous scene with a lower alpha onto the same texture + the actual scene. It actualy worked, but my implementation has limits : whem I am in full screen (under win32) the motion blur is not working, the alpha it's not used, a scene is drawed over another one witha alpha 1.0f. Can anybody tell me why?
Here is a bit of my code (the motion blur rendering to a txture routine) :



void RenderToMotionBlurTexture(bool FirstRenderTexture, int SceneFunc()) {

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();

glViewport(0, 0, motionBlur_Texture_X, motionBlur_Texture_Y);

if(FirstRenderTexture)
SceneFunc();

// render the old texture
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(motionBlur_Red_intensity,
motionBlur_Green_intensity,
motionBlur_Blue_intensity,
motionBlur_Alpha_intensity);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
// put the texture on the screen

glBindTexture(GL_TEXTURE_2D, motionBlur_Texture);

// go to orthogonal view
glLoadIdentity();
glOrtho( 0, motionBlur_Texture_X, motionBlur_Texture_Y, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);

glTexCoord2f(0.0, 1.0); glVertex2f(0, 0);
glTexCoord2f(0.0, 0.0); glVertex2f(0, motionBlur_Texture_Y);
glTexCoord2f(1.0, 0.0); glVertex2f(motionBlur_Texture_X, motionBlur_Texture_Y);
glTexCoord2f(1.0, 1.0); glVertex2f(motionBlur_Texture_X, 0);

glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

glPopMatrix();

if(!FirstRenderTexture)
SceneFunc();

glBindTexture(GL_TEXTURE_2D, motionBlur_Texture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, motionBlur_Texture_type, 0, 0,
motionBlur_Texture_X, motionBlur_Texture_Y, 0);

glViewport(0, 0, windowX, windowY);

glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();
}



void ShowMotionBlurTexture() {
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();

// put the texture on the screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, motionBlur_Texture);

// go to orthogonal view
glLoadIdentity();
glOrtho(0, windowX, windowY, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);

glTexCoord2f(0.0, 1.0); glVertex2f(0, 0);
glTexCoord2f(0.0, 0.0); glVertex2f(0, windowY);
glTexCoord2f(1.0, 0.0); glVertex2f(windowX, windowY);
glTexCoord2f(1.0, 1.0); glVertex2f(windowX, 0);

glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}Am I doing something wrong? Its from my video card? Where is the problem?

ximian
02-03-2005, 08:27 AM
Ok so i noticed that i have not mentioned exactly what is the problem. I render to a texture the same tex with a lower alpha and a object with another coords (rotated or moved). The purpose is to make motion blur efect. The problem is the old colors do not tend to reach 0 (maning black) they die at gray stade and the mouving object leaves a trace behind (a unwanted gray trace). Does anybody know how to eliminate this? I have posted the code in the prvious post.
pls help, i am confused :confused:

Humus
02-03-2005, 09:18 AM
If I understand your problem right the problem is likely that the buffer is only 8 bits. So when the numbers get small, and they are multiplied with a scale they will remain the same after rounding. This leave gray trails around. I've bumped into the same problem with for instance my LightTrail demo, but solved it by simply subtracting a small number so that rounding would always go down.