0

My current renderer draws forms by the position and color of their vertices. It can't handle textures yet.

struct Form
{
    unsigned int Positions, Colors, Elements, Program;
};

Now I want to implement the capability of textures. Since there won't be forms without texture but colors in the final game I want to replace the color feature instead of developing a alongside feature.

So the renderer has to draw forms by the position and texture coordinate of their vertices now.

struct Form
{
    unsigned int Positions, Coordinates, Elements, Texture, Program;
};

So far, so good. But without the ability to render without texture my application can't draw forms if their image file doesn't exist or can't be loaded for some reason. Therefore I want to implement a fallback to a given solid color (say pink) if the image file can't be loaded.

How can I realize this fallback without completely developing two alongside features? Is there a common approach? I wish that this could be done in a shader. Or maybe I could send a pink texture to the video card and then treat all forms as equal. Sadly I have no idea how to implement that or if is even possible.

danijar
  • 32,406
  • 45
  • 166
  • 297

2 Answers2

4

I've solved this problem by creating a dummy checker texture procedurally and assigning it to any object which does not have any valid texture. It is not based on vertex color or mesh color, but works. And no shader changes required, only a few lines of code for checking null textures.

Matzi
  • 13,770
  • 4
  • 33
  • 50
  • Even better not using color, that is what I meant. Can you reveal some implementation details, please? – danijar Jan 03 '13 at 15:34
  • 1
    Prepare a small texture for this case. You probably have some resource manager for textrues. If you can't find the right texture, use the one you prepared for this scenario. I have no code right now. – Matzi Jan 03 '13 at 15:39
  • I understand the approach. So I will find out how to define a one pixel texture in code. – danijar Jan 03 '13 at 15:40
  • 2
    The same way as you create ones from file: 'glTexImage2D' with size of one and a pointer to a single pixel data, an integer for example. – Matzi Jan 03 '13 at 15:41
1

If you sample from a texture unit to which no texture is bound, the samples retrieved always come out white; it is not an error to sample from a texture without a complete texture bound. You can hence use that white to modulate it with the fallback color.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • White is just fine as fallback color, that doesn't really matter. So I can pass a null pointer to `glTexImage2D` and the mesh will get drawn? – danijar Jan 03 '13 at 15:35
  • 1
    @sharethis: Passing a null pointer to glTexImage2D actually initializes a texture object. If you do this for all mipmap levels you get a complete mipmappable texture! Or you just specify level 0 and switch off mipmapping. I was thinking of glBindTexture(GL_TEXTURE_…, 0), but initializing a texture with a null pointer is fine, too. – datenwolf Jan 03 '13 at 15:44
  • That's nice because initializing a texture with a null pointer does not require toggling mipmapping. Anyhow the color is black now, but that doesn't matter. Maybe because it's the default texture border color... – danijar Jan 03 '13 at 18:48
  • @sharethis: If you initialize a texture without specifying data, the contents are undefined, which means, it may be anything. – datenwolf Jan 03 '13 at 19:09