views:

92

answers:

2

Hi, I am making a piece of code for DirectX app. It's meaning is to not show faces that are not visible. Normaly it would be just using Z-buffer, but I'm making many moves and rotations of mesh, so I would like to not do them and save computing power. I will describe this on cube. You are looking from the front so you see just one face and you don't need to rotate the 5 that left. If you would have one side of cube from 100*100 meshes, it would be great to not have to turn around 50k meshes that you really don't need. So I have stored X,Y,Z rotation of camera(the Z rotation I'm not using), and also X,Y,Z rotation of faces. In this cube simplified I would see faces that makes this statement true:

cRot //camera rotation in degrees

oRot //face rotation in degrees

if(oRot.x > cRot.x-90 && oRot.x < cRot.x+90
    && oRot.y > cRot.y-90 && oRot.y < cRot.y+90)

But there comes a problem. If I will rotate arround, the camera can get to value 330 for exapmple. In this state, I would see front and right side of cube. Right side have rotation 270 so that's allright in IF statement. Problem is with 0 rotation of front face, which is also 360 degrees.

So my question is how to make this statement to work, because when I use modulo, it will be failing for that right side and in this way it won't work for 0=360.

+1  A: 
adf88
yes that's what I was looking for, I compared rotations instead of their difference. One mistake probably because of my explanation I need values from interval so that's your solution inverted like: if (diff <= 90 || diff >= 270)
Raven
A: 

Gotta say I strongly recommend against such optimizations. On a modern GPU, the pipelined rotation of vertex buffers is substantially faster than such CPU tests intended to save it.

Ofek Shilon
Thanks for the opinion I will be still benchmarking this before I will use it. I will say after I'll try, but I think that skiping calculating 3 times the final matrix (I'm making 2 moves and 1 rotation to each mesh before actualy rendering it) with 2 calculations of absolute value and one if statement will be faster.
Raven
ok looks like you were right, I'm having on 100x100x100 meshes cube 9-10 FPS with this IF and without it's around 11-12. So it's bit faster(I didn't realized that computing of matrixes belongs to GPU). Still I need to find where's the real problem slowing app down...
Raven
@Raven: If you're actually drawing 50000 meshes like you say in your post, then that is your bottleneck. There is a per-object overhead for each drawprimitive call you make.
Alan
@Alan: Okay so what do you recommend. I am building rubik cube for example and I need to be able to turn around squares in the faces. My current solution I made 6 models of different colors and I am building cube out of these, rotating, moving and rendering them. so 3x3x3 actualy need for rendering 6x render of blue, green and next 4 colors. In 100x100x100 I need to render these 10000x each. So any recomendation?
Raven
@Raven: google for mesh-instancing (can't give a link, because you didn't specify your platfor/API). Your performence should rocket.
Ofek Shilon
Agreed, mesh instancing is the solution to this problem. It should work well for you since you have large batches of objects that share the same texture (or color, etc). There are different approaches, so google around for details. One method is to take all your objects that share a set of render states (and textures) and pack them all together in one big vertex array (or VBO). Each vertex in the array also gets the model/view matrix for that object packed in with it. In a vertex shader, apply that matrix to move that sub-object to the correct location. This is very, very fast.
Alan
thank you for help I looked onto it and it seems to be quite clever idea..
Raven