如何在opengl中计算glTranslatef函数的移位参数 [英] How to calculate the shift parameters of glTranslatef function in opengl

查看:144
本文介绍了如何在opengl中计算glTranslatef函数的移位参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opengl的新手,并对如何计算glTranslatef的参数感到困惑. 这是我的显示代码

I am a newbie to opengl and confused about how to caculate the parameters of glTranslatef. Here is my display code

void display()
{


    glPushMatrix();
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);   // background

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    glScalef(1.f,1.f,1.f);
    glTranslatef(2.f,0.f,0);


    glBegin(GL_QUADS);
    glVertex2f(-1.0f,-1.0f);
    glTexCoord2f(0.0f,1.0f); 

    glVertex2f(1.0f,-1.0f);
    glTexCoord2f(0.0f,0.0f); 

    glVertex2f(1.0f,1.0f);
    glTexCoord2f(1.0f,0.0f); 

    glVertex2f(-1.0f,1.0f);
    glTexCoord2f(1.0f,1.0f); 

    glEnd();  
    //here is my drawing code,ignore it
    runCuda();
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);

    glBindTexture(GL_TEXTURE_2D, textureID);

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 768,/*window_width, window_height,*/ 
        GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glPopMatrix();
    glFlush();
}

这是我的初始gl代码

 bool initGL(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    glutInitWindowSize(window_width, window_height);
    glutCreateWindow("Cuda GL Interop Demo (adapted from NVDIA's simpleGL)");

    glutDisplayFunc(fpsDisplay);

    glewInit();
    if(!glewIsSupported("GL_VERSION_2_0"))
    {
        fprintf(stderr, "ERROR: Support for necessary OpengGL extensions missing.");
        return false;
    }

    glViewport(0, 0, window_width, window_height);  

    glClearColor(0.0, 0.0, 0.0, 1.0);  
    glDisable(GL_DEPTH_TEST);  

    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  

    glMatrixMode(GL_PROJECTION); // Add this line
    glLoadIdentity(); 

    gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);//, -1.0f, 1.0f);  


    return true;
    }

问题是,当nScale = 1.f时,我们应用gltranslatef(1.f,0,0)在窗口中间向左移动,而gltranslatef(2.f,0,0)进行移动图片移出窗口(2.f是将图片移出窗口的最小移位值).但是如果我将nScale更改为2.f,我们需要将x_shift更改为1.5f(gltranslatef(1.5f,0,0 ))要移动窗口的图片,或将nScale更改为4.f,我们需要将x_shift更改为1.25f才能移动窗口的图片.我对gltranslate函数中的x_shift(或y_shift)感到困惑.看来我们需要将shift值更改为(1 + 1/nScale ^ 2),是正确的吗?如何获得该方程式?希望有人可以帮助我. 这是原始图片:

The problem is that,When nScale = 1.f,we apply gltranslatef(1.f,0,0) to move leftside at the middle of the window, and gltranslatef(2.f,0,0) to move the picture out of window(2.f is the munimum shift value to move picture out of window).But if I change the nScale to 2.f ,we need change to x_shift to 1.5f(gltranslatef(1.5f,0,0))to move the picture of the window ,or change the nScale to 4.f ,we need change to x_shift to 1.25f to move the picture of the window.I am confused about x_shift(or y_shift) in gltranslate function. It looks that we need change the shift value to (1 + 1/nScale^2),is that correct?How to get that equation?I hope someone can help me. here is the original pic:

这是应用于(glScalef(1.f,1.f,1.f),glTranslatef(1.f,0.f,0)的图片)

here is the pic applied to (glScalef(1.f,1.f,1.f),glTranslatef(1.f,0.f,0))

这是应用于(glScalef(1.f,1.f,1.f),glTranslatef(2.f,0.f,0))的图片,该图片正好移出了窗口

here is the pic applied to (glScalef(1.f,1.f,1.f),glTranslatef(2.f,0.f,0)),which is moved out of window just right

推荐答案

OpenGL中的转换按照指定顺序的反向顺序进行应用.换句话说,最后一个转换是指定的将首先应用于顶点.

Transformations in OpenGL are applied in the reverse order of the order they are specified in. In other words, the last transformation that is specified is applied to the vertices first.

在这种情况下,代码的编写方式(用于说明的值不同):

In this specific case, the way the code is written (with different values for illustration):

glScalef(1.5f, 1.5f, 1.5f);
glTranslatef(2.0f, 0.0f, 0.0f);

这首先将所有顶点在x方向上平移2.0.然后将整个对象缩放1.5.由于第一步将图像的原始中心转换为(2.0,0.0,0.0),因此缩放1.5后的图像中心现在为(3.0,0.0,0.0).

This first translates all your vertices by 2.0 in the x-direction. And then scales the whole thing by 1.5. Since the original center of the image was translated to (2.0, 0.0, 0.0) in the first step, the center of the image after scaling by 1.5 is now at (3.0, 0.0, 0.0).

要将平移应用于已缩放的图像,需要调换这些调用的顺序:

To apply the translation to the already scaled image, the order of these calls needs to be reversed:

glTranslatef(2.0f, 0.0f, 0.0f);
glScalef(1.5f, 1.5f, 1.5f);

现在缩放将首先应用于顶点,然后将其平移.经过这两次变换后,图像的中心将位于(2.0,0.0,0.0).

Now the scaling will be applied to your vertices first, and then they will be translated. The center of the image after these two transformations will be at (2.0, 0.0, 0.0).

这篇关于如何在opengl中计算glTranslatef函数的移位参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆