【翻译】广告牌技术教程:球体广告牌

本文介绍了球体广告牌技术,这是一种使3D物体始终面向摄像机的方法。文章详细解释了如何通过两次旋转操作实现这一效果,并提供了具体的实现代码。

The spherical version is a simple extension to the cylindrical case. After the object is rotated using the cylindrical approach, all that is left to do is to tilt the object until it truly faces the camera.

球体版本的广告牌是圆柱体广告牌的一个简单扩展。在使用圆柱广告牌技术将物体旋转之后,所剩下的工作就是使物体倾斜以达到真正面对照相机的效果。

The axis of rotation is the right vector. As for the angle, a cosine can be obtained by the inner product between objToCamProj and objToCam.

旋转轴是右向量。至于这个角度嘛,可以用objTocamProj向量和objToCam向量的内积来求出角度的cosine值。

As in the previous billboards a function can be defined to setup the billboard. The code is as follows:
正如先前的广告牌那样,可以定义一个函数来生成广告牌。代码如下:

 

 1 void billboardSphericalBegin(float camX, float camY, float camZ,float objPosX, float objPosY, float objPosZ) {
 2 
 3     float lookAt[3],objToCamProj[3], objToCam[3], upAux[3];
 4     float modelview[16],angleCosine;
 5 
 6     glPushMatrix();
 7 
 8 // objToCamProj is the vector in world coordinates from the 
 9 // local origin to the camera projected in the XZ plane
10     objToCamProj[0] = camX - objPosX ;
11     objToCamProj[1] = 0;
12     objToCamProj[2] = camZ - objPosZ ;
13 
14 // This is the original lookAt vector for the object 
15 // in world coordinates
16     lookAt[0] = 0;
17     lookAt[1] = 0;
18     lookAt[2] = 1;
19 
20 
21 // normalize both vectors to get the cosine directly afterwards
22     mathsNormalize(objToCamProj);
23 
24 // easy fix to determine wether the angle is negative or positive
25 // for positive angles upAux will be a vector pointing in the 
26 // positive y direction, otherwise upAux will point downwards
27 // effectively reversing the rotation.
28 
29     mathsCrossProduct(upAux,lookAt,objToCamProj);
30 
31 // compute the angle
32     angleCosine = mathsInnerProduct(lookAt,objToCamProj);
33 
34 // perform the rotation. The if statement is used for stability reasons
35 // if the lookAt and objToCamProj vectors are too close together then 
36 // |angleCosine| could be bigger than 1 due to lack of precision
37    if ((angleCosine < 0.99990) && (angleCosine > -0.9999))
38       glRotatef(acos(angleCosine)*180/3.14,upAux[0], upAux[1], upAux[2]);    
39       
40 // so far it is just like the cylindrical billboard. The code for the 
41 // second rotation comes now
42 // The second part tilts the object so that it faces the camera
43 
44 // objToCam is the vector in world coordinates from 
45 // the local origin to the camera
46     objToCam[0] = camX - objPosX;
47     objToCam[1] = camY - objPosY;
48     objToCam[2] = camZ - objPosZ;
49 
50 // Normalize to get the cosine afterwards
51     mathsNormalize(objToCam);
52 
53 // Compute the angle between objToCamProj and objToCam, 
54 //i.e. compute the required angle for the lookup vector
55 
56     angleCosine = mathsInnerProduct(objToCamProj,objToCam);
57 
58 
59 // Tilt the object. The test is done to prevent instability 
60 // when objToCam and objToCamProj have a very small
61 // angle between them
62 
63     if ((angleCosine < 0.99990) && (angleCosine > -0.9999))
64         if (objToCam[1] < 0)
65             glRotatef(acos(angleCosine)*180/3.14,1,0,0);    
66         else
67             glRotatef(acos(angleCosine)*180/3.14,-1,0,0);    
68       
69 }

 

The function billboardEnd() should be called after rendering the object as shown in the following example.

函数billboardEnd()应该在渲染完物体后被调用,正如我们再先前的那个例子中做的。

 

1 billboardCylindricalBegin(cx,cy,cz,ox,oy,oz);
2     drawObject();
3 billboardEnd();

 

 

 

 

转载于:https://www.cnblogs.com/huster-zj/archive/2013/04/13/billboar_Spherical.html

先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值