开发者

OpenGL 3D textures and mapping on part of a quad

I'm making a Minecraft-ish terrain engine to learn some OpenGL and 3D and everything works fine except for the FPS which I'm not happy with.

OpenGL 3D textures and mapping on part of a quad

The current: I'm currently only drawing the visible faces and using frustum culling, the cubes are in 16 by 16 by 32 cube chunks (for now).

I use 6 display lists per chunk, one for each side (left, right, front, back, up, down) so cubes aren't drawn one by one, but a whole chunk side at the time.

With this method I get about 20-100fps which isn't great. Profiling says I send about 100k vertices to the graphics card and as soon as I look at the sky and 开发者_运维百科the vertices fall under 10k I'm up to 200+fps, so I guess that's my bottleneck.

What I want to do: Since less is more I went ahead and found this: http://ogre3d.org/forums/viewtopic.php?f=2&t=60381&p=405688#p404631

From what I understand Kojack uses a 3D mesh of huge planes and somehow draws textures from a 2D texture atlas to certain locations of these huge planes. He says he uses a 256x256x256 3D texture and the green and blue channels to map to the 2D texture atlas.

His method totals a constant number of vertices for the planes and sometimes useless ones if my map would've been applied, but it's still less than what I have now.

My problem: I didn't understand anything he said :(, I want to try his method, but I don't understand what he did exactly, mostly I think it's my lack of experience with 3D textures (I'm imagining a block of pixels). How did he map a texture from a texture atlas to only a part of a plane and this by using some 3D texture map...

Can anyone explain what Kojack did or point me to some helpful place, even Google isn't of any help right now.


Here's an answer to your question. First he's treating the scene as a 256 x 256 x 256 cube of cubes. For each axis, he renders 256 squares (each made from two triangles) the full size of the cube at each axis value (i.e. from 0,0,0 to 0,256,256, and then 1,0,0 to 1,256,256 and so on up to 256,0,0 to 256,256,256). He then does the same thing facing the other direction - with opposite normals - to give a total of 512 squares / 1024 triangles per axis. That process is repeated for y and z to give a total of 3072 triangles and 3072 vertices (vertices only occur along the edges of the 256 x 256 x 256 cube).

Now the texturing. Effectively he is drawing every cube, but by making some invisible by making the texture transparent at that location. He has two textures, a 256 x 256 x 256 3D texture and a 16 x 16 texture atlas made up of smaller textures. To draw the texture, first he uses the coordinates of the cube he's drawing as texture coordinates in the 3D texture. So if he's drawing the cube at position e.g. 42, 7, 13, then the 3D texture coordinates are 42, 7, 13. Inside the 3D texture all he stores are green and blue values which correspond to the u, v coordinates of the sub-texture in the texture atlas to draw. So in his example, the colour in the 3D texture is R = 0 (unused), G = 2, B = 4, A = 0 (unused). He uses the 2 & the 4 as the u, v coordinates and then draws the texture in position 2, 4 in the texture atlas. Since the texture atlas is basically just one huge texture which corresponds to 16 x 16 (256) smaller textures, only one of the smaller textures is drawn (in this case the 2 and 4 will be converted to texture coordinates by dividing them by 16, so they become 2/16 = 0.125 and 4/16 = 0.25. Those correspond to 0,0 for the sub-texture in position 2,4 in the atlas, and the 1,1 for that sub-texture are at 0.125 + 1/16 and 0.25 + 1/16. Those coordinates are used to texture the cube.

If he wants to dig out a cube, all he needs to do is change the value stored in the 3D texture at that point to point to a transparent sub-texture in the atlas, so if 0,0 in the texture atlas is a sub-texture that is entirely transparent, he can just set the texture in the 3D atlas at that location to R = 0 (unused), G = 0, B = 0, A = 0 (unused).


Try putting your face textures in a texture atlas. A texture bind per face is way too many.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜