Unexpected behaviour with glFramebufferTexture1D

I am using render to texture concept with glFramebufferTexture1D. I am drawing a cube on non-default FBO with all the vertices as -1,1 (maximum) in X Y Z direction. Now i am setting viewport to X while rendering on non default FBO. My background is red with white color of cube.

For default FBO, i have created 1-D texture and attached this texture to above FBO with color attachment. I am setting width of texture equal to width*height of above FBO view-port. Now, when i render this texture to on another cube, i can see continuous white color on start or end of each face of the cube. That means part of the face is white and rest is red. I am not sure whether this behavior is correct or not. I expect all the texels should be white as i am using -1 and 1 coordinates for cube rendered on non-default FBO.

what is expected behavior in this case ?

First of all, what is a default FBO?

Please post the code. It’s much easier this way. Don’t forget to use ‘[‘code’]’ code here ‘[’/code’]’ tags.

default FBO is glBindFramebuffer(GL_FRAMEBUFFER, 0)

Please post the code. It’s much easier this way. Don’t forget to use ‘[‘code’]’ code here ‘[’/code’]’ tags.


#define WIDTH 8
#define HEIGHT 8

GLfloat vertices8[]={
		1.0f,1.0f,1.0f,
		-1.0f,1.0f,1.0f,
		-1.0f,-1.0f,1.0f,
		1.0f,-1.0f,1.0f,//face 1
				
		1.0f,-1.0f,-1.0f,
		-1.0f,-1.0f,-1.0f,
		-1.0f,1.0f,-1.0f,
		1.0f,1.0f,-1.0f,//face 2
				
		1.0f,1.0f,1.0f,
		1.0f,-1.0f,1.0f,
		1.0f,-1.0f,-1.0f,
		1.0f,1.0f,-1.0f,//face 3				

		-1.0f,1.0f,1.0f,
		-1.0f,1.0f,-1.0f,
		-1.0f,-1.0f,-1.0f,
		-1.0f,-1.0f,1.0f,//face 4
						
		1.0f,1.0f,1.0f,
		1.0f,1.0f,-1.0f,
		-1.0f,1.0f,-1.0f,
		-1.0f,1.0f,1.0f,//face 5

		-1.0f,-1.0f,1.0f,
		-1.0f,-1.0f,-1.0f,
		1.0f,-1.0f,-1.0f,
		1.0f,-1.0f,1.0f//face 6		


};
GLfloat vertices[]=
	{
		0.5f,0.5f,0.5f,
		-0.5f,0.5f,0.5f,
		-0.5f,-0.5f,0.5f,
		0.5f,-0.5f,0.5f,//face 1
				
		0.5f,-0.5f,-0.5f,
		-0.5f,-0.5f,-0.5f,
		-0.5f,0.5f,-0.5f,
		0.5f,0.5f,-0.5f,//face 2
				
		0.5f,0.5f,0.5f,
		0.5f,-0.5f,0.5f,
		0.5f,-0.5f,-0.5f,
		0.5f,0.5f,-0.5f,//face 3				

		-0.5f,0.5f,0.5f,
		-0.5f,0.5f,-0.5f,
		-0.5f,-0.5f,-0.5f,
		-0.5f,-0.5f,0.5f,//face 4
						
		0.5f,0.5f,0.5f,
		0.5f,0.5f,-0.5f,
		-0.5f,0.5f,-0.5f,
		-0.5f,0.5f,0.5f,//face 5

		-0.5f,-0.5f,0.5f,
		-0.5f,-0.5f,-0.5f,
		0.5f,-0.5f,-0.5f,
		0.5f,-0.5f,0.5f//face 6		

	};

	GLuint indices[] =
      {
         0, 2, 1,
         0, 3, 2, 
         4, 5, 6,
         4, 6, 7,
         8, 9, 10,
         8, 10, 11, 
         12, 15, 14,
         12, 14, 13, 
         16, 17, 18,
         16, 18, 19, 
         20, 23, 22,
         20, 22, 21
      };



GLfloat texcoord[] = 
{
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,

		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,

		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,

		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,

		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,

		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0
};

glGenTextures(1, &id1);
glBindTexture(GL_TEXTURE_1D, id1);

glGenFramebuffers(1, &Fboid);
for(int i=0;i<6;i++)
{
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,  WIDTH*HEIGHT , 0, GL_RGBA, GL_UNSIGNED_BYTE,0);


}
glBindFramebuffer(GL_FRAMEBUFFER, Fboid);


    for(int i=0;i<6;i++)                      
glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_1D,id1,0);


draw_cube();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

draw();

}
draw_cube()
{

	glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
		
//	glClearDepth(1.0f);
	
	glClear(GL_COLOR_BUFFER_BIT);
	
	glEnableVertexAttribArray(glGetAttribLocation(temp.psId,"position"));

	glVertexAttribPointer(glGetAttribLocation(temp.psId,"position"), 3, GL_FLOAT, GL_FALSE, 0,vertices8);

	glDrawArrays (GL_TRIANGLE_FAN, 0, 24);
	
}

draw()
{
	glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

	
	glClearDepth(1.0f);

	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnableVertexAttribArray(glGetAttribLocation(shader_data.psId,"tk_position"));

	glVertexAttribPointer(glGetAttribLocation(shader_data.psId,"tk_position"), 3, GL_FLOAT, GL_FALSE, 0,vertices);
	nResult = GL_ERROR_CHECK((GL_NO_ERROR, "glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0,vertices);"));

	glEnableVertexAttribArray(glGetAttribLocation(shader_data.psId,"inputtexcoord"));

	glVertexAttribPointer(glGetAttribLocation(shader_data.psId,"inputtexcoord"), 2, GL_FLOAT, GL_FALSE, 0,texcoord);
	
	glBindTexture(*target11, id1);
	
	glDrawElements ( GL_TRIANGLES, 36,GL_UNSIGNED_INT, indices );
}

default FBO is glBindFramebuffer(GL_FRAMEBUFFER, 0)

Nope, you don’t bind a default FBO. There is no default FBO. You bind the GL to the default framebuffer. The latter is window-system provided while FBOs are managed by the GL.



glGenFramebuffers(1, &Fboid); 

for(int i=0;i<6;i++) { 
    glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);       
    glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);   
    glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,  WIDTH*HEIGHT , 0, GL_RGBA, GL_UNSIGNED_BYTE,0);     
}

What’s that supposed to do? You create the textures data store 6 times - 5 times of that are completely unecessary. You don’t use any function arguments depending on the loop in any way.


 for(int i=0;i<6;i++)                       
     glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_1D,id1,0);

The same goes for this. You attach the same texture to COLOR_ATTACHMENT0 6 times - 5 times wasted.

Then you say your clear color on the FBO is red, which contradicts the function call

 glClearColor(0.0f, 0.0f, 0.5f, 1.0f); 

For default FBO, i have created 1-D texture[…]

No, you don’t create a texture for the default framebuffer. You create a texture object when calling glBindTexture(). This texture object is managed by the GL and has no direct connection to the default framebuffer. You could create and modify texture objects without a default framebuffer being present.

Now, when i render this texture to on another cube, i can see continuous white color on start or end of each face of the cube. That means part of the face is white and rest is red.

Screenshots please. From your description I can’t come to a precise conclusion.


#define WIDTH 3
#define HEIGHT 3
 
GLfloat vertices8[]={
		1.0f,1.0f,1.0f,
		-1.0f,1.0f,1.0f,
		-1.0f,-1.0f,1.0f,
		1.0f,-1.0f,1.0f,//face 1
 
		1.0f,-1.0f,-1.0f,
		-1.0f,-1.0f,-1.0f,
		-1.0f,1.0f,-1.0f,
		1.0f,1.0f,-1.0f,//face 2
 
		1.0f,1.0f,1.0f,
		1.0f,-1.0f,1.0f,
		1.0f,-1.0f,-1.0f,
		1.0f,1.0f,-1.0f,//face 3				
 
		-1.0f,1.0f,1.0f,
		-1.0f,1.0f,-1.0f,
		-1.0f,-1.0f,-1.0f,
		-1.0f,-1.0f,1.0f,//face 4
 
		1.0f,1.0f,1.0f,
		1.0f,1.0f,-1.0f,
		-1.0f,1.0f,-1.0f,
		-1.0f,1.0f,1.0f,//face 5
 
		-1.0f,-1.0f,1.0f,
		-1.0f,-1.0f,-1.0f,
		1.0f,-1.0f,-1.0f,
		1.0f,-1.0f,1.0f//face 6		
 
 
};
GLfloat vertices[]=
	{
		0.5f,0.5f,0.5f,
		-0.5f,0.5f,0.5f,
		-0.5f,-0.5f,0.5f,
		0.5f,-0.5f,0.5f,//face 1
 
		0.5f,-0.5f,-0.5f,
		-0.5f,-0.5f,-0.5f,
		-0.5f,0.5f,-0.5f,
		0.5f,0.5f,-0.5f,//face 2
 
		0.5f,0.5f,0.5f,
		0.5f,-0.5f,0.5f,
		0.5f,-0.5f,-0.5f,
		0.5f,0.5f,-0.5f,//face 3				
 
		-0.5f,0.5f,0.5f,
		-0.5f,0.5f,-0.5f,
		-0.5f,-0.5f,-0.5f,
		-0.5f,-0.5f,0.5f,//face 4
 
		0.5f,0.5f,0.5f,
		0.5f,0.5f,-0.5f,
		-0.5f,0.5f,-0.5f,
		-0.5f,0.5f,0.5f,//face 5
 
		-0.5f,-0.5f,0.5f,
		-0.5f,-0.5f,-0.5f,
		0.5f,-0.5f,-0.5f,
		0.5f,-0.5f,0.5f//face 6		
 
	};
 
	GLuint indices[] =
      {
         0, 2, 1,
         0, 3, 2, 
         4, 5, 6,
         4, 6, 7,
         8, 9, 10,
         8, 10, 11, 
         12, 15, 14,
         12, 14, 13, 
         16, 17, 18,
         16, 18, 19, 
         20, 23, 22,
         20, 22, 21
      };
 
 
 
GLfloat texcoord[] = 
{
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,
 
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,
 
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,
 
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,
 
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0,
 
		0.0, 0.0,
        1.0, 0.0,
        1.0, 1.0,
		0.0, 1.0
};
 
glGenTextures(1, &id1);
glBindTexture(GL_TEXTURE_1D, id1);
 
glGenFramebuffers(1, &Fboid);

glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA,  WIDTH*HEIGHT , 0, GL_RGBA, GL_UNSIGNED_BYTE,0);
 
glBindFramebuffer(GL_FRAMEBUFFER, Fboid);
 
glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_1D,id1,0);
 
 
draw_cube();
 
glBindFramebuffer(GL_FRAMEBUFFER, 0);
 
draw();
 
}
draw_cube()
{
 	glViewport(0, 0, WIDTH, HEIGHT);
	glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
 
 
	glClear(GL_COLOR_BUFFER_BIT);
 
	glEnableVertexAttribArray(glGetAttribLocation(temp.psId,"position"));
 
	glVertexAttribPointer(glGetAttribLocation(temp.psId,"position"), 3, GL_FLOAT, GL_FALSE, 0,vertices8);
 
	glDrawArrays (GL_TRIANGLE_FAN, 0, 24);
 
}
 
draw()
{
	glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
 
 
	glClearDepth(1.0f);
 
 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
	glEnableVertexAttribArray(glGetAttribLocation(shader_data.psId,"tk_position"));
 
	glVertexAttribPointer(glGetAttribLocation(shader_data.psId,"tk_position"), 3, GL_FLOAT, GL_FALSE, 0,vertices);
	nResult = GL_ERROR_CHECK((GL_NO_ERROR, "glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0,vertices);"));
 
	glEnableVertexAttribArray(glGetAttribLocation(shader_data.psId,"inputtexcoord"));
 
	glVertexAttribPointer(glGetAttribLocation(shader_data.psId,"inputtexcoord"), 2, GL_FLOAT, GL_FALSE, 0,texcoord);
 
	glBindTexture(*target11, id1);
 
	glDrawElements ( GL_TRIANGLES, 36,GL_UNSIGNED_INT, indices );
}

I have modified the code. And attaching a screenshot.
[ATTACH=CONFIG]320[/ATTACH]

Well, the good thing is that you actually have the rendered texture on the second cube. Judging from your code you want to render one side of you cube to the texture, where the cube is supposed to fill the complete viewport, and render the second cube with this texture on all sides. Correct? If so, why a cube? Why not a screen-filling quad?

Furthermore, what does your shader for the RTT cube look like? The coordinates of the cube already fulfill the property of coinciding with the canonical volume obtained after perspective divide, so technically you should just see one side of the cube spanning the comlete viewport - unless you apply some transformation in the vertex shader.

Let’s see the vertex shader please.

when i change WIDTH=HEIGHT=2, and call a glreadpixels with height, width equal to 4 in draw_cube() i can see first 2 pixels with white color, next two with blue(glclearcolor), next two white and then blue and so on… Now when i change width parameter in glTeximage1D to 16 then ideally i should see alternate patches of white and blue right? But its not the case here. why so?