Write a GLSL Texture

I want to be able (in the fragment shader) to add one texture to another. Right now I have projective texturing and you want to expand it.

Here is what I still have: CnzsFSh.png

Im also draws a viewfrustum along which a blue / gray test image is projected onto a geometry that is in constant rotation.

My vertex shader:

ProjTexCoord = ProjectorMatrix * ModelTransform * raw_pos; 

My shader fragment:

 vec4 diffuse = texture(texture1, vs_st); vec4 projTexColor = textureProj(texture2, ProjTexCoord); vec4 shaded = diffuse; // max(intensity * diffuse, ambient); -- no shadows for now if (ProjTexCoord[0] > 0.0 || ProjTexCoord[1] > 0.0 || ProjTexCoord[0] < ProjTexCoord[2] || ProjTexCoord[1] < ProjTexCoord[2]){ diffuse = shaded; }else if(dot(n, projector_aim) < 0 ){ diffuse = projTexColor; }else{ diffuse = shaded; } 

What I want to achieve:

When, for example, the user presses a button, I want the blue / gray texture to be written on the gray texture on the sphere and rotate with it. Imagine this as an β€œimage” or painting on top of a sphere, so that the blue / gray texture rotates around the sphere after pressing a button.

Since the fragment shader works on every pixel, it should be possible to copy pixel pixels from one texture to another, but I don’t know how maybe I would look for the wrong material.

How can I achieve this technically? Which method is the most universal? Suggestions are greatly appreciated, please let me know if more code is required.

+2
source share
2 answers

To be clear, you would like to bake stickers in your realm of gray texture.

The problem with writing a gray texture when drawing another object is not one to one. You may be writing twice or more in the same texel, or one piece may need to be written to many texels in your gray texture. This may seem attractive, since you already have the coordinates in just one place, but I wouldn’t.

I would start by creating a texture containing the position space of the objects of each texel in your gray texture. This is the key, so when you click, you can visualize your gray texture (using FBO) and know where each texel is in your current view or in your projective texture view. There may be edge cases where the same texture bit appears on multiple triangles. You can do this by casting your sphere to a gray texture, using texture coordinates as vertex positions. For this, you probably need a floating-point texture, and the following image is probably not a reflection of the texture of the sphere, but this will be done to demonstrate: P.

enter image description here

So, when you click, you show the full square of the screen to your gray texture with alpha blending turned on. Using the positions of the gray object of the texture object, each fragment calculates the position of the image space in the blue texture projection. Discard fragments that are outside the texture and the sample / mixture in those that are inside.

enter image description here

+2
source

I think that you are excessive.

  • Writes textures inside classic shaders (i.e. does not calculate a shader), only for the latest equipment and the latest versions and extensions of OpenGL.
  • This can be terribly slow if used incorrectly. It is so easy to introduce conveyor racks and CPU-GPU sync points.
  • The pixel shader can become a terribly slow, unattainable mess of branches and texture catch.
  • And all this mess will be made for every single pixel for every frame

Solution: KISS

  • Just update the texture on the processor side.
  • Write a texture, replacing parts of it with the desired content
  • An update is needed only once and only when you need it. Data is saved until you overwrite it (even once for each frame, but only once for a change request).
  • Pixel Shader - Dead Brains Simple: No Branching, One Texture
  • To get the target pixels, implement beam selection (you still need any non-trivial interactive 3D graphics program).

PS "Everything should be made as simple as possible, but not simpler." Albert Einstein.

0
source

Source: https://habr.com/ru/post/1209178/


All Articles