运用到翻译到整个OpenGL ES的场景 - iPhone场景、OpenGL、ES、iPhone

2023-09-08 10:38:59 作者:弥漫

我有一个OpenGL ES的场景,它是由约20个对象。在每个对象的渲染方法,我有code的缩放,旋转和位置(使用glmultmatrix)在每个场景中的正确位置的对象(见下文code)。

我的问题是我怎么能那么应用转型,整个场景作为一个整体?例如规模/ 2放大到整个场景?

  glPushMatrix();
glLoadIdentity();

//将某些对象。

如果(hasAnimations){

    的glTranslatef(kBuildingOffset);
    //规模
    glScalef(kModelScale);

    // glMultMatrixf(testAnimation);

    zRotation = kBuildingzRotation
    xRotation = kBuildingxRotation
    yRotation = kBuildingyRotation
    glRotatef(yRotation,0.0,1.0,0.0);
    glRotatef(xRotation,1.0F,0.0,0.0);
    glRotatef(zRotation,0.0,0.0,1.0F);

    //的NSLog(@动画帧为%d,animationFrame);
    //的NSLog(@矩阵的计算IS%f,采用animationArray [0] [0]);
    glMultMatrixf(animationArray [animationFrame]);
    // glMultMatrixf(matricesArray);
    glMultMatrixf(matricePivotArray);

    // glMultMatrixf(testAnimation);
}

//首先旋转我们的对象是必需的。

如果([对象​​名isEqualToString:@movingobject1]){

    的glTranslatef(kFan1Position);
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    的glTranslatef(0,0,0);

    zRotation + = kFanRotateSpeed​​;
    yRotation = kyFanFlip;
    xRotation = kxFanRotation;
    glRotatef(zRotation,0.0,0.0,1.0F);
    glRotatef(yRotation,0.0,1.0F,0.0);
    glRotatef(xRotation,1.0F,0.0,0.0);

    的glTranslatef(0.0,0.0,-300);

}

如果([对象​​名isEqualToString:@movingobject2]){

    的glTranslatef(kFan2Position);
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    的glTranslatef(0,0,0);

    zRotation + = kFanRotateSpeed​​;
    yRotation = kyFanFlip;
    xRotation = kxFanRotation;
    glRotatef(-kFan3YOffset,0.0,1.0,0.0);
    glRotatef(zRotation,0.0,0.0,1.0F);
    glRotatef(yRotation,0.0,1.0F,0.0);
    glRotatef(xRotation,1.0F,0.0,0.0);
    glRotatef(kFan3YOffset,0.0,1.0F,0.0);

    的glTranslatef(0.0,0.0,-300);

}

如果([对象​​名isEqualToString:@movingobject3]){

    的glTranslatef(kFan3Position);
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    的glTranslatef(0,0,0);

    zRotation + = kFanRotateSpeed​​;
    yRotation = kyFanFlip;
    xRotation = kxFanRotation;
    glRotatef(-kFan2YOffSet,0.0,1.0,0.0);
    glRotatef(zRotation,0.0,0.0,1.0F);
    glRotatef(yRotation,0.0,1.0F,0.0);
    glRotatef(xRotation,1.0F,0.0,0.0);
    glRotatef(kFan2YOffSet,0.0,1.0,0.0);

    的glTranslatef(0.0,0.0,-300);

}

//然后放置场景中的对象的其余部分。

如果([对象​​名isEqualToString:@movingobject1]!)
    如果([对象​​名isEqualToString:@movingobject2]!)
        如果([对象​​名isEqualToString:@movingobject3]!)
            如果(!hasAnimations){

    glLoadIdentity();
    的glTranslatef(kBuildingOffset);

//规模
    glScalef(kModelScale);

    zRotation = kBuildingzRotation
    xRotation = kBuildingxRotation
    yRotation = kBuildingyRotation
    glRotatef(yRotation,0.0,1.0,0.0);
    glRotatef(xRotation,1.0F,0.0,0.0);
    glRotatef(zRotation,0.0,0.0,1.0F);

如果([矩阵数]!= 0){
    glMultMatrixf(matricesArray);
}

如果(hasPivotNode){
    glMultMatrixf(matricePivotArray);
}
}

[网渲染]
glPopMatrix();
//恢复矩阵
 

解决方案

您应该能够通过推动变换矩阵,你的愿望到矩阵堆栈,你做任何你的对象,specifc变换之前实现这一点很容易不够,但随后的不的每次按下另一个矩阵压入堆栈时加载单位矩阵。实事求是地讲,这将改变所有后续矩阵运算。这是基本的模式...

  //按单位矩阵在栈底...
glPushMatrix();
glLoadIdentity();

//现在它的规模,因此,所有后续的变换会
//放大2倍。
glScalef(2.F,2.F,2.F);

的foreach(目){

    glPushMatrix();
    // glLoadIdentity();这将删除规模以上设置。
    glDoABunchOfTransforms();
    [网渲染]
    glPopMatrix();
}
 
最权威测试 iPhone SE和6s的差别

I have an OpenGL ES scene which is made up of about 20 objects. In the render method for each object I have code which scales, rotates and positions (using glmultmatrix) that object in the correct place in each scene (see code below).

My question is how can I then apply a transformation to the entire scene as a whole ? E.g scale / enlarge the entire scene by 2 ?

glPushMatrix();
glLoadIdentity();

//Move some objects.

if (hasAnimations) {

    glTranslatef(kBuildingOffset);
    //scale
    glScalef(kModelScale);

    //glMultMatrixf(testAnimation);

    zRotation = kBuildingzRotation
    xRotation = kBuildingxRotation
    yRotation = kBuildingyRotation
    glRotatef(yRotation, 0.0f, 1.0, 0.0f);
    glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
    glRotatef(zRotation, 0.0f, 0.0f, 1.0f);

    //NSLog(@"ANIMATION FRAME IS %d", animationFrame);
    //NSLog(@"MATRICE IS %f", animationArray[0][0]);
    glMultMatrixf(animationArray[animationFrame]);
    //glMultMatrixf(matricesArray);
    glMultMatrixf(matricePivotArray);

    //glMultMatrixf(testAnimation);
}

//First rotate our objects as required.

if ([objectName isEqualToString:@"movingobject1"]) {

    glTranslatef(kFan1Position); 
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    glTranslatef(0, 0, 0);

    zRotation +=kFanRotateSpeed;
    yRotation =kyFanFlip;
    xRotation = kxFanRotation;
    glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
    glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
    glRotatef(xRotation, 1.0f, 0.0, 0.0f);

    glTranslatef(0.0, 0.0, -300);

}

if ([objectName isEqualToString:@"movingobject2"]) {

    glTranslatef(kFan2Position);
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    glTranslatef(0, 0, 0);

    zRotation +=kFanRotateSpeed;
    yRotation = kyFanFlip;
    xRotation = kxFanRotation;
    glRotatef(-kFan3YOffset, 0.0, 1.0, 0.0);
    glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
    glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
    glRotatef(xRotation, 1.0f, 0.0, 0.0f);
    glRotatef(kFan3YOffset, 0.0f, 1.0f, 0.0f);

    glTranslatef(0.0, 0.0, -300);

}

if ([objectName isEqualToString:@"movingobject3"]) {

    glTranslatef(kFan3Position); 
    glScalef(kModelScale);
    glMultMatrixf(matricesArray);

    glTranslatef(0, 0, 0);

    zRotation +=kFanRotateSpeed;
    yRotation =kyFanFlip;
    xRotation =kxFanRotation;
    glRotatef(-kFan2YOffSet, 0.0, 1.0, 0.0);
    glRotatef(zRotation, 0.0f, 0.0f, 1.0f);
    glRotatef(yRotation, 0.0f, 1.0f, 0.0f);
    glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
    glRotatef(kFan2YOffSet, 0.0, 1.0, 0.0);

    glTranslatef(0.0, 0.0, -300);

}

//Then position the rest of the scene objects.

if (![objectName isEqualToString:@"movingobject1"])
    if (![objectName isEqualToString:@"movingobject2"])
        if(![objectName isEqualToString:@"movingobject3"])
            if (!hasAnimations) {

    glLoadIdentity();
    glTranslatef(kBuildingOffset);

//scale
    glScalef(kModelScale);

    zRotation = kBuildingzRotation
    xRotation = kBuildingxRotation
    yRotation = kBuildingyRotation
    glRotatef(yRotation, 0.0f, 1.0, 0.0f);
    glRotatef(xRotation, 1.0f, 0.0f, 0.0f);
    glRotatef(zRotation, 0.0f, 0.0f, 1.0f);

if ([Matrices count]!=0) {
    glMultMatrixf(matricesArray);
}

if (hasPivotNode) {
    glMultMatrixf(matricePivotArray);
}
}

[mesh render];
glPopMatrix();
//restore the matrix

解决方案

You should be able to achieve this easily enough by pushing the transform matrix you desire on to the matrix stack before you do any of your object-specifc transforms, but then don't load the identity matrix each time you push another matrix onto the stack. Practically speaking, this will transform all subsequent matrix operations. This is the basic pattern...

// Push an identity matrix on the bottom of the stack...
glPushMatrix();
glLoadIdentity();

// Now scale it, so all subsequent transforms will be
// scaled up 2x.
glScalef(2.f, 2.f, 2.f);

foreach(mesh) {

    glPushMatrix();
    //glLoadIdentity(); This will erase the scale set above.
    glDoABunchOfTransforms();
    [mesh render];
    glPopMatrix();
}