缩放和转换矩阵

时间:2018-12-27 12:27:54

标签: lwjgl

我正在使用点光源实现延迟照明。我的灯光渲染方法如下

 Take an Unit Sphere[S]
 Center=(0,0,0)
 Radius=1 unit

我正在渲染4个点光源,每个点光源的位置和影响力都不同

Point Light 1
Position[P]==(-5f,0.4f,-6f)
Radius_Of_Influence[R]=1.5f

Point Light 2
Position[P]==(5f,0.4f,-6f)
Radius_Of_Influence[R]=2.5f

Point Light 3
Position[P]==(-5f,0.4f,6f)
Radius_Of_Influence[R]=0.8f

Point Light 4
Position[P]==(-5f,0.4f,6f)
Radius_Of_Influence[R]=0.7f

现在我有一个Matrix4f对象,每个光源

for(PointLight light:lights)
{
 translate(light.position(),modelMatrix);/*Translate the matrix with position*/
 scale(light.radius(),modelMatrix);/*Scale the matrix with radius of influence*/
 render_sphere();
} 

这是转换矩阵的方法

public void translate(float x,float y,float z)
  {
    translation.negate(); /*Undo the previous translation*/
    Matrix4f.translate(translation.toVector(temp),modelMatrix,modelMatrix);

    translation.set(x,y,z);/*translate with new values*/
    Matrix4f.translate(translation.toVector(temp),modelMatrix,modelMatrix);
  } 

这是缩放矩阵的方法

 public void scale(float x,float y,float z)
  { 
   scale.set(1/scale.x,1/scale.y,1/scale.z);/*undo previous scaling*/
   Matrix4f.scale(scale,modelMatrix,modelMatrix);

   scale.set(x,y,z);/*apply new scale*/
   Matrix4f.scale(scale,modelMatrix,modelMatrix);
}

每个方法都可以正常工作,但是当我一起调用它们时 翻译和缩放 光源位置无限增加

这是每个帧中每个光源的模型矩阵

    2.899367 0.0 0.0 -5.0
    0.0 2.899367 0.0 0.4
    0.0 0.0 2.899367 -6.0
    0.0 0.0 0.0 1.0

    2.899367 0.0 0.0 23.993671
    0.0 2.899367 0.0 0.39999998
    0.0 0.0 2.899367 -6.0
    0.0 0.0 0.0 1.0

    3.6717935 0.0 0.0 -5.0
    0.0 3.6717935 0.0 0.39999998
    0.0 0.0 3.6717935 22.993671
    0.0 0.0 0.0 1.0

    4.380832 0.0 0.0 31.717934
    0.0 4.380832 0.0 0.39999998
    0.0 0.0 4.380832 22.993671
    0.0 0.0 0.0 1.0

    2.899367 0.0 0.0 -12.090389
    0.0 2.899367 0.0 0.39999998
    0.0 0.0 2.899367 -20.81465
    0.0 0.0 0.0 1.0

4.380832 0.0 0.0 -1549.4392
0.0 4.380832 0.0 0.39999998
0.0 0.0 4.380832 -3280.6797
0.0 0.0 0.0 1.0

2.899367 0.0 0.0 -1593.2476
0.0 2.899367 0.0 0.39999998
0.0 0.0 2.899367 -3324.488
0.0 0.0 0.0 1.0

2.899367 0.0 0.0 -1564.2539
0.0 2.899367 0.0 0.39999998
0.0 0.0 2.899367 -3324.488
0.0 0.0 0.0 1.0

3.6717935 0.0 0.0 -1593.2476
0.0 3.6717935 0.0 0.39999998
0.0 0.0 3.6717935 -3295.4944
0.0 0.0 0.0 1.0

4.380832 0.0 0.0 -1556.5295
0.0 4.380832 0.0 0.39999998
0.0 0.0 4.380832 -3295.4944
0.0 0.0 0.0 1.0

2.899367 0.0 0.0 -1600.3379
0.0 2.899367 0.0 0.39999998
0.0 0.0 2.899367 -3339.3027
0.0 0.0 0.0 1.0

最右列是翻译向量,其值变化很快。

如果我为每个光源使用单独的模型矩阵,它可以正常工作,但我想节省内存并仅对一个Matrix对象执行操作。

想法是撤消先前帧的平移和缩放,并应用新的变换

关于如何在一个矩阵上执行此操作的任何想法?旋转也必须保留

0 个答案:

没有答案
相关问题