Im attempting to texture an .obj file that is not a skybox. So the question is, every tutorial about cubemap creates their hardcoded cubes with vertices ranging from [-1, 1]. Probably not, but is it necessary to scale my .obj model to be in that range?

Also, to map my fragments with the right texel - I saw people online telling that I should take a point p then subtract it with the model center c in the shader. Some said that I should normalize them? normalize(p - c) I hope you can help me deliver a brief explanation.


Do the vertices need to be in the [-1,1] range?

No. This is only done to improve readability of the code. As long as the model you're using is a cube, and the vertices are all outside the near clip plane of the projection, everything will be fine.*

Do I need to use p-c?

No. The direction of the vertex, which is what you use to do the texture lookup, is just the position of the vertex relative to the origin of the model, which is the model space position of the vertex. So to do the texture lookup you can just pass the model space position straight through the vertex shader without any changes directly into the fragment shader, and use the code texture(MY_TEXTURE,MODELSPACE_VERTEX_POSITION);

What about normalize()?

From the OpenGL wiki (emphasis mine):

The texture coordinates for cubemaps are 3D vector directions. These are conceptually directions from the center of the cube to the texel you want to appear. The vectors do not have to be normalized.

So you don't need to use normalize() either, as OpenGL will automatically use a normalized version of the lookup vector when you use texture()


The vertices of the model don't need to be in the [-1,1] range, p-c is equal to the model space vertex position, and you don't need to normalize().

| improve this answer | |
  • \$\begingroup\$ Very great post Samuel. Unfortunately I'm not trying to map a skybox, I'm trying to texture custom .obj file (like a NVidia bunny) with cube mapping. Regardless, your post explains the first part of the question about [-1,1] range. But p - c for custom 3D models is necessary, because program doesn't know where is the center of the model(I can do that by setting c manually no problem, but should I normalize? I guess not looking at skybox example, but maybe I'm missing something) \$\endgroup\$ – Pavels May 26 '17 at 13:18
  • \$\begingroup\$ Ah. I'll fix my answer once I get home, but for now: the p-c would be necessary, that can be done by either using a uniform to specify the centre and actually subtracting, or using viewprojectionvec4(local position,0) to calculate the relative position. Either way, the texture() call will automagically normalize the vector, so you won't have to do any normalization. \$\endgroup\$ – TheTurboTurnip May 26 '17 at 15:05
  • \$\begingroup\$ I can also set the c value manually by passing it from C++ code to the shader, just wondering if there are more roadblocks I should be concerned with for this to work correctly. \$\endgroup\$ – Pavels May 26 '17 at 15:43

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.