legacy opengl - rendering cube map layout, understanding glTexCoord3f parameters

纵饮孤独 提交于 2019-12-10 19:06:02

问题


I would like to render a cube map layout (orthographic projection) - something like this. Let's focus on the +X face (right on the picture). The code which renders +X face looks this way:

glBegin(GL_QUADS);

glTexCoord3f(1, 1, -1);
glVertex2i(0,0);

glTexCoord3f(1, 1, 1);
glVertex2i(256,0);

glTexCoord3f(1, -1, 1);
glVertex2i(256, 256);

glTexCoord3f(1, -1, -1);
glVertex2i(0, 256);

glEnd();

It looks like that glTexCoord3f uses ndc values, but I don't understand why y value is positive for the first two vertices and negative for the next, from my understanding it should be opposite ( I imagine that I observe cube faces from the cube center).


回答1:


No glTexCoord3D does not use NDC !!! its a direction vector instead.

I was struggling in the past with the same ... GL_TEXTURE_CUBE_MAP are hard to start with ...

To make things easier here is mine render in old API (C++ member from mine engine) for those:

void OpenGL_TXR::cube_draw_CW(double size,int unit)
    {
    int i,j;
    double  a=size;
    double  pnt[8][3]=
        {
        +a,-a,+a,
        +a,+a,+a,
        -a,+a,+a,
        -a,-a,+a,
        +a,-a,-a,
        +a,+a,-a,
        -a,+a,-a,
        -a,-a,-a
        };
    int     tab[24]=
        {
        0,1,2,3,
        7,6,5,4,
        4,5,1,0,
        5,6,2,1,
        6,7,3,2,
        7,4,0,3
        };
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    for (i=23;i>=0;i--)
        {
        j=tab[i];
        glMultiTexCoord3dv(GL_TEXTURE0+unit,pnt[j]);
        glVertex3dv(pnt[j]);
        }
    glEnd();
    }

size is the cube half size and unit is texture unit where is your cube map binded. But it renders a textured cube. If you want to render your layout then just use different vertexes but the same texture coords. Something like this:

void cube_draw2D_CW(double size,int unit)
    {
    int i,j;
    //   U
    // W N E S
    //   D
    const double a=size,a0=-3.0*a,a1=a0+a+a,a2=a1+a+a,a3=a2+a+a;
    const double b=1.7320508075688772935274463415059; // sqrt(3.0)
    double  pnttxr[8][3]=
        {
        +b,-b,+b,
        +b,+b,+b,
        -b,+b,+b,
        -b,-b,+b,
        +b,-b,-b,
        +b,+b,-b,
        -b,+b,-b,
        -b,-b,-b
        };
    double  pntver[24][3]=
        {
        a1+a,a0+a-a,+0.0,
        a1+a,a0+a+a,+0.0,
        a1-a,a0+a+a,+0.0,
        a1-a,a0+a-a,+0.0,

        a1+a,a2+a-a,+0.0,
        a1+a,a2+a+a,+0.0,
        a1-a,a2+a+a,+0.0,
        a1-a,a2+a-a,+0.0,

        a0+a,a1+a-a,+0.0,
        a0+a,a1+a+a,+0.0,
        a0-a,a1+a+a,+0.0,
        a0-a,a1+a-a,+0.0,

        a1+a,a1+a-a,+0.0,
        a1+a,a1+a+a,+0.0,
        a1-a,a1+a+a,+0.0,
        a1-a,a1+a-a,+0.0,

        a2+a,a1+a-a,+0.0,
        a2+a,a1+a+a,+0.0,
        a2-a,a1+a+a,+0.0,
        a2-a,a1+a-a,+0.0,

        a3+a,a1+a-a,+0.0,
        a3+a,a1+a+a,+0.0,
        a3-a,a1+a+a,+0.0,
        a3-a,a1+a-a,+0.0,
        };
    int     tabtxr[24]=
        {
        4,0,3,7,    // D
        1,5,6,2,    // U
        3,2,6,7,    // W
        0,1,2,3,    // N
        4,5,1,0,    // E
        7,6,5,4,    // S
        };
    int     tabver[24]=
        {
        0,1,2,3,
        4,5,6,7,
        8,9,10,11,
        12,13,14,15,
        16,17,18,19,
        20,21,22,23,
        };
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    for (i=23;i>=0;i--)
        {
        j=tabtxr[i];
        glMultiTexCoord3dv(GL_TEXTURE0+unit,pnttxr[j]);
        j=tabver[i];
        glVertex3dv(pntver[j]);
        }
    glEnd();
    }

Here preview:



来源:https://stackoverflow.com/questions/58125554/legacy-opengl-rendering-cube-map-layout-understanding-gltexcoord3f-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!