BaseView / BaseDraw Manual

About

A BaseView object represents a viewport window. Such a BaseView can be obtained from the active BaseDocument to get information on the viewport window. The class BaseDraw is based on BaseView. It can be used to draw something in a viewport window via a "Draw" function.

Warning
It is only allowed to draw into the viewport inside a "Draw" function. See Draw Manual.

Access

The BaseDraw windows used to display the active BaseDocument can be accessed with:

See BaseDocument Editor Windows.

Such a BaseDraw can be used to access the scene camera.

// This example accesses the active BaseDraw to get its camera.
// Then it moves the active object into the view of that camera.
BaseDraw* baseDraw = doc->GetActiveBaseDraw();
if (!baseDraw)
return false;
BaseObject* cameraObject = baseDraw->GetEditorCamera();
// move active object into the camera view
const Matrix cameraMatrix = cameraObject->GetMg();
Matrix targetPosition = activeObject->GetMg();
targetPosition.off = cameraMatrix.off + (900.0 * cameraMatrix.v3);
activeObject->SetMg(targetPosition);

Update

The viewport windows are redrawn if something in the active document changed:

  • EventAdd(): Informs Cinema 4D that something in the active document changed. DrawViews() will be called.
  • DrawViews(): Manually triggers the scene execution and a viewport redraw. Typically only used directly in ToolData based tools.

See also Core Messages Manual and Cinema 4D Threads Manual.

Properties

The parameters of a given BaseDraw are accessed as usual with C4DAtom::GetParameter() and C4DAtom::SetParameter(). The parameter IDs are defined in dbasedraw.h

// This example enables the display of polygon normals.
// scale normal size
const Float newScale = oldScale * 2.0;
// This example changes the projection type and display mode.
const DescID projectionID = DescLevel(BASEDRAW_DATA_PROJECTION);
const Int32 projectionType = BASEDRAW_PROJECTION_FRONT;
bd->SetParameter(projectionID, projectionType, DESCFLAGS_SET_0);
const Int32 displayType = BASEDRAW_SDISPLAY_HIDDENLINE;
bd->SetParameter(displayID, displayType, DESCFLAGS_SET_0);

A BaseDraw defines also certain display filters that decide what element types are displayed in the viewport windows.

Note
To edit the filter settings use the parameters defined in dbasedraw.h
// This example checks if the given BaseObject is visible in the active editor view.
BaseDraw* bd = doc->GetActiveBaseDraw();
if (!bd)
return false;
const DISPLAYFILTER filter = bd->GetDisplayFilter();
const Bool displayFilter = CheckDisplayFilter(object, filter);
// check editor visibility
const Bool editorVisibiliy = CheckEditorVisibility(object);
// check if the object is visible in the viewport
if (displayFilter && editorVisibiliy)
{
GePrint("The object is visible in the Editor");
}
else
{
GePrint("The object is not visible in the Editor");
}
// This example switches the filter setting
// of the SDS filter.
const DISPLAYFILTER filter = bd->GetDisplayFilter();
// switch filter for SDS
else

Rotation

A viewport window with planar projection can be rotated:

// This example restores the rotation of all editor windows.
const Int32 count = doc->GetBaseDrawCount();
for (Int32 i = 0; i < count; ++i)
{
BaseDraw* bd = doc->GetBaseDraw(i);
if (bd)
{
bd->InitUndo(doc);
bd->SetPlanarRotation(0.0f);
}
}

Camera

A viewport window with a perspective projection displays the scene from the point of view of a camera. This camera is defined using an object. This can be a scene camera, a scene object or the editor camera.

See also CameraObject Manual.

// This example accesses or sets the scene camera.
// get the BaseDraw
BaseDraw* bd = doc->GetActiveBaseDraw();
if (!bd)
return false;
// check if a scene camera is used
if (bd->HasCameraLink())
{
// get scene camera and print the name
BaseObject* cam = bd->GetSceneCamera(doc);
if (cam)
GePrint("Camera Object: " + cam->GetName());
return true;
}
else
{
// create a new camera object
if (!camera)
return false;
doc->InsertObject(camera, nullptr, nullptr);
// use this camera as the scene camera
bd->SetSceneCamera(camera);
}

Scene Elements

The viewport also displays a background and some global light settings. Both the background and the global seetings are defines using scene elements.

// This example accesses the sky object and
// environment object from the given BaseDraw.
BaseObject* envObject = baseDraw->GetEnv();
if (envObject)
{
GeData data;
const Vector color = data.GetVector();
GePrint("Environment Color: " + String::VectorToString(color));
}
// check the used sky object
BaseObject* sky = baseDraw->GetSky();
if (sky)
{
BaseTag* tag = sky->GetTag(Ttexture);
if (tag)
{
GeData data;
C4DAtom* atom = data.GetLinkAtom(doc);
BaseList2D* material = static_cast<BaseList2D*>(atom);
if (material)
GePrint("Material on Sky: " + material->GetName());
}
}

GPU Renderer

The GPU renderer of Cinema 4D can render inside a given BaseDraw window.

// This example checks if the given BaseDraw is marked as
// a GPU render view and is currently using the GPU renderer.
if (baseDraw->IsMarkedAsGPURenderer())
{
if (baseDraw->IsGPURenderer())
{
GePrint("BaseDraw is rendering using the GPU renderer.");
}
}

Drawing

Inside a "Draw" function one can use the following functions to draw into the viewport windows. See Draw Manual.

Warning
It is not allowed to draw in any other context.

While drawing one should check if the drawing thread has been aborted:

// This example tests if the current drawing operation should be aborted.
{
// check if all pointers are valid
if (!node || !doc || !bd || !bh)
return false;
// check if the drawing operation should be aborted
if (bd->TestBreak())
return true;

Drawing Pass

The viewport is drawn in several passes. One should check if the drawing operation is needed in the current pass. This is especially important for ObjectData::Draw().

Parameters

Several parameters configure the drawing operations that follow.

The parameters are:

Note
At the end of the drawing operation these parameters have to be restored.
// This example changes the line width, draws a line and resets the old value.
// store and set line width
const GeData oldLineWidth = bd->GetDrawParam(DRAW_PARAMETER_LINEWIDTH);
// draw lines
bd->SetPen(Vector(1.0, 0.0, 0.0));
bd->DrawLine(Vector(0, 0, 0), Vector(100, 100, 0), NOCLIP_D);
// reset parameter

Settings

The draw settings configure the drawing operations that follow.

Note
The color used with BaseDraw::SetPen() may be obtained with GetViewColor(). See also Colors.
// This example draws some points with random color and size.
bd->SetMatrix_Matrix(nullptr, Matrix(), 6);
Random rnd;
for (Int32 i = 0; i < 100; ++i)
{
// set random color
const Vector color = Vector(rnd.Get01(), rnd.Get01(), rnd.Get01());
bd->SetPen(color);
// set random size
const Float size = rnd.Get01() * 10.0;
bd->SetPointSize(size);
// draw point with custom size
const Vector pos = Vector(i * 10.0, 0.0, 0.0);
}

The light list defines what lights should influence the shading on 3D drawing operations. Typically used to disable lights.

Transparency settings for polygon drawing are defined with:

See also DRAW_PARAMETER_ALPHA_THRESHOLD above.

Matrix

A drawing operation may be performed in screen, camera or world space. Before any drawing operation the operation space must be defined.

// This example draws some points in various spaces.
// draw something in screen space
const Vector screenPos = Vector(100, 100.0, 0.0);
bd->DrawHandle(screenPos, DRAWHANDLE_BIG, 0);
// draw something in camera space
const Vector camPos = Vector(0.0); // center of non-perspective camera
bd->DrawHandle(camPos, DRAWHANDLE_BIG, 0);
// draw something in world space
const Vector worldPos = Vector(100.0, 100.0, 100.0);
bd->SetMatrix_Matrix(nullptr, Matrix());
bd->DrawHandle(worldPos, DRAWHANDLE_BIG, 0);

2D Drawing Operations

These functions perform drawing operations in 2D space:

Note
To calculate screen space coordinates see Transformation.
// This example draws a 2D line, circle and point.
const Vector center = Vector(100, 100, 0);
const Vector tangentPoint = Vector(150, 100, 0);
// draw line and circle
bd->SetPen(Vector(0.8));
bd->DrawLine2D(center, tangentPoint);
bd->DrawCircle2D((Int32)center.x, (Int32)center.y, tangentPoint.x - center.x);
// draw point
bd->SetPen(Vector(1.0));
bd->DrawPoint2D(center);

Text can easily be drawn with:

Note
These functions are typically used with a scene hook.
// This example draws two text boxes in the viewport in a scene hook.
const DRAWPASS pass = bd->GetDrawPass();
// check if the current pass is the object pass with inverted highlight flag
{
HUDTextEntry entryA;
entryA._txt = "This is the first entry";
entryA._position = Vector(200, 220, 0);
hudEntries.Append(entryA);
HUDTextEntry entryB;
entryB._txt = "This is the second entry";
entryB._position = Vector(200, 260, 0);
hudEntries.Append(entryB);
bd->DrawMultipleHUDText(hudEntries);
}

General Drawing Operations

Depending on the current transformation matrix these operations can draw in 2D or 3D space.

The z-buffer is configured with:

These clipping flags used with 3D drawing operations:

  • NOCLIP_D: Clip against the viewport window.
  • NOCLIP_Z: Clip against near and far plane.

The drawing operations are:

// This example fills a GeClipMap.
// The internal BaseBitmap is used to draw the content to the screen.
if (clipMap)
{
clipMap->Init(100, 50, 32);
clipMap->BeginDraw();
// fill background
clipMap->SetColor(255, 0, 0, 64);
clipMap->FillRect(0, 0, 100, 50);
// draw text
clipMap->SetColor(0, 0, 0, 255);
clipMap->TextAt(0, 11, "Hello World");
clipMap->EndDraw();
const BaseBitmap* bitmap = clipMap->GetBitmap();
if (bitmap)
{
// draw texture in screen space
bd->DrawTexture(bitmap, positions, colors, normals, uvcoords, 4, alpha, texture);
}
}

Each BaseDraw::DrawPolygon() puts the polygon into an internal array. The drawing of this array can be triggered with:

A simple shading value is often used with BaseDraw::DrawPoly() and BaseDraw::DrawPolygon():

// This example shades four vertices and uses the
// calculated colors with BaseDraw::DrawPolygon().
// base color
const Vector color(1.0, 0.0, 0.0);
// calculate shades
colors[0] = color * bd->SimpleShade(vertice[0], normals[0]);
colors[1] = color * bd->SimpleShade(vertice[1], normals[1]);
colors[2] = color * bd->SimpleShade(vertice[2], normals[2]);
colors[3] = color * bd->SimpleShade(vertice[3], normals[3]);
// draw in object space
const Matrix& mg = bh->GetMg();
bd->SetMatrix_Matrix(op, mg);
// no additional shading
// set transparency
bd->SetTransparency(128);
// draw polygon
bd->DrawPolygon(vertice, colors, true);

Objects

Complete polygon objects can be drawn with:

// This example re-draws the currently active polygon object in a ObjectData::Draw() function.
BaseDocument* doc = bd->GetDocument();
// check if the current pass is the object pass
if (doc && drawpass == DRAWPASS_OBJECT)
{
// get the active object
BaseObject* activeObject = doc->GetActiveObject();
// check if the active object is not the host object itself
// and if it is a polygon object
if (activeObject && activeObject != op && activeObject->IsInstanceOf(Opolygon))
{
// draw the object
bd->SetMatrix_Matrix(nullptr, Matrix());
bd->DrawObject(bh, activeObject, DRAWOBJECT_0, drawpass, nullptr);
return DRAWRESULT_OK;
}
}

Line Strip

The line strip functions allow to easily draw line paths:

// This example draws a random path composed of several lines.
Random randomGen;
// start position
Vector pos(0);
// init
const GeData oldLineWidth = bd->GetDrawParam(DRAW_PARAMETER_LINEWIDTH);
bd->SetMatrix_Matrix(nullptr, Matrix());
// draw random lines
for (Int32 i = 0; i < 100; ++i)
{
// get random color
const Vector color = Vector(randomGen.Get01(), randomGen.Get01(), randomGen.Get01());
bd->LineStrip(pos, color, NOCLIP_Z);
// apply random offset
pos += Vector(randomGen.Get01() * 100, randomGen.Get01() * 100, randomGen.Get01() * 100);
}
bd->LineStripEnd();
// reset parameter

XOR Lines

XOR lines are used with selection tools in the viewport. Typically one should use the LassoSelection class instead.

See also EditorWindow::DrawXORLine().

// This example draws a XOR line in ToolData::MouseInput().
// check if the view can be initialized
{
bd->DrawXORPolyLine(point, 2);
bd->EndDrawXORPolyLine(true);
const DRAWFLAGS flags =
DrawViews(flags, bd);
}
else
{
win->DrawXORLine((Int32)point[0], (Int32)point[1], (Int32)point[2], (Int32)point[3]);
}

Colors

Colors can be obtained and converted with:

// This example gets the object color for the object itself in ObjectData::Draw().
// This color is then used to draw a circle.
const Vector color = bd->GetObjectColor(bh, op);
bd->SetPen(color);
bd->SetMatrix_Matrix(nullptr, Matrix());
Matrix circleSettings = bh->GetMg();
circleSettings.v1 *= 200.0;
circleSettings.v2 *= 200.0;
circleSettings.v3 *= 200.0;
bd->DrawCircle(circleSettings);

Frame

The dimensions of a viewport window are accessed with:

// This example prints the frame size of the given BaseDraw.
// PrintFrameSize() is a custom utility function.
Int32 cl, ct, cr, cb;
// frame size
bd->GetFrame(&cl, &ct, &cr, &cb);
PrintFrameSize(cl, ct, cr, cb);
// safe frame
bd->GetSafeFrame(&cl, &ct, &cr, &cb);
PrintFrameSize(cl, ct, cr, cb);

Stereo

These functions get and change the stereo settings for the given viewport window:

Projection

A viewport window can display the scene using orthogonal or perspective projection:

Transformation

The following functions allow to transform a given point between world, screen and camera space:

// This example draws a circle around the position of the active object.
BaseObject* object = doc->GetActiveObject();
if (object)
{
const Vector worldSpacePos = object->GetMg().off;
const Vector screenSpacePos = bd->WS(worldSpacePos);
bd->DrawCircle2D((Int32)screenSpacePos.x, (Int32)screenSpacePos.y, 100);
}
// This example picks an object in the viewport within a ToolData:MouseInput() function.
// The distance of the picked object is obtained from the C4DObjectList and used
// with BaseView::SW() to get the world space position.
Int32 xpos = (Int32)x;
Int32 ypos = (Int32)y;
const Bool res = ViewportSelect::PickObject(bd, doc, xpos, ypos, 1,
VIEWPORT_PICK_FLAGS_0, nullptr, list, &m);
if (res && (list->GetCount() > 0))
{
const Float distance = list->GetZ(0);
const Vector worldSpacePos = bd->SW(Vector(x, y, distance));
GePrint("World Space Hit Point: " + String::VectorToString(worldSpacePos));
}

Also sizes can be transformed:

  • BaseView::PW_S(): Returns the size in world units for a single pixel at the given Z-depth.
  • BaseView::WP_S(): Returns the size in pixels for a single world unit at the given Z-depth.
  • BaseView::PW_W(): Returns the size in world units for a single pixel at the given screen space position.
  • BaseView::WP_W(): Returns the size in screen space pixels for a single world unit at world position.

These 3D projection functions are typically used to handle mouse input in tools:

// This example moves an object on the base plane in a ToolData::MouseInput() function.
Int32 err = 0;
Vector pos = bd->ProjectPointOnPlane(Vector(0), Vector(0, 1, 0), positionX, positionY, &err);
if (err == 0)
{
// place object
Matrix mg = object->GetMg();
mg.off = pos;
object->SetMg(mg);
// update managers and viewport
}

Testing and Clipping

The following tests can be performed:

// This example draws a 2D circle for each point of the given polygon object.
// The object is only handled if its bounding box is visible in the viewport.
// A circle is only drawn if the point is inside the viewport window.
// get polygon object properties
const Matrix mg = polyObject->GetMg();
const Vector bbox = polyObject->GetMp();
const Vector rad = polyObject->GetRad();
// test if the poly object is visible at all
if (bd->TestClipping3D(bbox, rad, mg, nullptr, nullptr))
{
// set pen color
Vector color(0.6, 0.6, 0.6);
color = bd->CheckColor(color);
bd->SetPen(color);
// points
const Vector* points = polyObject->GetPointR();
const Int32 pointCount = polyObject->GetPointCount();
// get each point
for (Int32 i = 0; i < pointCount; ++i)
{
const Vector point = mg * points[i];
const Vector screenSpacePos = bd->WS(point);
// check each point
if (bd->TestPoint(screenSpacePos.x, screenSpacePos.y))
{
// draw circle for each point
bd->DrawCircle2D(SAFEINT32(screenSpacePos.x), SAFEINT32(screenSpacePos.y), 10.0f);
}
}
}

A given line define by two points can be clipped:

A view can use near- and far-clipping:

Undo

A BaseDraw has its own undo buffer that stores changes:

// This example restores the rotation of all editor windows.
const Int32 count = doc->GetBaseDrawCount();
for (Int32 i = 0; i < count; ++i)
{
BaseDraw* bd = doc->GetBaseDraw(i);
if (bd)
{
bd->InitUndo(doc);
bd->SetPlanarRotation(0.0f);
}
}

Miscellaneous

Further functions are:

// This example accesses the OpenGl statistics of the given BaseDraw
// and prints them to the console window.
// get draw statistics
if (bd->GetDrawStatistics(stats))
{
String text("Statistics: \n");
GePrint(text);
}

BaseDrawHelp

BaseDrawHelp is a utility class that contains useful information. It is mostly used in the context of ObjectData::Draw(). An instance is typically handed over as an argument but can also be created on demand:

The BaseDrawHelp object provides access to these properties:

Information on the BaseObject that is supposed to be drawn:

A BaseObject may be drawn with a specific display mode as defined in Tdisplay.h.

// This example draws a given polygon object using a BaseDrawHelp in ObjectData::Draw().
// allocate BaseDrawHelp
if (!bhelp)
// get the current display options
BaseContainer displayOptions = bh->GetDisplay();
// set mode to "Wire"
// set display options
bhelp->SetDisplay(&displayOptions);
// get Matrix from original BaseDrawHelp object
const Matrix originalMatrix = bh->GetMg();
bhelp->SetMg(originalMatrix);
// draw an object
bd->DrawObject(bhelp, object, flags, DRAWPASS_OBJECT, nullptr, Vector(1.0, 0, 0));
// free BaseDrawHelp

Further Reading