To begin 3D graphics programming, you'll need some basic components: graphics resource, a renderer (drawing engine), and a display window.
This section walks you through preparing all of these essential components in one go.
First, you'll need a data area to store the contents to be drawn in 3D graphics (such as images, buffers, or contexts).
In VCSSL, these are collectively referred to as **graphics resource**. As explained in the 2D graphics section, graphics resource can be created using the newGraphics function from the Graphics library.
- Function Format -
This function allocates an empty graphics resource buffer and returns a unique graphics resoure ID for it.
Next, you'll need to create a 3D renderer -- a drawing engine that will render 3D graphics into the graphics resource.
Use the newGraphics3DRenderer function for this.
- Function Format -
Arguments:
This function creates a renderer capable of 3D rendering and returns a unique renderer ID. This ID will be passed to various 3D control functions in later steps.
You can use a newly created 3D renderer as-is, but you can also customize it with additional settings. Here are some common configuration options, along with a basic operation that you'll use every time.
To set the background color, use the "setGraphics3DColor" function.
- Function Format -
Arguments:
The RGBA format includes Red, Green, and Blue color channels, along with an Alpha channel that represents transparency. Alpha = 0 means completely transparent, and higher values increase opacity.
The colors are blended using additive color mixing, the same principle used in light blending (not paint mixing). For example:
To change the zoom level (magnification) of the view, use the "setGraphics3DMagnification" function.
- Function Format -
Arguments:
Magnification works in conjunction with camera distance. However, adjusting the camera position requires manipulating coordinate systems, which will be covered in a later section.
To actually render the 3D graphics into your graphics resource using the renderer, use the "paintGraphics3D" function.
- Function Format -
Argument:
The basic flow of 3DCG rendering in VCSSL is:
While it's technically possible to render 3D graphics without a display, most programs will need to show the output in a window.
To create an 800 x 600 window, use the following code:
// Create display window (graphicsID refers to the graphics resource to display)
int windowID = newWindow( 0, 0, 800, 600, " Hello 3DCG ! " );
int labelID = newImageLabel( 0, 0, 800, 600, graphicsID );
mountComponent( labelID, windowID );
NewWindow.vcssl
We'll skip the detailed explanation of GUI components here. For more information, see the VCSSL GUI Library Guide. For now, this simple setup is more than enough to display the 3D content we'll be working with.
Instead of displaying graphics in a window, you can also save them to image files using the exportGraphics function from the "Graphics" library. Supported formats include BMP, PNG, and JPEG.
- Function Format -
Arguments:
Let's put everything together into a simple sample program. This will create a window, render a black 3D scene, and optionally save it as a PNG file named "Test.png":
import Graphics;
import Graphics3D;
import GUI;
// Create graphics data and 3D renderer
int graphicsID = newGraphics( );
int rendererID = newGraphics3DRenderer( 800, 600, graphicsID );
// Create display window
int windowID = newWindow( 0, 0, 800, 600, " Hello 3DCG ! " );
int labelID = newImageLabel( 0, 0, 800, 600, graphicsID );
mountComponent( labelID, windowID );
setGraphics3DColor( rendererID, 0, 0, 0, 255 ); // Set background to black
paintGraphics3D( rendererID ); // Render 3DCG
// Refresh GUI
paintComponent( labelID );
paintComponent( windowID );
// To export the image as a PNG file, uncomment the following line:
//exportGraphics( graphicsID, "Test.png", "PNG" );
Sample.vcssl
When you run this program, a window will appear showing a completely black screen. While it may look like nothing is there, youfre actually seeing an empty 3D virtual space -- no objects have been placed yet.