tex2Dlod related question

Started by
5 comments, last by Yann L 16 years ago
I have some elaborate code which gives a pretty result at the firest frame but then it disappears. Now, I isolated the problem and made a very small program to illustrate this is my fragment shader

void rgbLightingFragmentProgram(
	float4 position	: TEXCOORD0,
	float4 iPosition	: TEXCOORD1,
	float3 normal	: TEXCOORD2,
	float3 view		: TEXCOORD3,
	float3 refl		: TEXCOORD4,

	out float4 color	: COLOR,
	
	uniform sampler2D envmap)
{
	
	float3 R = normalize(refl);
	
	color = tex2Dlod(envmap,float4(R.x,R.y,0,9));
	
}





If I run the program it just draws a teapot rotated under angle 0, I use 2 keys to rotate in one of each directions hence drawing a new frame. Now the first frame looks a syou'd expect but whatever I do rotate, move another window over it, random click, anyway whatever makes new frames being generated, the teapot disappears. The image is being uploaded and the shader initialised in openGL like follows loading image:

static GLuint mipmapTexture;
struct floatStruct{
	float r;
	float g;
	float b;
	float a;
};
void initEnvData(){

	char * infix[] = {"1_536_s","0_768_s","0_384_s","0_192_s","0_096_s","0_048_s","0_024_s","0_012_s","0_006_s","0_003_s","1p_s"};
	GLenum target = GL_TEXTURE_2D;
	char str[40];
	int dimx = 0;
	int dimy = 0;
	glGenTextures(1, &mipmapTexture);
	for (int i = 0; i<11;i++){
		strcpy(str,infix);
		strcat(str,".exr");
		Imf::Rgba * pixelBuffer;
		floatStruct * floatBuffer;
		try{

			Imf::RgbaInputFile in(str);
			Imath::Box2i win = in.dataWindow();

			Imath::V2i dim(win.max.x - win.min.x + 1, win.max.y - win.min.y + 1);
			dimx = dim.x;
			dimy = dim.y;
			pixelBuffer = new Imf::Rgba[dim.x * dim.y];
			floatBuffer = new floatStruct[dim.x * dim.y];
			
			int dx = win.min.x;
			int dy = win.min.y;

			in.setFrameBuffer(pixelBuffer - dx - dy * dim.x, 1, dim.x);
			in.readPixels(win.min.y, win.max.y);
		}catch(Iex::BaseExc & e){
			std::cerr << e.what() << std::endl;
		}

		cout<<"Image loaded to buffer: " << str <<endl;

		for(int q = 0; q < dimx*dimy; q++){
			floatBuffer[q].r = pixelBuffer[q].r;
			floatBuffer[q].g = pixelBuffer[q].g;
			floatBuffer[q].b = pixelBuffer[q].b;
			floatBuffer[q].a = pixelBuffer[q].a;
		}

		cout<<"Halfs converted to floats: " << str <<endl;

		cout <<floatBuffer[0].r<<" "<<floatBuffer[0].g<<" "<<floatBuffer[0].b<<" "<<floatBuffer[0].a<<" "<<endl;

		glEnable(target);
		glBindTexture(target, mipmapTexture);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		
		glTexImage2D(target, i, GL_RGBA32F_ARB, dimx, dimy, 0,GL_RGBA, GL_FLOAT, floatBuffer);

		float* data = (float*)malloc(dimx*dimy*4*sizeof(float));
		glGetTexImage(target, i, GL_RGBA, GL_FLOAT, data);
		cout <<data[0]<<" "<<data[1]<<" "<<data[2]<<" "<<data[3]<<" "<<endl;
		free(data);


		cout << "level "<< i <<": "<<dimx <<" * " <<dimy <<endl; 
	}
	cgGLSetTextureParameter(cg_envmap, mipmapTexture);
	cout << mipmapTexture <<endl;
	checkCgErrors();
	cgGLEnableTextureParameter(cg_envmap);
	cout << "envmap loaded" << endl;

}





initializing shader

void initPhongRGBCgPrograms(){
	// Create the vertex program
	vp = cgCreateProgramFromFile(context, CG_SOURCE, "rgbLightingVertexProgram.cg", vertexProfile, "rgbLightingVertexProgram", NULL);
	if(vp != NULL){
		cgGLLoadProgram(vp);

		// Grab the input parameters
		cg_position = cgGetNamedParameter(vp, "position");
		cg_normal = cgGetNamedParameter(vp, "normal");
		cg_eyePosition = cgGetNamedParameter(vp, "eyePosition");

		cgGLSetParameter3f(cg_eyePosition,d*sqrt3,d*sqrt3,d*sqrt3);
		checkCgErrors();
		std::cout<<"Vertex program loaded.\n";
	}
	else{
		std::cout<<"The vertex program could not be loaded.\n";
		exit(0);
	}
	
	// Create the fragment program
	fp = cgCreateProgramFromFile(context, CG_SOURCE, "rgbLightingFragmentProgram.cg", fragmentProfile, "rgbLightingFragmentProgram", NULL);
	if(fp != NULL){
		cgGLLoadProgram(fp);

		// Grab the input parameters

		cg_envmap = cgGetNamedParameter(fp, "envmap");
		checkCgErrors();

		std::cout<<"Fragment program loaded.\n";
	}
	else{
		std::cout<<"The fragment program could not be loaded.\n";
		exit(0);
	}
}





Anyone an idea why happens what apparently happens ? Illustration: frame1 frame2 frame3
Advertisement
and if I do

color = tex2D(envmap,float2(R.x,R.y));

well, I basically get the same disappearing teapot problem >_>

I am using stuff like FBO's and all that but when I use other shaders there's no problem so the problem doesn't sit there.
bah i've been fiddling with it a whole day now and no progress >_<
Just a shot in the dark... since you mention "I use FBO" and since the second and third images have super high contrast (practically black and white)... you don't do HDR and tone mapping by any chance?
If you do, have you checked that you don't accidentially change an uniform which affects your tone mapping later? Feeding some very extreme values as input to tone mapping might produce something that looks like this.
I do HDR and tonemapping, but I disabled the tonemapping shaders while I'm testing this so that's not the problem.
other suggestions
There's absolutely no way to give any kind of meaningful suggestions from the information you provided. It could be virtually anything.

You have probably not correctly set/reset some state somewhere. A matrix, a uniform, a binding, anything. Take your code apart step by step, simplify it until the problem goes away. Then work backwards. Regularily check the GL error state.

This topic is closed to new replies.

Advertisement