timscaffidi / ofxVolumetrics

Simple volumetric rendering addon for openFrameworks.
Other
53 stars 15 forks source link

Using camera perspective #4

Closed wasawi closed 10 years ago

wasawi commented 10 years ago

Hi Tim,

First of all thank you so much for this addon. I'm really learning a lot from it! I have difficulties using the perspective of the camera. I would like to know why do you need to load projection and view matrices. And why you load them like this:

     glGetFloatv( GL_MODELVIEW_MATRIX, modl);
     glGetFloatv(GL_PROJECTION_MATRIX, proj);

instead of using the camera like so:

    modl = cam->getModelViewMatrix();
    proj = cam->getModelViewProjectionMatrix();

so what is the difference between them? When I try the second the shader won't be aligned anymore.. I guess I have to change something on the vertex shader here:

cameraPosition = (gl_ModelViewMatrixInverse * vec4(0.,0.,0.,1.)).xyz;

You can see a "working" example here: https://github.com/wasawi/ofxVolumetrics/tree/camera_example

The need for using the camera perspective is because I want to draw other things and they need to be aligned with the volume's perspective.

cheers!

timscaffidi commented 10 years ago

Hi, Thanks for trying out my addon!

First of all, you can use cameras with this addon. In fact the example that comes with it is using a camera. You don't need to do anything special to get cameras to work, just draw the volume between cam.begin() and cam.end().

This is precisely the reason I'm getting the matricies this way, so as to get whatever the currently applyied model/projection matricies are. This allows the volume to be drawn with whatever transforms you already have applied, be they from a camera or something else.

Is there some reason you can't use the camera in the way it is being done in the example? See: https://github.com/timscaffidi/ofxVolumetrics/blob/master/ofxVolumetricsExample/src/testApp.cpp#L64

wasawi commented 10 years ago

Hey Tim!

Thank you for the fast response! I'm starting to understand now. So you are loading the current state of view and projection matrices no matter what transformation the camera has done.. (or something like that? i don't know how to explain it) and then you apply that to the fbo?

The problem is that the camera seems to move in the other way around. but this issue fixed it https://github.com/timscaffidi/ofxVolumetrics/issues/2

So now i see that there is no need to load camera matrix. all can be done with the example you provided :+1:

Another question (off topic) is.. have you tried to do depth testing on the volume? is there a way to draw objects intersected by the volume?

timscaffidi commented 10 years ago

Cool, glad you got it working!

Yes, that is exactly how it works. I intentionally designed the addon so that in general it would behave just like drawing any other object in your scene using the current transformations (barring some edge cases).

Currently, no. It will draw into the FBO, which is then just drawn to the screen flat. You wont be able to have regular 3d objects sticking into the volume, they would just be layered over or under the entire FBO, depending on the draw order (or depth sort if enabled).

If you wanted to achieve this effect somewhat efficiently, I could see rendering your entire scene (sans-volume) as a depth buffer, and passing it to the volume shader. The volume shader would then need to check if anything is in front of the volume, don't even bother to start casting that ray, and if the ray has "hit" anything in the depth buffer, stop the ray where it is and return the color. Then you would render the scene normally and at the last step draw the volume FBO on top and everything should be perfectly composited.

This would indeed be a very cool feature! I don't have the resources to devote to developing it right now but feel free to give it a go!

Best, Tim