Change viewport "camera" and render to image file

Hi all,

I’m new to Rhino developping so my question is maybe stupid ;o)

From a C++ plugin i want to change the camera of a viewport to different orientations and render several images. By example : I want to turn the camera to the left, render an image, turn the camera up, render an image and so on.

I didn’t find how to do a “rendering” (from C++) to an image file (myImage.jpg by example).
If i use a “render” command it opens a window and i have no control over what happens.

I have serached in all samples and didn’t find something.

Can someone give me some directions to find out how to do this ?

Thanks :o)

Hi Georges,

First, here is the C++ samples repository - you may want to bookmark this.

When you say “turn the camera”, is there a particular Rhino command you are trying to emulate?

Until I hear back, here are a couple of samples that manipulate a view that you might want to take a look at:

cmdSamplePlaceCameraTarget.cpp
cmdSampleRotateView.cpp

Also, when you say “render”, do you want to run Rhino’s render command so the current render plug-in generates the image? If so, then just script the Render command using CRhinoApp::RunScript.

If you want to do something like Rhino’s ViewCaptureToFile command, then you might look at this sample:

cmdSampleViewCaptureToFile.cpp

Of course, more information as to what you want to do will be helpful…

Hi Dale,

Thank you very much for your fast and detailed answer :o)
It seem’s to me that the samples you send here will answer my questions.

What i want/need to do is rendering (with current renderer) a camera that i turn in space.
For this i want to use the current renderer installed and selected by user.

The goal is to build a cubic panorama from 6 camera positions and export them in 6 square image files.
After that i have all the code (in C++) to build the panorama and user interface for viewing it.

Hi all,

After playing around with all this this morning i have something working but the result is not correct. :sweat_smile:

For the rendering i use the code below.
This gives me rendering of the current view for the moment so i can control the camera rotations.
Later i’ll have to use other rendering method because this one renders with a camera dependant lighting.
(i mean that the ligthing/shadows change when camera position changes)

// Render current view
bool CCommandIVisit::RenderCurrentView(int view, int resolution, CString folderPath)
{
    // Test command for rendering view port to a file :
    // _-ViewCaptureToFile C:\Users\Georges\Desktop\image1.jpg _Height=800 _Width=800 _enter

    // Prepare data
    WCHAR r[256];
    CString ifile = L"";
    ifile = folderPath + L"\\image";
    wsprintf(r, L"%d", view);
    ifile += r;
    ifile += ".jpg";
    wsprintf(r, L"%d", resolution);

    // Build the Rhino command
    CString cmd = L"!_-ViewCaptureToFile\n\"" + ifile + L"\"\n";
    cmd += L"_Width=";
    cmd += r;
    cmd += L"\n";
    cmd += L"_Height=";
    cmd += r;
    cmd += L"\n";
    cmd += L"_enter";

    // Run the command
    return RhinoApp().RunScript(cmd);
}

For the camera rotation i have tried out a lot of things but none is correct for me.
Here is the code i use (this is the most simple implementation i have found…doing the same as more complex camera position and target calculations).

void CCommandIVisit::RotateCamera(int view)
{
    ON_3dPoint direction;
    switch(view)
    {
        case 1: // Front
            direction = ON_3dPoint(-1,0,0);
            mFrontViewDirection = direction;
            break;
        case 2: // Left
            direction = ON_3dPoint(0,-1,0);
            break;
        case 3: // Back
            direction = ON_3dPoint(1,0,0);
            break;
        case 4: // Right
            direction = ON_3dPoint(0,1,0);
            break;
        case 5: // Up
            direction = ON_3dPoint(0,0,1);
            break;
        case 6: // Down
            direction = ON_3dPoint(0,0,-1);
            break;
        default:
            break;
    }
    RhinoApp().ActiveView()->ActiveViewport().SetCameraDirection(direction);
    RhinoApp().ActiveView()->Redraw();
}

The up and down views are not correct because i have to roll the camera to have a correct orientation.
I didn’t find out how to roll the camera.

Another problem i have is that i don’t know how to set the “field of view” correctly.
I need a FOV of 90° to have correct frames that i can assemble.
(i have tried SetCameraAngle with no success)

I have also tried the LeftRightRotate and UpDownRotate but these commands seem to rotate position and target of camera wich is not correct for what i need.

Maybe someone can tell me where i am wrong. Sorry if these questions seem a bit basic to you…i am total beginner in Rhino. (and also…english is not my native language as you have probably discovered :smile: )

Thanks a lot.

If you were to orient the camera just using Rhino command, how would you do it?

Knowing how you use Rhino will help me provide you with an SDK method.

Thanks,

– Dale

Hi Dale,

Thanks for your answer.
I didn’t try any commands or scripting in Rhino because i prefer doing all code in C++.

What i want to do is the following:

  • allways keep camera position at the same place,
  • set field of view of the camera to 90°,
  • turn the camera (by moving only the target) to 6 different “targets”,
  • make a square image file (.jpg by example) from each of the 6 positions.

After this the 6 images are assembled to produce a cubic panorama.
That is why i need the 90° field of view.

The code i have done a few days ago is working partially (i pasted it above).
I have problems with the up and down view. I must “roll” the camera 90° left or right so that these views are correctly oriented. (edit : maybe “Roll” is called “Tilt” in Rhino…i have just looked at some camera code in Rhino scripting :wink: )
I have also problems to assemble the 6 views because i didn’t find how to set the field of view of the camera to 90°.

I’ll see if i can do things around this with commands and tell you if i find something. I use Rhino only for a few days now so i’m really not experienced :wink:

Georges

I have found that the following command give a view angle of 90°:

// Set the viewing angle
    CString cmd = L"_-PerspectiveAngle 45";
    RhinoApp().RunScript(cmd);

Can’t really remember how i have found this…and can’t explain why 45 :smiley:

I have finally found out a correct solution for all of my points above.
If my solution can help someone don’t hesitate to post/ask here for some sample code.

Thanks :sunny: