C++ 在openGL中将动画应用于对象
在编写一个使用openGL为形状设置动画的程序时,我偶然发现了一个问题 目前在程序中,我正在创建一些形状,使用以下代码段C++ 在openGL中将动画应用于对象,c++,algorithm,opengl,3d,C++,Algorithm,Opengl,3d,在编写一个使用openGL为形状设置动画的程序时,我偶然发现了一个问题 目前在程序中,我正在创建一些形状,使用以下代码段 for(int i=50;i<=150;i=i+50){ for(int j=50;j<=750;j=j+200){ //Draw rectangle shape at position(j,i); //shape has additional capability for animations } } for(int i=50;i不在CPU上设置几何体的动画,
for(int i=50;i<=150;i=i+50){
for(int j=50;j<=750;j=j+200){
//Draw rectangle shape at position(j,i); //shape has additional capability for animations }
}
for(int i=50;i不在CPU上设置几何体的动画,而是在CPU上设置比例/位置矩阵的动画,并通过MVP矩阵将几何体的变换保留到顶点着色器。对所有矩形使用一个相同的比例矩阵。(或者,如果比例因子在X和Y上不同,则使用两个矩阵)
附:这里有一个例子:
float sc = 0;
void init()
{
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void on_each_frame()
{
// do other things
// draw pulsating rectangles
sc += 0.02;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef((float)sin(sc) + 1.5f);
// draw rectangles as usual, **without** scaling them
glPopMatrix();
// do other things
}
考虑实现一个“DrawableAnimatableObject”,它是一个高级3D对象,能够自行设置动画和绘制,并包含多边形(本例中有多个矩形)作为内部数据。请参阅以下不完整的代码以了解情况:
class DrawableAnimatableObject {
private:
Mesh *mesh;
Vector3 position;
Quaternion orientation;
Vector3 scale;
Matrix transform;
public:
DrawableAnimatableObject();
~DrawableAnimatableObject();
//update the object properties for the next frame.
//it updates the scale, position or orientation of your
//object to suit your animation.
void update();
//Draw the object.
//This function converts scale, orientation and position
//information into proper OpenGL matrices and passes them
//to the shaders prior to drawing the polygons,
//therefore no need to resize the polygons individually.
void draw();
//Standard set-get;
void setPosition(Vector3 p);
Vector3 getPosition();
void setOrientation(Quaternion q);
Quaternion getOrientation();
void setScale(float f);
Vector3 getScale();
};
在这段代码中,网格是包含多边形的数据结构。简单地说,它可以是顶点面列表,也可以是更复杂的结构,如半边。DrawableAnimatableObject::draw()函数应该如下所示:
DrawableAnimatableObject::draw() {
transform = Matrix::CreateTranslation(position) * Matrix::CreateFromQuaternion(orientation) * Matrix::CreateScale(scale);
// in modern openGL this matrix should be passed to shaders.
// in legacy OpenGL you will apply this matrix with:
glPushMatrix();
glMultMatrixf(transform);
glBegin(GL_QUADS);
//...
// Draw your rectangles here.
//...
glEnd();
glPopMatrix();
}
假设opengl中没有这样的常见功能,您必须始终单独管理图形对象的渲染。@Sorceror:看待这个问题的另一种方式是将两个动画应用于某个形状。(例如:一个矩形在Y轴上旋转,而那个已经设置了动画的矩形本身在X轴上进一步旋转。)这与PowerPoint动画类似,PowerPoint动画可以同时应用于对象。从这个方向看,它对如何将两个动画应用于单个对象有帮助吗?如果要将更多动画应用于一个对象,最好的方法是使用矩阵变换,然后可以将任意多个变换应用于一个对象即使是单个对象,只需将它们(变换矩阵)相乘即可..请看我在程序中处理动画的方式是在每个帧中使用两个值,并在绘图中使用这些两个值矩形glRectangle(x,y,tweenedX,tweenedY)当tWeeNeX和TWeeNey,我得到一个缩放的矩形。我没有完全得到你的解决方案。如果你能给这个问题添加更多的细节,那就太好了,因为到现在为止我一直在关注C++而不是OpenGL(在OpenGL中是一个相当的初学者)。