jjzjj

c++ - 这个 CG 程序我做错了什么吗?

coder 2023-11-17 原文

我使用 Ogre3D 作为图形引擎。

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

我在顶点程序中什么都不做,有一个非常简单的片段程序。我已经包含了这两个程序以及用于解释的 Material 文件。

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

我还附上了左侧输出的屏幕截图和右侧的原始图像。片段着色器还在顶部绘制了一个简单的网格,因此我可以确保正确传递 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
            }
        }
    }
}

动画文件:

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;
}
}

最佳答案

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

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

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

给我线索的第一件事是当我在 photoshop 中缩放我的 Canvas 时,发现它创建的混合颜色与我在 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();

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

现在要传递这个纹理,我必须将它传递给正确的技术并传递我的 Material ,这是按如下方式完成的:

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 是 map 本身的大小(假设它是正方形),tas 是纹理图集大小(不同的数量)纹理正方形横跨图集的宽度)。

为了让我的 Material 能够理解我刚刚传入的内容,我需要按如下方式稍微修改我的 Material 文件:

// 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 纹理图集的输出示例。

关于c++ - 这个 CG 程序我做错了什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11282868/

有关c++ - 这个 CG 程序我做错了什么吗?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  7. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  8. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  9. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  10. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

随机推荐