Nebula3绘制2D纹理

上次已经绘制过基本图元了, 这次只不过要贴张图而已.....

本来我想用Graphics的Model渲染流程来做, 不过这一层太高级了, 都是什么场景管理资源映射之类的

做低级的事情, 就要用低级的API嘛

图形渲染的底层是CoreGraphics, 这个层我不打算再单独写(翻译)一篇了, 因为都是Direct3D概念的一些抽象. 也就是说D3D用熟了基本上一看就明白(用GL的我就不清楚啦, 嘿嘿, N3的作者都放弃用GL去实现@_@).

还记得D3D Tutorial中的Textured例子不? 需要的东西有带纹理坐标的点, 纹理. N3中也一样, 不过, 这里没法用固定管线了.

N3的设计的时候就放弃了固定管线(多么明智呀, 别喷我-_-, 我只会shader.......), 所以在这之前我们要先写一个shader来进行绘制.

因为我们只是进行简单的演示, 就尽量简单了, 写一个2D的纹理绘制, 你可以用来做UI:

  1. //------------------------------------------------------------------------------
  2. //  texture2d.fx
  3. //  texture shader for 2D(UI)
  4. //  (C) xoyojank
  5. //------------------------------------------------------------------------------
  6. float2 halfWidthHeight  : HalfWidthHeight;
  7. texture diffMap     : DiffMap0;
  8. sampler diffMapSampler = sampler_state
  9. {
  10.     Texture = <diffMap>;
  11.     AddressU = Clamp;
  12.     AddressV = Clamp;
  13.     MinFilter = Point;
  14.     MagFilter = Point;
  15.     MipFilter = None;
  16. };
  17. struct VS_INPUT
  18. {
  19.     float3 pos  : POSITION;
  20.     float2 uv       : TEXCOORD;
  21. };
  22. struct VS_OUTPUT
  23. {
  24.     float4 pos  : POSITION;
  25.     float2 uv       : TEXCOORD;
  26. };
  27. //------------------------------------------------------------------------------
  28. /**
  29. */
  30. VS_OUTPUT
  31. VertexShaderFunc(VS_INPUT input)
  32. {
  33.     VS_OUTPUT output;
  34.     output.pos.xy = float2(input.pos.x - halfWidthHeight.x, halfWidthHeight.y - input.pos.y) / halfWidthHeight;
  35.     output.pos.zw = float2(input.pos.z, 1.0f);
  36.     output.uv = input.uv;
  37. return output;
  38. }
  39. //------------------------------------------------------------------------------
  40. /**
  41. */
  42. float4
  43. PixelShaderFunc(float2 uv : TEXCOORD0) : COLOR
  44. {
  45. return tex2D(diffMapSampler, uv);
  46. }
  47. //------------------------------------------------------------------------------
  48. /**
  49. */
  50. technique Default
  51. {
  52.     pass p0
  53.     {
  54.         ColorWriteEnable  = RED|GREEN|BLUE|ALPHA;
  55.         ZEnable           = False;
  56.         ZWriteEnable      = False;
  57.         StencilEnable     = False;
  58.         FogEnable         = False;
  59.         AlphaBlendEnable  = True;
  60.         SrcBlend          = SrcAlpha;
  61.         DestBlend         = InvSrcAlpha;
  62.         AlphaTestEnable   = False;
  63.         ScissorTestEnable = False;
  64.         CullMode          = CW;        
  65.         VertexShader = compile vs_3_0 VertexShaderFunc();
  66.         PixelShader = compile ps_3_0 PixelShaderFunc();
  67.     }
  68. }

值得一提的是CullMode = CW, 为什么? 因为N3用的右手坐标系, 这点又跟D3D不一样了........为什么呢? 难道写MAYA跟MAX的插件的时候比较省事?

还是要跟上一次一样设置顶点格式并载入VertexBuffer:

  1. // vertex
  2.             Array<VertexComponent> vertexComponents;
  3.             vertexComponents.Append(VertexComponent(VertexComponent::Position, 0, VertexComponent::Float3));
  4.             vertexComponents.Append(VertexComponent(VertexComponent::TexCoord, 0, VertexComponent::Float2));
  5. float vertex[4][5] = {
  6.                 {0.0f,  0.0f,   0.0f,   0.0f, 0.0f},
  7.                 {0.0f,  256.0f, 0.0f,   0.0f, 1.0f}, 
  8.                 {256.0f,0.0f,   0.0f,   1.0f, 0.0f}, 
  9.                 {256.0f,256.0f, 0.0f,   1.0f, 1.0f}
  10.             };
  11.             vertexBuffer = VertexBuffer::Create();
  12.             Ptr<MemoryVertexBufferLoader> vbLoader = MemoryVertexBufferLoader::Create();
  13.             vbLoader->Setup(vertexComponents, 4, vertex, 4 * 5 * sizeof(float));
  14.             vertexBuffer->SetLoader(vbLoader.upcast<ResourceLoader>());
  15.             vertexBuffer->Load();
  16.             vertexBuffer->SetLoader(NULL);

纹理的创建其实跟顶点差不多, 因为它都是属于资源的一种, 详见Nebula3资源子系统

  1. // texture
  2.             texture = Texture::Create();
  3.             texture->SetResourceId(ResourceId("bin:razor.jpg"));
  4.             texture->SetLoader(StreamTextureLoader::Create());
  5.             texture->Load();
  6.             texture->SetLoader(NULL);

shader的加载跟上一次一样, 只是参数不同:

  1. // shader
  2. this->shaderInstance = this->shaderServer->CreateShaderInstance(ResourceId("shd:texture2d"));
  3.             Ptr<ShaderVariable> halfWidthHeight = this->shaderInstance->GetVariableBySemantic(ShaderVariable::Semantic("HalfWidthHeight"));
  4.             float2 halfWH = float2(this->renderDevice->GetDefaultRenderTarget()->GetWidth(), this->renderDevice->GetDefaultRenderTarget()->GetHeight()) * 0.5f;
  5.             halfWidthHeight->SetFloatArray(&halfWH.x(), 2);
  6.             Ptr<ShaderVariable> diffMap = this->shaderInstance->GetVariableBySemantic(ShaderVariable::Semantic("DiffMap0"));
  7.             diffMap->SetTexture(texture);

绘制嘛, 当然改成矩形了, 图片可贴不到一跟线上:

  1. this->renderDevice->BeginFrame();
  2. this->renderDevice->BeginPass(this->renderDevice->GetDefaultRenderTarget(), this->shaderInstance);
  3.         PrimitiveGroup primGroup;
  4.         primGroup.SetBaseVertex(0);
  5.         primGroup.SetNumVertices(4);
  6.         primGroup.SetPrimitiveTopology(PrimitiveTopology::TriangleStrip);
  7. this->renderDevice->SetVertexBuffer(this->vertexBuffer);
  8. this->renderDevice->SetPrimitiveGroup(primGroup);
  9. this->renderDevice->Draw();
  10. this->renderDevice->EndPass();
  11. this->renderDevice->EndFrame();
  12. this->renderDevice->Present();

上图:

posted @ 2008-12-14 22:03  至尊王者  阅读(677)  评论(0编辑  收藏  举报