微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 渲染时纹理显示为灰色

我目前正在通过“开始C#编程”工作,并在绘制纹理时在第7章遇到了问题.

我使用了与演示CD上相同的代码,虽然我必须将纹理的路径更改为绝对路径,但在渲染时它会显示为灰色.

我已经调试了程序来写入加载纹理的文件,这很好 – 没有问题.所以在那之后发生了一些错误.

以下是一些代码片段:

public void InitializeGraphics()
{
    // set up the parameters
    Direct3D.PresentParameters p = new Direct3D.PresentParameters();
    p.SwapEffect = Direct3D.SwapEffect.discard;
    ...
    graphics = new Direct3D.Device( 0,Direct3D.DeviceType.Hardware,this,Direct3D.CreateFlags.softwareVertexProcessing,p );
    ...
    // set up varIoUs drawing options
    graphics.RenderState.CullMode = Direct3D.Cull.None;
    graphics.RenderState.AlphaBlendEnable = true;
    graphics.RenderState.AlphaBlendOperation = Direct3D.BlendOperation.Add;
    graphics.RenderState.DestinationBlend = Direct3D.Blend.InvSourceAlpha;
    graphics.RenderState.sourceBlend = Direct3D.Blend.sourceAlpha;
    ...
}

public void InitializeGeometry()
{
    ...
    texture = Direct3D.TextureLoader.FromFile( 
            graphics,"E:\\Programming\\SharpDevelop_Projects\\AdvancedFrameworkv2\\texture.jpg",Direct3D.Format.UnkNown,Direct3D.Pool.Managed,Direct3D.Filter.Linear,0 );
...
}

protected virtual void Render()
{
    graphics.Clear( Direct3D.ClearFlags.Target,Color.White,1.0f,0 );
    graphics.BeginScene();

    // set the texture
    graphics.SetTexture( 0,texture );

    // set the vertex format
    graphics.VertexFormat = Direct3D.CustomVertex.TransformedTextured.Format;

    // draw the triangles
    graphics.DrawUserPrimitives( Direct3D.PrimitiveType.TriangleStrip,2,vertexes );

    graphics.EndScene();
    graphics.Present();
    ...
}

我无法弄清楚这里出了什么问题.显然,如果我在Windows中加载纹理它显示正常 – 所以在书中给出的代码示例中有一些不正确的东西.它实际上并不起作用,并且我的环境肯定会出现问题.

解决方法

你在那里使用了一种非常古老的技术……我猜你正在尝试制作游戏(正如我们在开始时所做的那样!),尝试使用XNA.我最好的猜测是它是你的图形驱动程序.我知道这听起来像个警察,但是说真的,我以前见过这个,一旦我换掉了我的旧显卡就换了一个新的显卡!我并不是说它已经坏了,或者它不可能让它发挥作用.但我最好的两个建议是:

1)开始使用XNA并使用http://www.xnadevelopment.com/tutorials.shtml上的教程2)更换显卡(如果你想继续你正在做的事情).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐