colorbuffer to texture in mipmap

I want to copy the color buffer to a texture and this texture is the mipmap of color buffer,

my code as follows:

glGenTextures(1, fptexture);
glBindTexture(GL_TEXTURE_RECTANGLE_NV, fptexture);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV,MLEVEL , GL_FLOAT_RGBA_NV,texWIDTH, texHEIGHT,
0, GL_RGBA, GL_FLOAT, NULL);


glBindTexture(GL_TEXTURE_RECTANGLE_NV, fptexture)
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, MLEVEL, 0, 0, 0, 0, texWIDTH, texHEIGHT);

but it doesn’t work. if MLEVEL is 0(that is just copy), it is ok.

what is the problem? Thanks

texture rectangles do not support mipmapping. All operations performed on a texture rectangle with a mipmap level other than 0 will generate an error.

Try using the TEXTURE_2D target.

Nico

Thanks. But if I use texture_2D, I think I could hardly keep the 32 float precise, Is there any alternate?

I use mipmap just want to add the RGBA value of each fragment in color buffer together, and get the sum of R G B A values respectively. But I do not want to transform all the color back to CPU, and add in CPU. Because the cost of tranform back to CPU may very high.

Or I should write a fragment program by myself? But I do not know how to write such a program
any one could help ? Thanks!

First of all, on recent hardware it is possible to use 32fp precision on the texture_2D target with the ati_texture_float extension.

Second, I believe you do not fully understand the concept of mipmaps since you’re trying to fill a mipmap level other then zero with the original resolution of the texture at level 0.

A mipmap at level M has a resolution of (originalwidth>>M)x(originalwidth>>M).

I believe you were looking for the generate_mipmap_sgis extension which automatically generates the mipmaps at levels M and up when performing a copytex(sub)image at level M. It should be noted though that automatic mipmap generation is not hardware accelerated for fp textures (not on nvidia as far as i know anyway).

So it looks like the best way to do what you want to do, is to create a 32fp pbuffer with the original resolution of your texture and insert the following piece of code.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBindTexture(GL_TEXTURE_RECTANGLE_NV, fptexture);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		    
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	
for (int M=1;M<=nrmipmaplevels;++M)
{
    glViewport(0,0,texwidth>>M,texheight>>M);

    glBegin(GL_QUADS);
    glTexCoord2i(0,0);
    glVertex2f(-1.0,-1.0);
    glTexCoord2i(texwidth>>(M-1),0);
    glVertex2f(1.0,-1.0);
    glTexCoord2i(texwidth>>(M-1),texheight>>(M-1));
    glVertex2f(1.0,1.0);
    glTexCoord2i(0,texheight>>(M-1));
    glVertex2f(-1.0,1.0);
    glEnd();

    glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV,0,0,0,0,0,texwidth>>M,texheight>>M);
}

By doing this, all pixels at level M contain the sum of 4 other pixels at level M-1 divided by 4.
You can easily recompensate after readout.

Nico

Thanks, Nico.

But is there any way of mipmap the data in framebuffer directly to a texture?
Or
I must copy the data in framebuffer to a texture at first, then change viewport and render again to get the mipmap texture.