写入纹理GLSL [英] Write to texture GLSL

查看:305
本文介绍了写入纹理GLSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够(在片段着色器中)将一个纹理添加到另一个纹理。现在我有投影纹理,想扩展。



这里是我到目前为止:



Im也绘制viewfrustum,蓝色/灰色测试图像投射到恒定旋转的几何图形上。



$ b

我的顶点着色器

ProjTexCoord = ProjectorMatrix * ModelTransform * raw_pos;

我的片段着色器

  vec4 diffuse = texture(texture1,vs_st); 
vec4 projTexColor = textureProj(texture2,ProjTexCoord);
vec4 shaded = diffuse; // max(intensity * diffuse,ambient); - 现在没有阴影
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;
}

我想达成的目标: >

当例如 - 用户按下按钮时,我想将蓝色/灰色纹理写入球体上的灰色纹理,并旋转与它。想象一下,它是一种拍照或绘制在球体的顶部,以便按下按钮后,蓝色/灰色纹理与球体旋转。



片段着色器对每个像素进行操作,应该可以逐像素地从一个纹理复制到另一个纹理,但我不知道如何,我可能会搜索错误的东西。



如何在技术上实现?什么方法最通用?建议非常感谢,请让我知道如果更多的代码是必要的。

解决方案

为了清楚起见,您想要将贴图贴到球体的灰色纹理中。



在绘制另一个对象时写入灰色纹理的麻烦是不是一对一的。你可能对同一个纹理写两次或更多次,或者单个片段可能需要写入您的灰色纹理中的许多纹素。它可能听起来有吸引力,因为你已经在一个地方的一切的坐标,但我不会这样做。



我开始创建一个纹理包含您的灰色纹理中每个纹素的对象空间位置。这是关键,所以当你点击你可以渲染到你的灰色纹理(使用FBO),并知道每个纹理在当前视图或投影纹理的视图中。可能存在边缘情况,其中相同位的纹理出现在多个三角形上。你可以通过使用纹理坐标作为你的顶点位置渲染你的球体到灰色纹理。你可能需要一个浮点纹理,下面的图片可能不是球体的纹理映射,但它会做演示:P





因此,当您点击时,一个全屏幕的四分之一到你的灰色纹理与alpha混合启用。使用灰色纹理对象空间位置,每个片段计算蓝色纹理的投影内的图像空间位置。




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

Here is what I have so far :

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

My vertex shader:

    ProjTexCoord = ProjectorMatrix * ModelTransform * raw_pos;

My Fragment Shader:

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 to the gray texture on the sphere and rotate with it. Imagine it as sort of "taking a picture" or painting on top of the sphere so that the blue/gray texture spins with the sphere after a button is pressed.

As the fragment shader operates on each pixel it should be possible to copy pixel-by-pixel from one texture to the other, but I have no clue how, I might be googling for the wrong stuff.

How can I achieve this technically? What method is most versatile? Suggestions are very much appreciated, please let me know If more code is necessary.

解决方案

Just to be clear, you'd like to bake decals into your sphere's grey texture.

The trouble with writing to the grey texture while drawing another object is it's not one to one. You may be writing twice or more to the same texel, or a single fragment may need to write to many texels in your grey texture. It may sound attractive as you already have the coordinates of everything in the one place, but I wouldn't do this.

I'd start by creating a texture containing the object space position of each texel in your grey texture. This is key, so that when you click you can render to your grey texture (using an FBO) and know where each texel is in your current view or your projective texture's view. There may be edge cases where the same bit of texture appears on multiple triangles. You could do this by rendering your sphere to the grey texture using the texture coordinates as your vertex positions. You probably need a floating point texture for this, and the following image probably isn't the sphere's texture mapping, but it'll do for demonstration :P.

So when you click, you render a full screen quad to your grey texture with alpha blending enabled. Using the grey texture object space positions, each fragment computes the image space position within the blue texture's projection. Discard the fragments that are outside the texture and sample/blend in those that are inside.

这篇关于写入纹理GLSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆