【问题标题】:Am I doing something wrong with this CG program?我在这个 CG 程序上做错了吗?
【发布时间】:2012-07-01 14:43:13
【问题描述】:

我使用 Ogre3D 作为图形引擎。

我手动创建了一个网格,它可以正常工作,uvs 是正确的并且设置为表示网格坐标(对于这个例子,网格是 10 x 10)

我在顶点程序中什么都不做,并且有一个非常简单的片段程序。我已经包含了这两个程序和材料文件来解释。

我的问题是,即使过滤设置为无,颜色似乎与我的原始图像不同(这只是我使用的测试图像,因为我在 ogre 中手动创建纹理时遇到问题)。事实证明,问题不在于我在 ogre 中的代码,而更可能与材质文件或片段/顶点程序有关。

我还包括了左侧输出的屏幕截图和右侧的原始图像。片段着色器还在顶部绘制了一个简单的网格,因此我可以确保正确传递 uv 坐标。他们似乎是。

任何见解都将不胜感激,因为我真的不确定自己做错了什么。

材料文件:

// CG Vertex shader definition
vertex_program PlainTexture_VS cg            
{
    // Look in this source file for shader code
    source GameObjStandard.cg
    // Use this function for the vertex shader            
    entry_point main_plain_texture_vp    
    // Compile the shader to vs_1_1 format    
    profiles arbvp1       

    // This block saves us from manually setting parameters in code
    default_params                    
    {
        // Ogre will put the worldviewproj into our 'worldViewProj' parameter for us.
        param_named_auto worldViewProj worldviewproj_matrix        
        // Note that 'worldViewProj' is a parameter in the cg code.
    }
}

// CG Pixel shader definition
fragment_program PlainTexture_PS cg            
{
    // Look in this source file for shader code
    source GameObjStandard.cg        
    // Use this function for the pixel shader    
    entry_point main_plain_texture_fp    
    // Compile to ps_1_1 format    
    profiles arbfp1             
}

material PlainTexture
{
    // Material has one technique
    technique                  
    {
        // This technique has one pass
        pass                   
        {
            // Make this pass use the vertex shader defined above
            vertex_program_ref PlainTexture_VS    
            {
            }
            // Make this pass use the pixel shader defined above
            fragment_program_ref PlainTexture_PS    
            {
            }
            texture_unit 0
            {
                filtering none
                // This pass will use this 2D texture as its input
                texture test.png 2d        
            }
            texture_unit 1
            {
                texture textureatlas.png 2d
                tex_address_mode clamp
                filtering none
            }
        }
    }
}

CG 文件:

void main_plain_texture_vp(
    // Vertex Inputs
    float4 position        : POSITION,    // Vertex position in model space
    float2 texCoord0    : TEXCOORD0,    // Texture UV set 0

    // Outputs
    out float4 oPosition    : POSITION,    // Transformed vertex position
    out float2 uv0        : TEXCOORD0,    // UV0

    // Model Level Inputs
    uniform float4x4 worldViewProj)
{
    // Calculate output position
    oPosition = mul(worldViewProj, position);

    // Simply copy the input vertex UV to the output
    uv0 = texCoord0;
}

void main_plain_texture_fp(
    // Pixel Inputs
    float2 uv0        : TEXCOORD0,    // UV interpolated for current pixel

    // Outputs
    out float4 color    : COLOR,    // Output color we want to write

    // Model Level Inputs
    uniform sampler2D Tex0: TEXUNIT0,

uniform sampler2D Tex1: TEXUNIT1)        // Texture we're going to use
{

//get the index position by truncating the uv coordinates
float2 flooredIndexes = floor(uv0);

if((uv0.x > 0.9 && uv0.x < 1.1)
|| (uv0.x > 1.9 && uv0.x < 2.1)
|| (uv0.x > 2.9 && uv0.x < 3.1)
|| (uv0.x > 3.9 && uv0.x < 4.1)
|| (uv0.x > 4.9 && uv0.x < 5.1)
|| (uv0.x > 5.9 && uv0.x < 6.1)
|| (uv0.x > 6.9 && uv0.x < 7.1)
|| (uv0.x > 7.9 && uv0.x < 8.1)
|| (uv0.x > 8.9 && uv0.x < 9.1)) {
   float4 color1 = {1.0,0,0,0};
   color = color1;
} else if((uv0.y > 0.9 && uv0.y < 1.1)
|| (uv0.y > 1.9 && uv0.y < 2.1)
|| (uv0.y > 2.9 && uv0.y < 3.1)
|| (uv0.y > 3.9 && uv0.y < 4.1)
|| (uv0.y > 4.9 && uv0.y < 5.1)
|| (uv0.y > 5.9 && uv0.y < 6.1)
|| (uv0.y > 6.9 && uv0.y < 7.1)
|| (uv0.y > 7.9 && uv0.y < 8.1)
|| (uv0.y > 8.9 && uv0.y < 9.1)) {
   float4 color1 = {1.0,0,0,0};
   color = color1;
} else {
   //get the colour of the index texture Tex0 at this floored coordinate
   float4 indexColour = tex2D(Tex0, (1.0/10)*flooredIndexes);
   color = indexColour;
}
}

【问题讨论】:

  • 感谢编辑,我不知道如何把图片放进去:)
  • float4 indexColour = tex2D(Tex0, (1.0/10)*flooredIndexes);改为float4 indexColour = tex2D(Tex0, (1.0/20)*flooredIndexes);可能会解决问题
  • 不确定是否有帮助,它没有解决问题。我乘以 1/10 的原因是因为 uv0 中的索引当前在 u 和 v 方向上从 0 到 10 运行。我将此值设置为底值,因为它在片段程序中进行了插值,因此我得到了 5.6 之类的值,如果我将此值底值,我得到 5 除以瓷砖宽度的数量(10),得到我需要的 uv 坐标,0.5
  • 啊我明白你在做什么,我的坐标基本上指向两个像素之间的边界。我想如果我将每个图块增加到 3 x 3 像素,我可以让 cg 专注于中心像素。我可以告诉 cg 以特定像素而不是 uv 坐标为目标吗?
  • 所以我现在已经解决了这个问题。如果有人对此感兴趣,我将在今晚晚些时候发布一个答案,详细说明问题和解决方案。

标签: c++ shader ogre3d ogre cg


【解决方案1】:

好吧,我找到解决我的问题的方法已经有一段时间了,很遗憾没有在线,所以希望这对遇到类似问题的人有所帮助。

在创建任何纹理时,您应该始终将纹理设置为以 texels 2^n * 2^m 为单位的大小,其中 mn 是纹理的宽度和高度。这是我的第一个错误,虽然我当时没有意识到。

我没有发现这一点的原因是因为我的主要纹理图集基于此原理,并且是 1024 x 1024 纹理。我没有考虑的是我作为纹理索引创建的纹理的大小。由于我的地图是 10 x 10,我为索引创建了一个 10 x 10 的纹理,这是我假设然后以某种方式拉伸(不确定它在后端如何工作)为 16 x 16 或 8 x 8,混合像它那样将纹素放在一起。

给我线索的第一件事是,当我在 Photoshop 中缩放画布时,发现它创建的混合颜色与我在 ogre3d 输出中得到的颜色相同。

继续前进..

一旦我明白了这一点,我就可以在 Ogre 中创建纹理并将其传递如下

//Create index material
Ogre::TexturePtr indexTexture = Ogre::TextureManager::getSingleton().createManual("indexTexture","General",Ogre::TextureType::TEX_TYPE_2D, 16, 16, 0, Ogre::PixelFormat::PF_BYTE_BGRA, Ogre::TU_DEFAULT);

Ogre::HardwarePixelBufferSharedPtr pixelBuffer = indexTexture->getBuffer();
pixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL);

const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();
Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);

Ogre::uint8 counter = 0;
for (size_t j = 0; j < 16; j++) {
for(size_t i = 0; i < 16; i++)
{       
        if(i==8 || i==7) {
    *pDest++ = 3;   // B
    *pDest++ = 0;   // G
    *pDest++ = 0;   // R
    *pDest++ = 0;   // A
    } else {
    *pDest++ = 1;   // B 
    *pDest++ = 0;   // G
    *pDest++ = 0;   // R
    *pDest++ = 0;   // A
    }
    counter++;
}
}

pixelBuffer->unlock();

所以现在我有一个纹理可以用作索引,其中包含一些我添加的值用于测试,这些值最终将在运行时通过单击图块来填充。

现在要传递这个纹理,我必须将它传递给正确的技术并传递我的材质,操作如下:

Ogre::MaterialPtr material = Ogre::MaterialPtr(Ogre::MaterialManager::getSingleton().getByName("PlainTexture"));
float mapSize = 16;
float tas = 2;
material->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("mapSize",mapSize);
material->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant("tas",tas);
material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName("indexTexture");    

这也传递了两个值,mapSize 是贴图本身的大小(假设它是一个正方形),tas 是纹理图集的大小(不同的数量图集宽度上的纹理方块)。

为了让我的材料能够理解我刚刚传入的内容,我需要稍微修改我的材料文件,如下所示:

// CG Pixel shader definition
fragment_program PlainTexture_PS cg            
{
    source GameObjStandard.cg    
    entry_point main_plain_texture_fp 
    profiles arbfp1 
default_params
{
    param_named tas float
    param_named 
}            
}

我的通行证也被重新定义了一点

pass                   
{
                    // Make this pass use the vertex shader defined above
    vertex_program_ref PlainTexture_VS    
    {
    }
                    // Make this pass use the pixel shader defined above
    fragment_program_ref PlainTexture_PS    
    {
    }
    texture_unit 0
    {
        filtering none        
    }
texture_unit 1
{
    texture textureatlas.png 2d
    tex_address_mode clamp
    filtering anisotropic
}
}

然后我重写了 cg 纹理片段程序以考虑我所做的更改。

void main_plain_texture_fp(
    float2 uv0 : TEXCOORD0,    // UV interpolated for current pixel
    out float4 color    : COLOR,    // Output color we want to write
uniform float tas,
uniform float mapSize,

    // Model Level Inputs
    uniform sampler2D Tex0: TEXUNIT0,
uniform sampler2D Tex1: TEXUNIT1)
{
//get the index position by truncating the uv coordinates
float2 flooredIndexes = floor(uv0);

//get the colour of the index texture Tex0 at this floored coordinate
float4 indexColour = tex2D(Tex0, ((1.0/mapSize) * flooredIndexes)+(0.5/mapSize));

//calculate the uv offset required for texture atlas range = 0 - 255
float indexValue = (255 * indexColour.b) + (255 * indexColour.g) + (255 * indexColour.r);

//float indexValue = (tas * tas) - indexValue0;

if(indexValue < tas*tas) {
    float row = floor(indexValue/tas);
    float col = frac(indexValue/tas) * tas;

    float uvFraction = 1.0/tas;

    float uBase = col * uvFraction;
    float vBase =  1 - ((tas - row) * uvFraction);

    float uOffset = frac(uv0.x)/tas;
    float vOffset = (frac(uv0.y))/tas;

    float uNew = uBase + uOffset;
    float vNew = vBase + vOffset;

    float2 uvNew = {uNew, vNew};

    if(frac(uv0.x) > 0.99 || frac(uv0.x) < 0.01) {
    float4 color1 = {1,1,1,0};
    color = (0.2*color1) + (0.8*tex2D(Tex1,uvNew));
    } else if(frac(uv0.y) > 0.99 || frac(uv0.y) < 0.01) {
    float4 color1 = {1,1,1,0};
    color = (0.2*color1) + (0.8*tex2D(Tex1,uvNew));
    } else {
    color = tex2D(Tex1,uvNew);
    }


} else {
    float4 color2 = {0.0,0,0,0};
    color = color2;
}
}

这会从纹理图集中计算所需的正确纹素,它还会通过组合 80% 纹素颜色和 20% 白色在顶部覆盖一个微弱的网格。

如果纹理图集没有索引纹理指定的颜色索引,则它只输出黑色(这主要是因为它很容易发现。

以下是使用 2 x 2 纹理图集的输出示例。

【讨论】:

  • @TheSHEEEP 希望这对您有所帮助/感兴趣。
  • @TheSHEEEP 没问题,如果您想知道的话,我忘了提到底部的渲染图像实际上并不是您使用上面的代码得到的。上面的代码在上面放置了一个白色的半透明网格,图像没有考虑网格下面的纹理,只是把它涂成黑色。
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
相关资源
最近更新 更多