跳到主要内容
← Vertex Transformations

Vertex Transformations

Homogeneous Coordinates

好难!

实时渲染 · 网格 HLSL

第 19 课:Homogeneous Coordinates

我的解:好难!

我的解

cbuffer Uniforms : register(b0) {
  float iTime;
  float4x4 viewMatrix;
  float4x4 projectionMatrix;
};

struct VSInput {
  float3 position : Position;
};

struct VSOutput {
  float4 position : SV_Position;
  float3 wolrdPos : POSITION0;
};
float4x4 RotY(float a){
    float c = cos(a), s = sin(a);
    return float4x4(  c, 0, s, 0,
                      0, 1, 0, 0,
                     -s, 0, c, 0,
                      0, 0, 0, 1);
 }

float4x4 RotZ(float a){
    float c = cos(a), s = sin(a);
    return float4x4(  c,-s, 0, 0,
                      s, c, 0, 0,
                      0, 0, 1 , 0, 
                      0, 0, 0, 1);
 }
float4x4 Translate(float3 t){
    return float4x4(1,0,0,0,
                    0,1,0,0,
                    0,0,1,0,
                    t.x, t.y, t.z, 1);
}


VSOutput main(VSInput input) {
  VSOutput output;
  float aSpin  =  -iTime;
  float aOrbit =  iTime;
  float  r     =  1;

  // setPosition3D

  // child: 自转
  float4x4 M_child = RotY(aSpin);

  // parent 平移+公转
  float3 orbitPos = float3(r * cos(aOrbit), r * sin(aOrbit), 0.0);
  float4x4 M_parent = Translate(orbitPos);
  float4x4 M_total = mul(M_parent,M_child);

  float4 p = mul(M_total, float4(input.position, 1));
  float4 viewPos  = mul(viewMatrix, p);
  output.position = mul(projectionMatrix, viewPos);
  output.wolrdPos = p.xyz;

  return output;
}

复盘笔记

  • 好难!