Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if the active texture is texture id 0 in GLSL?

I have model assets that are untextured and I am tired of rendering them as black without lighting. This is because if texture id 0 is bound and I ask the sampler it tells me its black. Later leading to 0 * lighting making the entire object black without lighting. Ideally I would like it colored, materialed and lit, ( which will happen if we don't multiply it by 0 )

I have considered using a uniform flag and toggling it per object, but I was hoping for a simpler higher performing way if we need partially textured assets later.

I have also though of binding a white texture pushing a white pixel and setting it to wrap for instance, but this seems like "Chi Ting".

Note. A valid texture may return black so we can't just ignore black texture reads. well, we could, but it wouldn't look good. Currently we aren't using any buffer objects, just the deprecated glBegin() glEnd() method with display lists.

Any other ideas would be appreciated.

like image 429
EnabrenTane Avatar asked May 30 '11 09:05

EnabrenTane


People also ask

What is sampler2D?

A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.

What is GL texture?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.

Why are OpenGL textures upside down?

You probably noticed that the texture is flipped upside-down! This happens because OpenGL expects the 0.0 coordinate on the y-axis to be on the bottom side of the image, but images usually have 0.0 at the top of the y-axis.


2 Answers

Use empty white texture. It's going to be as fast as using uniform and you don't have to handle extra cases.

If you really worry about speed, combine your textures into one, leaving small white area for untextured parts. Changing texCoords is much faster than changing textures or uniforms.

like image 176
Piotr Praszmo Avatar answered Sep 21 '22 12:09

Piotr Praszmo


In my own attempts at learning openGL, I went with a boolean uniform, as you suggested.

It makes sense that using a texture or having a fixed color is a property of each model object; therefore setting this property on the shader for each object via a uniform seems logical, no 'cheating' involved !

If it makes you feel any better, you can have the texture (if any) as a property of your object and set the uniform based on its presence or absence, instead of duplicating information by having a texture property and a textured/not textured flag.

like image 45
Nicolas Lefebvre Avatar answered Sep 21 '22 12:09

Nicolas Lefebvre