How to sample texture to frame buffer?

Hi,

I have a texture in texture memory. I want to pass the texture into the fragment shader and read the texture value in the fragment. Then, sample to the frame buffer and set the color of the pixel.

I have done the code but not sure whether it correct or not.


static const char *fragment_source = {

	// texture global variable, set by application
	"uniform sampler2DRect texture;"

	"void main()"
	"{"
		//Read texture coordinates
		"vec2 texCoord = gl_TexCoord[0].xy;"
    		//Read value of texture of current pixel
		"vec4 original_value = texture2DRect(texture,texCoord);"
		//Perform pattern matching
		"if(original_value[0]=='t'){"
			//Sampling The Texture And Passing It To The Frame Buffer
			"gl_FragColor = texture2DRect(texture,texCoord);"
			//Set the particular pixel to Red color
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]=='r'){"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]=='o'){"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]=='j'){"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]=='a'){"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]=='n'){"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else{"
			"gl_FragColor = texture2DRect(texture,texCoord);"
			"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);}"	
	"}"

};

My texture data is in bytes. And each coordinates only have one value. I use GL_LUMINANCE format for the texture. I hope that I can read the red/white color out only from the frame buffer if it success.

According to rgba format,

gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); → red
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); → black

Is it correct? Please correct me if I m wrong. Thank for every one helps.

I want to pass the texture into the fragment shader and read the texture value in the fragment. Then, sample to the frame buffer and set the color of the pixel.

This made sense until you said, “sample to the frame buffer”. I don’t know what you mean by that.

  	"gl_FragColor = texture2DRect(texture,texCoord);"
  	"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"

I’m confused. You write a value to a variable. And then, you immediately overwrite the value of that variable with another value. What do you think these two lines are going to do?

Furthermore, you already got this value from the texture. Back in “original_value”. You haven’t changed this value, so there’s no need for you to fetch the same value from the texture again.

original_value[0]==‘t’

GLSL doesn’t have the concept of string literals or character literals (for obvious reasons). So I highly doubt it will compile.

Also, GLSL has the switch statement (assuming you’re using a recent version of GLSL).

Is it correct?

No. I’m not even sure how you came to this conclusion (as any kind of internet searching on colorspaces would have told you otherwise). If turning off all color channels were white, it wouldn’t make sense that turning on just the red channel would be red. White is what you get when you have all colors combined. Red is defined by the absence of colors other than red.

If you have neither red, green, nor blue, you have nothing. IE: black, the absence of color.

Hem…try to digest your words. I found that the linking error I mentioned in other thread is actually the compilation error.

I had modified my code in this way. And no more compilation and link error.


static const char *fragment_source = {

	// texture global variable, set by application
	"uniform sampler2DRect texture;"

	"void main()"
	"{"
		//Read texture coordinates
		"vec2 texCoord = gl_TexCoord[0].xy;"
    		//Read value of texture of current pixel
		"vec4 original_value = texture2DRect(texture,texCoord);"
		//Perform pattern matching
		"if(original_value[0]==116){"
			//Set the particular pixel to Red color
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]==114){"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]==111){"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]==106){"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]==97){"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else if(original_value[0]==110){"
			"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
		"else{"
			//Set the particular pixel to Black color
			"gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);}"	
	"}"

I change the character to decimal in ASCII. Actually I not care whether it will output the red color o not. I do not render it to display. I just will read the read component out from the frame buffer only. So, gl_FragColor will directly output the color to the frame buffer right?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.