Tutorials:Ofelia Tutorial/Examples/3dPrimitives

"In construction"

This example shows how to draw 3d primitives (sphere, icosphere, plane, cylinder, cone and box) on screen and also giving a simple example of how to manipulate three point lights and how to set material. It is based on 3dPrimitivesExample tutorial from openFrameworks/examples.

The code has 4 parts, each in separate ofelia functions, driven by  ofSetup,  ofUpdate, ofDraw  and  ofExit

ofSetup
In the setup, the code can be also broken in some parts:
 * 
 * 
 * 
 * 

Basic Settings
ofBackground(20); ofSetSmoothLighting(true); ofSetSphereResolution(24);

Instantiation of Primitives
local width, height = ofGetWidth * 0.12, ofGetHeight * 0.12;

sphere = ofSpherePrimitive; sphere:setRadius(width);

icoSphere = ofIcoSpherePrimitive; icoSphere:setRadius(width);

plane = ofPlanePrimitive; plane:set(width * 1.5, height * 1.5);

cylinder = ofCylinderPrimitive; cylinder:set(width * 0.7, height * 2.2);

cone = ofConePrimitive; cone:set(width * 0.75, height * 2.2);

box = ofBoxPrimitive; box:set(width * 1.25);

local screenWidth, screenHeight = ofGetWidth, ofGetHeight;

plane:setPosition(screenWidth * 0.2, screenHeight * 0.25, 0);

box:setPosition(screenWidth * 0.5, screenHeight * 0.25, 0);

sphere:setPosition(screenWidth * 0.8, screenHeight * 0.25, 0);

icoSphere:setPosition(screenWidth * 0.2, screenHeight * 0.75, 0);

cylinder:setPosition(screenWidth * 0.5, screenHeight * 0.75, 0);

cone:setPosition(screenWidth * 0.8, screenHeight * 0.75, 0);

Instantiation of Point Lights
pointLight = ofLight; pointLight:setPointLight; pointLight:setDiffuseColor(ofFloatColor(0.85, 0.85, 0.55)); pointLight:setSpecularColor(ofFloatColor(1, 1, 1));

pointLight2 = ofLight; pointLight2:setPointLight; pointLight2:setDiffuseColor(ofFloatColor(238 / 255, 57 / 255, 135 / 255)); pointLight2:setSpecularColor(ofFloatColor(0.8, 0.8, 0.9));

pointLight3 = ofLight; pointLight3:setPointLight; pointLight3:setDiffuseColor(ofFloatColor(19 / 255, 94 / 255, 77 / 255)); pointLight3:setSpecularColor(ofFloatColor(18 / 255, 150 / 255, 135 / 255));

Instantiation of Material
material = ofMaterial; material:setShininess(120); material:setSpecularColor(ofFloatColor(1, 1, 1));

Update Light Positions
Here the code updates the position of the lights.

local width, height, time = ofGetWidth, ofGetHeight, ofGetElapsedTimef; pointLight:setPosition((width * 0.5) + math.cos(time * 0.5) * (width * 0.3), height / 2, 500); pointLight2:setPosition((width * 0.5) + math.cos(time * 0.15) * (width * 0.3), height * 0.5 + math.sin(time * 0.7) * height, -300); pointLight3:setPosition(math.cos(time * 1.5) * width * 0.5, math.sin(time * 1.5) * width * 0.5, math.cos(time * 0.2) * width);

ofDraw
local spinX = math.sin(ofGetElapsedTimef * 0.35); local spinY = math.cos(ofGetElapsedTimef * 0.075);

ofEnableDepthTest; ofEnableLighting;

pointLight:enable; pointLight2:enable; pointLight3:enable;

material:beginMaterial;

plane:rotateDeg(spinX, 1, 0, 0); plane:rotateDeg(spinY, 0, 1, 0); plane:draw;

box:rotateDeg(spinX, 1, 0, 0); box:rotateDeg(spinY, 0, 1, 0); box:draw;

sphere:rotateDeg(spinX, 1, 0, 0); sphere:rotateDeg(spinY, 0, 1, 0); sphere:draw;

icoSphere:rotateDeg(spinX, 1, 0, 0); icoSphere:rotateDeg(spinY, 0, 1, 0); icoSphere:draw;

cylinder:rotateDeg(spinX, 1, 0, 0); cylinder:rotateDeg(spinY, 0, 1, 0); cylinder:draw;

cone:rotateDeg(spinX, 1, 0, 0); cone:rotateDeg(spinY, 0, 1, 0); cone:draw;

material:endMaterial;

ofDisableLighting; ofDisableDepthTest;

Free Resources
pointLight = nil; pointLight2 = nil; pointLight3 = nil; collectgarbage;