问题
I'm using OpenGL 3.2+. I created a cube VBO of 8 verticies (each vertex consists of 8 floats: x,y,z,r,g,b,s,t), an EBO to create the faces for that cube, and a VAO to tie it all together.
I have a sprite atlas consisting of hundreds of 16x16 sprites. I'm trying to get 1 sprite from that atlas to appear on all sides of the cube (any orientation is fine).
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
Do I need to modify my shader to perform some kind of custom UV wrapping on the atlas?
回答1:
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
At least you have to use separate vertices and attributes, for 2 edges of the cube. This means you need at least 8+2*2=12 different sets of vertex attributes.
x y z u v
0 : -1 1 1 0 0
1 : 1 1 1 1 0
2 : -1 -1 1 0 1
3 : 1 -1 1 1 1
4 : -1 -1 -1 0 0
5 : 1 -1 -1 1 0
6 : -1 1 -1 0 1
7 : 1 1 -1 1 1
8 : -1 1 1 1 1
9 : -1 1 -1 1 0
10 : 1 1 1 0 1
11 : 1 1 -1 0 0
Note, the vertex attribute sets with the indices 0, 2, 4 and 6 have an identically u-coordinate of 0. And the vertex attribute sets with the indices 1, 3, 5 and 7 have an identically u-coordinate of 1.
If you want to wrap a texture to a quad you have to vary the u and the v coordinates. Because of that you have to add the separated vertex attribute sets 8, 9, 10 and 11.
Note, if you want to add normal vectors which are perpendicular to the side planes of the cube, the you need 24 vertex attributes sets. 4 for each side of the cube.
来源:https://stackoverflow.com/questions/47977887/how-do-i-wrap-a-sprite-around-a-cube-without-gl-repeat