# OpenGL Primary Color vs. Diffuse (irrelevant title)

## Recommended Posts

Now, I was tinkering around today trying to get my font stuff caught up with Evil Steve (who, funnily enough is using D3D for his rendering). What we both did is load the fonts white onto a pure-alpha texture, so that we could color them on the fly with diffuse colors. Coming from D3D (years ago), diffuse colors are the color components defined per-vertex. Apparently this is not the case with OpenGL, which defines diffuse per-material. My dilemma is - is there anyway to emulate the behavior of blending together the primary color with the texture fragment such that it would behave as if it were the diffuse? (Aside from writing a shader, which would be... ugh.) The Red Book suggested using GL_COMBINE with various ... stuffs, and the one that was the closest was GL_INTERPOLATE. It interpolated by a weighted value. Which, I mean, was close, but not what I wanted. Basically, I need a way to color my fonts which can be batched with everything else that's being rendered (everything is drawn in 1 call; though this could be changed, I rely on drawing order to sort stuff right now, so batching per-material is not possible. I'd have to break that 1 call into an ungodly number of calls to change materials). Ugh, I suck at explanations. I guess, uhh, what I need is like -
Color Fragment | Texture Fragment | Desired Output
(r,g,b)             (1,1,1)            (r,g,b)
(1,1,1)             (r,g,b)            (r,g,b)
(rf,gf,bf)          (rs,gs,bs)         actually... I have no idea. 
Auuguhhh I know this isn't going to work. Time to approach from a different perspective, I think.
Color Fragment | Texture Fragment | Desired Output
(rf,gf,bf)          (rs,gs,bs,0)       (rf,gf,bf)
(rf,gf,bf)          (rs,gs,bs,1)       (rs,gs,bs)
Hurr, from that we can induce that the formula that I need is - Cf*(1-As)+Cs*As ...and my Red Book is packed in the car since I'm leaving for next semester tommorrow. ARGH. Any ideas on this one guys? :S

##### Share on other sites
Quote:
 Coming from D3D (years ago), diffuse colors are the color components defined per-vertex. Apparently this is not the case with OpenGL, which defines diffuse per-material.

If you look at D3D9, it's pretty similar to OpenGL in how this works. The diffuse color comes from the material, but when you enable COLOR_MATERIAL and set ColorMaterial to AMBIENT_AND_DIFFUSE, you typically get what you want.

When it comes to the combiners, the interpolation function you want is, likely, DECAL. Note that vertex lighting comes in the diffuse color, so if your object is lit, the alpha=1 parts of the texture will be un-lit.

You don't need the red book, btw, the OpenGL specification PDF is available online (on OpenGL.org), and explains everything in great detail. I use no other reference for the non-extension stuff.

##### Share on other sites
Hrm, I was tinkering around with some GUI code, and for some reason the font stuff is magically working the way I wanted it too. I'm not sure exactly what happened.... but yeah.

Thanks :)

##### Share on other sites
Excuse me if this is horribly naive, but what is wrong with gl_color3f? Are you trying to batch multi-colored text into a single VB? I would consider the different colors the same as different textures, and as such they couldn't be batched.

Of course, I'm blitting my fonts with SDL and using a "software shader" to colorize them. So, I'm clearly not concerned about speed.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978471
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33