Masking OpenGL texture by a pattern
Posted
by
user1304844
on Game Development
See other posts from Game Development
or by user1304844
Published on 2013-11-03T20:13:22Z
Indexed on
2013/11/03
22:20 UTC
Read the original article
Hit count: 300
Tiled terrain. User wants to build a structure. He presses build and for each tile there is an "allow" or "disallow" tile sprite added to the scene. FPS drops right away, since there are 600+ tiles added to the screen. Since map equals screen, there is no scrolling. I came to an idea to make an allow grid covering the whole map and mask the disallow fields.
Approach 1:
- Create allow and disallow grid textures.
- Draw a polygon on screen.
- Pass both textures to the fragment shader.
- Determine the position inside the polygon and use color from allowTexture if the fragment belongs to the allow field, disallow otherwise
Problem: How do I know if I'm on the field that isn't allowed if I cannot pass the matrix representing the map (enum FieldStatus[][] (Allow / Disallow)) to the shader? Therefore, inside the shader I don't know which fragments should be masked.
Approach 2:
- Create allow texture.
- Create an empty texture buffer same size as the allow texture
- Memset the pixels of the empty texture to desired color for each pixel that doesn't allow building.
- Draw a polygon on screen.
- Pass both textures to the fragment shader.
- Use texture2 color if alpha > 0, texture1 color otherwise.
Problem: I'm not sure what is the right way to manipulate pixels on a texture. Do I just make a buffer with width*height*4 size and memcpy the color[] to desired coordinates or is there anything else to it? Would I have to call glTexImage2D after every change to the texture?
Another problem with this approach is that it takes a lot more work to get a prettier effect since I'm manipulating the color pixels instead of just masking two textures.
varying vec2 TexCoordOut;
uniform sampler2D Texture1;
uniform sampler2D Texture2;
void main(void){
vec4 allowColor = texture2D(Texture1, TexCoordOut);
vec4 disallowColor = texture2D(Texture2, TexCoordOut);
if(disallowColor.a > 0){
gl_FragColor= disallowColor;
}else{
gl_FragColor= allowColor;
}}
I'm working with OpenGL on Windows. Any other suggestion is welcome.
© Game Development or respective owner