<p>You need to set the colour back to white before drawing the framebuffer to the screen. Otherwise, the whole framebuffer gets multiplied with red.</p>

<pre><code>glBindFramebuffer(GL_FRAMEBUFFER, 0);

glColor3f(1, 1, 1); // &lt;------- add this (doesn't have to be exactly here)

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
</code></pre>

CC BY-SA 4.0
2019-08-22T11:41:41.110Z
Or even better, change the texture environment to `GL_REPLACE` rather than `GL_MODULATE`, if that is really the problem. (Provided entirely doing away with this legacy fixed function stuff is out of the question already.)
Christian Rau
CC BY-SA 4.0
2019-08-23T00:13:32.683Z
@ChristianRau Why is that better?
Criticize SE actions means ban
CC BY-SA 4.0
2019-08-23T00:16:36.707Z
Because that's the actual problem and a more dirtect solution. You don't *want* to combine your texture's color with a constant color in this scenario anyway. So rather than setting the constant to 1, changing the texture to actually *not* combine with the color feels like the more natural solution to the problem. (If anything, though, it might at least be good to add to the answer that *that*, the texture environment, is the actual cause of the problem, if it is.)
Christian Rau
CC BY-SA 4.0
2019-08-23T00:19:22.643Z
@ChristianRau I'd wager that most fixed-function apps draw un-colour-modulated textures by using GL_MODULATE and a white colour. That is natural because it's the default.
Criticize SE actions means ban
CC BY-SA 4.0
2019-08-22T04:41:36.067Z
2019-08-22T04:41:36.067Z
Criticize SE actions means ban
<p>The code is</p>

<pre><code>glColor3f(1, 1, 1);
gluSphere(gluq, 0.2, 10, 10);

glColor3f(1, 0, 0);
glTranslatef(1, 0, 0);
gluSphere(gluq, 0.2, 10, 10);
</code></pre>

<p>In short, when I draw directly to screen, it's correct.</p>

<p><a href="https://i.sstatic.net/ZKAP9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZKAP9.png" alt="enter image description here"></a></p>

<p>But when I create a framebuffer object, attach a texture as color component, draw on the fbo, then draw the texture as rectangle on to the screen, the colors went wrong</p>

<p><a href="https://i.sstatic.net/4fqnS.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4fqnS.png" alt="enter image description here"></a></p>

<p>It seems like the right sphere at (1,0,0) changed the color of the sphere drawn before it.</p>

<p>And even more bizzare is that if i change the first sphere's color to blue (0,0,1), it becomes black after the second sphere is drawn.</p>

<p><a href="https://i.sstatic.net/HZpHt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/HZpHt.png" alt="enter image description here"></a></p>

<p>The way I rendered it is like this :</p>

<pre><code>glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
    glViewport(0, 0, SCRWIDTH, SCRHEIGHT);
    glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)SCRWIDTH / (float)SCRHEIGHT, 0.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);


    glColor3f(0, 0, 1);
    gluSphere(gluq, 0.2, 10, 10);

    glColor3f(1, 0, 0);
    glTranslatef(1, 0, 0);
    gluSphere(gluq, 0.2, 10, 10);

}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
</code></pre>

<p>And I created the fbo and texture like this :</p>

<pre><code>glGenTextures(1, &amp;fbo_tex[0]);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, SCRWIDTH, SCRHEIGHT, 0, GL_RGBA, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &amp;fbo[0]);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fbo_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
</code></pre>

<p>If feels like that consequtive glColor3f caused the colors to mix, taking the minimum RGB values of previous used colors. So white becomes red, but blue turns to black. And the first sphere has the color written to the texture, then the second sphere somehow picks the mixed color and overwrite the first one's pixels. How can I fix this?</p>

CC BY-SA 4.0
2019-08-14T03:10:55.630Z
Try calling glDisable(GL_TEXTURE_2D) before calling gluSphere(...)
PaulHK
CC BY-SA 4.0
2019-08-19T08:32:36.173Z
Intuitive guess from looking at the images: Looks like you only get or show the red channel. Try to set the white color to grey and see if it turns up dark red.
beyond
<opengl><color><frame-buffer>
Weird glColor3f results in framebuffer objects
seilgu
# Weird glColor3f results in framebuffer objects

(Asked by: username_0 on 2019-08-13)

The code is

```
glColor3f(1, 1, 1);
gluSphere(gluq, 0.2, 10, 10);

glColor3f(1, 0, 0);
glTranslatef(1, 0, 0);
gluSphere(gluq, 0.2, 10, 10);

```

In short, when I draw directly to screen, it's correct.

[![enter image description here](https://i.sstatic.net/ZKAP9.png)](https://i.sstatic.net/ZKAP9.png)

But when I create a framebuffer object, attach a texture as color component, draw on the fbo, then draw the texture as rectangle on to the screen, the colors went wrong

[![enter image description here](https://i.sstatic.net/4fqnS.png)](https://i.sstatic.net/4fqnS.png)

It seems like the right sphere at (1,0,0) changed the color of the sphere drawn before it.

And even more bizzare is that if i change the first sphere's color to blue (0,0,1), it becomes black after the second sphere is drawn.

[![enter image description here](https://i.sstatic.net/HZpHt.png)](https://i.sstatic.net/HZpHt.png)

The way I rendered it is like this :

```
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
    glViewport(0, 0, SCRWIDTH, SCRHEIGHT);
    glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (float)SCRWIDTH / (float)SCRHEIGHT, 0.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);

    glColor3f(0, 0, 1);
    gluSphere(gluq, 0.2, 10, 10);

    glColor3f(1, 0, 0);
    glTranslatef(1, 0, 0);
    gluSphere(gluq, 0.2, 10, 10);

}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);

```

And I created the fbo and texture like this :

```
glGenTextures(1, &fbo_tex[0]);

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, SCRWIDTH, SCRHEIGHT, 0, GL_RGBA, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &fbo[0]);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[0]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fbo_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

```

If feels like that consequtive glColor3f caused the colors to mix, taking the minimum RGB values of previous used colors. So white becomes red, but blue turns to black. And the first sphere has the color written to the texture, then the second sphere somehow picks the mixed color and overwrite the first one's pixels. How can I fix this?

**Tags:** opengl, color, frame-buffer

> **Comments:**
> **username_1**: Try calling glDisable(GL_TEXTURE_2D) before calling gluSphere(...)  
> **username_2**: Intuitive guess from looking at the images: Looks like you only get or show the red channel. Try to set the white color to grey and see if it turns up dark red.  

---

## 1 answers

### Answer by username_3 on 2019-08-22. Score: 3

You need to set the colour back to white before drawing the framebuffer to the screen. Otherwise, the whole framebuffer gets multiplied with red.

```
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glColor3f(1, 1, 1); // <------- add this (doesn't have to be exactly here)

glBindTexture(GL_TEXTURE_2D, fbo_tex[0]);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, SCRWIDTH, SCRHEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
{
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex3i(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3i(0, SCRHEIGHT, 0);
    glTexCoord2f(1, 1); glVertex3i(SCRWIDTH, SCRHEIGHT, 0);
    glTexCoord2f(1, 0); glVertex3i(SCRWIDTH, 0, 0);
    glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);

```

> **Comments:**
> **username_4**: Or even better, change the texture environment to `GL_REPLACE` rather than `GL_MODULATE`, if that is really the problem. (Provided entirely doing away with this legacy fixed function stuff is out of the question already.)  
> **username_3**: @username_4 Why is that better?  
> **username_4**: Because that's the actual problem and a more dirtect solution. You don't *want* to combine your texture's color with a constant color in this scenario anyway. So rather than setting the constant to 1, changing the texture to actually *not* combine with the color feels like the more natural solution to the problem. (If anything, though, it might at least be good to add to the answer that *that*, the texture environment, is the actual cause of the problem, if it is.)  
> **username_3**: @username_4 I'd wager that most fixed-function apps draw un-colour-modulated textures by using GL_MODULATE and a white colour. That is natural because it's the default.  

---

