PDA

View Full Version : gl_clipdistance - clipping - example



Iblues76
05-19-2011, 02:59 PM
Hi,

can anyone point me to an example to do custom clipping. I have read some of the forums threads but there bit of pieces here and there.

thanks,
Francisco

Ilian Dinev
05-19-2011, 03:12 PM
Vtx shader:


in vec4 inVertex;
in vec3 inNormal;

out vec3 varNormal;

//----[ VERTEX UNIFORMS ]------------------------[
uniform mat4 u_ModelView;
uniform mat4 u_Projection;

uniform vec4 u_plane0=vec4(0,-1,0,0); // defined in view-space
//-----------------------------------------------/





void main(){
varNormal = inNormal;

vec4 vsPos = u_ModelView * inVertex; // position of vertex in viewspace
gl_Position = u_Projection * vsPos;


gl_ClipDistance[0] = dot(u_plane0,vsPos);
}




In your C++ code, this:



glEnable(GL_CLIP_PLANE0);

Ilian Dinev
05-19-2011, 03:20 PM
Also, some C++ to quickly get you started constructing a plane-equation:



struct Plane{
public:
vec3 norm;
float d;

Plane() { }
Plane(float a, float b, float c, float d) : norm(a,b,c), d(d) { }

void FromTriangle(const vec3& p0,const vec3& p1,const vec3& p2);
void Normalize();
Mat4 MakeReflectionMatrix();
};

void Plane::FromTriangle(const vec3& p0,const vec3& p1,const vec3& p2){
norm = cross(p1-p0,p2-p0);
norm.normalize();
d = -dot(norm,p0);
}

the u_plane0.xyz = norm.xyz, u_plane0.w = d;

Iblues76
05-19-2011, 03:42 PM
This is great. I'm going to take a look at it!!!!

Iblues76
05-19-2011, 03:47 PM
A question. the reason that I'm trying to change my clip distance is to render less objects.
The problem that I'm having is that I want to build a cube composed of many slices (256x256x256) for my PhD dissertation experiments and currently, I'm just testing with small cubes and different colors and at N=128 it becomes very slow and N=256 it doesn't even render.
I'm finding solutions. I'm reading the opengl bible and I also found a few things in advanced opengl that maybe useful.

So, my question, do you think I'm on the right track by clipping my own plane so I render less.
as OpenGL do the depth_test, I think with so many little cubes, the performance is terrible.

any advice?

Alfonse Reinheart
05-19-2011, 04:15 PM
So, my question, do you think I'm on the right track by clipping my own plane so I render less.

No. Clipping happens after vertex processing. So you're still going to be rendering 16+ million cubes; you simply won't be rasterizing all of them.

If you're rendering each cube by setting a matrix and calling glDraw*, there's no way you're going to be able to render 16+ million of them. That's way too many state changes.

Unless these cubes are transparent, why not just render the ones that are visible? Or why not render your 256x256x256 dataset using a 3D texture that gets rendered into layers?

Ilian Dinev
05-20-2011, 12:32 AM
Aside from the 3D texture idea, you could shove cubes in an octree with depth ~ 3 or 4. (at least 1000 cubes in a leaf/node); and check if the octree node's 8 vertices are outside a frustum with an already-small zFar.

Iblues76
05-20-2011, 07:41 AM
Can you provide some information how load this n images that I have into a 3d texture

thank you so much for your help.