0
\$\begingroup\$

I am trying to apply this texture as a cubemap. This is my result:updated image with corrected texture coordinates

Clearly I am doing something with my texture coordinates, but I cannot for the life of me figure out what. I don't even see a pattern to the texture fragments. They just seem like a jumble of different faces. Can anyone shed some light on this?

Vertex shader:

#version 400
in  vec4 vPosition;
in  vec3 inTexCoord;

smooth out vec3 texCoord;

uniform mat4 projMatrix;

void main()
{
   texCoord = inTexCoord;
   gl_Position = projMatrix * vPosition;
}

My fragment shader:

#version 400
smooth in vec3 texCoord;

out vec4 fColor;

uniform samplerCube textures

void main()
{
           fColor = texture(textures,texCoord);
}

Vertices of cube:

point4 worldVerts[8] = {
    vec4(  15,  15,  15, 1 ),
    vec4( -15,  15,  15, 1 ),
    vec4( -15,  15, -15, 1 ),
    vec4(  15,  15, -15, 1 ),
    vec4( -15, -15,  15, 1 ),
    vec4(  15, -15,  15, 1 ),
    vec4(  15, -15, -15, 1 ),
    vec4( -15, -15, -15, 1 )
};

Cube rendering:

void
worldCube(point4* verts, int& Index, point4* points, vec3* texVerts)
{

    quadInv( verts[0], verts[1], verts[2], verts[3], 1, Index, points, texVerts);
    quadInv( verts[6], verts[3], verts[2], verts[7], 2, Index, points, texVerts);
    quadInv( verts[4], verts[5], verts[6], verts[7], 3, Index, points, texVerts);
    quadInv( verts[4], verts[1], verts[0], verts[5], 4, Index, points, texVerts);
    quadInv( verts[5], verts[0], verts[3], verts[6], 5, Index, points, texVerts);
    quadInv( verts[4], verts[7], verts[2], verts[1], 6, Index, points, texVerts);
}

Backface function (since this is the inside of the cube):

void quadInv( const point4& a, const point4& b, const point4& c, const point4& d , 
      int& Index, point4* points, vec3* texVerts)

{
    quad( a, d, c, b, Index, points, texVerts, a.to_3(), d.to_3(), c.to_3(), b.to_3());
}

And the quad drawing function:

void quad( const point4& a, const point4& b, const point4& c, const point4& d,
      int& Index, point4* points, vec3* texVerts, const vec3& tex_a, const vec3& tex_b, const vec3& tex_c, const vec3& tex_d)

{
    texVerts[Index] = tex_a.normalized(); points[Index] = a; Index++;
    texVerts[Index] = tex_b.normalized(); points[Index] = b; Index++;
    texVerts[Index] = tex_c.normalized(); points[Index] = c; Index++;
    texVerts[Index] = tex_a.normalized(); points[Index] = a; Index++;
    texVerts[Index] = tex_c.normalized(); points[Index] = c; Index++;
    texVerts[Index] = tex_d.normalized(); points[Index] = d; Index++;
}

Edit: I forgot to mention, in the image, the camera is pointed directly at the back face of the cube. You can kind of see the diagonals leading out of the corners, if you squint.

I tried writing the texture coordinates as colors as Nathan Reed suggested, and still got random values from the fragment shader, so I tried replacing all texture coordinates with (0,1,0) (so as colors, all faces should be green). Instead, I got this: test image

Old result with b and d vertices swapped in texture: Rendered

\$\endgroup\$
  • \$\begingroup\$ Please post your vertex shader too \$\endgroup\$ – Maximus Minimus Oct 29 '13 at 23:17
  • \$\begingroup\$ Vertex shader now included. \$\endgroup\$ – shade4159 Oct 29 '13 at 23:28
  • 2
    \$\begingroup\$ When calling quad you've got a, d, c, b for the points but a, b, c, d for the texture coordinates. That looks suspicious. \$\endgroup\$ – Nathan Reed Oct 29 '13 at 23:29
  • \$\begingroup\$ Whoops! Thanks, fixed that, updated code and resulting image. \$\endgroup\$ – shade4159 Oct 30 '13 at 11:54
  • \$\begingroup\$ You might try editing your pixel shader to write fColor = normalize(texCoord) * 0.5 + 0.5; - this will visualize the vectors directly as RGB colors, so it should be easier for you to see what's going wrong. E.g. the +X side of the cube should have a high red component and the -X side should not, etc. \$\endgroup\$ – Nathan Reed Oct 30 '13 at 18:25
1
\$\begingroup\$

Ok, I figured it out! glVertexAttribPointer() needs size set to 4 for cubemaps, even though only 3 coordinates are passed.

Thank you to everyone who helped me on this. I have to say I never would have thought of writing the texture coordinates as color data!

\$\endgroup\$
  • 1
    \$\begingroup\$ Glad you got it working! Viewing vectors as colors is a quite handy trick for shader debugging, useful in all sorts of situations. And the size-4 thing is probably related to the memory layout of your vectors - I suppose vec3 is 16 bytes long instead of 12. \$\endgroup\$ – Nathan Reed Oct 31 '13 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.