3

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).

Spektre
  • 49,595
  • 11
  • 110
  • 380
Irbis
  • 1,432
  • 1
  • 13
  • 39

1 Answers1

3

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:

preview

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Why do you use sqrt(3) for pnttxr ? Does glMultiTexCoord3dv normalize that vector ? – Irbis Sep 27 '19 at 09:01
  • @Irbis yes Cube maps normalize the vector on their own so you can use `1.0` instead. I put `sqrt(3.0)` in there only because its just constant so no additional computational are made and when I look at the stuff in few years it will hit me its a vector instead of position ... – Spektre Sep 27 '19 at 17:14
  • When I apply coordinates from your example I get inverted y view for each face. I load textures using freeimage lib. The following tutorial also uses the same coordinates as your example: https://learnopengl.com/Advanced-OpenGL/Cubemaps When I use freeimage instead of stb image in the tutorial code I also get inverted y view. What's is going on ? – Irbis Sep 29 '19 at 23:14
  • @Irbis you have most likely different projection or modelview matrix ... you can invert y axis in vertex coordinates (change last `+/-a` to `-/+a` in y axis) to match yours or scale y in modelview by `-1`. It can be also your texture is loaded inverted in comparison to mine... – Spektre Sep 30 '19 at 06:31
  • Please take a look on that: https://stackoverflow.com/questions/11685608/convention-of-faces-in-opengl-cubemapping Know I'm even more confused than before, it looks like your example has fillpped y coordinates. – Irbis Sep 30 '19 at 07:44
  • @Irbis ooow now I see I am using my own texture loaders and there the cube maps are handled slightly differently then for other textures so I most likely not flipping the y axis in my engine ... (you know for images its standard that `(0,0)` is upper left corner but in GL its not ...) never mind you can still flip to match what you need does not matter when (if on load or on render) ... I think you can even flip the `GL_TEXTURE` matrix but not sure if that works for cube maps ... – Spektre Sep 30 '19 at 09:27