Cube texturing using cube map

Started by kiwhen, September 09, 2014, 18:47:11

Previous topic - Next topic

kiwhen

I've been updating my game project to work with the latest stuff in LWJGL, which among other things means moving to VAOs and VBOs. I've been doing a lot of reading on this, and I'm well on my way to getting stuff operational.

I am currently using the interleaved VBO-technique described in the tutorials on the wiki, and I have my test cube set up like this:
Random r = new Random();

vertices[0] = new Vertex().setPosition(-0.5f, 0.5f, 0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(-0.5f, 0.5f, 0.5f).calculate();
vertices[1] = new Vertex().setPosition(-0.5f, -0.5f, 0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(-0.5f, -0.5f, 0.5f).calculate();
vertices[2] = new Vertex().setPosition(0.5f, -0.5f, 0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(0.5f, -0.5f, 0.5f).calculate();
vertices[3] = new Vertex().setPosition(0.5f, 0.5f, 0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(0.5f, 0.5f, 0.5f).calculate();

vertices[4] = new Vertex().setPosition(-0.5f, 0.5f, -0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(-0.5f, 0.5f, -0.5f).calculate();
vertices[5] = new Vertex().setPosition(-0.5f, -0.5f, -0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(-0.5f, -0.5f, -0.5f).calculate();
vertices[6] = new Vertex().setPosition(0.5f, -0.5f, -0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(0.5f, -0.5f, -0.5f).calculate();
vertices[7] = new Vertex().setPosition(0.5f, 0.5f, -0.5f).setColor(r.nextFloat(), r.nextFloat(), r.nextFloat()).setCoordinates(0.5f, 0.5f, -0.5f).calculate();


This piece of code basically gives me the equalient of the TexturedVertex class in the tutorials. Eight points to form a cube (with random colors). There are three components; position, color and texture coordinates. The latter part is what I'm struggeling with. I read somewhere that texture coordinates for cube maps are given as vectors pointing to the corners of the cube. That didn't really make much sense to me, but it may be because I have misunderstood where the vector is supposed to be pointing from. In the above code, I've basically written the stuff as I did for position, making it a vector from the center of the cube. If this is correct, then the coordinates would probably always be the same as the "vector" for position, and that doesn't sound quite right.

The next part is setting up the actual texture. I load it from a PNG file, sort of like the tutorial, and then I go like this:
int id = GL11.glGenTextures();

GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, id);

GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL12.GL_TEXTURE_WRAP_R, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
GL11.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

GL30.glGenerateMipmap(GL13.GL_TEXTURE_CUBE_MAP);
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);


The "buffer" is of course the image buffer, and the "image" object is a BufferedImage. This method works well for 2D textures, but not for cube maps. Allthough, the problem might not be here. It's more likely to be found in my shader:
out_Color = texture(texture_diffuse, pass_TextureCoord);

This is a line from my fragment shader. The vertex shader just pass TextureCoord as a vec3. Color is a vec4 and texture_diffuse is a uniform sampler2D. Again, this works for 2D. When I introduced the texture() method, things took a turn for the worse.

I'm aware that I can set up 6 separate planes to form the cube with separate 2D textures - but that's very ineffective and requires a lot more vertices and indices (overlapping). All tutorials and such I have seen do it that way.

If I remove the line in the shader program, and just do regular coloring instead of going through texture(), this renders a colored cube, so there's nothing wrong with the rest of this stuff. It's just the textures that are causing issues.

Any pointers to set me off in the right direction with this?
Presented in stereo where available.

kiwhen

Aw, man. I had just put in the wrong sampler type in the shader. I had it set to sampler2D - changed to uniform samplerCube, and poof! Fixed.
Presented in stereo where available.