简体   繁体   English

OpenGl翻译

[英]OpenGl translation

I'm trying to translate a triangle up to a fixed quad in a loop and don't know why my loop is running one less than the condition I've applied to run the loop . 我试图在循环中将一个三角形平移到一个固定的四边形,但不知道为什么我的循环比运行循环所应用的条件少运行一个。 And the thing I couldn't understand that I have given a condition to translate triangle within the loop but after the condition satisfies for translation , if there are some more iterations left for the loop but no more translating condition satisfying , triangle comes back to its original position and I'm expecting triangle to its translated position. 我无法理解的是,我已经给出了在循环内平移三角形的条件,但是在条件满足平移之后,如果循环还有更多迭代但又不满足平移条件,则三角形回到其平缓状态原始位置,我希望三角形达到其平移位置。

Ps: I have to apply for multiple objects but 1st I'm only applying for a triangle. 附:我必须申请多个对象,但是第一,我只申请一个三角形。

Here is the code of display function. 这是显示功能的代码。

void display(void){

float tr;

for (tr = 0.0; tr<=0.8; tr += 0.1)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
    glPushMatrix();

    Thread::Sleep(100);

    cout <<tr<<endl;    // checking the loop variable values

    glBegin(GL_QUADS);  // making a fixed quad

    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f(-0.5f, 0.0f, 0.0f);


    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f(0.5f, 0.0f, 0.0f);

     glColor3f(1.0f, 1.0f, 0.0f);
     glVertex3f(0.5f, -1.0f, 0.0f);

     glColor3f(1.0f, 1.0f, 0.0f);
     glVertex3f(-0.5f, -1.0f, 0.0f);
     glEnd();

         if (tr<=0.5)    //condition to translate the triangle 
         glTranslatef(0.0,-tr, 0.0);


     glBegin(GL_TRIANGLES); //Triangle to be translated

     glColor3f(0.2f, 0.6f, 0.2f);
     glVertex3f(-0.5f, 0.5f, 0.0f);


     glColor3f(0.2f, 0.6f, 0.2f);
     glVertex3f(0.5f, 0.5f, 0.0f);

     glColor3f(0.2f, 0.6f, 0.2f);
     glVertex3f(0.0f, 1.0f, 0.0f);
     glEnd();

     glPopMatrix();


    glFlush();
}
}

Sounds from the comments like you got your code working, but I believe the real problem was elsewhere, and not related to OpenGL at all. 从注释中听起来好像您的代码可以正常工作,但是我认为真正的问题出在其他地方,根本与OpenGL不相关。 It has to do with floating point precision. 它与浮点精度有关。 The issue is with this loop: 问题在于此循环:

for (tr = 0.0; tr<=0.8; tr += 0.1)

Floating point values are represented in a binary format ( IEE 754 on most computers), with limited precision. 浮点值以二进制格式表示(大多数计算机上为IEE 754 ),精度有限。 While values like 0.1 and 0.8 can be represented precisely in a decimal floating point format, they cannot be precisely represented in the binary format your computer uses. 虽然像0.1和0.8这样的值可以用十进制浮点数格式精确表示,但是它们不能以计算机使用的二进制格式精确表示。 Which means in your case that starting with 0.0 and adding 0.1 eight times will not result in exactly 0.8. 这意味着在您的情况下,以0.0开头并加0.1八次将不等于0.8。 I printed out the result with high precision for your loop, and it exits with a value of 0.80000007, one iteration earlier than you expected because that value is not <= the approximation of 0.8 anymore. 我为您的循环高精度地打印了结果,并以0.80000007的值退出,比您预期的早了一个迭代,因为该值不再等于0.8。

The robust way of doing what you intended is with a loop that looks like this: 做您想要的目标的强大方法是使用一个看起来像这样的循环:

for (int i = 0; i <= 8; ++i)
{
    float tr = i * 0.1f;

One minor item that is not causing your problem, but is very much related: You are mixing float and double precision values in your code. 一项不会引起您的问题的小项目,但与之密切相关:您在代码中混合了float和double精度值。 The constants 0.1 and 0.8 in your code are double values, while your variable is a float. 代码中的常数0.10.8是双精度值,而变量是浮点型。 If you mix float and double, the operations will be executed in double. 如果混合使用float和double,则将以double形式执行操作。 For example, if you write tr += 0.1 , tr will first be converted to a double, then 0.1 added in double precision, and the result rounded back to a float. 例如,如果您编写tr += 0.1 ,则tr将首先转换为双精度,然后以双精度加0.1,然后将结果四舍五入为浮点型。 While there's no direct damage, this is inefficient and wasteful. 虽然没有直接的损坏,但这是低效且浪费的。 When operating with floats, it's generally much better to use float constants, which would be 0.1f and 0.8f in this case. 当使用浮点运算时,通常最好使用浮点常量,在这种情况下,其0.8f 0.1f0.8f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM